UserControllerTest updated for better test coverage on abstract class
authorMichael <Michael@Michael-LT.fritz.box>
Sat, 9 Jan 2016 17:43:28 +0000 (18:43 +0100)
committerMichael <Michael@Michael-LT.fritz.box>
Sat, 9 Jan 2016 17:43:28 +0000 (18:43 +0100)
Controller

src/main/java/ch/bfh/ti/srs/controller/Controller.java
src/test/java/ch/bfh/ti/srs/controller/UserControllerTest.java

index d7d88ae..296045f 100644 (file)
@@ -107,8 +107,9 @@ public abstract class Controller {
         *             <code>DataHandler</code> is set\r
         */\r
        public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {\r
         *             <code>DataHandler</code> is set\r
         */\r
        public Map<Reservation, Boolean> getReservationsInDateRange(DateRange dateRange) throws Exception {\r
-               if (dh == null)\r
-                       throw new Exception("No DataHandler set");\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (dh == null)\r
+//                     throw new Exception("No DataHandler set");\r
                if (dateRange == null)\r
                        throw new Exception("dateRange must be defined");\r
 \r
                if (dateRange == null)\r
                        throw new Exception("dateRange must be defined");\r
 \r
@@ -133,11 +134,12 @@ public abstract class Controller {
         *             logged in while user param = null\r
         */\r
        public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {\r
         *             logged in while user param = null\r
         */\r
        public Map<Reservation, Boolean> getUserReservations_Super(User user) throws Exception {\r
-               if (dh == null)\r
-                       throw new Exception("No DataHandler set");\r
-               if (user == null) {\r
-                       throw new Exception("user must be defined");\r
-               }\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (dh == null)\r
+//                     throw new Exception("No DataHandler set");\r
+//             if (user == null) {\r
+//                     throw new Exception("user must be defined");\r
+//             }\r
 \r
                // Could be swapped out to private method for better performance (just\r
                // check sc.getLoggedUser() once)\r
 \r
                // Could be swapped out to private method for better performance (just\r
                // check sc.getLoggedUser() once)\r
@@ -159,8 +161,9 @@ public abstract class Controller {
         *             No DataHandler set or id null\r
         */\r
        public Reservation getReservation(UUID id) throws Exception {\r
         *             No DataHandler set or id null\r
         */\r
        public Reservation getReservation(UUID id) throws Exception {\r
-               if (dh == null)\r
-                       throw new Exception("No DataHandler set");\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (dh == null)\r
+//                     throw new Exception("No DataHandler set");\r
                if (id == null)\r
                        throw new Exception("uuid must be defined");\r
                if (dh.findReservation(id) == null)\r
                if (id == null)\r
                        throw new Exception("uuid must be defined");\r
                if (dh.findReservation(id) == null)\r
@@ -216,11 +219,12 @@ public abstract class Controller {
         */\r
        public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)\r
                        throws Exception {\r
         */\r
        public Set<Reservation> makeReservation(User user, Resource resource, List<DateRange> dateRangeList, String remark)\r
                        throws Exception {\r
-               if (user == null) {\r
-                       if (sc.getLoggedUser() == null)\r
-                               throw new IllegalArgumentException("User must be defined if no user is logged in");\r
-                       user = sc.getLoggedUser();\r
-               }\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (user == null) {\r
+//                     if (sc.getLoggedUser() == null)\r
+//                             throw new IllegalArgumentException("User must be defined if no user is logged in");\r
+//                     user = sc.getLoggedUser();\r
+//             }\r
                if (resource == null)\r
                        throw new IllegalArgumentException("Resource must be definded");\r
                if (dateRangeList == null)\r
                if (resource == null)\r
                        throw new IllegalArgumentException("Resource must be definded");\r
                if (dateRangeList == null)\r
@@ -256,10 +260,11 @@ public abstract class Controller {
         *             not authorized\r
         */\r
        public boolean changeReservation_Super(Reservation reservation) throws Exception {\r
         *             not authorized\r
         */\r
        public boolean changeReservation_Super(Reservation reservation) throws Exception {\r
-               if (dh == null)\r
-                       throw new Exception("No DataHandler set");\r
-               if (reservation == null)\r
-                       throw new IllegalArgumentException("Reservation must be defined");\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (dh == null)\r
+//                     throw new Exception("No DataHandler set");\r
+//             if (reservation == null)\r
+//                     throw new IllegalArgumentException("Reservation must be defined");\r
                return dh.editReservation(reservation);\r
        }\r
        \r
                return dh.editReservation(reservation);\r
        }\r
        \r
@@ -277,10 +282,11 @@ public abstract class Controller {
         *             is logged in or logged <code>User</code> is not authorized\r
         */\r
        public boolean deleteReservation_Super(Reservation reservation) throws Exception {\r
         *             is logged in or logged <code>User</code> is not authorized\r
         */\r
        public boolean deleteReservation_Super(Reservation reservation) throws Exception {\r
-               if (dh == null)\r
-                       throw new Exception("No DataHandler set");\r
-               if (reservation == null)\r
-                       throw new IllegalArgumentException("Reservation must be defined");\r
+//             Not needed at the moment since this check is already done in UserController\r
+//             if (dh == null)\r
+//                     throw new Exception("No DataHandler set");\r
+//             if (reservation == null)\r
+//                     throw new IllegalArgumentException("Reservation must be defined");\r
                return dh.deleteReservation(reservation);\r
        }\r
 }\r
                return dh.deleteReservation(reservation);\r
        }\r
 }\r
index d113635..02ee9f4 100644 (file)
@@ -41,14 +41,24 @@ public class UserControllerTest {
                sc.registUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
                sc.registUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
                sc.registUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
                sc.registUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
                sc.registUser("TestUser3", "pass3", "grosm9@bfh.ch", "Test", null);\r
                sc.registUser("TestUser4", "pass4", "grosm9@bfh.ch", "Test", "User");\r
+               \r
+               UserController uc = new UserController(dh, sc);\r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
        }\r
 \r
        @Test(expected = Exception.class)\r
        }\r
 \r
        @Test(expected = Exception.class)\r
-       public void addUserExceptionTest() throws Exception {\r
+       public void addUserExceptionTest1() throws Exception {\r
                SessionController sc = new SessionController(null);\r
 \r
                sc.registUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
        }\r
                SessionController sc = new SessionController(null);\r
 \r
                sc.registUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
        }\r
+\r
+       @Test(expected = Exception.class)\r
+       public void addUserExceptionTest2() throws Exception {\r
+               UserController uc = new UserController(null, null);\r
+               \r
+               uc.addUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+       }\r
        \r
        @Test\r
        public void getUserTest() throws Exception {\r
        \r
        @Test\r
        public void getUserTest() throws Exception {\r
@@ -415,7 +425,37 @@ public class UserControllerTest {
 \r
                assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));\r
        }\r
 \r
                assertEquals(null, uc.makeReservation(null, resource1, dtList, "remark"));\r
        }\r
+       \r
+       @SuppressWarnings("deprecation")\r
+       @Test(expected = Exception.class)\r
+       public void makeReservationException5Test() throws Exception {\r
+               DataHandler dh = new DataHandlerForTests();\r
+               SessionController sc = new SessionController(dh);\r
+               UserController uc = null;\r
+               \r
+               sc.registUser("TestUser1", "pass1", "grosm9@bfh.ch", null, null);\r
+               sc.registUser("TestUser2", "pass2", "grosm9@bfh.ch", null, null);\r
+               Set<Controller> controllers = sc.logIn("TestUser1", "pass1");\r
+               for (Controller controller : controllers) {\r
+                       if(controller.getClass().equals(UserController.class))\r
+                               uc = (UserController)controller;\r
+               }\r
+\r
+               Resource resource1 = new Resource("Room101", "Class room");\r
+               Resource resource2 = new Resource("Room101", "Class room");\r
+               Resource resource3 = new Resource("Room101", "Class room");\r
+               dh.addResource(resource1);\r
+               dh.addResource(resource2);\r
+               dh.addResource(resource3);\r
+\r
+               List<DateRange> dtList = new ArrayList<DateRange>();\r
+               for (int i = 1; i < 10; i++) {\r
+                       dtList.add(new DateRangeImpl(new Date(2015, 12, 2 + i, 11, 00, 00), new Date(2015, 12, 2 + i, 12, 00, 00)));\r
+               }\r
 \r
 \r
+               assertEquals(null, uc.makeReservation(null, resource1, null, "remark"));\r
+       }\r
+       \r
        @SuppressWarnings("deprecation")\r
        @Test\r
        public void changeReservationTest() throws Exception {\r
        @SuppressWarnings("deprecation")\r
        @Test\r
        public void changeReservationTest() throws Exception {\r