diff --git a/quickfixj-codegenerator/src/main/resources/org/quickfixj/codegenerator/MessageSubclass.xsl b/quickfixj-codegenerator/src/main/resources/org/quickfixj/codegenerator/MessageSubclass.xsl index 4333c3b62c..85ded0abf2 100644 --- a/quickfixj-codegenerator/src/main/resources/org/quickfixj/codegenerator/MessageSubclass.xsl +++ b/quickfixj-codegenerator/src/main/resources/org/quickfixj/codegenerator/MessageSubclass.xsl @@ -197,7 +197,7 @@ import quickfix.Group; - + 1), the correct delimiter field number is used. + */ +public class NestedComponentDelimiterTest { + + private File outputDirectory = new File("./target/test-output/nested-components"); + private File dictDirectory = new File("./src/test/resources/org/quickfixj/codegenerator"); + private File schemaDirectory = new File("./src/main/resources/org/quickfixj/codegenerator"); + private String fieldPackage = "quickfix.field"; + private String utcTimestampPrecision = null; + private boolean orderedFields = true; + private boolean decimal = true; + private MessageCodeGenerator generator; + + @Before + public void setup() throws IOException { + if (outputDirectory.exists()) { + FileUtils.cleanDirectory(outputDirectory); + } else { + outputDirectory.mkdirs(); + } + generator = new MessageCodeGenerator(); + System.out.println("Successfully created an instance of the QuickFIX source generator"); + } + + @Test + public void testNestedComponentDelimiterGeneration() throws MojoExecutionException { + MessageCodeGenerator.Task task = new MessageCodeGenerator.Task(); + System.out.println("Initialising code generator task for nested components test"); + + String packaging = "quickfix.test"; + File dictFile = new File(dictDirectory, "NestedComponentsTest.xml"); + generate(generator, task, dictFile, packaging, true); + + // Check that NestedTwice component was generated + String nestedTwiceFilePath = outputDirectory.getAbsolutePath() + + "/quickfix/test/component/NestedTwice.java"; + File nestedTwiceFile = new File(nestedTwiceFilePath); + assertTrue("NestedTwice.java should be generated", nestedTwiceFile.exists()); + + // Read the file and check for the correct delimiter + String content = readFileContent(nestedTwiceFile); + + // The NoEntries group should have the correct delimiter (58 = Text field number) + // Expected: super(20001, 58, ORDER); + assertTrue("Generated code should contain 'super(20001, 58, ORDER);'", + content.contains("super(20001, 58, ORDER);")); + + // Verify structural elements are present + assertTrue("Generated code should contain NoEntries class", + content.contains("public static class NoEntries extends Group")); + + assertTrue("Generated code should contain ORDER array", + content.contains("private static final int[] ORDER")); + } + + private void generate(MessageCodeGenerator generator, MessageCodeGenerator.Task task, + File dictfile, String packaging, boolean overwrite) throws MojoExecutionException { + if (dictfile != null && dictfile.exists()) { + task.setSpecification(dictfile); + } else { + throw new MojoExecutionException("File could not be found or was NULL!"); + } + + System.out.println("Processing " + dictfile); + + task.setName(dictfile.getName()); + task.setTransformDirectory(schemaDirectory); + task.setMessagePackage(packaging); + task.setOutputBaseDirectory(outputDirectory); + task.setFieldPackage(fieldPackage); + task.setUtcTimestampPrecision(utcTimestampPrecision); + task.setOverwrite(overwrite); + task.setOrderedFields(orderedFields); + task.setDecimalGenerated(decimal); + generator.generate(task); + } + + private String readFileContent(File file) { + StringBuilder content = new StringBuilder(); + try (Scanner scanner = new Scanner(file)) { + while (scanner.hasNextLine()) { + content.append(scanner.nextLine()).append("\n"); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + fail("Failed to read file: " + file.getAbsolutePath()); + } + return content.toString(); + } +} diff --git a/quickfixj-codegenerator/src/test/resources/org/quickfixj/codegenerator/NestedComponentsTest.xml b/quickfixj-codegenerator/src/test/resources/org/quickfixj/codegenerator/NestedComponentsTest.xml new file mode 100644 index 0000000000..4c991c9ad9 --- /dev/null +++ b/quickfixj-codegenerator/src/test/resources/org/quickfixj/codegenerator/NestedComponentsTest.xml @@ -0,0 +1,37 @@ + + +
+ + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +