-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOverwriteTest.java
More file actions
135 lines (110 loc) · 4.97 KB
/
OverwriteTest.java
File metadata and controls
135 lines (110 loc) · 4.97 KB
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
package org.quickfixj.codegenerator;
import static org.junit.jupiter.api.Assertions.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import org.apache.maven.plugin.MojoExecutionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.apache.commons.io.FileUtils;
public class OverwriteTest {
private File outputDirectory = new File ("./target/test-output/");
private File dictDirectory = new File ("./src/test/resources");
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;
@BeforeEach
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 testFieldOverwrittenWhenOverwriteTrue() {
boolean overwrite = true;
MessageCodeGenerator.Task task = new MessageCodeGenerator.Task();
System.out.println("Initialising code generator task");
try {
String packaging = "quickfix.fix41";
File fix41Dictfile = new File( dictDirectory, "FIX41.xml" );
generate(generator, task, fix41Dictfile, packaging, overwrite);
packaging = "quickfix.fix42"; // this does not affect this test
File fix42Dictfile = new File( dictDirectory, "FIX42.xml" );
generate(generator, task, fix42Dictfile, packaging, overwrite);
} catch (MojoExecutionException e) {
e.printStackTrace();
fail();
}
String expectedFilePath = outputDirectory.getAbsolutePath() + "/quickfix/field/AllocShares.java";
File file = new File(expectedFilePath);
assertTrue(file.exists());
boolean isAllocSharesDecimal = isAllocSharesDecimal(file);
assertTrue(isAllocSharesDecimal);
}
@Test
public void testFieldNotOverwrittenWhenOverwriteFalse() {
boolean overwrite = false;
MessageCodeGenerator.Task task = new MessageCodeGenerator.Task();
System.out.println("Initialising code generator task");
try {
String packaging = "quickfix.fix41";
File fix41Dictfile = new File( dictDirectory, "FIX41.xml" );
generate(generator, task, fix41Dictfile, packaging, overwrite);
packaging = "quickfix.fix42"; // this does not affect this test
File fix42Dictfile = new File( dictDirectory, "FIX42.xml" );
generate(generator, task, fix42Dictfile, packaging, overwrite);
} catch (MojoExecutionException e) {
e.printStackTrace();
fail();
}
String expectedFilePath = outputDirectory.getAbsolutePath() + "/quickfix/field/AllocShares.java";
File file = new File(expectedFilePath);
assertTrue(file.exists());
boolean isAllocSharesDecimal = isAllocSharesDecimal(file);
assertFalse(isAllocSharesDecimal);
}
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 boolean isAllocSharesDecimal(File file) {
boolean isAllocSharesDecimal = false;
try (Scanner scanner = new Scanner(file)) {
//now read the file line by line...
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if(line.contains("AllocShares extends DecimalField")) {
isAllocSharesDecimal = true;
break;
}
}
} catch(FileNotFoundException e) {
e.printStackTrace();
fail();
}
return isAllocSharesDecimal;
}
}