From 156a5dca2f0dcc4bafcdb5ba99260bc35394378d Mon Sep 17 00:00:00 2001 From: amevck Date: Wed, 13 Jul 2016 20:36:01 +0530 Subject: [PATCH 1/2] test case testAddStudentTwice added @Before fn created est case Search studentsbyname added est case Cleanregister added --- pom.xml | 6 +- .../co328/studentSystem/StudentRegister.java | 4 +- .../studentSystem/StudentRegisterTest.java | 111 +++++++++++++----- 3 files changed, 89 insertions(+), 32 deletions(-) diff --git a/pom.xml b/pom.xml index ec9ccc4..5f25ac9 100644 --- a/pom.xml +++ b/pom.xml @@ -1,10 +1,10 @@ - 4.0.0 CO314 - lab2 + E12083_lab2 1.0-SNAPSHOT diff --git a/src/main/java/lk/ac/pdn/co328/studentSystem/StudentRegister.java b/src/main/java/lk/ac/pdn/co328/studentSystem/StudentRegister.java index 5d99706..fe99965 100644 --- a/src/main/java/lk/ac/pdn/co328/studentSystem/StudentRegister.java +++ b/src/main/java/lk/ac/pdn/co328/studentSystem/StudentRegister.java @@ -56,13 +56,13 @@ public ArrayList findStudentsByName(String name) { if(studentList.get(i).getFirstName().contains(name)) { - studentList.add(studentList.get(i)); + students.add(studentList.get(i)); } if(studentList.get(i).getLastName().contains(name)) { - studentList.add(studentList.get(i)); + students.add(studentList.get(i)); } } return students; diff --git a/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java b/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java index a331c44..e767731 100644 --- a/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java +++ b/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java @@ -5,18 +5,6 @@ public class StudentRegisterTest { StudentRegister register; - @Before - public void setupTest() - { - System.out.println("A new test is starting."); - } - - @After - public void finishTest() - { - System.out.println("Test finished"); - } - @BeforeClass public static void beforeClass() { @@ -29,13 +17,35 @@ public static void afterClass() System.out.println("All tests are done"); } + @Before + public void setupTest() + { + + register = new StudentRegister(); + try + { + register.addStudent(new Student(2, "nimal", "kumara")); + + } + catch (Exception ex) + { + Assert.fail("Adding student failed"); + } + } + + @After + public void finishTest() + { + System.out.println("Test finished"); + } + @Test public void testAddStudent() { - register = new StudentRegister(); + try { - register.addStudent(new Student(2, "nimal", "kumara")); + register.addStudent(new Student(5, "fawzan", "mohomad")); } catch (Exception ex) @@ -51,17 +61,26 @@ public void testAddStudent() @Test public void testAddStudentTwice() { - // Implement your test code here. Adding a student with same registration number twice should generate an exception. - Assert.fail("Test case is not yet implemented for adding student twice. So it is set to fail always"); + + try + { + register.addStudent(new Student(2, "nimal", "kumara")); + //register.addStudent(new Student(2, "nimal", "kumara")); + + } + catch (Exception ex) + { + System.out.println("Adding student failed"); // this exeption means test pass + } + } @Test public void testRemoveStudent() { - register = new StudentRegister(); + try { - register.addStudent(new Student(2, "nimal", "kumara")); register.addStudent(new Student(1, "ruwan", "tharaka")); register.addStudent(new Student(5, "gayan", "chamara")); } @@ -77,22 +96,60 @@ public void testRemoveStudent() @Test public void testGetRegNumbers() { + + try + { + register.addStudent(new Student(1, "ruwan", "tharaka")); + register.addStudent(new Student(5, "gayan", "chamara")); + } + catch (Exception ex) + { + Assert.fail("Add student failed"); + } + ArrayList numbers = register.getAllRegistrationNumbers(); + ArrayList expected = new ArrayList(); + expected.add(2); + expected.add(1); + expected.add(5); + + Assert.assertTrue(numbers.equals(expected)); + } + + @Test + public void testfindStudentsByName(){ + register = new StudentRegister(); try { register.addStudent(new Student(1, "ruwan", "tharaka")); - register.addStudent(new Student(2, "nimal", "kumara")); - register.addStudent(new Student(5, "gayan", "chamara")); + register.addStudent(new Student(5, "gayan", "ruwan")); } catch (Exception ex) { - Assert.fail("Adding student failed"); + Assert.fail("Add student failed"); + } + + ArrayList got = register.findStudentsByName("ruwan"); + ArrayList expected = new ArrayList(); + expected.add(new Student(1, "ruwan", "tharaka")); + Assert.assertTrue(got.get(0).getFirstName().equals("ruwan")); + Assert.assertTrue(got.get(1).getLastName().equals("ruwan")); + } + @Test + public void testCleanRegestor() { + register = new StudentRegister(); + try { + register.addStudent(new Student(1, "ruwan", "tharaka")); + register.addStudent(new Student(5, "gayan", "chamara")); + } catch (Exception ex) { + Assert.fail("Add student failed"); + } + register.reset(); + try { + + ArrayList numbers = register.getAllRegistrationNumbers(); + } catch (NullPointerException e) { + System.out.print("NullPointerException caught"); // this exeption means regestor is cleaned } - ArrayList numbers = register.getAllRegistrationNumbers(); - ArrayList expected = new ArrayList(); - expected.add(1); - expected.add(2); - expected.add(5); - Assert.assertTrue(numbers.equals(expected)); } } From 45d9e0c940feec21032fa1a18636c835a6cc2dc1 Mon Sep 17 00:00:00 2001 From: amevck Date: Wed, 13 Jul 2016 20:38:50 +0530 Subject: [PATCH 2/2] test case testAddStudentTwice added @Before fn created est case Search studentsbyname added est case CleanStudentregister added --- .../java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java b/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java index e767731..f13a8aa 100644 --- a/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java +++ b/src/test/java/lk/ac/pdn/co328/studentSystem/StudentRegisterTest.java @@ -136,7 +136,7 @@ public void testfindStudentsByName(){ Assert.assertTrue(got.get(1).getLastName().equals("ruwan")); } @Test - public void testCleanRegestor() { + public void testCleanStudentRegestor() { register = new StudentRegister(); try { register.addStudent(new Student(1, "ruwan", "tharaka"));