-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitTester.java
More file actions
1209 lines (1052 loc) · 51.5 KB
/
UnitTester.java
File metadata and controls
1209 lines (1052 loc) · 51.5 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
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
import java.io.File;
import java.util.Scanner;
import java.lang.ClassNotFoundException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.lang.InstantiationException;
import java.lang.IllegalAccessException;
import java.lang.IllegalArgumentException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Arrays;
import java.util.stream.*;
/** UnitTester.java
This class will complete unit tests for student functions and programs
@author Peter Olson
@version 10/01/21
*/
public class UnitTester {
/* The name of the file containing the unit test data
FORMAT
#keywords,go,here,delimited,by,commas#After the second hashtag, the description of the method being tested goes here. If there are more than one unit test, they are separated by the pipe symbol "|".
1 2 3 & true | 1 2 3 & false <-- do not include the arrow or this description. The input values are on the left hand side of the data and are delimited by spaces. The output is on the
4 0 -2 & false | 5 6 -2 & true <-- right-hand side of the '&' symbol and denotes the return value. Currently, any primitive data type can be accepted as inputs or outputs
15 5 1 & true <-- if a method predominately allows for two different outputs for a given set of data, it is still okay to have just one unit test on a line
#keywords,go,here,for,second,method#This begins the second method. Note that the keywords help identify which method to search for.
any kind of primitive will do & 10 | inputs strings and outputs are ints & 5 <-- an example where the total number of inputs are 6 strings, and the output is an integer
...
This class does not take unit tests for the main function. The text file data for unit tests may include
any number of units tests and any number of methods to be tested that can be found in the java file
*/
public final String CURRENT_UNIT_TEST_FILE;
//didn't actually need to use this mapping
private static final Map<Class<?>, Class<?>> primitiveToWrapper = Map.of(
void.class, Void.class,
boolean.class, Boolean.class,
byte.class, Byte.class,
char.class, Character.class,
short.class, Short.class,
int.class, Integer.class,
long.class, Long.class,
float.class, Float.class,
double.class, Double.class );
public enum DataType {
BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, CHAR, STRING, BOOLEAN, NULL
}
/**
Default constructor
@param currentUnitTestFile The test file that is used to run the unit test
@see CURRENT_UNIT_TEST_FILE
*/
public UnitTester( String currentUnitTestFile ) {
CURRENT_UNIT_TEST_FILE = currentUnitTestFile;
}
/**
Runs the unit tests with the CURRENT_UNIT_TEST_FILE information
with the given student java file
@param javaFile The java file to be read
@param unitTestFile The text document of unit tests to read
@return int The number of errors found when running unit tests
@see CURRENT_UNIT_TEST_FILE in Checker.java
@see Class.forName( String className )
@see Class.getDeclaredMethods()
@see sortMethodList( Method[] methodList, Scanner fileScanner )
@see runUnitTests( Method[] sortedMethodList, Scanner fileScanner )
*/
public int runUnitTests( File javaFile, String unitTestFile ) {
Class className = null;
//Get Class object from java file
try {
className = Class.forName( javaFile.getName().replace(".java", "") );
} catch( ClassNotFoundException e ) {
e.printStackTrace();
}
//Create file scanner.
Scanner fileScanner = null;
try {
fileScanner = new Scanner( new File( CURRENT_UNIT_TEST_FILE ) );
} catch( FileNotFoundException e ) {
e.printStackTrace();
}
//Get list of methods in class
Method[] methodList = className.getDeclaredMethods();
//Sort methods into correct order as identified by the CURRENT_UNIT_TEST_FILE
Method[] sortedMethodList = sortMethodList( methodList, fileScanner );
//@@DEBUG - print method list
/*
for( int i = 0; i < sortedMethodList.length; i++ ) {
SOPln("Method #" + (i+1) + ": " + sortedMethodList[i].getName() );
}
*/
//Renew the file scannner
try {
fileScanner = new Scanner( new File( CURRENT_UNIT_TEST_FILE ) );
} catch( FileNotFoundException e ) {
e.printStackTrace();
}
//run the unit tests for each method
int totalUnitTestErrors = runUnitTests( className, sortedMethodList, fileScanner );
return totalUnitTestErrors;
}
/**
Runs the unit tests stored in the CURRENT_UNIT_TEST_FILE
This method scans the text document and converts String input data,
which is used to invoke the student java file methods and produce an output
The outputs are then compared, and if they are different than the expected
output that appears in the file, it is counted as an error. The total number
of errors across all unit tests for all relevant methods is returned
@param className
@param sortedMethodList The list of methods, which have been sorted into the proper order
(matching with the text file order)
@param fileScanner The scanner object which is scanning the CURRENT_UNIT_TEST_FILE text file
@return int The total number of unit test error which have been incurred across the
relevant methods being tested
@see Class.getParameterTypes()
@see Method.getName()
@see Method.invoke( Object obj, Object ... args )
@see Exception.printStackTrace();
@see Method.getReturnType()
@see Arrays.toString(...)
@see Object.getClass()
*/
private int runUnitTests( Class className, Method[] sortedMethodList, Scanner fileScanner ) {
Object classObj = null;
try {
classObj = className.newInstance();
} catch( InstantiationException e ) {
e.printStackTrace();
} catch( IllegalAccessException e ) {
e.printStackTrace();
}
int totalUnitTestErrors = 0;
int methodCounter = -1; //keeps track of the method the program is on
int testNumber = 1;
while( fileScanner.hasNextLine() ) {
String line = fileScanner.nextLine();
//First # will allow index to start at zero
if( line.contains("#") ) {
methodCounter++;
continue;
}
boolean testPassed = false; //used if the first test passed as to not penalize the second test if it does not pass
String[] unitTestDataList = line.split("\\|");
int indexAdjustment = 0;
//run all result options
for( int i = 0; i < unitTestDataList.length; i++ ) {
String[] unitTest = unitTestDataList[i].split("&");
//get parameter types
Class<?>[] classList = sortedMethodList[ methodCounter ].getParameterTypes();
//get String inputs and outputs
String[] inputs = unitTest[0].trim().split(" ");
String output = unitTest[1].trim();
Object[] expectedResult = new Object[inputs.length];
//determine type of inputs and outputs using Objects to cast
Object[] parameterData = new Object[ inputs.length ];
DataType listDataType = DataType.NULL;
//catch mismatch errors for comparing method parameter count to file parameter count
if( classList.length != inputs.length ) {
SOPln("Method parameters and file parameters do not match.");
SOPln("Line: " + line);
SOPln("Method: " + sortedMethodList[ methodCounter ].getName() );
}
//Match input data to correct object type
for( int j = 0; j < classList.length; j++ ) {
if ( classList[j].getName().equals("int") ) parameterData[j] = Integer.valueOf ( inputs[j] );
else if( classList[j].getName().equals("double") ) parameterData[j] = Double.valueOf ( inputs[j] );
else if( classList[j].getName().equals("char") ) parameterData[j] = Character.valueOf( inputs[j].charAt(0) );
else if( classList[j].getName().equals("java.lang.String") ) parameterData[j] = inputs[j] ;
else if( classList[j].getName().equals("boolean") ) parameterData[j] = Boolean.valueOf ( inputs[j] );
else if( classList[j].getName().equals("float") ) parameterData[j] = Float.valueOf ( inputs[j] );
else if( classList[j].getName().equals("long") ) parameterData[j] = Long.valueOf ( inputs[j] );
else if( classList[j].getName().equals("short") ) parameterData[j] = Short.valueOf ( inputs[j] );
else if( classList[j].getName().equals("byte") ) parameterData[j] = Byte.valueOf ( inputs[j] );
else if( classList[j].isArray() ) {
String arrayName = classList[j].getComponentType().getName(); //Data type of list, eg. int
//System.out.println("Name is: " + arrayName);
if( arrayName.equals("int") ) {
Integer[] parameterList = convertObjToIntList( parseList( inputs[j], "int" ) );
parameterData[j] = intList( parameterList );
} else if( arrayName.equals("double") ) {
Double[] parameterList = convertObjToDoubleList( parseList( inputs[j], "double" ) );
parameterData[j] = doubleList( parameterList );
} else if( arrayName.equals("char") ) {
Character[] parameterList = convertObjToCharList( parseList( inputs[j], "char" ) );
parameterData[j] = charList( parameterList );
} else if( arrayName.equals("java.lang.String") ) {
String[] parameterList = convertObjToStringList( parseList( inputs[j], "java.lang.String" ) );
parameterData[j] = parameterList;
} else if( arrayName.equals("boolean") ) {
Boolean[] parameterList = convertObjToBooleanList( parseList( inputs[j], "boolean" ) );
parameterData[j] = booleanList( parameterList );
} else if( arrayName.equals("float") ) {
Float[] parameterList = convertObjToFloatList( parseList( inputs[j], "float" ) );
parameterData[j] = floatList( parameterList );
} else if( arrayName.equals("long") ) {
Long[] parameterList = convertObjToLongList( parseList( inputs[j], "long" ) );
parameterData[j] = longList( parameterList );
} else if( arrayName.equals("short") ) {
Short[] parameterList = convertObjToShortList( parseList( inputs[j], "short" ) );
parameterData[j] = shortList( parameterList );
} else if( arrayName.equals("byte") ) {
Byte[] parameterList = convertObjToByteList( parseList( inputs[j], "byte" ) );
parameterData[j] = byteList( parameterList );
//2D Arrays
} else if( arrayName.contains("[") ) {
//SOPln("data type name: " + arrayName ); //Should print something of the form "[D", where the [ represents an array and D is for Double
String dataName = classList[j].getComponentType().getComponentType().getName();
//SOPln("data name: " + dataName); //Should print the primitive data type
listDataType = parse2DList( parameterData, j, inputs[j], dataName );
}
}
}
//get return type of method
Class<?> returnClass = sortedMethodList[ methodCounter ].getReturnType();
//Use different return value structure in order to trick compiler for data parsing and conversion
boolean returnTypeIs2DList = false;
Object[][] returnValue2D = new Object[parameterData.length][parameterData.length];
if( returnClass.getName().contains("[[") ) {
returnTypeIs2DList = true;
}
//get return value
Object[] returnValue = new Object[parameterData.length];
try {
if( returnTypeIs2DList )
returnValue2D[0][0] = sortedMethodList[ methodCounter ].invoke( classObj, parameterData );
else
returnValue[0] = sortedMethodList[ methodCounter ].invoke( classObj, parameterData );
} catch( IllegalAccessException e ) {
e.printStackTrace();
} catch( IllegalArgumentException e ) {
e.printStackTrace();
} catch( InvocationTargetException e ) {
e.printStackTrace();
//SOPln("Column entry 0-6 detected. Trying second test..."); //@@TEMP -- This line and next only needed for Project 3 ConnectFour
//continue;
}
//For 2D arrays, instead of trying to compare their data literally, just compare their String forms instead
if( returnTypeIs2DList ) {
returnValue2D[0][0] = convertToString( returnValue2D[0][0], returnClass.getComponentType().getComponentType().getName() );
returnValue2D[0][0] = format2DList( (String)returnValue2D[0][0] );
}
//Match output data to correct object type
if ( returnClass.getName().equals("int") ) expectedResult[0] = Integer.valueOf ( output );
else if( returnClass.getName().equals("double") ) expectedResult[0] = Double.valueOf ( output );
else if( returnClass.getName().equals("char") ) expectedResult[0] = Character.valueOf( output.charAt(0) );
else if( returnClass.getName().equals("java.lang.String") ) expectedResult[0] = output ;
else if( returnClass.getName().equals("boolean") ) expectedResult[0] = Boolean.valueOf ( output );
else if( returnClass.getName().equals("float") ) expectedResult[0] = Float.valueOf ( output );
else if( returnClass.getName().equals("long") ) expectedResult[0] = Long.valueOf ( output );
else if( returnClass.getName().equals("short") ) expectedResult[0] = Short.valueOf ( output );
else if( returnClass.getName().equals("byte") ) expectedResult[0] = Byte.valueOf ( output );
else if( returnClass.isArray() ) {
String arrayName = returnClass.getComponentType().getName(); //Data type of list, eg. int
//System.out.println("Name is: " + arrayName);
if( arrayName.equals("int") ) {
Integer[] parameterList = convertObjToIntList( parseList( output, "int" ) );
expectedResult[0] = parameterList;
listDataType = DataType.INT;
} else if( arrayName.equals("double") ) {
Double[] parameterList = convertObjToDoubleList( parseList( output, "double" ) );
expectedResult[0] = parameterList;
listDataType = DataType.DOUBLE;
} else if( arrayName.equals("char") ) {
Character[] parameterList = convertObjToCharList( parseList( output, "char" ) );
expectedResult[0] = parameterList;
listDataType = DataType.CHAR;
} else if( arrayName.equals("java.lang.String") ) {
String[] parameterList = convertObjToStringList( parseList( output, "java.lang.String" ) );
expectedResult[0] = parameterList;
listDataType = DataType.STRING;
} else if( arrayName.equals("boolean") ) {
Boolean[] parameterList = convertObjToBooleanList( parseList( output, "boolean" ) );
expectedResult[0] = parameterList;
listDataType = DataType.BOOLEAN;
} else if( arrayName.equals("float") ) {
Float[] parameterList = convertObjToFloatList( parseList( output, "float" ) );
expectedResult[0] = parameterList;
listDataType = DataType.FLOAT;
} else if( arrayName.equals("long") ) {
Long[] parameterList = convertObjToLongList( parseList( output, "long" ) );
expectedResult[0] = parameterList;
listDataType = DataType.LONG;
} else if( arrayName.equals("short") ) {
Short[] parameterList = convertObjToShortList( parseList( output, "short" ) );
expectedResult[0] = parameterList;
listDataType = DataType.SHORT;
} else if( arrayName.equals("byte") ) {
Byte[] parameterList = convertObjToByteList( parseList( output, "byte" ) );
expectedResult[0] = parameterList;
listDataType = DataType.BYTE;
} else if( arrayName.contains("[") ) {
//2D lists are saved as Strings
expectedResult[0] = output;
listDataType = DataType.STRING;
}
}
//Convert returnValue[0] back to Wrapper data type array
if( !returnTypeIs2DList && expectedResult[0].getClass().isArray() )
returnValue[0] = toWrapper( returnValue[0], listDataType );
//Print out unit test results
if( !returnTypeIs2DList && returnValue[0].getClass() == expectedResult[0].getClass() && !testPassed ) {
if( returnValue[0].getClass().isArray() ) {
if( Arrays.equals( (Object[])returnValue[0], (Object[])expectedResult[0] ) ) {
SOPln("Test #" + testNumber + " Passed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
Arrays.toString( (Object[])returnValue[0] ) + ", Expected Output: " + output);
if( i == 0 )
testPassed = true;
testNumber++;
// Can't fail first optional test
} else if( (( i != 0 && unitTestDataList.length > 1 ) || ( i == 0 && unitTestDataList.length == 1 )) ) {
//Unequal lists
SOPln("Test #" + testNumber + " Failed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
Arrays.toString( (Object[])returnValue[0] ) + ", Expected Output: " + output);
testNumber++;
totalUnitTestErrors++;
testPassed = false;
}
} else if( returnValue[0].equals( expectedResult[0] ) ) {
SOPln("Test #" + testNumber + " Passed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
String.valueOf( returnValue[0] ) + ", Expected Output: " + output);
if( i == 0 )
testPassed = true;
testNumber++;
} else if( (( i != 0 && unitTestDataList.length > 1 ) || ( i == 0 && unitTestDataList.length == 1 )) ) {
SOPln("Test #" + testNumber + " Failed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
String.valueOf( returnValue[0] ) + ", Expected Output: " + output);
testNumber++;
totalUnitTestErrors++;
testPassed = false;
}
} else if( !returnTypeIs2DList && !testPassed && (( i != 0 && unitTestDataList.length > 1 ) ||
( i == 0 && unitTestDataList.length == 1 )) ) {
if( returnValue.getClass().isArray() ) {
SOPln("Test #" + testNumber + " Failed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
Arrays.toString( (Object[])returnValue[0] ) + ", Expected Output: " + output);
testNumber++;
} else {
SOPln("Test #" + testNumber + " Failed! Incompatible outputs. Inputs: " + Arrays.toString( inputs ) + ", Output: " +
String.valueOf( returnValue[0] ) + ", Expected Output: " + output);
testNumber++;
}
totalUnitTestErrors++;
testPassed = false;
}
if( returnTypeIs2DList ) {
if( returnValue2D[0][0].equals( expectedResult[0] ) ) {
SOPln("Test #" + testNumber + " Passed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
String.valueOf( returnValue2D[0][0] ) + ", Expected Output: " + output);
if( i == 0 )
testPassed = true;
testNumber++;
} else if( (( i != 0 && unitTestDataList.length > 1 ) || ( i == 0 && unitTestDataList.length == 1 )) ) {
SOPln("Test #" + testNumber + " Failed! Inputs: " + Arrays.toString( inputs ) + ", Output: " +
String.valueOf( returnValue2D[0][0] ) + ", Expected Output: " + output);
testNumber++;
totalUnitTestErrors++;
testPassed = false;
}
}
//Only run 1 test
if( testPassed ) break;
}//end for
}
return totalUnitTestErrors;
}
/**
Format a 2D list that has been converted to a String. Specifically, convert decimals that are whole numbers
to be whole numbers (remove .0s)
@param string2DList The 2D list in String format
@return String The formatted String
*/
private String format2DList( String string2DList ) {
if( string2DList.endsWith(".0") )
string2DList = string2DList.substring(0, string2DList.length() - 2);
string2DList = string2DList.replaceAll(".0,",",");
string2DList = string2DList.replaceAll(".0\\$","\\$");
return string2DList;
}
/**
Convert a 2D array to its String one-liner representation
@param returnValue An array of return values (only need 1), which stores Strings
@param tempReturnValue An array of return values (only need 1), which stores a 2D array of Objects
@return DataType The String representation of the 2D array
*/
private String convertToString( Object returnValue, String dataType ) {
String stringForm = "";
String rowSeparator = "$";
String colSeparator = ",";
DataType dataEnum = DataType.NULL;
Object[][] grid = toWrapper( returnValue, dataType );
for( int row = 0; row < grid.length; row++ ) {
for( int col = 0; col < grid[row].length; col++ ) {
switch( dataType ) {
case "byte": stringForm += String.valueOf( ( (Byte)grid[row][col] ).byteValue() ); break;
case "short": stringForm += String.valueOf( ( (Short)grid[row][col] ).shortValue() ); break;
case "int": stringForm += String.valueOf( ( (Integer)grid[row][col] ).intValue() ); break;
case "long": stringForm += String.valueOf( ( (Long)grid[row][col] ).longValue() ); break;
case "float": stringForm += String.valueOf( ( (Float)grid[row][col] ).floatValue() ); break;
case "double": stringForm += String.valueOf( ( (Double)grid[row][col] ).doubleValue() ); break;
case "char": stringForm += String.valueOf( ( (Character)grid[row][col] ).charValue() ); break;
case "boolean": stringForm += String.valueOf( ( (Boolean)grid[row][col] ).booleanValue() ); break;
case "String": stringForm += (String)grid[row][col]; break;
default: SOPln("Unrecognized Object in Object[][] grid."); break;
}
if( col != grid[row].length - 1 )
stringForm += colSeparator;
}
if( row != grid.length - 1 )
stringForm += rowSeparator;
}
return stringForm;
}
/**
Parses the String input data representing a 2D list and converts it to the corresponding data type Wrapper class lists, before
converting the wrapper class lists to primitive data type lists. This is done for each row of the 2D array before being set to the
correct position in the parameterData array, which now contains a 2D array as one of its elements
@param parameterData The array of parameter Objects
@param parameterIndex The index of which Object to be set
@param string2DList The String representing the 2D list, delimiter by $ for rows, and commas for elements in each row
@param dataType A String representing the data type stored in the 2D array
@return DataType [ENUM] The type of data being looked at
*/
private DataType parse2DList( Object[] parameterData, int parameterIndex, String string2DList, String dataType ) {
String[] rowList = string2DList.split("\\$");
int rows = rowList.length;
int cols = rowList[0].split(",").length;
DataType dataEnum = DataType.NULL;
//If you think is ugly, blame Java's lack of generics for primitive data types, not me --> can't stuff a primitive data type grid in a 2D object array
if( dataType.equals("byte") ) {
dataEnum = DataType.BYTE;
byte[][] byteGrid = new byte[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Byte[] parameterList = convertObjToByteList( parseList( rowList[rep], "byte" ) );
byteGrid[rep] = byteList( parameterList );
}
parameterData[parameterIndex] = byteGrid;
} else if( dataType.equals("short") ) {
dataEnum = DataType.SHORT;
short[][] shortGrid = new short[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Short[] parameterList = convertObjToShortList( parseList( rowList[rep], "short" ) );
shortGrid[rep] = shortList( parameterList );
}
parameterData[parameterIndex] = shortGrid;
} else if( dataType.equals("int") ) {
dataEnum = DataType.INT;
int[][] intGrid = new int[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Integer[] parameterList = convertObjToIntList( parseList( rowList[rep], "int" ) );
intGrid[rep] = intList( parameterList );
}
parameterData[parameterIndex] = intGrid;
} else if( dataType.equals("long") ) {
dataEnum = DataType.LONG;
long[][] longGrid = new long[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Long[] parameterList = convertObjToLongList( parseList( rowList[rep], "long" ) );
longGrid[rep] = longList( parameterList );
}
parameterData[parameterIndex] = longGrid;
} else if( dataType.equals("float") ) {
dataEnum = DataType.FLOAT;
float[][] floatGrid = new float[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Float[] parameterList = convertObjToFloatList( parseList( rowList[rep], "float" ) );
floatGrid[rep] = floatList( parameterList );
}
parameterData[parameterIndex] = floatGrid;
} else if( dataType.equals("double") ) {
dataEnum = DataType.DOUBLE;
double[][] doubleGrid = new double[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Double[] parameterList = convertObjToDoubleList( parseList( rowList[rep], "double" ) );
doubleGrid[rep] = doubleList( parameterList );
}
parameterData[parameterIndex] = doubleGrid;
} else if( dataType.equals("char") ) {
dataEnum = DataType.CHAR;
char[][] charGrid = new char[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Character[] parameterList = convertObjToCharList( parseList( rowList[rep], "char" ) );
charGrid[rep] = charList( parameterList );
}
parameterData[parameterIndex] = charGrid;
} else if( dataType.equals("boolean") ) {
dataEnum = DataType.BOOLEAN;
boolean[][] booleanGrid = new boolean[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
Boolean[] parameterList = convertObjToBooleanList( parseList( rowList[rep], "boolean" ) );
booleanGrid[rep] = booleanList( parameterList );
}
parameterData[parameterIndex] = booleanGrid;
} else if( dataType.equals("java.lang.String") ) {
dataEnum = DataType.STRING;
String[][] stringGrid = new String[rows][cols];
for( int rep = 0; rep < rows; rep++ ) {
String[] parameterList = convertObjToStringList( parseList( rowList[rep], "java.lang.String" ) );
stringGrid[rep] = parameterList;
}
parameterData[parameterIndex] = stringGrid;
} else {
SOPln("Error. Unsupported 2D array of Objects of unknown Class.");
}
return dataEnum;
}
/**
Converts an array of primitives to their respective wrapper data types
@param returnData A primitive array
@param dataType [ENUM] The type of primitive data composing returnData
@return Object[] A wrapper data type array
*/
private Object[] toWrapper( Object returnData, DataType dataType ) {
Object[] returnValue;
switch( dataType ) {
case BYTE: returnValue = toByteWrapper( (byte[])returnData ); break;
case SHORT: returnValue = toShortWrapper( (short[])returnData ); break;
case INT: returnValue = toIntWrapper( (int[])returnData ); break;
case LONG: returnValue = toLongWrapper( (long[])returnData ); break;
case FLOAT: returnValue = toFloatWrapper( (float[])returnData ); break;
case DOUBLE: returnValue = toDoubleWrapper( (double[])returnData ); break;
case CHAR: returnValue = toCharWrapper( (char[])returnData ); break;
case BOOLEAN: returnValue = toBooleanWrapper( (boolean[])returnData ); break;
default: SOPln("Error. Invalid listDataType enum, for returnValue[0]");
returnValue = null; break;
}
return returnValue;
}
/**
Converts an array of primitives to their respective wrapper data types
@param returnData An object containing some kind of 2D array
@param dataType The type of primitive data composing returnData
@return Object[][] A wrapper data type 2D array
*/
private Object[][] toWrapper( Object returnData, String dataType ) {
Object[][] grid;
switch( dataType ) {
case "byte": grid = toByteWrapper( (byte[][])returnData ); break;
case "short": grid = toShortWrapper( (short[][])returnData ); break;
case "int": grid = toIntWrapper( (int[][])returnData ); break;
case "long": grid = toLongWrapper( (long[][])returnData ); break;
case "float": grid = toFloatWrapper( (float[][])returnData ); break;
case "double": grid = toDoubleWrapper( (double[][])returnData ); break;
case "char": grid = toCharWrapper( (char[][])returnData ); break;
case "boolean": grid = toBooleanWrapper( (boolean[][])returnData ); break;
case "java.lang.String": grid = (String[][])returnData; break;
default: SOPln("Data type not found."); grid = null; break;
}
return grid;
}
/**
Convert a byte array to a Byte array
@param oldList The array of bytes
@return Byte[] The array of Bytes
*/
private Byte[] toByteWrapper( byte[] oldList ) {
Byte[] newArray = new Byte[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Byte.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D byte array to a 2D Byte array
@param oldList The 2D array of bytes
@return Byte[][] The 2D array of Bytes
*/
private Byte[][] toByteWrapper( byte[][] oldList ) {
Byte[][] newArray = new Byte[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Byte.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a short array to a Short array
@param oldList The array of shorts
@return Short[] The array of Shorts
*/
private Short[] toShortWrapper( short[] oldList ) {
Short[] newArray = new Short[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Short.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D short array to a 2D Short array
@param oldList The 2D array of shorts
@return Short[][] The 2D array of Shorts
*/
private Short[][] toShortWrapper( short[][] oldList ) {
Short[][] newArray = new Short[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Short.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert an int array to an Integer array
@param oldList The array of ints
@return Integer[] The array of Integers
*/
private Integer[] toIntWrapper( int[] oldList ) {
Integer[] newArray = new Integer[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Integer.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D int array to a 2D Integer array
@param oldList The 2D array of ints
@return Integer[][] The 2D array of Integers
*/
private Integer[][] toIntWrapper( int[][] oldList ) {
Integer[][] newArray = new Integer[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Integer.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a long array to a Long array
@param oldList The array of longs
@return Long[] The array of Longs
*/
private Long[] toLongWrapper( long[] oldList ) {
Long[] newArray = new Long[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Long.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D long array to a 2D Long array
@param oldList The 2D array of longs
@return Long[][] The 2D array of Longs
*/
private Long[][] toLongWrapper( long[][] oldList ) {
Long[][] newArray = new Long[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Long.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a float array to a Float array
@param oldList The array of floats
@return Float[] The array of Floats
*/
private Float[] toFloatWrapper( float[] oldList ) {
Float[] newArray = new Float[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Float.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D float array to a 2D Float array
@param oldList The 2D array of floats
@return Float[][] The 2D array of Floats
*/
private Float[][] toFloatWrapper( float[][] oldList ) {
Float[][] newArray = new Float[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Float.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a double array to a Double array
@param oldList The array of doubles
@return Double[] The array of Doubles
*/
private Double[] toDoubleWrapper( double[] oldList ) {
Double[] newArray = new Double[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Double.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D double array to a 2D Double array
@param oldList The 2D array of doubles
@return Double[][] The 2D array of Doubles
*/
private Double[][] toDoubleWrapper( double[][] oldList ) {
Double[][] newArray = new Double[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Double.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a char array to a Character array
@param oldList The array of chars
@return Character[] The array of Characters
*/
private Character[] toCharWrapper( char[] oldList ) {
Character[] newArray = new Character[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Character.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D char array to a 2D Character array
@param oldList The 2D array of chars
@return Character[][] The 2D array of Characters
*/
private Character[][] toCharWrapper( char[][] oldList ) {
Character[][] newArray = new Character[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Character.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a boolean array to a Boolean array
@param oldList The array of booleans
@return Boolean[] The array of Booleans
*/
private Boolean[] toBooleanWrapper( boolean[] oldList ) {
Boolean[] newArray = new Boolean[oldList.length];
for( int rep = 0; rep < oldList.length; rep++ )
newArray[rep] = Boolean.valueOf( oldList[rep] );
return newArray;
}
/**
Convert a 2D boolean array to a 2D Boolean array
@param oldList The 2D array of booleans
@return Boolean[][] The 2D array of Booleans
*/
private Boolean[][] toBooleanWrapper( boolean[][] oldList ) {
Boolean[][] newArray = new Boolean[oldList.length][oldList[0].length];
for( int row = 0; row < oldList.length; row++ )
for( int col = 0; col < oldList[row].length; col++ )
newArray[row][col] = Boolean.valueOf( oldList[row][col] );
return newArray;
}
/**
Convert a Byte array to a byte array
@param oldList The array of Bytes
@return double[] The array of bytes
*/
private byte[] byteList( Byte[] oldList ) {
byte[] tempArray = new byte[ oldList.length ];
int i = 0;
for( Byte d : oldList )
tempArray[i++] = (byte)d;
return tempArray;
}
/**
Convert a Short array to a short array
@param oldList The array of Shorts
@return short[] The array of shorts
*/
private short[] shortList( Short[] oldList ) {
short[] tempArray = new short[ oldList.length ];
int i = 0;
for( Short d : oldList )
tempArray[i++] = (short)d;
return tempArray;
}
/**
Convert an Integer array to an int array
@param oldList The array of Integers
@return int[] The array of ints
*/
private int[] intList( Integer[] oldList ) {
int[] tempArray = new int[ oldList.length ];
int i = 0;
for( Integer d : oldList )
tempArray[i++] = (int)d;
return tempArray;
}
/**
Convert a Long array to a long array
@param oldList The array of Longs
@return long[] The array of longs
*/
private long[] longList( Long[] oldList ) {
long[] tempArray = new long[ oldList.length ];
int i = 0;
for( Long d : oldList )
tempArray[i++] = (long)d;
return tempArray;
}
/**
Convert a Float array to a float array
@param oldList The array of Floats
@return float[] The array of floats
*/
private float[] floatList( Float[] oldList ) {
float[] tempArray = new float[ oldList.length ];
int i = 0;
for( Float d : oldList )
tempArray[i++] = (float)d;
return tempArray;
}
/**
Convert a Double array to a double array
@param oldList The array of Doubles
@return double[] The array of doubles
*/
private double[] doubleList( Double[] oldList ) {
double[] tempArray = new double[ oldList.length ];
int i = 0;
for( Double d : oldList )
tempArray[i++] = (double)d;
return tempArray;
}
/**
Convert a Character array to a char array
@param oldList The array of Characters
@return char[] The array of chars
*/
private char[] charList( Character[] oldList ) {
char[] tempArray = new char[ oldList.length ];
int i = 0;
for( Character d : oldList )
tempArray[i++] = (char)d;
return tempArray;
}
/**
Convert a Boolean array to a boolean array
@param oldList The array of Booleans
@return boolean[] The array of booleans
*/
private boolean[] booleanList( Boolean[] oldList ) {
boolean[] tempArray = new boolean[ oldList.length ];
int i = 0;
for( Boolean d : oldList )
tempArray[i++] = (boolean)d;
return tempArray;
}
/**
Parse a String delimiter by commas and return it as a list of the given data type
@param strList A list of data separated by commas
@param dataType A String representing the data type of the list
@return Object[] An array of data of type dataType
*/
private Object[] parseList( String strList, String dataType ) {
String[] list = strList.split(",");
Object[] objList = new Object[list.length];
for( int rep = 0; rep < list.length; rep++ ) {
if( dataType.equals("int") )
objList[rep] = Integer.valueOf(list[rep]);
else if( dataType.equals("double") )
objList[rep] = Double.valueOf(list[rep]);
else if( dataType.equals("float") )
objList[rep] = Float.valueOf(list[rep]);
else if( dataType.equals("char") )
objList[rep] = Character.valueOf(list[rep].charAt(0));
else if( dataType.equals("java.lang.String") )
objList[rep] = list[rep];
else if( dataType.equals("boolean") )
objList[rep] = Boolean.valueOf(list[rep]);
else if( dataType.equals("long") )
objList[rep] = Long.valueOf(list[rep]);
else if( dataType.equals("short") )
objList[rep] = Short.valueOf(list[rep]);
else if( dataType.equals("byte") )
objList[rep] = Byte.valueOf(list[rep]);
}
return objList;
}
/**
Returns the correct order of methods based on the keywords in the
CURRENT_UNIT_TEST_FILE document
This method is required in the case that one or more methods are
not within the correct order as outlined in the Java file
@param methodList The original list of methods, in order from which they
were written in the Java file
@param fileScanner The scanner which is reading the CURRENT_UNIT_TEST_FILE file
@return Method[] The list of methods in the correct order as listed within the
CURRENT_UNIT_TEST_FILE document
@see runUnitTests( File javaFile, Scanner javaScanner, Scanner fileScanner )
@see CURRENT_UNIT_TEST_FILE in the Checker.java class
@see Method.getName()
@see capitalizeFirstLetter( String str )
*/
private Method[] sortMethodList( Method[] methodList, Scanner fileScanner ) {
//List to keep track of methods remaining