Skip to content

Commit d853257

Browse files
committed
More refactoring.
1 parent eb9cc0e commit d853257

File tree

3 files changed

+228
-446
lines changed

3 files changed

+228
-446
lines changed

src/test/java/common/RaceTest.java

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package common;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import uk.ac.standrews.cs.utilities.FileManipulation;
5+
6+
import java.io.FileInputStream;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.*;
12+
import java.util.stream.Stream;
13+
14+
import static org.junit.jupiter.api.Assertions.*;
15+
16+
public abstract class RaceTest {
17+
18+
private Properties properties;
19+
private Path resources_expected_outputs;
20+
private Path temp_directory;
21+
private Path temp_output_sub_directory;
22+
23+
protected abstract Race makeRace(Properties properties) throws IOException;
24+
protected abstract String getResourcesPath();
25+
26+
protected void configureTest(String test_resource_root) throws IOException {
27+
28+
// Swap these when debugging and you don't want the test results to be immediately deleted.
29+
30+
temp_directory = Files.createTempDirectory(null);
31+
//temp_directory = Paths.get("/Users/gnck/Desktop/temp");
32+
33+
Path temp_input_sub_directory = Files.createDirectories(temp_directory.resolve("input"));
34+
temp_output_sub_directory = Files.createDirectories(temp_directory.resolve("output"));
35+
36+
Path resources_root = Paths.get("src/test/resources/" + getResourcesPath() + test_resource_root);
37+
Path resources_inputs = resources_root.resolve("input");
38+
resources_expected_outputs = resources_root.resolve("expected");
39+
Path resources_config = resources_inputs.resolve("config.txt");
40+
41+
copyFilesBetweenDirectories(resources_inputs, temp_input_sub_directory);
42+
43+
properties = getProperties(resources_config);
44+
properties.setProperty("WORKING_DIRECTORY", temp_directory.toString());
45+
}
46+
47+
private static void copyFilesBetweenDirectories(Path source, Path destination) throws IOException {
48+
49+
try (Stream<Path> list = Files.list(source)) {
50+
for (Iterator<Path> iterator = list.iterator(); iterator.hasNext(); ) {
51+
Path file = iterator.next();
52+
Files.copy(file, destination.resolve(file.getFileName()));
53+
}
54+
}
55+
}
56+
57+
private static Properties getProperties(Path path) throws IOException {
58+
59+
try (FileInputStream in = new FileInputStream(path.toFile())) {
60+
Properties properties = new Properties();
61+
properties.load(in);
62+
return properties;
63+
}
64+
}
65+
66+
@AfterEach
67+
public void tearDown() throws IOException {
68+
69+
// Disable this when debugging and you don't want the test results to be immediately deleted.
70+
71+
FileManipulation.deleteDirectory(temp_directory);
72+
}
73+
74+
public void testExpectedException(String configuration_name, String expected_error_message) throws Exception {
75+
76+
configureTest(configuration_name);
77+
78+
RuntimeException thrown = assertThrows(
79+
RuntimeException.class,
80+
() -> makeRace(properties).processResults()
81+
);
82+
83+
assertEquals(expected_error_message, thrown.getMessage());
84+
}
85+
86+
protected void testExpectedCompletion(String configuration_name) throws Exception {
87+
88+
configureTest(configuration_name);
89+
makeRace(properties).processResults();
90+
assertThatDirectoryContainsAllExpectedContent(resources_expected_outputs, temp_output_sub_directory);
91+
}
92+
93+
private static void assertThatDirectoryContainsAllExpectedContent(final Path expected, final Path actual) throws IOException {
94+
95+
final Set<String> directory_listing_expected = getDirectoryEntries(expected);
96+
97+
for (final String file_name : directory_listing_expected) {
98+
99+
if (!file_name.equals(".DS_Store")) {
100+
101+
final Path path_expected = expected.resolve(file_name);
102+
final Path path_actual = actual.resolve(file_name);
103+
104+
if (Files.isDirectory(path_expected)) {
105+
assertTrue(Files.isDirectory(path_actual));
106+
assertThatDirectoryContainsAllExpectedContent(path_expected, path_actual);
107+
} else {
108+
assertFalse(Files.isDirectory(path_actual));
109+
assertThatFilesHaveSameContent(path_expected, path_actual);
110+
}
111+
}
112+
}
113+
}
114+
115+
private static Set<String> getDirectoryEntries(final Path directory) throws IOException {
116+
117+
final Set<String> directory_listing = new HashSet<>();
118+
119+
try (Stream<Path> entries = Files.list(directory)) {
120+
for (Iterator<Path> iterator = entries.iterator(); iterator.hasNext(); ) {
121+
final Path file = iterator.next();
122+
directory_listing.add(file.getFileName().toString());
123+
}
124+
}
125+
126+
return directory_listing;
127+
}
128+
129+
private static void assertThatFilesHaveSameContent(final Path path1, final Path path2) throws IOException {
130+
131+
byte[] expected = Files.readAllBytes(path1);
132+
byte[] actual = Files.readAllBytes(path2);
133+
134+
if (!Arrays.equals(expected, actual) && !filesHaveSameContentIgnoreWhitespace(path1, path2))
135+
fail("Files differ: " + path1 + ", " + path2);
136+
}
137+
138+
private static boolean filesHaveSameContentIgnoreWhitespace(Path path1, Path path2) throws IOException {
139+
String fileContent1 = getFileContent(path1);
140+
String fileContent2 = getFileContent(path2);
141+
142+
if (!fileContent1.equals(fileContent2)) {
143+
System.out.println("f1: " + fileContent1);
144+
System.out.println("f2: " + fileContent2);
145+
}
146+
return fileContent1.equals(fileContent2);
147+
}
148+
149+
private static String getFileContent(Path path) throws IOException {
150+
151+
return Files.readAllLines(path).stream().reduce((s1, s2) -> s1 + s2).orElseThrow().replaceAll("\t", "").replaceAll("\n", "").replaceAll(" ", "");
152+
}
153+
}
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,26 @@
11
package individual_race.balmullo;
22

3+
import common.Race;
4+
import common.RaceTest;
35
import individual_race.IndividualRace;
4-
import org.junit.jupiter.api.AfterEach;
56
import org.junit.jupiter.api.Test;
6-
7-
import java.io.FileInputStream;
87
import java.io.IOException;
9-
import java.nio.file.Files;
10-
import java.nio.file.Path;
11-
import java.nio.file.Paths;
128
import java.util.*;
13-
import java.util.stream.Stream;
14-
15-
import static org.junit.jupiter.api.Assertions.*;
16-
17-
public class IndividualRaceTest {
189

19-
Properties properties;
20-
Path resources_expected_outputs;
21-
Path temp_directory;
22-
Path temp_output_sub_directory;
23-
24-
@AfterEach
25-
public void tearDown() throws IOException {
26-
27-
// Disable this when debugging and you don't want the test results to be immediately deleted.
28-
29-
//FileManipulation.deleteDirectory(temp_directory);
30-
}
10+
public class IndividualRaceTest extends RaceTest {
3111

3212
@Test
3313
public void simple() throws Exception {
34-
35-
processingCompletes("simple");
36-
}
37-
38-
private void processingCompletes(String configuration_name) throws Exception {
39-
40-
configureTest(configuration_name);
41-
new IndividualRace(properties).processResults();
42-
assertThatDirectoryContainsAllExpectedContent(resources_expected_outputs, temp_output_sub_directory);
14+
testExpectedCompletion("simple");
4315
}
4416

45-
private void configureTest(String test_resource_root) throws IOException {
46-
47-
// Swap these when debugging and you don't want the test results to be immediately deleted.
48-
49-
//temp_directory = Files.createTempDirectory(null);
50-
temp_directory = Paths.get("/Users/gnck/Desktop/temp");
51-
52-
Path temp_input_sub_directory = Files.createDirectories(temp_directory.resolve("input"));
53-
temp_output_sub_directory = Files.createDirectories(temp_directory.resolve("output"));
54-
55-
Path resources_root = Paths.get("src/test/resources/individual_race/balmullo/" + test_resource_root);
56-
Path resources_inputs = resources_root.resolve("input");
57-
resources_expected_outputs = resources_root.resolve("expected");
58-
Path resources_config = resources_inputs.resolve("config.txt");
59-
60-
copyFilesBetweenDirectories(resources_inputs, temp_input_sub_directory);
61-
62-
properties = getProperties(resources_config);
63-
properties.setProperty("WORKING_DIRECTORY", temp_directory.toString());
17+
@Override
18+
protected Race makeRace(Properties properties) throws IOException {
19+
return new IndividualRace(properties);
6420
}
6521

66-
private static void copyFilesBetweenDirectories(Path source, Path destination) throws IOException {
67-
68-
try (Stream<Path> list = Files.list(source)) {
69-
for (Iterator<Path> iterator = list.iterator(); iterator.hasNext(); ) {
70-
Path file = iterator.next();
71-
Files.copy(file, destination.resolve(file.getFileName()));
72-
}
73-
}
74-
}
75-
76-
private static Properties getProperties(Path path) throws IOException {
77-
78-
try (FileInputStream in = new FileInputStream(path.toFile())) {
79-
Properties properties = new Properties();
80-
properties.load(in);
81-
return properties;
82-
}
83-
}
84-
85-
private static void assertThatDirectoryContainsAllExpectedContent(final Path expected, final Path actual) throws IOException {
86-
87-
final Set<String> directory_listing_expected = getDirectoryEntries(expected);
88-
89-
for (final String file_name : directory_listing_expected) {
90-
91-
if (!file_name.equals(".DS_Store")) {
92-
93-
final Path path_expected = expected.resolve(file_name);
94-
final Path path_actual = actual.resolve(file_name);
95-
96-
if (Files.isDirectory(path_expected)) {
97-
assertTrue(Files.isDirectory(path_actual));
98-
assertThatDirectoryContainsAllExpectedContent(path_expected, path_actual);
99-
} else {
100-
assertFalse(Files.isDirectory(path_actual));
101-
assertThatFilesHaveSameContent(path_expected, path_actual);
102-
}
103-
}
104-
}
105-
}
106-
107-
private static Set<String> getDirectoryEntries(final Path directory) throws IOException {
108-
109-
final Set<String> directory_listing = new HashSet<>();
110-
111-
try (Stream<Path> entries = Files.list(directory)) {
112-
for (Iterator<Path> iterator = entries.iterator(); iterator.hasNext(); ) {
113-
final Path file = iterator.next();
114-
directory_listing.add(file.getFileName().toString());
115-
}
116-
}
117-
118-
return directory_listing;
119-
}
120-
121-
private static void assertThatFilesHaveSameContent(final Path path1, final Path path2) throws IOException {
122-
123-
byte[] expected = Files.readAllBytes(path1);
124-
byte[] actual = Files.readAllBytes(path2);
125-
126-
if (!Arrays.equals(expected, actual) && !filesHaveSameContentIgnoreWhitespace(path1, path2))
127-
fail("Files differ: " + path1 + ", " + path2);
128-
}
129-
130-
private static boolean filesHaveSameContentIgnoreWhitespace(Path path1, Path path2) throws IOException {
131-
String fileContent1 = getFileContent(path1);
132-
String fileContent2 = getFileContent(path2);
133-
134-
if (!fileContent1.equals(fileContent2)) {
135-
System.out.println("f1: " + fileContent1);
136-
System.out.println("f2: " + fileContent2);
137-
}
138-
return fileContent1.equals(fileContent2);
139-
}
140-
141-
private static String getFileContent(Path path) throws IOException {
142-
143-
return Files.readAllLines(path).stream().reduce((s1, s2) -> s1 + s2).orElseThrow().replaceAll("\t", "").replaceAll("\n", "").replaceAll(" ", "");
22+
@Override
23+
protected String getResourcesPath() {
24+
return "individual_race/balmullo/";
14425
}
14526
}

0 commit comments

Comments
 (0)