-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTest.java
More file actions
273 lines (221 loc) · 9.1 KB
/
UnitTest.java
File metadata and controls
273 lines (221 loc) · 9.1 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Field;
public class UnitTest {
//Unit Testing QueryBuilder class (trexei mia xara)
@Test
public void testQuery() {
// Sample data for testing
String[] questions = {"Question1", "Question2", "Question3"};
String[] answers = {"Answer1", "Answer2", "Answer3"};
String message = "Additional message";
// Call the query method and store the result
String result = QueryBuilder.query(questions, answers, message);
// Expected result based on the provided data
String expected = "Σε ενα πανεπιστημιο υπαρχουν 8 τμηματα: " +
"1) διεθνων ευρωπαικων και οικονομικων σπουδων, " +
"2) οικονομικων επιστημων, " +
"3) διοικητικης επιστημης και τεχνολογιας, " +
"4) οργανωσης και διοικησης επιχειρησεων, " +
"5) λογιστικης και χρηματοοικονομικης, " +
"6) μαρκετινγκ και επικοινωνιας, " +
"7) πληροφορικης, " +
"8) στατιστικης. " +
"Ενας μαθητης θελει να σπουδασει σε αυτο το πανεπιστημιο, αλλα δεν ξερει ποιο τμημα να διαλεξει. " +
"Ο μαθητης απαντησε στο παρακατω ερωτηματολογιο οσων αφορα στις προτιμησεις του: " +
"Question1 Answer1 Question2 Answer2 Question3 Answer3 Additional message";
// Check if the result matches the expected value
assertEquals(expected, result);
}
//Unit Testing Questions class (trexei mia xara)
@Test
public void testCreateQuestionsOnly() {
// Arrange
Questions.createQuestions(); // Initialize questions in fullQuestions array
// Act
String[] questionsOnly = Questions.createQuestionsOnly(Questions.fullQuestions);
// Assert
Assert.assertNotNull(questionsOnly);
Assert.assertEquals(User.answersLength, questionsOnly.length);
}
//Unit Testing User class
private User testUser;
@Before
public void setUp() {
User.createDefaultUsers(); // Initialize nullUser and guestUser
testUser = new User("TestUser", "TestPassword");
}
@Test
public void testSignUp() {
User signedUpUser = User.signUp("NewUser", "NewPassword");
assertNotNull(signedUpUser);
assertEquals("NewUser", signedUpUser.username);
assertEquals("NewPassword", signedUpUser.password);
assertTrue(User.UserList.contains(signedUpUser));
}
@Test
public void testSignUpWithInvalidCredentials() {
User invalidUser = User.signUp("", "");
assertEquals(User.nullUser, invalidUser);
}
@Test
public void testSignUpWithTakenUsername() {
User existingUser = User.signUp("Guest", "1234");
assertEquals(User.nullUser, existingUser);
}
@Test
public void testLogIn() {
User loggedInUser = User.logIn("TestUser", "TestPassword");
assertNotNull(loggedInUser);
assertEquals("TestUser", loggedInUser.username);
assertEquals("TestPassword", loggedInUser.password);
}
@Test
public void testLogInWithInvalidCredentials() {
User invalidUser = User.logIn("", "");
assertEquals(User.nullUser, invalidUser);
}
@Test
public void testLogInWithWrongPassword() {
User wrongPasswordUser = User.logIn("TestUser", "WrongPassword");
assertEquals(User.nullUser, wrongPasswordUser);
}
@Test
public void testLogInWithNonexistentUsername() {
User nonExistentUser = User.logIn("NonExistentUser", "SomePassword");
assertEquals(User.nullUser, nonExistentUser);
}
@Test
public void testClearAnswers() {
// Assuming clearAnswers method works as expected
testUser.clearAnswers();
for (String answer : testUser.answers) {
assertEquals("", answer);
}
}
@Test
public void testCountAnswers() {
// Assuming countAnswers method works as expected
assertEquals(0, testUser.countAnswers());
// Let's simulate answering a few questions
testUser.answers[0] = "Answer1";
testUser.answers[5] = "Answer2";
testUser.answers[10] = "Answer3";
assertEquals(3, testUser.countAnswers());
}
//Unit Test Labels class ( trexei mia xara )
@Test
public void testCreatePurpose() {
Labels.createPurpose();
Assert.assertNotNull("Purpose label is null", Labels.PURPOSE);
Assert.assertTrue("Purpose label is empty", Labels.PURPOSE.trim().length() > 0);
}
@Test
public void testCreateTeam() {
Labels.createTeam();
Assert.assertNotNull("Team label is null", Labels.TEAM);
Assert.assertTrue("Team label is empty", Labels.TEAM.trim().length() > 0);
}
@Test
public void testCreateHelp() {
Labels.createHelp();
Assert.assertNotNull("Help label is null", Labels.HELP);
Assert.assertTrue("Help label is empty", Labels.HELP.trim().length() > 0);
}
@Test
public void testCreateFAQ() {
Labels.createFAQ();
Assert.assertNotNull("FAQ label is null", Labels.FAQ);
Assert.assertTrue("FAQ label is empty", Labels.FAQ.trim().length() > 0);
}
@Test
public void testCreateLabels() {
Labels.createLabels();
Assert.assertNotNull("Purpose label is null", Labels.PURPOSE);
Assert.assertNotNull("Team label is null", Labels.TEAM);
Assert.assertNotNull("Help label is null", Labels.HELP);
Assert.assertNotNull("FAQ label is null", Labels.FAQ);
Assert.assertTrue("Purpose label is empty", Labels.PURPOSE.trim().length() > 0);
Assert.assertTrue("Team label is empty", Labels.TEAM.trim().length() > 0);
Assert.assertTrue("Help label is empty", Labels.HELP.trim().length() > 0);
Assert.assertTrue("FAQ label is empty", Labels.FAQ.trim().length() > 0);
}
//Unit Test Message class (trexei mia xara)
@Test
public void testRun() {
// Arrange
String expectedResponse = "Test response";
String messageContent = "Test message";
// Act
Message message = new Message(messageContent);
message.run();
// Assert
assertEquals(expectedResponse, Message.retResponse);
}
//Unit Testing ProgressBar
@Test
public void testProgressBar() {
final ProgressBar progressBar = new ProgressBar();
progressBar.start();
try {
// Sleep for some time to allow the progress bar to update
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Check if the progress bar's value has been updated
assertTrue(progressBar.bar.getValue() > 0);
// Check if the progress bar's string has been updated
assertNotNull(progressBar.bar.getString());
assertFalse(progressBar.bar.getString().isEmpty());
// Stop the progress bar thread
progressBar.interrupt();
}
@Test
public void testErrorCreation() throws Exception {
Error error1 = Error.invalidCredentials;
Field field = Error.class.getDeclaredField("errorName");
field.setAccessible(true);
assertEquals("Invalid Credentials", (String) field.get(error1));
field = Error.class.getDeclaredField("errorMessage");
field.setAccessible(true);
assertEquals("You have not entered valid credentials. Please try again", (String) field.get(error1));
}
/*
Epomenes klaseis
*/
private void executeTests() {
UnitTest unitTest = new UnitTest();
//Unit Testing QueryBuilder class
unitTest.testQuery();
// Unit Testing Questions class
unitTest.testCreateQuestionsOnly();
//Unit Testing User class
unitTest.testSignUp();
unitTest.testLogIn();
unitTest.testClearAnswers();
unitTest.testCountAnswers();
// Unit Testing User class
unitTest.testCreateDefaultUsers();
unitTest.testSignUp();
unitTest.testLogIn();
unitTest.testClearAnswers();
unitTest.testCountAnswers();
//Unit Test Labels class
unitTest.testCreatePurpose();
unitTest.testCreateTeam();
unitTest.testCreateHelp();
unitTest.testCreateFAQ();
unitTest.testCreateLabels();
//Unit Test Message class
unitTest.testMessage();
//Unit Testing ProgressBar
unitTest.testProgressBar()
//Unit Testing Error
unitTest.testErrorCreation();
}
}