-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.java
275 lines (223 loc) · 7.87 KB
/
Test.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
import checker.Checkstyle;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
class Config {
private String homework;
private String description;
private Integer checkstyleScore;
private Integer homeworkDesignScore;
private Integer readmeScore;
private Integer gitScore;
private List<TestType> testTypes;
public String getHomework() {
return homework;
}
public void setHomework(final String homework) {
this.homework = homework;
}
public String getDescription() {
return description;
}
public void setDescription(final String description) {
this.description = description;
}
public Integer getCheckstyleScore() {
return checkstyleScore;
}
public void setCheckstyleScore(final Integer checkstyleScore) {
this.checkstyleScore = checkstyleScore;
}
public Integer getHomeworkDesignScore() {
return homeworkDesignScore;
}
public void setHomeworkDesignScore(final Integer homeworkDesignScore) {
this.homeworkDesignScore = homeworkDesignScore;
}
public Integer getReadmeScore() {
return readmeScore;
}
public void setReadmeScore(final Integer readmeScore) {
this.readmeScore = readmeScore;
}
public List<TestType> getTestTypes() {
return testTypes;
}
public void setTestTypes(final List<TestType> testTypes) {
this.testTypes = testTypes;
}
public Integer getGitScore() {
return gitScore;
}
public void setGitScore(Integer gitScore) {
this.gitScore = gitScore;
}
}
class TestType {
private Integer score;
private String type;
public Integer getScore() {
return score;
}
public void setScore(final Integer score) {
this.score = score;
}
public String getType() {
return type;
}
public void setType(final String type) {
this.type = type;
}
}
public final class Test {
private static final String IN_FOLDER = "in/";
private static final String REF_FOLDER = "ref/";
private static final String CHECKER_RESOURCES_FOLDER = "checker/resources/";
private static final File TEST_INPUTS_FILE = new File(CHECKER_RESOURCES_FOLDER + IN_FOLDER);
private static final String OUT_FILE = "results.out";
private static final File TEST_OUT_FILE = new File(OUT_FILE);
private static final File CONFIG_FILE = new File(CHECKER_RESOURCES_FOLDER + "config.json");
private static final int MAX_MILLISECONDS_PER_TEST = 1000;
private static int score = 0;
private static int totalScore = 0;
private Test() { }
/**
* Method to be called for testing the homework
* @param argv String[]
*/
public static void main(final String[] argv) {
runTests();
//preTestCleanUp();
System.exit(0);
}
private static Config loadConfig() {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.readValue(CONFIG_FILE, Config.class);
} catch (IOException e) {
System.out.println("Could not find config file.");
System.exit(-1);
}
return null;
}
private static void runTests() {
Config config = loadConfig();
totalScore = config.getCheckstyleScore();
int manualScore = config.getReadmeScore()
+ config.getHomeworkDesignScore()
+ config.getGitScore();
for (final File testFile: Objects.requireNonNull(TEST_INPUTS_FILE.listFiles())) {
String testFileName = testFile.getName();
preTestCleanUp();
final String[] testArgv = createTestArgv(testFile);
final Future<Object> future = createTimerTask(testArgv);
runTest(testFileName, config, future);
}
boolean checkstylePassed = Checkstyle.testCheckstyle();
if (checkstylePassed) {
score += config.getCheckstyleScore();
}
System.out.println("Total score: .......................... " + score
+ "/" + totalScore);
System.out.println("Up to "
+ manualScore
+ " points will be awarded manually by the teaching assistants."
+ " (README & GIT)");
System.out.println("The final value can be exceeded for great implementations.");
}
private static void runTest(
final String testFileName,
final Config config,
final Future<Object> task
) {
ObjectMapper objectMapper = new ObjectMapper();
File refFile = new File(CHECKER_RESOURCES_FOLDER + REF_FOLDER + testFileName);
try {
task.get(MAX_MILLISECONDS_PER_TEST, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
printMessage(testFileName, "Timeout");
return;
} catch (Exception e) {
printMessage(testFileName, "Program ended with exception: " + e.getMessage());
return;
} finally {
task.cancel(true);
}
if (!TEST_OUT_FILE.exists()) {
printMessage(testFileName, "Output file not found. Skipping test...");
} else {
try {
var actual = objectMapper.readTree(TEST_OUT_FILE);
var expected = objectMapper.readTree(refFile);
final int testScore = testMaxScore(config, testFileName);
totalScore += testScore;
if (expected.equals(actual)) {
score += testScore;
printMessage(testFileName, testScore + "/" + testScore, true);
} else {
printMessage(testFileName, "0/" + testScore, true);
}
} catch (IOException e) {
printMessage(testFileName,
"Output file badly formatted. Skipping test... + " + e.getMessage());
}
}
}
private static Future<Object> createTimerTask(final String[] argv) {
ExecutorService executor = Executors.newCachedThreadPool();
Callable<Object> task = () -> {
Main.main(argv);
return null;
};
return executor.submit(task);
}
private static String[] createTestArgv(final File testFile) {
List<String> listArgv = new ArrayList<>();
listArgv.add(testFile.getAbsolutePath());
listArgv.add(OUT_FILE);
String[] argv = new String[0];
return listArgv.toArray(argv);
}
private static void preTestCleanUp() {
TEST_OUT_FILE.delete();
}
private static void printMessage(
final String testFileName,
final String message
) {
printMessage(testFileName, message, false);
}
private static void printMessage(
final String testFileName,
final String message,
final boolean trail
) {
String fileName = testFileName.split("\\.")[0];
if (trail) {
System.out.println("[" + fileName + "]: ..................... " + message);
} else {
System.out.println("[" + fileName + "]: " + message);
}
}
private static int testMaxScore(
final Config config,
final String testFileName
) {
for (TestType testType: config.getTestTypes()) {
if (testFileName.contains(testType.getType())) {
return testType.getScore();
}
}
printMessage(testFileName, "Test score not found. Skipping test...");
return 0;
}
}