Procházet zdrojové kódy

Converted UserFactoryTest to kotlin

master
BinHong Lee před 7 roky
rodič
revize
4d8541b6b2
5 změnil soubory, kde provedl 130 přidání a 192 odebrání
  1. +14
    -27
      src/main/java/libsys/DeleteDialog.java
  2. +2
    -3
      src/main/kotlin/libsys/BookFactory.kt
  3. +0
    -157
      src/test/java/libsys/UserFactoryTest.java
  4. +5
    -5
      src/test/kotlin/libsys/BookTest.kt
  5. +109
    -0
      src/test/kotlin/libsys/UserFactoryTest.kt

+ 14
- 27
src/main/java/libsys/DeleteDialog.java Zobrazit soubor

@@ -8,8 +8,7 @@ import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DeleteDialog extends JDialog
{
public class DeleteDialog extends JDialog {
private JPanel contentPane;
private JButton buttonOK;
private JButton buttonCancel;
@@ -17,41 +16,33 @@ public class DeleteDialog extends JDialog
private JLabel bookNameLbl;
private boolean returnStatus;

public DeleteDialog(String name)
{
public DeleteDialog(String name) {
setContentPane(contentPane);
setModal(true);
getRootPane().setDefaultButton(buttonOK);

buttonOK.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
buttonOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onOK();
}
});

buttonCancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
buttonCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
});

// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
onCancel();
}
});

// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(new ActionListener()
{
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onCancel();
}
@@ -61,27 +52,23 @@ public class DeleteDialog extends JDialog
bookNameLbl.setText(name + "?");
}

private void onOK()
{
private void onOK() {
// add your code here
returnStatus = true;
dispose();
}

private void onCancel()
{
private void onCancel() {
// add your code here if necessary
returnStatus = false;
dispose();
}

public boolean getReturnStatus()
{
public boolean getReturnStatus() {
return returnStatus;
}

public static void main(String[] args)
{
public static void main(String[] args) {
DeleteDialog dialog = new DeleteDialog("SomeBook or SomeUser");
dialog.setVisible(true);
System.exit(0);
@@ -127,7 +114,7 @@ public class DeleteDialog extends JDialog
panel3.add(warningLbl, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_SOUTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
bookNameLbl = new JLabel();
bookNameLbl.setFont(new Font(bookNameLbl.getFont().getName(), bookNameLbl.getFont().getStyle(), 20));
bookNameLbl.setText("{Book Name}");
bookNameLbl.setText("{Name} ?");
panel3.add(bookNameLbl, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
}



+ 2
- 3
src/main/kotlin/libsys/BookFactory.kt Zobrazit soubor

@@ -129,10 +129,9 @@ class BookFactory
{
try
{
books.removeAt(id)
return true
return books.remove(getBook(id))
}
catch(e: Exception)
catch (e: Exception)
{
return false
}


+ 0
- 157
src/test/java/libsys/UserFactoryTest.java Zobrazit soubor

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

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

import java.io.File;

/**
* Test UserFactory related operations
*/
public class UserFactoryTest extends TestCase
{
private UserFactory userFactory;

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

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

/**
* Set up before testing
* @throws Exception Exception
*/
public void setUp() throws Exception
{
super.setUp();
userFactory = new UserFactory();
userFactory.setUserFileName("testUserFactory.json");
}

/**
* Run all tests
*/
public void testApp()
{
newUserTest();
updateTest();
fileIOTest();
exceptionTest();
}

/**
* Test the newUser function
*/
private void newUserTest()
{
User user1 = userFactory.newUser("User1", 10);
User user2 = userFactory.newUser("User2", 3);
assertEquals("User1 is the same User object as the one in userFactory", user1, userFactory.getUser(0));
assertEquals("User2 is the same User object as the one in userFactory", user2, userFactory.getUser(1));
assertEquals("Name of User1 in userFactory is the same as returned", user1, userFactory.getUser("User1"));
assertEquals("Name of User2 in userFactory is the same as returned", user2, userFactory.getUser("User2"));
assertEquals("Name of User1 is \"User1\"", user1.getName(), "User1");
assertEquals("Name of User2 is \"User2\"", user2.getName(), "User2");
assertEquals("Name of User1 in userFactory is \"User1\"", userFactory.getUser(0).getName(), "User1");
assertEquals("Name of User1 in userFactory is \"User1\"", userFactory.getUser(1).getName(), "User2");
assertEquals("Limit of User1 is 10", user1.getLimit(), 10);
assertEquals("Limit of User2 is 3", user2.getLimit(), 3);
assertEquals("Limit of User1 in userFactory is 10", userFactory.getUser(0).getLimit(), 10);
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 = ".";
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);
assertEquals("newUserFactory : Name of User1 is \"User1\"", newUserFactory.getUser(0).getName(), "User1");
assertEquals("newUserFactory : Name of User2 is \"User2\"", newUserFactory.getUser(1).getName(), "User2");
assertEquals("newUserFactory : Name of User1 is 10", newUserFactory.getUser(0).getLimit(), 10);
assertEquals("newUserFactory : Name of User2 is 3", newUserFactory.getUser(1).getLimit(), 3);
}

/**
* Test exception cases and error handling of the class
*/
private void exceptionTest()
{
UserFactory anotherUserFactory = new UserFactory("noSuchFile.json");
assertEquals("Check ID of new User of non-existent import file", anotherUserFactory.newUser("Some person", 1).getId(), 0);

try
{
anotherUserFactory.getUser(1);
assert false;
}
catch (Exception e)
{
assert true;
}

try
{
anotherUserFactory.getUser("alk;jcnalk");
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("testUserFactory.json");
if(!file.delete())
{
System.out.println("testUserFactory.json is not found / deleted");
}

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");
}
}
}

+ 5
- 5
src/test/kotlin/libsys/BookTest.kt Zobrazit soubor

@@ -10,11 +10,6 @@ class BookTest
private var book1: Book? = null
private var book2: Book? = null

init
{

}

/**
* Set up before testing
*/
@@ -24,6 +19,11 @@ class BookTest
book2 = Book("Book2", 12, "NOT AVAILABLE")
}

init
{

}

/**
* Test Book related operations
*/


+ 109
- 0
src/test/kotlin/libsys/UserFactoryTest.kt Zobrazit soubor

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

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

/**
* Test Book related operations
*/
class UserFactoryTest {
private var userFactory: UserFactory? = null

init
{

}

/**
* Set up before testing
*/
@Before fun prepareTest()
{
userFactory = UserFactory()
userFactory!!.setUserFileName("testUserFactory.json")
}

@After fun cleanupTest()
{
var file = File("testUserFactory.json")
if (!file.delete())
{
println("testUserFactory.json is not found / deleted")
}

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")
}
}

/**
* Test UserFactory related operations
*/
@Test fun userFactoryTest()
{
var user1 = userFactory!!.newUser("User1", 10)
val user2 = userFactory!!.newUser("User2", 3)
Assert.assertEquals("User1 is the same User object as the one in userFactory", user1, userFactory!!.getUser(0))
Assert.assertEquals("User2 is the same User object as the one in userFactory", user2, userFactory!!.getUser(1))
Assert.assertEquals("Name of User1 in userFactory is the same as returned", user1, userFactory!!.getUser("User1"))
Assert.assertEquals("Name of User2 in userFactory is the same as returned", user2, userFactory!!.getUser("User2"))
Assert.assertEquals("Name of User1 is \"User1\"", user1.name, "User1")
Assert.assertEquals("Name of User2 is \"User2\"", user2.name, "User2")
Assert.assertEquals("Name of User1 in userFactory is \"User1\"", userFactory!!.getUser(0).name, "User1")
Assert.assertEquals("Name of User1 in userFactory is \"User1\"", userFactory!!.getUser(1).name, "User2")
Assert.assertEquals("Limit of User1 is 10", user1.limit, 10)
Assert.assertEquals("Limit of User2 is 3", user2.limit, 3)
Assert.assertEquals("Limit of User1 in userFactory is 10", userFactory!!.getUser(0).limit, 10)
Assert.assertEquals("Limit of User2 in userFactory is 3", userFactory!!.getUser(1).limit, 3)

user1 = userFactory!!.getUser(0)
user1.borrowNewBook(0)
user1.borrowNewBook(1)
userFactory!!.update(userFactory!!.getUser(0), user1)

var filename = "."
userFactory!!.setUserFileName(filename)
userFactory!!.toJsonFile()
Assert.assertFalse("File does not exist", File(filename).exists() && !File(filename).isDirectory)
filename = "someTestFile.json"
userFactory!!.setUserFileName(filename)
userFactory!!.toJsonFile()
val newUserFactory = UserFactory(filename)
Assert.assertEquals("newUserFactory : Name of User1 is \"User1\"", newUserFactory.getUser(0).name, "User1")
Assert.assertEquals("newUserFactory : Name of User2 is \"User2\"", newUserFactory.getUser(1).name, "User2")
Assert.assertEquals("newUserFactory : Name of User1 is 10", newUserFactory.getUser(0).limit, 10)
Assert.assertEquals("newUserFactory : Name of User2 is 3", newUserFactory.getUser(1).limit, 3)

val anotherUserFactory = UserFactory("noSuchFile.json")
Assert.assertEquals("Check ID of new User of non-existent import file", anotherUserFactory.newUser("Some person", 1).id, 0)

try
{
anotherUserFactory.getUser(1)
assert(false)
}
catch (e: Exception)
{
assert(true)
}

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

}
}

Načítá se…
Zrušit
Uložit