Przeglądaj źródła

Converted BookFactoryTest to kotlin

master
BinHong Lee 7 lat temu
rodzic
commit
8ce327bf0d
2 zmienionych plików z 115 dodań i 150 usunięć
  1. +0
    -150
      src/test/java/libsys/BookFactoryTest.java
  2. +115
    -0
      src/test/kotlin/libsys/BookFactoryTest.kt

+ 0
- 150
src/test/java/libsys/BookFactoryTest.java Wyświetl plik

@@ -1,150 +0,0 @@
package libsys;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.io.File;

/**
* Test BookFactory related operations
*/
public class BookFactoryTest extends TestCase
{
private BookFactory bookFactory;

/**
* Test BookFactory related operations
* @param testName name of the test case
*/
public BookFactoryTest(String testName)
{
super(testName);
}

/**
* @return suite of tests being tested
*/
public static Test suite()
{
return new TestSuite(BookFactoryTest.class);
}

/**
* Set up before testing
* @throws Exception Exception
*/
public void setUp() throws Exception
{
super.setUp();
bookFactory = new BookFactory();
bookFactory.setBookFileName("testBookFactory.json");
}

/**
* Run all tests
*/
public void testApp()
{
newBookTest();
outputAndInputTest();
exceptionTest();
}

/**
* Test the newBook function
*/
private void newBookTest()
{
Book book1 = bookFactory.newBook("Whole new Book", "AVAILABLE");
assertTrue("Book ID is 0", book1.getId() == 0);
assertTrue("Book title is \"Whole new Book\"", book1.getTitle().equals(bookFactory.getBook(0).getTitle()));
assertTrue("Book status is \"AVAILABLE\"", book1.getStatus().equals(bookFactory.getBook(0).getStatus()));
assertTrue("Comparing Book with to the Book in BookFactory", bookFactory.getBook(0).equals(book1));
}

/**
* Test the class in writing to and reading from files
*/
private void outputAndInputTest()
{
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"));
}

/**
* Test exception cases and error handling of the class
*/
private void exceptionTest()
{
BookFactory anotherBookFactory = new BookFactory("noSuchFile.json");
assertTrue("Check ID for new Book of non-existent import file", anotherBookFactory.newBook("Some book", "NOT AVAILABLE").getId() == 0);

try
{
anotherBookFactory.getBook(20);
assert false;
}
catch (Exception e)
{
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("No Such Book");
assert false;
}
catch (Exception e)
{
assert true;
}
}

/**
* Delete files created during test
* @throws Exception Exception
*/
public void tearDown() throws Exception
{
super.tearDown();
File file = new File("noSuchFile.json");
if(!file.delete())
{
System.out.println("noSuchFile.json is not found / deleted");
}

file = new File("someTestFile.json");
if(!file.delete())
{
System.out.println("someTestFile.json is not found / deleted");
}

file = new File("testBookFactory.json");
if(!file.delete())
{
System.out.println("testBookFactory.json is not found / deleted");
}
}
}

+ 115
- 0
src/test/kotlin/libsys/BookFactoryTest.kt Wyświetl plik

@@ -0,0 +1,115 @@
package libsys

import org.junit.*
import java.io.File

/**
* Test BookFactory related operations
*/
class BookFactoryTest
{
private var bookFactory: BookFactory? = null

init
{

}

/**
* Set up before testing
*/
@Before fun prepareTest()
{
bookFactory = BookFactory()
bookFactory!!.setBookFileName("testBookFactory.json")
}

/**
* Delete files created during test
*/
@After fun cleanupTest()
{
var file = File("noSuchFile.json")
if (!file.delete())
{
println("noSuchFile.json is not found / deleted")
}

file = File("someTestFile.json")
if (!file.delete())
{
println("someTestFile.json is not found / deleted")
}

file = File("testBookFactory.json")
if (!file.delete())
{
println("testBookFactory.json is not found / deleted")
}
}

/**
* Test BookFactory related operations
*/
@Test fun bookFactoryTest()
{
var book1 = bookFactory!!.newBook("Whole new Book", "AVAILABLE")
Assert.assertTrue("Book ID is 0", book1.id == 0)
Assert.assertTrue("Book title is \"Whole new Book\"", book1.title == bookFactory!!.getBook(0).title)
Assert.assertTrue("Book status is \"AVAILABLE\"", book1.status == bookFactory!!.getBook(0).status)
Assert.assertTrue("Comparing Book with to the Book in BookFactory", bookFactory!!.getBook(0) == book1)

var filename = "."
bookFactory!!.setBookFileName(filename)
bookFactory!!.toJsonFile()
Assert.assertFalse("File does not exist", File(filename).exists() && !File(filename).isDirectory)
filename = "someTestFile.json"
bookFactory!!.setBookFileName(filename)
bookFactory!!.toJsonFile()
val newBookFactory = BookFactory(filename)
Assert.assertTrue("newBookFactory : Book title is \"Whole new Book\"", newBookFactory.getBook(0).title == "Whole new Book")
Assert.assertEquals("newBookFactory : Book id is 0", newBookFactory.getBook("Whole new Book").id, 0)
Assert.assertTrue("newBookFactory : Book status is \"AVAILABLE\"", newBookFactory.getBook(0).status == "AVAILABLE")

val anotherBookFactory = BookFactory("noSuchFile.json")
Assert.assertTrue("Check ID for new Book of non-existent import file", anotherBookFactory.newBook("Some book", "NOT AVAILABLE").id == 0)

try
{
anotherBookFactory.getBook(20)
assert(false)
}
catch (e: Exception)
{
assert(true)
}

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

try
{
anotherBookFactory.getBook("No Such Book")
assert(false)
}
catch (e: Exception)
{
assert(true)
}

Assert.assertTrue(bookFactory!!.deleteBook(0))
Assert.assertFalse(bookFactory!!.deleteBook(0))
book1 = bookFactory!!.newBook("todelete", "AVAILABLE")
Assert.assertTrue(bookFactory!!.deleteBook(book1))
Assert.assertFalse(bookFactory!!.deleteBook(book1))
}
}

Ładowanie…
Anuluj
Zapisz