|
| 1 | +package org.quickfixj.codegenerator; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.fail; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Rule; |
| 17 | +import org.junit.Test; |
| 18 | +import org.junit.rules.TemporaryFolder; |
| 19 | + |
| 20 | +/** |
| 21 | + * Golden-file regression test for the code generator. |
| 22 | + * |
| 23 | + * <p>The test runs {@link MessageCodeGenerator} against the real FIX42 and FIX44 dictionaries |
| 24 | + * and compares every generated {@code .java} file byte-for-byte against the committed golden |
| 25 | + * files stored in {@code src/test/resources/golden/fix42} and |
| 26 | + * {@code src/test/resources/golden/fix44}. |
| 27 | + * |
| 28 | + * <p>FIX42 is used because it contains repeating groups (38 of them), while still being |
| 29 | + * smaller than FIX44. FIX44 is used because it contains 233 groups, including nested ones |
| 30 | + * (relevant to issue #1084). |
| 31 | + * |
| 32 | + * <p>If the generator output must intentionally change, regenerate the golden files by running |
| 33 | + * the {@code GenerateGoldenFiles} utility in the {@code src/test/java} tree and committing the |
| 34 | + * updated golden files together with the generator changes. |
| 35 | + */ |
| 36 | +public class GoldenFileTest { |
| 37 | + |
| 38 | + @Rule |
| 39 | + public TemporaryFolder tempFolder = new TemporaryFolder(); |
| 40 | + |
| 41 | + private File schemaDirectory = new File("./src/main/resources/org/quickfixj/codegenerator"); |
| 42 | + private File goldenBase = new File("./src/test/resources/golden"); |
| 43 | + |
| 44 | + private File fix42DictFile = new File( |
| 45 | + "../quickfixj-messages/quickfixj-messages-fix42/src/main/resources/FIX42.xml"); |
| 46 | + private File fix44DictFile = new File( |
| 47 | + "../quickfixj-messages/quickfixj-messages-fix44/src/main/resources/FIX44.xml"); |
| 48 | + |
| 49 | + private MessageCodeGenerator generator; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setup() { |
| 53 | + generator = new MessageCodeGenerator(); |
| 54 | + } |
| 55 | + |
| 56 | + // ------------------------------------------------------------------------- |
| 57 | + // FIX42 – has fields, messages, and repeating groups (no components) |
| 58 | + // ------------------------------------------------------------------------- |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testFix42GenerationMatchesGolden() throws Exception { |
| 62 | + File outputDir = tempFolder.newFolder("fix42"); |
| 63 | + generateCode(fix42DictFile, "FIX42", "quickfix.fix42", outputDir); |
| 64 | + assertMatchesGolden(new File(goldenBase, "fix42"), outputDir, "FIX42"); |
| 65 | + } |
| 66 | + |
| 67 | + // ------------------------------------------------------------------------- |
| 68 | + // FIX44 – has fields, messages, components, and 233 groups (incl. nested) |
| 69 | + // ------------------------------------------------------------------------- |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testFix44GenerationMatchesGolden() throws Exception { |
| 73 | + File outputDir = tempFolder.newFolder("fix44"); |
| 74 | + generateCode(fix44DictFile, "FIX44", "quickfix.fix44", outputDir); |
| 75 | + assertMatchesGolden(new File(goldenBase, "fix44"), outputDir, "FIX44"); |
| 76 | + } |
| 77 | + |
| 78 | + // ------------------------------------------------------------------------- |
| 79 | + // Helpers |
| 80 | + // ------------------------------------------------------------------------- |
| 81 | + |
| 82 | + private void generateCode(File dictFile, String name, String messagePackage, File outputDir) |
| 83 | + throws Exception { |
| 84 | + MessageCodeGenerator.Task task = new MessageCodeGenerator.Task(); |
| 85 | + task.setName(name); |
| 86 | + task.setSpecification(dictFile); |
| 87 | + task.setTransformDirectory(schemaDirectory); |
| 88 | + task.setMessagePackage(messagePackage); |
| 89 | + task.setOutputBaseDirectory(outputDir); |
| 90 | + task.setFieldPackage("quickfix.field"); |
| 91 | + task.setOverwrite(true); |
| 92 | + task.setOrderedFields(true); |
| 93 | + task.setDecimalGenerated(true); |
| 94 | + generator.generate(task); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Recursively walks the golden directory and verifies that each {@code .java} file has an |
| 99 | + * identical counterpart in the generated output directory. Also checks that no extra files |
| 100 | + * were generated that are absent from the golden directory. |
| 101 | + */ |
| 102 | + private void assertMatchesGolden(File goldenDir, File generatedDir, String label) |
| 103 | + throws IOException { |
| 104 | + List<String> errors = new ArrayList<>(); |
| 105 | + |
| 106 | + // Collect relative paths of all .java files in the golden directory |
| 107 | + List<String> goldenRelPaths = collectJavaPaths(goldenDir.toPath()); |
| 108 | + |
| 109 | + // Collect relative paths of all .java files in the generated output directory |
| 110 | + List<String> generatedRelPaths = collectJavaPaths(generatedDir.toPath()); |
| 111 | + |
| 112 | + // Files present in golden but missing from generated output |
| 113 | + List<String> missingFromGenerated = new ArrayList<>(goldenRelPaths); |
| 114 | + missingFromGenerated.removeAll(generatedRelPaths); |
| 115 | + for (String missing : missingFromGenerated) { |
| 116 | + errors.add("[" + label + "] Missing generated file: " + missing); |
| 117 | + } |
| 118 | + |
| 119 | + // Extra files in generated output that are not in golden |
| 120 | + List<String> extraInGenerated = new ArrayList<>(generatedRelPaths); |
| 121 | + extraInGenerated.removeAll(goldenRelPaths); |
| 122 | + for (String extra : extraInGenerated) { |
| 123 | + errors.add("[" + label + "] Unexpected generated file (not in golden): " + extra); |
| 124 | + } |
| 125 | + |
| 126 | + // Compare content of files present in both |
| 127 | + for (String relPath : goldenRelPaths) { |
| 128 | + if (!generatedRelPaths.contains(relPath)) { |
| 129 | + continue; // already reported as missing above |
| 130 | + } |
| 131 | + File goldenFile = new File(goldenDir, relPath); |
| 132 | + File generatedFile = new File(generatedDir, relPath); |
| 133 | + compareFileContent(goldenFile, generatedFile, relPath, label, errors); |
| 134 | + } |
| 135 | + |
| 136 | + if (!errors.isEmpty()) { |
| 137 | + fail(errors.size() + " golden file assertion(s) failed:\n" |
| 138 | + + String.join("\n", errors)); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private List<String> collectJavaPaths(Path root) throws IOException { |
| 143 | + if (!root.toFile().exists()) { |
| 144 | + return new ArrayList<>(); |
| 145 | + } |
| 146 | + try (Stream<Path> stream = Files.walk(root)) { |
| 147 | + return stream |
| 148 | + .filter(p -> p.toString().endsWith(".java")) |
| 149 | + .map(p -> root.relativize(p).toString()) |
| 150 | + .sorted() |
| 151 | + .collect(Collectors.toList()); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void compareFileContent(File goldenFile, File generatedFile, String relPath, |
| 156 | + String label, List<String> errors) throws IOException { |
| 157 | + List<String> goldenLines = Files.readAllLines(goldenFile.toPath()); |
| 158 | + List<String> generatedLines = Files.readAllLines(generatedFile.toPath()); |
| 159 | + |
| 160 | + int maxLines = Math.max(goldenLines.size(), generatedLines.size()); |
| 161 | + for (int i = 0; i < maxLines; i++) { |
| 162 | + String goldenLine = i < goldenLines.size() ? goldenLines.get(i) : "<EOF>"; |
| 163 | + String generatedLine = i < generatedLines.size() ? generatedLines.get(i) : "<EOF>"; |
| 164 | + if (!goldenLine.equals(generatedLine)) { |
| 165 | + errors.add(String.format( |
| 166 | + "[%s] %s line %d differs:%n golden: %s%n generated: %s", |
| 167 | + label, relPath, i + 1, goldenLine, generatedLine)); |
| 168 | + // Report only the first differing line per file to keep output manageable |
| 169 | + break; |
| 170 | + } |
| 171 | + } |
| 172 | + if (goldenLines.size() != generatedLines.size() && errors.isEmpty()) { |
| 173 | + // Line counts differ but all shared lines matched – report the length mismatch |
| 174 | + errors.add(String.format( |
| 175 | + "[%s] %s line count differs: golden=%d, generated=%d", |
| 176 | + label, relPath, goldenLines.size(), generatedLines.size())); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments