|
| 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 | +} |
0 commit comments