Browse Source

Increased code coverage

master
BinHong Lee 7 years ago
parent
commit
003197131d
7 changed files with 82 additions and 17 deletions
  1. +3
    -1
      src/main/java/libsys/Book.java
  2. +1
    -1
      src/main/java/libsys/UserFactory.java
  3. +28
    -1
      src/test/java/libsys/BookFactoryTest.java
  4. +2
    -1
      src/test/java/libsys/BookTest.java
  5. +23
    -12
      src/test/java/libsys/SettingsTest.java
  6. +24
    -1
      src/test/java/libsys/UserFactoryTest.java
  7. +1
    -0
      src/test/java/libsys/UserTest.java

+ 3
- 1
src/main/java/libsys/Book.java View File

@@ -131,11 +131,12 @@ public class Book
status = "AVAILABLE";
}

/**
/*
* Calculate the overdue fine
* @param currentDay Today's date
* @return Price of the fine
*/
/*
public double overdueFine(int[] currentDay)
{
double fine;
@@ -182,4 +183,5 @@ public class Book
return fine;
}
}
*/
}

+ 1
- 1
src/main/java/libsys/UserFactory.java View File

@@ -126,7 +126,7 @@ public class UserFactory
* @param name Name of the User to be found
* @return User with the given name
*/
public User getUser(String name)
User getUser(String name)
{
for (User temp : users) {
if (temp.getName().equals(name)) {


+ 28
- 1
src/test/java/libsys/BookFactoryTest.java View File

@@ -68,11 +68,16 @@ public class BookFactoryTest extends TestCase
*/
private void outputAndInputTest()
{
String filename = "someTestFile.json";
String filename = ".";
bookFactory.setBookFileName(filename);
bookFactory.toJsonFile();
assertFalse("File does not exist", (new File(filename)).exists() && !(new File(filename).isDirectory()));
filename = "someTestFile.json";
bookFactory.setBookFileName(filename);
bookFactory.toJsonFile();
BookFactory newBookFactory = new BookFactory(filename);
assertTrue("newBookFactory : Book title is \"Whole new Book\"", newBookFactory.getBook(0).getTitle().equals("Whole new Book"));
assertEquals("newBookFactory : Book id is 0", newBookFactory.getBook("Whole new Book").getId(), 0);
assertTrue("newBookFactory : Book status is \"AVAILABLE\"", newBookFactory.getBook(0).getStatus().equals("AVAILABLE"));
}

@@ -93,6 +98,28 @@ public class BookFactoryTest extends TestCase
{
assert true;
}

try
{
Book someBook = new Book(20);
Book anotherBook = someBook;
anotherBookFactory.update(someBook, anotherBook);
assert false;
}
catch (Exception e)
{
assert true;
}

try
{
anotherBookFactory.getBook(12);
assert false;
}
catch (Exception e)
{
assert true;
}
}

/**


+ 2
- 1
src/test/java/libsys/BookTest.java View File

@@ -108,7 +108,8 @@ public class BookTest extends TestCase
Book testBook2 = new Book("Test Title", 20, "RESERVED");
Book testBook3 = new Book(15, "Test Title 2", "RENTED", (new int[]{2019, 3, 23}));

assertTrue("Book1 id is 10", testBook1.getId() == 10);
assertEquals("Book name is \"UNDEFINED\"", testBook1.getTitle(), "UNDEFINED");
assertEquals("Book1 id is 10", testBook1.getId(), 10);
assertEquals("Book1 status is \"NOT AVAILABLE\"", testBook1.getStatus(), "NOT AVAILABLE");

assertEquals("Book2 title is \"Test Title\"", testBook2.getTitle(), "Test Title");


+ 23
- 12
src/test/java/libsys/SettingsTest.java View File

@@ -36,7 +36,6 @@ public class SettingsTest extends TestCase
public void testApp()
{
constructorTest();
setTitleTest();
setUsersFilenameTest();
setBooksFilenameTest();
}
@@ -46,20 +45,32 @@ public class SettingsTest extends TestCase
*/
private void constructorTest()
{
settings = new Settings("noSuchFile.txt");
settings = new Settings(".");
settings.setTitle("Failed to save");
File file = new File(".");
if (file.exists() && !file.isDirectory())
{
assert false;
}
else
{
assert true;
}
String filename = "noSuchFile.txt";
file = new File(filename);
if (file.exists())
{
file.delete();
}
settings = new Settings(filename);
assertEquals("Default size is 3", settings.size(), 3);
assertEquals("Default title", settings.get(settings.getKey(0)), "Welcome to the library");
assertEquals("Default users filename", settings.get(settings.getKey(1)), "users.json");
assertEquals("Default books filename", settings.get(settings.getKey(2)), "books.json");
}

/**
* Test the setTitle function
*/
private void setTitleTest()
{
String newTitle = "New Title";
settings.setTitle(newTitle);
assertEquals("Set settings title to \"New Title\"", settings.get(settings.getKey(0)), newTitle);
settings.setTitle("noSuchFile");
assertTrue("noSuchFile.txt is created", file.exists());
settings = new Settings(filename);
assertEquals("Default title", settings.get(settings.getKey(0)), "noSuchFile");
}

/**


+ 24
- 1
src/test/java/libsys/UserFactoryTest.java View File

@@ -49,6 +49,7 @@ public class UserFactoryTest extends TestCase
newUserTest();
fileIOTest();
exceptionTest();
updateTest();
}

/**
@@ -72,12 +73,24 @@ public class UserFactoryTest extends TestCase
assertEquals("Limit of User2 in userFactory is 3", userFactory.getUser(1).getLimit(), 3);
}

private void updateTest()
{
User user1 = userFactory.getUser(0);
user1.borrowNewBook(0);
user1.borrowNewBook(1);
userFactory.update(userFactory.getUser(0), user1);
}

/**
* Test the class in writing to and reading from files
*/
private void fileIOTest()
{
String filename = "someTestFile.json";
String filename = ".";
userFactory.setUserFileName(filename);
userFactory.toJsonFile();
assertFalse("File does not exist", (new File(filename)).exists() && !(new File(filename).isDirectory()));
filename = "someTestFile.json";
userFactory.setUserFileName(filename);
userFactory.toJsonFile();
UserFactory newUserFactory = new UserFactory(filename);
@@ -104,6 +117,16 @@ public class UserFactoryTest extends TestCase
{
assert true;
}

try
{
anotherUserFactory.getUser("alk;jcnalk");
assert false;
}
catch (Exception e)
{
assert true;
}
}

/**


+ 1
- 0
src/test/java/libsys/UserTest.java View File

@@ -100,6 +100,7 @@ public class UserTest extends TestCase
int[] bookIds = user1.bookStatus();
assertEquals("User1 is now borrowing Book3", bookIds[0], 3);
assertFalse("User1 reached the limit", user1.status());
assertFalse("User1 fail to borrow another book", user1.borrowNewBook(5));
assertFalse("User1 failed to return Book2 that was not borrowed", user1.returnBook(2));
assertTrue("User1 return Book3", user1.returnBook(3));
assertTrue("User1 can now borrow book", user1.status());


Loading…
Cancel
Save