-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP2Tester.java
1780 lines (1492 loc) · 64.6 KB
/
P2Tester.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Example of using unit tests for this assignment.
* To run them on the command line, make sure that
* the junit-cs211.jar is in the same directory.
* <p>
* On Mac/Linux:
* javac -cp .:junit-cs211.jar *.java # compile everything
* java -cp .:junit-cs211.jar P2Tester # run tests
* <p>
* On windows replace colons with semicolons: (: with ;)
* demo$ javac -cp .;junit-cs211.jar *.java # compile everything
* demo$ java -cp .;junit-cs211.jar P2Tester # run tests
*/
import org.junit.*;
import static org.junit.Assert.*;
import java.lang.reflect.*;
import java.util.*;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.Image;
public class P2Tester {
public static void main(String[] args) {
org.junit.runner.JUnitCore.main("P2Tester");
}
//// helper functions
// check whether class exists
private boolean isClassFound(String name) {
Class<?> c = null;
try {
c = Class.forName(name);
} catch (ClassNotFoundException cnfe) {
}
return c != null;
}
// assert class found
private void assertClassFound(String name) {
assertTrue(name + " class not found", isClassFound(name));
}
private void assertClassExists(String name)
{
try
{
Class<?> c = Class.forName(name);
}
catch (ClassNotFoundException cnfe)
{
fail("class " + name + " not found");
}
}
// get class when existing
private Class<?> getClassWhenExisting(String name) {
Class<?> c = null;
try {
c = Class.forName(name);
} catch (ClassNotFoundException cnfe) {
}
return c;
}
// check constructor
private void checkConstructor(Class<?> cls, Class<?>... parameterTypes) {
Constructor<?> c = null;
try {
c = cls.getConstructor(parameterTypes);
} catch (NoSuchMethodException nsme) {
}
assertTrue(cls.getName() + " constructor with correct parameters not found", c != null);
}
// check method
private void checkMethod(String className, String methodName, boolean checkPublic, boolean checkStatic, Class<?> returnType, Class<?>... parameterTypes) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Method m = null;
try {
Class[] cArg = (Class[]) parameterTypes;
m = c.getDeclaredMethod(methodName, cArg);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
assertTrue(methodName + " method not found", m != null);
assertTrue(methodName + " return type is incorrect", m.getReturnType() == returnType);
if (checkStatic)
assertTrue(methodName + " is not static", Modifier.isStatic(m.getModifiers()));
if (checkPublic)
assertTrue(methodName + " is not public", Modifier.isPublic(m.getModifiers()));
}
// check method when existing
private void checkMethodWhenExisting(String className, String methodName, boolean checkPublic, boolean checkStatic, Class<?> returnType, Class<?>... parameterTypes) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Method m = null;
try {
Class[] cArg = (Class[]) parameterTypes;
m = c.getDeclaredMethod(methodName, cArg);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if (m != null) {
// only check when existing
assertTrue(methodName + " return type is incorrect", m.getReturnType() == returnType);
if (checkStatic)
assertTrue(methodName + " is not static", Modifier.isStatic(m.getModifiers()));
if (checkPublic)
assertTrue(methodName + " is not public", Modifier.isPublic(m.getModifiers()));
}
}
// check class's fields
private void checkClassField(String className, String field, Class<?> type, boolean checkPublic, boolean checkStatic) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Field f = null;
try {
f = c.getField(field);
} catch (NoSuchFieldException e) {
}
assertTrue("Field " + field + " not found", f != null);
assertTrue("Field " + field + " should be " + type.getName(), f.getType() == type);
if (checkPublic)
assertTrue("Field " + field + " should be public", Modifier.isPublic(f.getModifiers()));
if (checkStatic)
assertTrue("Field " + field + " should be static", Modifier.isStatic(f.getModifiers()));
}
// get field
private Field getFieldWhenExisting(String className, String field) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Field f = null;
try {
f = c.getDeclaredField(field);
f.setAccessible(true); // required for private field
} catch (NoSuchFieldException e) {
}
assertTrue("Field " + field + " not found in class " + className, f != null);
return f;
}
// get method
private boolean isMethodExisting(String className, String methodName, Class<?>... parameterTypes) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Method m = null;
try {
Class[] cArg = (Class[]) parameterTypes;
m = c.getDeclaredMethod(methodName, cArg);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return m != null;
}
// does array contain element
private boolean contains(String[] array, String element) {
for (int i = 0; i < array.length; i++) {
if (array[i].equals(element))
return true;
}
return false;
}
private boolean methodsEqual(Method[] methods, String[] names) {
for (int i = 0; i < methods.length; i++) {
if (!contains(names, methods[i].getName())) return false;
}
return true;
}
// get all methods
private Method[] getAllMethods(Class<?> c, boolean includePublic, boolean includeProtected, boolean includePrivate, boolean isStatic) {
Method[] ms = null;
try {
ms = c.getDeclaredMethods();
} catch (SecurityException se) {
}
if (ms == null) return ms;
ArrayList<Method> result = new ArrayList<Method>();
boolean ispub = false;
boolean ispro = false;
boolean ispri = false;
boolean issta = false;
for (Method m : ms) {
ispub = Modifier.isPublic(m.getModifiers());
ispro = Modifier.isProtected(m.getModifiers());
ispri = Modifier.isPrivate(m.getModifiers());
issta = Modifier.isStatic(m.getModifiers());
if (isStatic == issta) {
if (includePublic && ispub) result.add(m);
else if (includeProtected && ispro) result.add(m);
else if (includePrivate && ispri) result.add(m);
}
}
return result.toArray(new Method[]{});
}
// get all fields
private Field[] getAllFields(Class<?> c, boolean includePublic, boolean includeProtected, boolean includePrivate, boolean splitStatic, boolean isStatic) {
Field[] fs = null;
try {
//get only the fields declared in the class, exclude inherited fields
fs = c.getDeclaredFields();
} catch (SecurityException se) {
}
if (fs == null) return fs;
ArrayList<Field> result = new ArrayList<Field>();
boolean ispub = false;
boolean ispro = false;
boolean ispri = false;
boolean issta = false;
for (Field f : fs) {
ispub = Modifier.isPublic(f.getModifiers());
ispro = Modifier.isProtected(f.getModifiers());
ispri = Modifier.isPrivate(f.getModifiers());
issta = Modifier.isStatic(f.getModifiers());
if (splitStatic) {
if (isStatic == issta) {
if (includePublic && ispub) result.add(f);
else if (includeProtected && ispro) result.add(f);
else if (includePrivate && ispri) result.add(f);
}
} else {
if (includePublic && ispub) result.add(f);
else if (includeProtected && ispro) result.add(f);
else if (includePrivate && ispri) result.add(f);
}
}
return result.toArray(new Field[]{});
}
// check class's all methods
private void checkAllPublicMethods(String className, String[] methods, boolean checkStatic) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Method[] ms = getAllMethods(c, true, false, false, checkStatic);
assertTrue("Public methods count check failed.", ms != null && ms.length == methods.length);
assertTrue("Public methods names check failed", methodsEqual(ms, methods));
}
// check no public fields
private void checkNoPublicFields(String className, boolean splitStatic, boolean isStatic) {
assertClassFound(className);
Class<?> c = getClassWhenExisting(className);
Field[] fs = getAllFields(c, true, false, false, splitStatic, isStatic);
assertTrue("Class " + className + " shouldn't have public fields", fs.length == 0);
}
// check private field through reflection
private void checkPrivateField(String className, String fieldName, Object expectedValue, Class<?>[] parameterTypes, Object... parameters) {
assertClassFound(className);
Class<?> cls = getClassWhenExisting(className);
Object obj = null;
try {
Constructor constructor = cls.getConstructor(parameterTypes);
obj = (Object) constructor.newInstance(parameters);
} catch (NoSuchMethodException nsme) {
} catch (InvocationTargetException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
assertTrue("Calling constructor failed", obj != null);
Field f = getFieldWhenExisting(className, fieldName);
Object value = null;
boolean accessFailed = false;
try {
value = (Object) f.get(obj);
} catch (IllegalAccessException iae) {
accessFailed = true;
}
assertTrue("Accessing private field " + fieldName + " failed", !accessFailed);
assertTrue("Private field " + fieldName + " check failed", value.equals(expectedValue));
}
// private check private field not null
private void checkPrivateFieldNotNull(String className, String fieldName, Class<?>[] parameterTypes, Object... parameters) {
assertClassFound(className);
Class<?> cls = getClassWhenExisting(className);
Object obj = null;
try {
Constructor constructor = cls.getConstructor(parameterTypes);
obj = (Object) constructor.newInstance(parameters);
} catch (NoSuchMethodException nsme) {
} catch (InvocationTargetException e) {
} catch (InstantiationException e) {
} catch (IllegalAccessException e) {
}
assertTrue("Calling constructor failed", obj != null);
Field f = getFieldWhenExisting(className, fieldName);
Object value = null;
boolean accessFailed = false;
try {
value = (Object) f.get(obj);
} catch (IllegalAccessException iae) {
accessFailed = true;
}
assertTrue("Accessing private field " + fieldName + " failed", !accessFailed);
assertTrue("Private field " + fieldName + " null check failed", value != null);
}
// reset all the configuration parameters to prevent the use of
// previously cached values
private void resetConfigurationParameters()
{
Configuration.ROWS = -1;
Configuration.COLS = -1;
Configuration.MINES = -1;
Configuration.CELL_SIZE = -1;
Configuration.BOARD_WIDTH = -1;
Configuration.BOARD_HEIGHT = -1;
Configuration.STATUS_COVERED = "";
Configuration.STATUS_OPENED = "";
Configuration.STATUS_MARKED = "";
Configuration.STATUS_WRONGLY_MARKED = "";
}
// Configuration
@Test(timeout = 1000)
public void test_Configuration_00() {
assertClassExists("Configuration");
}
@Test(timeout = 1000)
public void test_Configuration_01() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "ROWS", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_02() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "COLS", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_03() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "CELL_SIZE", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_04() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "MINES", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_05() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "BOARD_WIDTH", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_06() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "BOARD_HEIGHT", int.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_07() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "STATUS_COVERED", String.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_08() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "STATUS_OPENED", String.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_09() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "STATUS_MARKED", String.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_10() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "STATUS_WRONGLY_MARKED", String.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_11() {
String name = "Configuration";
assertClassFound(name);
checkClassField(name, "STATUS_WRONGLY_MARKED", String.class, true, true);
}
@Test(timeout = 1000)
public void test_Configuration_12() {
String name = "Configuration";
assertClassFound(name);
checkMethod(name, "loadParameters", true, true, void.class, String.class);
}
@Test(timeout = 1000)
public void test_Configuration_13() {
String name = "Configuration";
assertClassFound(name);
checkAllPublicMethods(name, new String[]{"loadParameters"}, true);
}
@Test(timeout = 1000)
public void test_Configuration_14() {
resetConfigurationParameters();
assertClassFound("Configuration");
Configuration.loadParameters("config1.test");
assertEquals("Configuration ROWS check failed", 20, Configuration.ROWS);
assertEquals("Configuration COLS check failed", 20, Configuration.COLS);
assertEquals("Configuration MINES check failed", 50, Configuration.MINES);
assertEquals("Configuration CELL_SIZE check failed", 15, Configuration.CELL_SIZE);
assertEquals("Configuration STATUS_COVERED check failed", "cover", Configuration.STATUS_COVERED);
assertEquals("Configuration STATUS_OPENED check failed", "open", Configuration.STATUS_OPENED);
assertEquals("Configuration STATUS_MARKED check failed", "mark", Configuration.STATUS_MARKED);
assertEquals("Configuration STATUS_WRONGLY_MARKED check failed", "wrongmark", Configuration.STATUS_WRONGLY_MARKED);
assertEquals("Configuration BOARD_WIDTH check failed", 301, Configuration.BOARD_WIDTH);
assertEquals("Configuration BOARD_HEIGHT check failed", 301, Configuration.BOARD_HEIGHT);
}
@Test(timeout = 1000)
public void test_Configuration_15() {
resetConfigurationParameters();
assertClassFound("Configuration");
Configuration.loadParameters("config2.test");
assertEquals("Configuration ROWS check failed", 38, Configuration.ROWS);
assertEquals("Configuration COLS check failed", 17, Configuration.COLS);
assertEquals("Configuration MINES check failed", 7, Configuration.MINES);
assertEquals("Configuration CELL_SIZE check failed", 15, Configuration.CELL_SIZE);
assertEquals("Configuration STATUS_COVERED check failed", "covered", Configuration.STATUS_COVERED);
assertEquals("Configuration STATUS_OPENED check failed", "opened", Configuration.STATUS_OPENED);
assertEquals("Configuration STATUS_MARKED check failed", "marked", Configuration.STATUS_MARKED);
assertEquals("Configuration STATUS_WRONGLY_MARKED check failed", "wrongly_marked", Configuration.STATUS_WRONGLY_MARKED);
assertEquals("Configuration BOARD_WIDTH check failed", 256, Configuration.BOARD_WIDTH);
assertEquals("Configuration BOARD_HEIGHT check failed", 571, Configuration.BOARD_HEIGHT);
}
private void check_Minefield_5() {
// 5. logic test: call the constructor and then count the number of MineCell objects to assert the number is correct
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
final int size = 10;
Minefield mf = new Minefield(size, size, size);
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (mf.getCellByRowCol(i, j) instanceof MineCell) count += 1;
}
}
assertTrue("the number of MineCell objects check failed", count == size);
}
private void check_Minefield_6() {
// 6. logic test: call the constructor and then count the number of InfoCell objects to assert the number is correct
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
final int size = 10;
Minefield mf = new Minefield(size, size, size);
int count = 0;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (mf.getCellByRowCol(i, j) instanceof InfoCell) count += 1;
}
}
assertTrue("the number of InfoCell objects check failed", count == size * size - size);
}
private void check_Minefield_11() {
// 11. logic test: call constructor and then assert getCellByRowCol
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
final int row = 7;
final int col = 7;
final int mines = 24;
Minefield mf = new Minefield(row, col, mines);
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
// get cell
Object cell = mf.getCellByRowCol(i, j);
assertTrue(String.format("Check Minefield at (%d,%d) failed", i, j), (cell instanceof MineCell) || (cell instanceof InfoCell));
}
}
}
private void check_Minefield_12() {
// 12. logic test: call loadParameters, then the constructor, and then assert getCellByScreenCoordinates
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
assertClassFound("Configuration");
Configuration.loadParameters("config1.test");
final int row = Configuration.ROWS;
final int col = Configuration.COLS;
final int mines = Configuration.MINES;
Minefield mf = new Minefield(row, col, mines);
for (int i = 0; i < Configuration.BOARD_WIDTH - 1; i++) {
for (int j = 0; j < Configuration.BOARD_HEIGHT - 1; j++) {
// get cell
Object cell = mf.getCellByScreenCoordinates(i, j);
assertTrue(String.format("Check Minefield at (%d,%d) failed", i, j), (cell instanceof MineCell) || (cell instanceof InfoCell));
}
}
}
private void check_Minefield_13() {
// 13. logic test: call setInfoCell and then assert by calling getCellByRowCol
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
final int row = 7;
final int col = 7;
final int mines = 24;
Minefield mf = new Minefield(row, col, mines);
InfoCell ic = new InfoCell(0, 0, 0);
mf.setInfoCell(0, 0, ic);
assertTrue("setInfoCell/getCellByRowCol check failed", ic.equals(mf.getCellByRowCol(0, 0)));
}
private void check_Minefield_14() {
// 14. logic test: call setMineCell and then assert by calling getCellByRowCol
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
final int row = 7;
final int col = 7;
final int mines = 24;
Minefield mf = new Minefield(row, col, mines);
MineCell mc = new MineCell(0, 0);
mf.setMineCell(0, 0, mc);
assertTrue("setMineCell/getCellByRowCol check failed", mc.equals(mf.getCellByRowCol(0, 0)));
}
private void check_Minefield_15() {
// 15. logic test: call constructor, then call setStatus to change the status of various cells, and then assert correctness of countCellsWithStatus
assertClassFound("Minefield");
assertClassFound("MineCell");
assertClassFound("InfoCell");
Configuration.loadParameters("config1.test");
final int row = 7;
final int col = 7;
final int mines = 24;
Minefield mf = new Minefield(row, col, mines);
// reset minefield
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
// fill with null
mf.setInfoCell(i, j, null);
}
}
// set info cell in the diagonal
for (int i = 0; i < row; i++)
mf.setInfoCell(i, i, new InfoCell(i, i, 0));
int count = mf.countCellsWithStatus("cover"); // 7
assertTrue("countCellsWithStatus check failed", count == row);
// set status
for (int i = 0; i < row; i++) {
InfoCell ic = (InfoCell) mf.getCellByRowCol(i, i);
ic.setStatus("open");
}
count = mf.countCellsWithStatus("open"); // 7
assertTrue("setStatus/countCellsWithStatus check failed", count == row);
}
private Minefield make_MineField_5x5_5mines_in_diagonal() {
// in 5x5 board, 5 mines in the diagonal
// ensure classes exist
assertClassFound("Board");
assertClassFound("Configuration");
assertClassFound("InfoCell");
assertClassFound("MineCell");
assertClassFound("Minefield");
// init Configuration
final int size = 5;
Configuration.ROWS = size;
Configuration.COLS = size;
Configuration.CELL_SIZE = size;
Configuration.MINES = size;
Configuration.BOARD_HEIGHT = size * size + 1;
Configuration.BOARD_WIDTH = size * size + 1;
Configuration.STATUS_COVERED = "cover";
Configuration.STATUS_MARKED = "mark";
Configuration.STATUS_OPENED = "open";
Configuration.STATUS_WRONGLY_MARKED = "wrongmark";
// change minefield, set mines in the diagonal,
Minefield minefield = new Minefield(Configuration.ROWS, Configuration.COLS, Configuration.MINES);
// reset minefield
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
// fill with null
minefield.setMineCell(i, j, null);
}
// set mine in the diagonal
minefield.setMineCell(i, i, new MineCell(i, i));
}
// call method to deploy infocells
minefield.addInfoCells();
return minefield;
}
private void check_Minefield_16() {
// 16. logic test: create a small minefield (e.g. 5x4) and hard-code the cells. Then change the status of an InfoCell to "uncovered" and call openCells(). Then check all the uncovered cells one by one to assert the correctness of openCells()
Minefield minefield = make_MineField_5x5_5mines_in_diagonal();
// [0, 3] is an infocell, if clicked, open its 8 neighbors:[0,2],[0,4],[1,2],[1,3],[1,4]
int row = 0;
int col = 3;
InfoCell cell = (InfoCell) minefield.getCellByRowCol(row, col);
cell.setStatus(Configuration.STATUS_OPENED);
minefield.openCells((Object) cell);
// check status
final int[][] opened = {
{0, 2},
{0, 3},
{0, 4},
{1, 2},
{1, 3},
{1, 4}
};
boolean pass = true;
for (int i = 0; i < opened.length; i++)
if (!((InfoCell) minefield.getCellByRowCol(opened[i][0], opened[i][1])).getStatus().equals(Configuration.STATUS_OPENED)) {
pass = false;
break;
}
assertTrue("openCells check failed", pass);
}
private void check_Minefield_17() {
// 17. logic test: create a small minefield (e.g. 5x4) and hard-code the cells. Then change the status of some InfoCells to "marked" and call revealIncorrectMarkedCells(). Then check the wrongly marked cells to assert that their status was changed to wrongly_marked
// left button on minecell -- STATUS_OPENED, gameover
// [1,1] is a mine cell
// right button on info cell, test wrong mark
// [2,1] is an infocell
Minefield minefield = make_MineField_5x5_5mines_in_diagonal();
InfoCell infoCell = (InfoCell) minefield.getCellByRowCol(2, 1);
infoCell.setStatus(Configuration.STATUS_MARKED);
// infocell status should be marked(actually wrongly mark)
minefield.revealIncorrectMarkedCells();
int number = minefield.countCellsWithStatus(Configuration.STATUS_WRONGLY_MARKED); // 1
assertTrue("revealIncorrectMarkedCells check failed", number == 1);
}
@Test(timeout = 1000)
public void test_Minefield_01() {
String name = "Minefield";
assertClassFound(name);
checkConstructor(getClassWhenExisting(name));
checkConstructor(getClassWhenExisting(name), int.class, int.class, int.class);
}
@Test(timeout = 1000)
public void test_Minefield_02() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkNoPublicFields(name, false, false);
}
@Test(timeout = 1000)
public void test_Minefield_03() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
if (isMethodExisting(name, "mineLaying", int.class, String.class)) {
// 2 mineLaying for honor section
checkAllPublicMethods(name, new String[]{"mineLaying", "mineLaying", "addInfoCells", "draw",
"getCellByScreenCoordinates", "getCellByRowCol", "setMineCell", "setInfoCell", "countCellsWithStatus",
"openCells", "revealIncorrectMarkedCells"}, false);
} else {
checkAllPublicMethods(name, new String[]{"mineLaying", "addInfoCells", "draw",
"getCellByScreenCoordinates", "getCellByRowCol", "setMineCell", "setInfoCell", "countCellsWithStatus",
"openCells", "revealIncorrectMarkedCells"}, false);
}
}
@Test(timeout = 1000)
public void test_Minefield_05() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_5();
}
@Test(timeout = 1000)
public void test_Minefield_06() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_6();
}
@Test(timeout = 1000)
public void test_Minefield_08() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "mineLaying", true, false, void.class, int.class);
// this is for honors section
checkMethodWhenExisting(name, "mineLaying", true, false, void.class, int.class, String.class);
}
@Test(timeout = 1000)
public void test_Minefield_09() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "addInfoCells", true, false, void.class);
}
@Test(timeout = 1000)
public void test_Minefield_10() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "draw", true, false, void.class, Graphics.class);
}
@Test(timeout = 1000)
public void test_Minefield_11() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "getCellByScreenCoordinates", true, false, Object.class, int.class, int.class);
}
@Test(timeout = 1000)
public void test_Minefield_12() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "getCellByRowCol", true, false, Object.class, int.class, int.class);
}
@Test(timeout = 1000)
public void test_Minefield_13() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "setMineCell", true, false, void.class, int.class, int.class, getClassWhenExisting("MineCell"));
}
@Test(timeout = 1000)
public void test_Minefield_14() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "setInfoCell", true, false, void.class, int.class, int.class, getClassWhenExisting("InfoCell"));
}
@Test(timeout = 1000)
public void test_Minefield_15() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "countCellsWithStatus", true, false, int.class, String.class);
}
@Test(timeout = 1000)
public void test_Minefield_16() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
checkMethod(name, "openCells", true, false, void.class, Object.class);
}
@Test(timeout = 1000)
public void test_Minefield_17() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_11();
}
@Test(timeout = 1000)
public void test_Minefield_18() {
check_Minefield_12();
}
@Test(timeout = 1000)
public void test_Minefield_19() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_13();
}
@Test(timeout = 1000)
public void test_Minefield_20() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_14();
}
@Test(timeout = 1000)
public void test_Minefield_21() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_15();
}
@Test(timeout = 1000)
public void test_Minefield_22() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_16();
}
@Test(timeout = 1000)
public void test_Minefield_23() {
String name = "Minefield";
assertClassFound(name);
assertClassFound("MineCell");
assertClassFound("InfoCell");
check_Minefield_17();
}
// MineCell
private void check_MineCell_7() {
// 7. logic tests: call the loadParameters() and the MineCell constructor and then check the return value of getHorizontalPosition(). Repeat for several MineCell objects
assertClassFound("Configuration");
assertClassFound("MineCell");
Configuration.loadParameters("config1.test");
int size = 10;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
MineCell mc = new MineCell(i, j);
assertTrue("getHorizontalPosition check failed", mc.getHorizontalPosition() == j * 15);
}
}
}
private void check_MineCell_12() {
// 12. logic tests: call the loadParameters() and the MineCell constructor and then check the return value of getVerticalPosition(). Repeat for several MineCell objects
assertClassFound("Configuration");
assertClassFound("MineCell");
Configuration.loadParameters("config1.test");
int size = 10;
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
MineCell mc = new MineCell(i, j);
assertTrue("getHorizontalPosition check failed", mc.getVerticalPosition() == i * 15);
}
}
}
private void check_MineCell_21() {
// 21. logic tests: call the loadParameters() and the MineCell constructor and then assert the getStatus value is correct. Do NOT use the variable Configuration.STATUS_COVERED but the String literal we have used in the config file.
assertClassFound("Configuration");
assertClassFound("MineCell");
Configuration.loadParameters("config1.test");
MineCell mc = new MineCell(1, 1);
assertTrue("getStatus check failed", mc.getStatus().equals("cover"));
}
private void check_MineCell_22() {
// 22. logic tests: call the setStatus and then the getStatus to assert they're correct. Repeat a few times.
assertClassFound("Configuration");
assertClassFound("MineCell");
Configuration.loadParameters("config1.test");
final String[] status = new String[]{"cover", "open", "mark"};
for (int i = 0; i < status.length; i++) {
MineCell mc = new MineCell(i, i);
mc.setStatus(status[i]);
assertTrue("setStatus check failed", mc.getStatus().equals(status[i]));
}
}
private void check_MineCell_27() {
// 27. logic tests: call the setStatus and then the getImage to assert it's correct. 3 separate tests, one for each status.
assertClassFound("Configuration");
assertClassFound("MineCell");
Configuration.loadParameters("config1.test");
final String[] status = new String[]{"cover", "open", "mark"};
final String[] imgName = new String[]{"img/covered_cell.png", "img/mine_cell.png", "img/marked_cell.png"};
MineCell mc = new MineCell(0, 0);
for (int i = 0; i < status.length; i++) {
mc.setStatus(status[i]);
// get image
assertTrue("getImage check failed when status is " + status[i], mc.getImage().equals(new ImageIcon(imgName[i]).getImage()));
}
}