-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMiniTesterJUnit.java
492 lines (426 loc) · 19.5 KB
/
MiniTesterJUnit.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import assignment1.*;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Tag;
import static org.junit.jupiter.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class MiniTesterJUnit { }
class SyntaxTest {
@Test
@Tag("score:0")
@DisplayName("Test if the expected class names exist")
void testClassNames() {
String[] expectedClassNames = {"Archer", "Worker", "Settler", "Tile", "Unit", "Warrior", "MilitaryUnit", "ListOfUnits"};
boolean allClassesExist = true;
for (String className : expectedClassNames) {
try {
Class.forName("assignment1." + className);
} catch (ClassNotFoundException e) {
System.out.println("Missing class: " + className);
allClassesExist = false;
}
}
// Print that all classes exist
assertTrue(allClassesExist, "Not all expected classes exist..");
System.out.println("All expected class names exist..");
}
@Test
@Tag("score:0")
@DisplayName("Test if Unit and MilitaryUnit are abstract")
void testAbstractClasses() {
// Test if Unit and MilitaryUnit are abstract
assertTrue(Modifier.isAbstract(Unit.class.getModifiers()), "Unit should be an abstract class..");
assertTrue(Modifier.isAbstract(MilitaryUnit.class.getModifiers()), "MilitaryUnit should be an abstract class..");
}
@Test
@Tag("score:0")
@DisplayName("Testing inheritance for Unit and MilitaryUnit")
void testInheritance() {
boolean allInheritanceCorrect = true;
allInheritanceCorrect &= isSubclass(Settler.class, Unit.class);
allInheritanceCorrect &= isSubclass(Worker.class, Unit.class);
allInheritanceCorrect &= isSubclass(MilitaryUnit.class, Unit.class);
allInheritanceCorrect &= isSubclass(Archer.class, MilitaryUnit.class);
allInheritanceCorrect &= isSubclass(Warrior.class, MilitaryUnit.class);
assertTrue(allInheritanceCorrect, "Not all inheritances are correct..");
}
@Test
@Tag("score:0")
@DisplayName("Test if all expected classes have a constructor")
void testIfAllClassesHaveAConstructor() {
String[] expectedClassNames = {"Archer", "Worker", "Settler", "Tile", "Unit", "Warrior", "MilitaryUnit"};
// Test if all classes have a constructor
boolean allClassesHaveAConstructor = true;
for (String className : expectedClassNames) {
try {
Constructor<?> constructor = Class.forName("assignment1." + className).getDeclaredConstructors()[0];
if (constructor.getParameterCount() == 0) {
System.out.println("Missing constructor for class: " + className);
allClassesHaveAConstructor = false;
}
} catch (ClassNotFoundException e) {
System.out.println("Missing class: " + className);
allClassesHaveAConstructor = false;
}
}
// Print that all classes have a constructor
assertTrue(allClassesHaveAConstructor, "Not all classes have a constructor..");
System.out.println("All classes have a constructor..");
}
@Test
@Tag("score:0")
@DisplayName("Test expected method names for each class")
void testMethodNamesForClasses() throws ClassNotFoundException {
boolean allMethodsExist = true;
//For Unit
allMethodsExist &= testMethodNamesForClass("Unit", "getPosition", "getHP", "getFaction", "moveTo", "receiveDamage", "equals");
//For Settler
allMethodsExist &= testMethodNamesForClass("Settler", "takeAction", "equals");
//For Worker
allMethodsExist &= testMethodNamesForClass("Worker", "takeAction", "equals");
//For MilitaryUnit
allMethodsExist &= testMethodNamesForClass("MilitaryUnit", "takeAction", "receiveDamage");
//For Warrior
allMethodsExist &= testMethodNamesForClass("Warrior", "equals");
//For Archer
allMethodsExist &= testMethodNamesForClass("Archer", "takeAction", "equals");
//For Tile
allMethodsExist &= testMethodNamesForClass("Tile", "getX", "getY", "isCity", "isImproved", "buildCity", "buildImprovement", "addUnit", "removeUnit", "selectWeakEnemy", "getDistance");
//For ListOfUnits
allMethodsExist &= testMethodNamesForClass("ListOfUnits", "getSize", "getList", "getUnit", "addUnit","indexOf", "removeUnit", "getArmy");
assertTrue(allMethodsExist, "Not all expected methods exist..");
System.out.println("All expected method names exist..");
}
@Test
@Tag("score:0")
@DisplayName("Test that Unit Class has abstract method takeAction")
void testAbstractMethod() {
// Test that Unit Class has abstract method takeAction
boolean hasAbstractMethodTakeAction = false;
for (Method method : Unit.class.getDeclaredMethods()) {
if(method.getName().equals("takeAction") && Modifier.isAbstract(method.getModifiers())) {
hasAbstractMethodTakeAction = true;
break;
}
}
assertTrue(hasAbstractMethodTakeAction, "Unit should have an abstract method takeAction..");
}
@Test
@Tag("score:0")
@DisplayName("Test that Tile class has a static method getDistance")
void testStaticMethod() {
boolean hasStaticMethodGetDistance = false;
for (Method method : Tile.class.getDeclaredMethods()) {
if(method.getName().equals("getDistance") && Modifier.isStatic(method.getModifiers())) {
hasStaticMethodGetDistance = true;
break;
}
}
assertTrue(hasStaticMethodGetDistance, "Tile class should have a static method getDistance..");
}
@Test
@Tag("score:0")
@DisplayName("Test if any expected declared fields are missing")
void testDeclaredFields() {
boolean allFieldsExist = true;
//For Unit
allFieldsExist &= checkFieldsForClass("Unit", Tile.class, double.class, int.class, String.class);
//For Worker
allFieldsExist &= checkFieldsForClass("Worker", int.class);
//For MilitaryUnit
allFieldsExist &= checkFieldsForClass("MilitaryUnit", double.class, int.class, int.class);
//For Archer
allFieldsExist &= checkFieldsForClass("Archer", int.class);
//For Tile
allFieldsExist &= checkFieldsForClass("Tile",int.class, int.class, boolean.class, boolean.class, ListOfUnits.class);
//For ListOfUnits
allFieldsExist &= checkFieldsForClass("ListOfUnits", Unit[].class, int.class);
// Warrior and Settler have no declared fields
assertTrue(allFieldsExist, "Not all expected declared fields exist..");
System.out.println("All expected declared fields exist..");
}
private boolean isSubclass(Class<?> childClass, Class<?> parentClass) {
if(!parentClass.isAssignableFrom(childClass)) {
System.out.println(childClass.getSimpleName() + " should be a subclass of " + parentClass.getSimpleName());
return false;
}
return true;
}
private boolean testMethodNamesForClass(String className, String... expectedMethodNames) throws ClassNotFoundException {
Class<?> clazz = Class.forName("assignment1." + className);
Method[] methods = clazz.getDeclaredMethods();
Map<String, Method> methodMap = new HashMap<>();
boolean allMethodsExist = true;
// Add all declared methods to a map for quick lookup
for (Method method : methods) {
methodMap.put(method.getName(), method);
}
// Check if each expected method name exists and print those that are missing
for (String methodName : expectedMethodNames) {
if (!methodMap.containsKey(methodName)) {
System.out.println("Missing expected method name in class " + className + ": " + methodName);
allMethodsExist = false;
}
}
return allMethodsExist;
}
private boolean checkFieldsForClass(String className, Class<?>... expectedFieldTypes) {
boolean classFieldsExist = true;
try {
Class<?> clazz = Class.forName("assignment1." + className);
Field[] fields = clazz.getDeclaredFields();
Set<Class<?>> expectedTypesSet = new HashSet<>();
for (Class<?> expectedFieldType : expectedFieldTypes) {
expectedTypesSet.add(expectedFieldType);
}
for (Field field : fields) {
if (expectedTypesSet.contains(field.getType())) {
expectedTypesSet.remove(field.getType());
}
}
if(!expectedTypesSet.isEmpty()) {
System.out.println("Missing expected declared field(s) in class " + className + ": " + expectedTypesSet);
classFieldsExist = false;
}
} catch (ClassNotFoundException e) {
// Handle class not found exception here if needed
return false;
}
return classFieldsExist;
}
}
class UnitTest { // Weight: 25%
@Test
@Tag("score:10")
@DisplayName("Basic exposed tests for methods in Unit Class")
void testMethodsUnitClass() {
Tile t1 = new Tile(0, 0);
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertEquals(unit.getPosition(), t1, "Unit getPosition() method did not work properly");
assertEquals(unit.getHP(), 50, "Unit getHP() method did not work properly");
assertEquals(unit.getFaction(), "Greeks", "Unit getFaction() method did not work properly");
unit.receiveDamage(30);
assertEquals(20.0, unit.getHP(), "Unit receiveDamage() method did not work properly");
Tile t2 = new Tile(1, 1);
assertTrue(unit.moveTo(t2), "Unit moveTo() method did not work properly");
}
}
class TileTest { // Weight: 20%
@Test
@Tag("score:8")
@DisplayName("Basic exposed tests for methods in Tile class")
void testMethodsTileClass() {
Tile t1 = new Tile(0, 1);
assertEquals(0, t1.getX(), "Tile getX() method did not work properly");
assertEquals(1, t1.getY(), "Tile getY() method did not work properly");
assertFalse(t1.isCity(), "Tile isCity() method did not work properly");
assertFalse(t1.isImproved(), "Tile isImproved() method did not work properly");
t1.buildCity();
assertTrue(t1.isCity(), "Tile buildCity() method did not work properly");
t1.buildImprovement();
assertTrue(t1.isImproved(), "Tile buildImprovement() method did not work properly");
}
}
class ListOfUnitsTest { // Weight: 15%
@Test
@Tag("score:6")
@DisplayName("Basic exposed tests for methods in ListOfUnits class")
void testMethodsLOUClass() {
ListOfUnits lou = new ListOfUnits();
Tile t1 = new Tile(0, 0);
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertEquals(0, lou.getSize(), "ListOfUnits getSize() method did not work properly");
assertEquals(0, lou.getList().length, "ListOfUnits getList() method did not work properly");
lou.addUnit(unit);
assertEquals(1, lou.getSize(), "ListOfUnits getSize() method did not work properly");
assertEquals(1, lou.getList().length, "ListOfUnits getList() method did not work properly");
assertNotNull(lou.getUnit(0), "ListOfUnits getUnit() method did not work properly");
assertEquals(0, lou.indexOf(lou.getUnit(0)), "ListOfUnits indexOf() method did not work properly");
assertTrue(lou.removeUnit(unit), "ListOfUnits removeUnit() method did not work properly");
assertEquals(0, lou.getSize(), "ListOfUnits getSize() method did not work properly");
MilitaryUnit mUnit = new Archer(t1, 50, "Greeks");
//Check that the military unit is inside
lou.addUnit(mUnit);
MilitaryUnit[] army = lou.getArmy();
assertEquals(1, army.length, "ListOfUnits getArmy() method did not work properly");
assertEquals(mUnit, army[0], "ListOfUnits getArmy() method did not work properly");
}
}
class SettlerTest { // Weight: 10%
@Test
@Tag("score:1")
@DisplayName("Test that equals methods of each class returns false as expected when checked with a different class")
void testMethodEqualsFalseSettler() {
Tile t1 = new Tile(0, 0);
Worker worker = new Worker(t1, 50, "Greeks");
Settler settler = new Settler(t1, 50, "Greeks");
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertFalse(settler.equals(unit), "Settler equals() method did not work properly");
assertFalse(settler.equals(worker), "Settler equals() method did not work properly");
assertFalse(settler.equals(t1), "Settler equals() method did not work properly");
}
@Test
@Tag("score:3")
@DisplayName("Basic exposed tests for takeAction methods in Settler")
void testMethodsSettlerClass() {
Tile position = new Tile(0, 0);
Settler settler = new Settler(position, 50, "Greeks");
settler.takeAction(position);
ListOfUnits listOfUnits = accessList(position);
assertEquals(-1,listOfUnits.indexOf(settler), "Settler takeAction() method did not work properly as unit was not removed");
}
private ListOfUnits accessList(Tile position) {
Field field = null;
Field[] fields = Tile.class.getDeclaredFields();
for(Field f : fields){
if(f.getType().equals(ListOfUnits.class)){
field = f;
break;
}
}
if (field == null) {
fail("ListOfUnits field not found in Tile class");
}
field.setAccessible(true);
try {
return (ListOfUnits) field.get(position);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
class WorkerTest { // Weight: 10%
@Test
@Tag("score:1")
@DisplayName("Test that equals methods of each class returns false as expected when checked with a different class")
void testMethodEqualsFalseWorker() {
Tile t1 = new Tile(0, 0);
Worker worker = new Worker(t1, 50, "Greeks");
Settler settler = new Settler(t1, 50, "Greeks");
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertFalse(worker.equals(unit), "Worker equals() method did not work properly");
assertFalse(worker.equals(settler), "Worker equals() method did not work properly");
assertFalse(unit.equals(t1), "Worker equals() method did not work properly");
}
@Test
@Tag("score:3")
@DisplayName("Basic exposed tests for takeAction methods in Worker class")
void testMethodsWorkerClass() {
Tile position = new Tile(0, 0);
Worker worker = new Worker(position, 50, "Greeks");
worker.takeAction(position);
ListOfUnits listOfUnits = accessList(position);
assertEquals(0,listOfUnits.indexOf(worker), "Worker takeAction() method did not work properly as unit was not removed");
}
private ListOfUnits accessList(Tile position) {
Field field = null;
Field[] fields = Tile.class.getDeclaredFields();
for(Field f : fields){
if(f.getType().equals(ListOfUnits.class)){
field = f;
break;
}
}
if (field == null) {
fail("ListOfUnits field not found in Tile class");
}
field.setAccessible(true);
try {
return (ListOfUnits) field.get(position);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
class MilitaryUnitTest {
//All methods are accessed in Archer and Warrior classes therefore not assessed in exposed tests
}
class ArcherTest { // Weight: 10%
@Test
@Tag("score:1")
@DisplayName("Test that equals methods of each class returns false as expected when checked with a different class")
void testMethodEqualsFalseArcher() {
Tile t1 = new Tile(0, 0);
Warrior warrior = new Warrior(t1, 50, "Greeks");
Archer archer = new Archer(t1, 50, "Greeks");
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertFalse(archer.equals(unit), "Archer equals() method did not work properly");
assertFalse(archer.equals(warrior), "Archer equals() method did not work properly");
assertFalse(archer.equals(t1), "Archer equals() method did not work properly");
}
@Test
@Tag("score:3")
@DisplayName("Basic test for takeAction in Archer class as well as receiveDamage in MilitaryUnit class")
void testMethodsArcherClass() throws IllegalAccessException {
Archer archer = new Archer(new Tile(0, 0), 50, "Greeks");
Field numberOfArrows = archer.getClass().getDeclaredFields()[0];
numberOfArrows.setAccessible(true);
assertEquals(5, numberOfArrows.get(archer));
// Test takeAction when numberOfArrows is 0
archer.takeAction(new Tile(1, 1));
assertEquals(4, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
archer.takeAction(new Tile(1, 1));
assertEquals(3, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
archer.takeAction(new Tile(1, 1));
assertEquals(2, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
archer.takeAction(new Tile(1, 1));
assertEquals(1, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
archer.takeAction(new Tile(1, 1));
assertEquals(0, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
archer.takeAction(new Tile(1, 1));
assertEquals(5, numberOfArrows.get(archer), "Archer takeAction() method did not work properly");
}
}
class WarriorTest { // Weight: 10%
@Test
@Tag("score:1")
@DisplayName("Test that equals methods of each class returns false as expected when checked with a different class")
void testMethodEqualsFalseWarrior() {
Tile t1 = new Tile(0, 0);
Warrior warrior = new Warrior(t1, 50, "Greeks");
Archer archer = new Archer(t1, 50, "Greeks");
Unit unit = new Unit(t1, 50, 2, "Greeks") {
@Override
public void takeAction(Tile tile) {
}
};
assertFalse(warrior.equals(unit), "Warrior equals() method did not work properly");
assertFalse(warrior.equals(archer), "Warrior equals() method did not work properly");
assertFalse(warrior.equals(t1), "Warrior equals() method did not work properly");
}
@Test
@Tag("score:3")
@DisplayName("Test methods for Warrior class")
void testMethodsWarriorClass() {
Warrior warrior = new Warrior(new Tile(0, 0), 50, "Greeks");
warrior.receiveDamage(10);
assertEquals(42, warrior.getHP(), "Warrior receiveDamage() method did not work properly");
}
}