/
LibraryTest.java
375 lines (335 loc) · 11 KB
/
LibraryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
*
*/
package library;
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* @author David Matuszek
*/
public class LibraryTest {
private Book contact;
private Book contact2;
private Book equalRites;
private Book sisters;
private Book witches;
private Book nightly;
private Book time;
private Book rings;
private ArrayList<Book> books;
private Library library;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
contact = new Book("Contact", "Carl Sagan");
equalRites = new Book("Equal Rites", "Terry Pratchett");
sisters = new Book("Weird Sisters", "Terry Pratchett");
witches = new Book("Witches Abroad", "Terry Pratchett");
nightly = new Book("Disappearing Nightly", "Laura Resnick");
contact2 = new Book("Contact", "Carl Sagan");
time = new Book("Nick of Time", "Ted Bell");
rings = new Book("Lord of the rings", "J. R. R. Tolkien");
books = new ArrayList<Book>();
books.add(contact);
books.add(witches);
books.add(sisters);
books.add(equalRites);
books.add(nightly);
books.add(contact2);
books.add(time);
books.add(rings);
library = new Library(books);
Patron dave1;
}
/**
* Test method for {@link library.Library#open()}.
*/
@Test
public void testOpen() {
// The open() method doesn't make any change that we can
// readily test, but we can at least make sure it returns
// an ArrayList<Book>.
assertEquals(new ArrayList<OverdueNotice>(), library.open());
}
private Patron openAndServeDave() {
library.open();
Patron dave = library.issueCard("Dave");
library.serve("Dave");
return dave;
}
private Patron openAndServeStanley(){
library.open();
Patron stanley = library.issueCard("Stanley");
library.serve("Stanley");
return stanley;
}
/**
* Test method for {@link library.Library#createOverdueNotices()}.
*/
@Test
public void testCreateOverdueNotices() {
ArrayList<OverdueNotice> notices;
openAndServeDave();
ArrayList<Book> foundBooks = library.search("Equal Rites");
assertEquals(equalRites, foundBooks.get(0));
library.checkOut(1);
int dueDate = 7;
// Don't send an overdue notice during the next seven days
for (int i = 0; i < dueDate; i++) {
library.close();
notices = library.open();
assertTrue(notices.isEmpty());
}
library.close();
//Send a notice on the 8th day
notices = library.open();
assertFalse(notices.isEmpty());
library.close();
// // Don't send another notice after that
notices = library.open();
assertTrue(notices.isEmpty());
}
/**
* Test method for {@link library.Library#issueCard(java.lang.String)}.
*/
@Test
public void testIssueCard() {
//issue a libray card
library.open();
Patron dave = library.issueCard("Dave");
assertTrue(dave instanceof Patron);
}
/**
* Test method for {@link library.Library#serve(java.lang.String)}.
*/
@Test
public void testServe() {
// We can test if the correct Patron is returned, but not if
// it's being saved. The tests for checkIn and checkOut can
// determine this.
library.open();
Patron dave = library.issueCard("Dave");
Patron paula = library.issueCard("Paula");
assertEquals(dave, library.serve("Dave"));
assertEquals(paula,library.serve("Paula"));
}
/**
* Test method for {@link library.Library#checkOut(int[])}.
*/
@Test
public void testCheckOutOneBook() {
Patron dave = openAndServeDave();
library.search("Time");
ArrayList<Book> booksCheckedOut = library.checkOut(1);
assertTrue(dave.getBooks().contains(time));
assertEquals(dave.getBooks(), booksCheckedOut);
// Book shouldn't still be in library
ArrayList<Book> booksFound = library.search("Time");
assertTrue(booksFound.isEmpty());
}
/**
* Test method for Check out method
*/
public void testCheckOutThreeBoooks(){
// check out three books
Patron stanley =openAndServeStanley();
library.search("nder");
ArrayList<Book>booksCheckedOut2 = library.checkOut(1,2,3);
assertTrue(stanley.getBooks().contains("nder"));
assertEquals(stanley.getBooks().size(),3);
assertEquals(stanley.getBooks(),booksCheckedOut2);
}
/**
* Test method for {@link library.Library#checkOut(int[])}.
*/
@Test
public void testCheckOutOneOfMultipleCopies() {
Patron dave = openAndServeDave();
ArrayList<Book> booksFound = library.search("Carl Sagan");
library.checkOut(1);
// There should still be another copy in the library
booksFound = library.search("Carl Sagan");
assertEquals(1, booksFound.size());
}
/**
* Test method for {@link library.Library#checkOut(int[])}.
*/
@Test
public void testCheckOutBooksInRandomOrder() {
Patron dave = openAndServeDave();
library.search("Terry Pratchett");
library.checkOut(1);
library.checkOut(3);
library.checkOut(2);
ArrayList<Book> davesBooks = dave.getBooks();
assertTrue(davesBooks.contains(witches));
assertTrue(davesBooks.contains(sisters));
assertTrue(davesBooks.contains(equalRites));
assertEquals(davesBooks.size(),3);
}
/**
* Check out more than three books
*/
public void testCheckOutBooksMoreThanThreeBooks(){
Patron stanley =openAndServeStanley();
library.search("nder");
ArrayList<Book>booksCheckedOut2 = library.checkOut(1,2,3,4);
assertEquals(booksCheckedOut2,null);
}
/**
* No Patron exists
*/
public void testCheckedOutBooksNoPattron(){
ArrayList<Book>booksCheckedOut2 = library.checkOut(1,2,3);
assertEquals(booksCheckedOut2,null);
}
/**
* Negative input
*/
public void testCheckedOutNegativeNuumbers(){
Patron stanley =openAndServeStanley();
library.search("nder");
ArrayList<Book>booksCheckedOut2 = library.checkOut(-1);
assertEquals(booksCheckedOut2,null);
}
/**
* Search First
*/
public void testCheckedOutSearchFirst(){
// before search
ArrayList<Book>booksCheckedOut2 = library.checkOut(1,2,3);
assertEquals(booksCheckedOut2,null);
//after search
library.search("nder");
booksCheckedOut2 = library.checkOut(1);
assertTrue(booksCheckedOut2.contains("nder"));
}
/**
* Test method for {@link library.Library#checkIn(int[])}.
*/
@Test
public void testCheckInOneBook() {
Patron dave = openAndServeDave();
ArrayList<Book> foundBooks = library.search("Disappearing Nightly");
// Checking out a book moves it from the library to the patron
library.checkOut(1);
assertTrue(dave.getBooks().contains(nightly));
assertTrue(library.search("Disappearing Nightly").isEmpty());
// Checking in a book moves it back from the patron to the library
library.serve("Dave");
library.checkIn(1);
assertFalse(library.search("Disappearing Nightly").isEmpty());
}
/**
* Test method for {@link library.Library#checkIn(int[])}.
*/
@Test
public void testCheckInManyBooks() {
openAndGiveBooksToDave();
Patron dave = library.serve("Dave");
ArrayList<Book> davesBooks = dave.getBooks();
assertEquals(3, davesBooks.size());
Book someBook = davesBooks.get(1); // Can't be sure which book this is
library.search(someBook.getTitle()).isEmpty();
}
private void openAndGiveBooksToDave() {
Patron dave = openAndServeDave();
library.search("Terry");
library.checkOut(1);
library.checkOut(2);
library.checkOut(3);
}
/*
* No patron exists for CheckIn functions
*/
@Test
public void testCheckInBooksNoPatron(){
// no patron exists
assertEquals(library.checkIn(1,2),null);
}
/**
* Neative input for CheckIn functions
*/
@Test
public void testCheckInNativeInput(){
Patron dave = openAndServeDave();
library.search("Terry Pratchett");
library.checkOut(1);
library.checkOut(3);
library.checkOut(2);
ArrayList<Book> davesBooks = dave.getBooks();
assertEquals(library.checkIn(-1),null);
assertEquals(davesBooks.size(),3);
}
/**
* The number of books you checked in is more than the books you have in check out list
*/
@Test
public void testCheckInMoreBooks(){
Patron dave = openAndServeDave();
library.search("Terry Pratchett");
library.checkOut(1);
library.checkOut(3);
library.checkOut(2);
ArrayList<Book> davesBooks = dave.getBooks();
assertEquals(library.checkIn(1,2,3,4),null);
assertEquals(davesBooks.size(),3);
}
/**
* Test method for {@link library.Library#search(java.lang.String)}.
*/
@Test
public void testSearchTitle() {
library.open();
ArrayList<Book> foundBooks = library.search("appear");
assertEquals(1, foundBooks.size());
assertEquals("Disappearing Nightly", foundBooks.get(0).getTitle());
foundBooks = library.search("xyzzy");
assertEquals(0, foundBooks.size());
}
/**
* Test method for {@link library.Library#search(java.lang.String)}.
*/
@Test
public void testSearchAuthor() {
library.open();
ArrayList<Book> foundBooks = library.search("Resnick");
assertEquals(1, foundBooks.size());
assertEquals("Laura Resnick", foundBooks.get(0).getAuthor());
}
/**
* Test method for {@link library.Library#search(java.lang.String)}.
*/
@Test
public void testSearchWithMixedCase() {
library.open();
ArrayList<Book> foundBooks = library.search("laura");
assertEquals(1, foundBooks.size());
foundBooks = library.search("NIGHTLY");
assertEquals(1, foundBooks.size());
foundBooks = library.search("Nick");
assertEquals(2, foundBooks.size());
}
/**
* Test method for {@link library.Library#search(java.lang.String)}.
*/
@Test
public void testSearchAndIgnoreDuplicates() {
library.open();
ArrayList<Book> foundBooks = library.search("Contact");
assertEquals(1, foundBooks.size());
}
/**
* No search results matched
*/
@Test
public void testSearchResultsNotMatched(){
ArrayList<Book> searched = library.search("lll");
assertEquals(searched,null);
}
}