-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserTest.java
204 lines (151 loc) · 5.96 KB
/
UserTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* This file is the Test class for the User class.
*
* TCSS 305 - Rentz
*/
package tests;
import model.User;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Objects;
import static org.junit.Assert.*;
/**
* @author hphobbs
* @version 1.0 Winter2020
*/
public class UserTest {
/** Instance Variable for the object. */
private User myUser;
/** Instance Variable for the object. */
private User myUser1;
@Before
public void setUp() {
myUser = new User("name", "password", true);
myUser1 = new User("name1", "password1", false);
}
/**
* Test method for {@link model.User#hashCode()}.
*/
@Test
public void testHashCode() {
// Test if hash code method returns the correct hash code value for the objects invoked
assertEquals("Hashcode failed",
Objects.hash(myUser.getMyName(), myUser.getMyPassword(),
myUser.getMyVIPStatus()),
myUser.hashCode());
// Test two objects with equal values
final User otherUser = new User("name", "password", true);
assertEquals("Hashcode method failed", myUser.hashCode(), otherUser.hashCode());
}
/**
* Test method for {@link model.User#getMyName()}.
*/
@Test
public void testGetMyName() {
// test if getter method returns the right user name for both constructors
assertEquals("Getter Failed", "name", myUser.getMyName());
}
/**
* Test method for {@link model.User#getMyPassword()}.
*/
@Test
public void testGetMyPassword() {
// test if getter method returns the right password for both constructors
assertEquals("Getter Failed", myUser.getMyPassword(), "password");
}
/**
* Test method for {@link model.User#getMyVIPStatus()}.
*/
@Test
public void testGetMyVIPStatus() {
// test if getter method returns the right VIP status for both constructors
assertEquals("Getter Failed", myUser.getMyVIPStatus(), true);
}
/**
* Test method for {@link model.User#toString()}.
*/
@Test
public void testToString() {
assertEquals("toString format is incorrect", myUser.toString(),
"User(name, password, true)");
}
/**
* Test the user(String, String, Boolean). Checks user name and password for null values.
* Checks if the values for user name, password and VIPstatus are the values expected. Test
* method for {@link model.User#User(java.lang.String, java.lang.String)}.
*/
@Test
public void testUserConstructorStringString() {
// checks whether the user name contains null values
assertNotNull("Object is null", myUser.getMyName());
// checks whether the password contains are null values,
assertNotNull("Object is null", myUser.getMyPassword());
// checks if user name equals name
assertEquals("Parameterized Constructor Failed", "name", myUser.getMyName());
// checks if user name equals password
assertEquals("Parameterized Constructor Failed", "password", myUser.getMyPassword());
}
/**
* Test the user(String, String, Boolean). Checks user name and password for null values.
* Checks if the values for user name, password and VIPstatus are the values expected.
* {@link model.User#User(java.lang.String, java.lang.String, java.lang.Boolean)}.
*/
@Test
public void testUserStringStringBoolean() {
// checks whether the user name contains null values
assertNotNull("Object is null", myUser.getMyName());
// checks whether the password contains are null values,
assertNotNull("Object is null", myUser.getMyPassword());
// checks if user name equals name
assertEquals("Parameterized Costructor Failed", myUser.getMyName(), "name");
// checks if user name equals password
assertEquals("Parameterized Costructor Failed", myUser.getMyPassword(), "password");
// checks if user name equals vipStatus
assertEquals("Parameterized Costructor Failed", myUser.getMyVIPStatus(), true);
}
/***/
@Test(expected = IllegalArgumentException.class)
public void nameEmptyString3Parameters() {
new User("", "password", true);
}
/***/
@Test(expected = IllegalArgumentException.class)
public void passwordEmptyString3Parameters() {
new User("name", "", true);
}
/***/
@Test(expected = IllegalArgumentException.class)
public void nameEmptyString() {
new User("", "password1");
}
/***/
@Test(expected = IllegalArgumentException.class)
public void passwordEmptyString() {
new User("name1", "");
}
/**
* Test method for {@link model.User#equals(java.lang.Object)}.
*/
@Test
public void testEqualsObjectPositive() {
final User userEqual = new User("name", "password", true);
// test if equals() reflexive property is satisfied
// 1stparameter.equals(2ndparameter)
assertEquals("equals() failed - reflexive", myUser, myUser);
// test two different objects with same values
assertEquals("Test failed", myUser, userEqual);
}
/**
* Test method for {@link model.User#equals(java.lang.Object)}.
*/
@Test
public void testEqualsObjectNegative() {
// test object for null values
assertNotEquals("equals() failed- null object", myUser, null);
// test two objects with different values
assertNotEquals("equals() failed- missmatch values", myUser, myUser1);
// test if the object is from a different class
assertNotEquals("equals() failed- wrong class", myUser, new ArrayList<Integer>());
}
}