Exception handling added in UserTest
[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 Möglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es
28 //eine allfällige 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() {
39 User testUser = new User(new String(),"pass");
40 }
41
42 @Test(expected=IllegalArgumentException.class)
43 public void createUserInvalidPasswordTest() {
44 User testUser = new User("TestUser", new String());
45 }
46
47 @Test
48 public void checkPasswordTest() throws Exception{
49 User testUser = new User("TestUser", "pass");
50 assertTrue(testUser.checkPassword("pass"));
51 assertFalse(testUser.checkPassword("notmypass"));
52 assertFalse(testUser.checkPassword(null));
53 }
54
55 @Test
56 public void changePasswordTest() throws Exception{
57 User testUser = new User("TestUser", "pass");
58 assertTrue(testUser.checkPassword("pass"));
59 testUser.changePassword("newpass");
60 assertTrue(testUser.checkPassword("newpass"));
61 assertFalse(testUser.checkPassword("pass"));
62 assertFalse(testUser.checkPassword("notmypass"));
63 assertFalse(testUser.checkPassword(null));
64 }
65
66 @Test(expected=IllegalArgumentException.class)
67 public void changePasswordInvalidPasswordTest() {
68 User testUser = new User("TestUser", "pass");
69 testUser.changePassword(null);
70 }
71
72 @Test
73 public void setMailTest() {
74 User testUser = new User("TestUser", "pass");
75 String goodMail = "grosm9@bfh.ch";
76 String wrongMail1 = "ioajfd.ch";
77 String wrongMail2 = "asjdfh@fds";
78
79 assertTrue(testUser.setMail(goodMail));
80 assertFalse(testUser.setMail(wrongMail1));
81 assertFalse(testUser.setMail(wrongMail2));
82 assertFalse(testUser.setMail(null));
83 }
84
85 @Test
86 public void getMailTest() {
87 User testUser = new User("TestUser", "pass");
88 String mail = "grosm9@bfh.ch";
89
90 testUser.setMail(mail);
91 assertEquals(mail, testUser.getMail());
92 }
93
94 @Test
95 public void setAndGetNameTest() {
96 User testUser = new User("TestUser", "pass");
97 testUser.setName("Tester");
98 assertEquals("Tester",testUser.getName());
99 testUser.setName(null);
100 assertEquals(null, testUser.getName());
101 }
102
103 @Test
104 public void setAndGetLastnameTest() {
105 User testUser = new User("TestUser", "pass");
106 testUser.setLastname("Dummy");
107 assertEquals("Dummy", testUser.getLastname());
108 testUser.setLastname(null);
109 assertEquals(null, testUser.getLastname());
110 }
111 }