Exception handling added in UserTest
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / core / UserTest.java
CommitLineData
7f912ebe
M
1/*\r
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
3 *\r
4 * Project Smart Reservation System.\r
5 *\r
6 * Distributable under GPL license. See terms of license at gnu.org.\r
7 */\r
8package ch.bfh.ti.srs.core;\r
9\r
b1b9997a
ED
10import static org.junit.Assert.assertEquals;\r
11import static org.junit.Assert.assertFalse;\r
12import static org.junit.Assert.assertTrue;\r
7f912ebe 13\r
e8cbfd56 14import org.junit.Rule;\r
917b3554 15import org.junit.Test;\r
e8cbfd56 16import org.junit.rules.ExpectedException;\r
7f912ebe
M
17\r
18public class UserTest {\r
e8cbfd56
M
19 //@Rule\r
20 //public ExpectedException createUserException = ExpectedException.none();\r
21 \r
917b3554 22 @Test\r
e8cbfd56 23 public void createUserTest() throws Exception{\r
b1b9997a
ED
24 User testUser = new User("TestUser", "pass");\r
25 assertEquals("TestUser", testUser.getUserName());\r
e8cbfd56
M
26 \r
27 //Zweite Möglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es\r
28 //eine allfällige mit 'throws Exception' an junit weiterzuleiten.\r
29 //--------------------------------------------------------------------------------------------\r
30 /*createUserException.expect(IllegalArgumentException.class);\r
31 createUserException.expectMessage("Invalid username");\r
32 User wrongArgumentUser1 = new User(null, "1234");\r
33 createUserException.expectMessage("Invalid password");\r
34 User wrongArgumentUser2 = new User("TestUser2", null);*/\r
35 }\r
36 \r
37 @Test(expected=IllegalArgumentException.class)\r
38 public void createUserInvalidUsernameTest() {\r
39 User testUser = new User(new String(),"pass");\r
40 }\r
41 \r
42 @Test(expected=IllegalArgumentException.class)\r
43 public void createUserInvalidPasswordTest() {\r
44 User testUser = new User("TestUser", new String());\r
7f912ebe 45 }\r
b1b9997a 46\r
836c10c9 47 @Test\r
e8cbfd56 48 public void checkPasswordTest() throws Exception{\r
b1b9997a
ED
49 User testUser = new User("TestUser", "pass");\r
50 assertTrue(testUser.checkPassword("pass"));\r
51 assertFalse(testUser.checkPassword("notmypass"));\r
e8cbfd56 52 assertFalse(testUser.checkPassword(null));\r
d6a2afc6 53 }\r
e8cbfd56 54 \r
836c10c9 55 @Test\r
e8cbfd56 56 public void changePasswordTest() throws Exception{\r
b1b9997a
ED
57 User testUser = new User("TestUser", "pass");\r
58 assertTrue(testUser.checkPassword("pass"));\r
59 testUser.changePassword("newpass");\r
60 assertTrue(testUser.checkPassword("newpass"));\r
61 assertFalse(testUser.checkPassword("pass"));\r
62 assertFalse(testUser.checkPassword("notmypass"));\r
e8cbfd56
M
63 assertFalse(testUser.checkPassword(null));\r
64 }\r
65 \r
66 @Test(expected=IllegalArgumentException.class)\r
67 public void changePasswordInvalidPasswordTest() {\r
68 User testUser = new User("TestUser", "pass");\r
69 testUser.changePassword(null);\r
d6a2afc6 70 }\r
b1b9997a 71\r
836c10c9 72 @Test\r
d6a2afc6 73 public void setMailTest() {\r
b1b9997a 74 User testUser = new User("TestUser", "pass");\r
d6a2afc6
M
75 String goodMail = "grosm9@bfh.ch";\r
76 String wrongMail1 = "ioajfd.ch";\r
77 String wrongMail2 = "asjdfh@fds";\r
b1b9997a
ED
78\r
79 assertTrue(testUser.setMail(goodMail));\r
80 assertFalse(testUser.setMail(wrongMail1));\r
81 assertFalse(testUser.setMail(wrongMail2));\r
e8cbfd56 82 assertFalse(testUser.setMail(null));\r
d6a2afc6 83 }\r
b1b9997a 84\r
836c10c9 85 @Test\r
d6a2afc6 86 public void getMailTest() {\r
b1b9997a 87 User testUser = new User("TestUser", "pass");\r
d6a2afc6 88 String mail = "grosm9@bfh.ch";\r
b1b9997a
ED
89\r
90 testUser.setMail(mail);\r
91 assertEquals(mail, testUser.getMail());\r
7f912ebe
M
92 }\r
93\r
836c10c9 94 @Test\r
8ceddb90 95 public void setAndGetNameTest() {\r
b1b9997a
ED
96 User testUser = new User("TestUser", "pass");\r
97 testUser.setName("Tester");\r
98 assertEquals("Tester",testUser.getName());\r
e8cbfd56
M
99 testUser.setName(null);\r
100 assertEquals(null, testUser.getName());\r
8ceddb90 101 }\r
b1b9997a 102\r
836c10c9 103 @Test\r
61e1becd 104 public void setAndGetLastnameTest() {\r
b1b9997a
ED
105 User testUser = new User("TestUser", "pass");\r
106 testUser.setLastname("Dummy");\r
107 assertEquals("Dummy", testUser.getLastname());\r
e8cbfd56
M
108 testUser.setLastname(null);\r
109 assertEquals(null, testUser.getLastname());\r
8ceddb90 110 }\r
7f912ebe 111}\r