forked from Harshil1823/Learning-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserListTest.java
More file actions
222 lines (194 loc) · 9.05 KB
/
UserListTest.java
File metadata and controls
222 lines (194 loc) · 9.05 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import static org.junit.jupiter.api.Assertions.*;
import java.io.ByteArrayInputStream;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class UserListTest {
private UserList userList;
@BeforeClass
public void oneTimeSetup() {
}
@AfterClass
public void oneTimeTearDown() {
}
@BeforeEach
public void setup() {
//runs before each test
userList = UserList.getInstance();
userList.clear();
}
@AfterEach
public void tearDown() {
//runs after each test
}
@Test
public void testGetInstanceReturnsNull() {
// Ensure that the instance is not null
UserList instance = UserList.getInstance();
assertEquals(UserList.class, instance.getClass());
}
@Test
public void testGetInstanceReturnsSameInstance() {
// Ensure that getInstance always returns the same instance
UserList instance1 = UserList.getInstance();
UserList instance2 = UserList.getInstance();
assertEquals(instance1, instance2);
}
@Test
public void testGetInstanceCalledTwiceReturnsSameInstance() {
// Ensure that calling getInstance twice in a row returns the same instance
UserList instance1 = UserList.getInstance();
UserList instance2 = UserList.getInstance();
assertEquals(instance1, instance2);
}
@Test
public void testAddUser() {
// Create a new user to add to the list
User user = new User("John", "Doe", "john.doe@example.com", null, null, null, null, null);
userList.addUser(user);
// Ensure that the user was added to the list
assertTrue(userList.contains(user));
}
@Test
public void testAddUserDoesNotAddDuplicate() {
// Create a new user to add to the list
User user = new User("John", "Doe", "john.doe@example.com", null, null, null, null, null);
userList.addUser(user);
// Add the same user again
userList.addUser(user);
// Ensure that the user was not added to the list twice
assertEquals(1, userList.size());
}
@Test
public void testAddUserAddsToEmptyList() {
// Ensure that the list is initially empty
assertEquals(0, userList.size());
// Create a new user to add to the list
User user = new User("John", "Doe", "john.doe@example.com", null, null, null, null, null);
userList.addUser(user);
// Ensure that the user was added to the list
assertEquals(1, userList.size());
assertTrue(userList.contains(user));
}
@Test
public void testGetUser() {
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
// Test getting a user with a valid username
User user = userList.getUser("johndoe");
assertNotNull(user);
assertEquals("John", user.getFirstName());
}
@Test
public void testGetUserNullUserName(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
// Test getting a user with a null username
User user = userList.getUser("null");
// Test getting a user with a null username
assertNull(user);
}
@Test
public void testGetUserWithInvalidUserName(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
// Test getting a user with a null username
User user = userList.getUser("kdlsjflkds");
// Test getting a user with an invalid username
assertNull(user);
}
@Test
public void testRemoveUser(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
userList.removeUser(user1);
assertEquals(1, userList.size());
}
@Test
public void testRemoveUserSameUser(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
userList.removeUser(user1);
assertEquals(1, userList.size());
//removing the same user again
userList.removeUser(user1);
assertEquals(1, userList.size());
}
@Test
public void testRemoveUserMutiple(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
userList.removeUser(user1);
userList.removeUser(user2);
assertEquals(0, userList.size());
}
@Test
public void testRemoveUserNull(){
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
userList.removeUser(null);
assertEquals(2, userList.size());
}
@Test
public void testClear() {
// add some users to the list
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
// make sure the list has 2 users
assertEquals(2, userList.size());
// clear the list
userList.clear();
// make sure the list is now empty
assertEquals(0, userList.size());
}
@Test
public void testContains() {
// create a user and add to the list
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
// check that the list contains the user
assertTrue(userList.contains(user1));
}
@Test
public void testContainsNotInList() {
// create a user and add to the list
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
// create another user, not in the list
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
// check that the list does not contain the new user
assertFalse(userList.contains(user2));
}
@Test
public void testSize() {
// add some users to the list
User user1 = new User("John", "Doe", "john.doe@example.com", "1234567890", "johndoe", "hashedpassword", "f558ac43-cc7a-4dcb-86ce-8720a3cf3d8e", null);
User user2 = new User("Jane", "Smith", "jane.smith@example.com", "0123456789", "jamessmith", "adsfadsf", "f558ac44-cc7a-4dcb-86ce-8720a3cf3d8e", null);
userList.addUser(user1);
userList.addUser(user2);
// check that the list size is 2
assertEquals(2, userList.size());
// remove a user from the list
userList.removeUser(user1);
// check that the list size is now 1
assertEquals(1, userList.size());
}
}