User and UserTest updated
[staff/due1/sed-hs15-srs-purple.git] / src / test / java / ch / bfh / ti / srs / core / UserTest.java
1 /*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8 package ch.bfh.ti.srs.core;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import org.junit.Rule;
15 import org.junit.Test;
16 import org.junit.rules.ExpectedException;
17
18 public class UserTest {
19 //@Rule
20 //public ExpectedException createUserException = ExpectedException.none();
21
22 @Test
23 public void createUserTest() throws Exception{
24 User testUser = new User("TestUser", "pass");
25 assertEquals("TestUser", testUser.getUserName());
26
27 //Zweite Moeglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es
28 //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.
29 //--------------------------------------------------------------------------------------------
30 /*createUserException.expect(IllegalArgumentException.class);
31 createUserException.expectMessage("Invalid username");
32 User wrongArgumentUser1 = new User(null, "1234");
33 createUserException.expectMessage("Invalid password");
34 User wrongArgumentUser2 = new User("TestUser2", null);*/
35 }
36
37 @Test(expected=IllegalArgumentException.class)
38 public void createUserInvalidUsernameTest() throws Exception{
39 User testUser = new User(null,"pass");
40 }
41
42 @Test(expected=IllegalArgumentException.class)
43 public void createUserTooShortUsernameTest() throws Exception{
44 User testUser = new User(new String(),"pass");
45 User testUser2 = new User(new String("tes"),"pass");
46 }
47
48 @Test(expected=IllegalArgumentException.class)
49 public void createUserInvalidPasswordTest() throws Exception{
50 User testUser = new User("TestUser", null);
51 }
52
53 @Test
54 public void checkPasswordTest() throws Exception{
55 User testUser = new User("TestUser", "pass");
56 assertTrue(testUser.checkPassword("pass"));
57 assertFalse(testUser.checkPassword("notmypass"));
58 }
59
60 @Test(expected=IllegalArgumentException.class)
61 public void checkPasswordInvalidTest() throws Exception{
62 User testUser = new User("TestUser", "pass");
63 assertTrue(testUser.checkPassword(null));
64 }
65
66 @Test
67 public void changePasswordTest() throws Exception{
68 User testUser = new User("TestUser", "pass");
69 assertTrue(testUser.checkPassword("pass"));
70 testUser.changePassword("newpass");
71 assertTrue(testUser.checkPassword("newpass"));
72 assertFalse(testUser.checkPassword("pass"));
73 assertFalse(testUser.checkPassword("notmypass"));
74 }
75
76 @Test(expected=IllegalArgumentException.class)
77 public void changePasswordInvalid1Test() throws Exception{
78 User testUser = new User("TestUser", "pass");
79 testUser.changePassword(null);
80 }
81
82 @Test(expected=IllegalArgumentException.class)
83 public void changePasswordInvalid2Test() throws Exception{
84 User testUser = new User("TestUser", "pass");
85 testUser.changePassword("pass");
86 }
87
88 @Test
89 public void setMailTest() throws Exception{
90 User testUser = new User("TestUser", "pass");
91 testUser.setMail("grosm9@bfh.ch");
92 }
93
94 @Test(expected=IllegalArgumentException.class)
95 public void setMailIllegal1Test() throws Exception{
96 User testUser = new User("TestUser", "pass");
97 testUser.setMail("fda.ds");
98
99 }
100
101 @Test(expected=IllegalArgumentException.class)
102 public void setMailIllegal2Test() throws Exception{
103 User testUser = new User("TestUser", "pass");
104 testUser.setMail("adfiug@fs");
105 }
106
107 @Test(expected=IllegalArgumentException.class)
108 public void setMailIllegal3Test() throws Exception{
109 User testUser = new User("TestUser", "pass");
110 testUser.setMail(null);
111 }
112
113 @Test
114 public void getMailTest() throws Exception{
115 User testUser = new User("TestUser", "pass");
116 String mail = "grosm9@bfh.ch";
117
118 testUser.setMail(mail);
119 assertEquals(mail, testUser.getMail());
120 }
121
122 @Test
123 public void setAndGetNameTest() throws Exception{
124 User testUser = new User("TestUser", "pass");
125 testUser.setName("Tester");
126 assertEquals("Tester",testUser.getName());
127 testUser.setName(null);
128 assertEquals(null, testUser.getName());
129 }
130
131 @Test
132 public void setAndGetLastnameTest() throws Exception{
133 User testUser = new User("TestUser", "pass");
134 testUser.setLastname("Dummy");
135 assertEquals("Dummy", testUser.getLastname());
136 testUser.setLastname(null);
137 assertEquals(null, testUser.getLastname());
138 }
139 }