|
|
|
@ -29,149 +29,149 @@ import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
class GeneratorTests { |
|
|
|
class GeneratorTests { |
|
|
|
|
|
|
|
|
|
|
|
private String path = "application.properties"; |
|
|
|
private String path = "application.properties"; |
|
|
|
|
|
|
|
|
|
|
|
private Configuration config; |
|
|
|
private Configuration config; |
|
|
|
|
|
|
|
|
|
|
|
private Generator generator; |
|
|
|
private Generator generator; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
@BeforeEach |
|
|
|
void setUp() throws Exception { |
|
|
|
void setUp() throws Exception { |
|
|
|
Map<String, Reader> input = new HashMap<>(); |
|
|
|
Map<String, Reader> input = new HashMap<>(); |
|
|
|
input.put(path, testProperties()); |
|
|
|
input.put(path, testProperties()); |
|
|
|
|
|
|
|
|
|
|
|
config = mock(Configuration.class); |
|
|
|
config = mock(Configuration.class); |
|
|
|
when(config.getInput()).thenReturn(input); |
|
|
|
when(config.getInput()).thenReturn(input); |
|
|
|
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
|
|
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
|
|
when(config.outputCharset()).thenCallRealMethod(); |
|
|
|
when(config.outputCharset()).thenCallRealMethod(); |
|
|
|
|
|
|
|
|
|
|
|
generator = new Generator(config); |
|
|
|
generator = new Generator(config); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void testClassDefinition() throws IOException, GeneratorException { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
when(config.getPackage()).thenReturn("de.kreth.property2java"); |
|
|
|
|
|
|
|
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StringWriter out = new StringWriter(); |
|
|
|
|
|
|
|
when(config.outWriter(anyString())).thenReturn(out); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generator.start(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String sourceCode = out.toString().trim(); |
|
|
|
|
|
|
|
StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n"); |
|
|
|
|
|
|
|
String linePackage = null; |
|
|
|
|
|
|
|
String lineClass = null; |
|
|
|
|
|
|
|
int countOpenBaces = 0; |
|
|
|
|
|
|
|
int countCloseBaces = 0; |
|
|
|
|
|
|
|
while (sourceTokenizer.hasMoreTokens()) { |
|
|
|
|
|
|
|
String line = sourceTokenizer.nextToken(); |
|
|
|
|
|
|
|
if (line.trim().startsWith("package")) { |
|
|
|
|
|
|
|
linePackage = line; |
|
|
|
|
|
|
|
} else if (line.trim().startsWith("public enum")) { |
|
|
|
|
|
|
|
lineClass = line; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (line.contains("{")) { |
|
|
|
|
|
|
|
countOpenBaces++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (line.contains("}")) { |
|
|
|
|
|
|
|
countCloseBaces++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
assertEquals(countCloseBaces, countOpenBaces, |
|
|
|
void testClassDefinition() throws IOException, GeneratorException { |
|
|
|
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); |
|
|
|
|
|
|
|
|
|
|
|
when(config.getPackage()).thenReturn("de.kreth.property2java"); |
|
|
|
assertNotNull(linePackage); |
|
|
|
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
|
|
assertNotNull(lineClass); |
|
|
|
|
|
|
|
|
|
|
|
StringWriter out = new StringWriter(); |
|
|
|
assertThat(linePackage, |
|
|
|
when(config.outWriter(anyString())).thenReturn(out); |
|
|
|
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); |
|
|
|
|
|
|
|
|
|
|
|
generator.start(); |
|
|
|
assertThat(lineClass, |
|
|
|
|
|
|
|
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); |
|
|
|
String sourceCode = out.toString().trim(); |
|
|
|
|
|
|
|
StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n"); |
|
|
|
} |
|
|
|
String linePackage = null; |
|
|
|
|
|
|
|
String lineClass = null; |
|
|
|
@Test |
|
|
|
int countOpenBaces = 0; |
|
|
|
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { |
|
|
|
int countCloseBaces = 0; |
|
|
|
|
|
|
|
while (sourceTokenizer.hasMoreTokens()) { |
|
|
|
Writer out = mock(Writer.class); |
|
|
|
String line = sourceTokenizer.nextToken(); |
|
|
|
Writer nonOut = mock(Writer.class); |
|
|
|
if (line.trim().startsWith("package")) { |
|
|
|
when(config.outWriter(anyString())).thenReturn(out, nonOut); |
|
|
|
linePackage = line; |
|
|
|
generator.start(); |
|
|
|
} |
|
|
|
verify(out).close(); |
|
|
|
else if (line.trim().startsWith("public enum")) { |
|
|
|
verify(nonOut, never()).close(); |
|
|
|
lineClass = line; |
|
|
|
verify(nonOut, never()).flush(); |
|
|
|
} |
|
|
|
} |
|
|
|
if (line.contains("{")) { |
|
|
|
|
|
|
|
countOpenBaces++; |
|
|
|
@Test |
|
|
|
} |
|
|
|
void testKeys() throws IOException, GeneratorException { |
|
|
|
if (line.contains("}")) { |
|
|
|
|
|
|
|
countCloseBaces++; |
|
|
|
StringWriter out = new StringWriter(); |
|
|
|
} |
|
|
|
when(config.outWriter(anyString())).thenReturn(out); |
|
|
|
} |
|
|
|
generator.start(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(countCloseBaces, countOpenBaces, |
|
|
|
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")) |
|
|
|
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); |
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(linePackage); |
|
|
|
assertEquals(21, lines.size()); |
|
|
|
assertNotNull(lineClass); |
|
|
|
assertLineMatch(lines, "label", "label"); |
|
|
|
|
|
|
|
assertLineMatch(lines, "label_addarticle", "label.addarticle"); |
|
|
|
assertThat(linePackage, |
|
|
|
assertLineMatch(lines, "label_user_register", "label.user.register"); |
|
|
|
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); |
|
|
|
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); |
|
|
|
|
|
|
|
assertLineMatch(lines, "message_invoiceitem_startbeforeend", |
|
|
|
assertThat(lineClass, |
|
|
|
"message.invoiceitem.startbeforeend"); |
|
|
|
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); |
|
|
|
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", |
|
|
|
|
|
|
|
"message.invoiceitem.allfieldsmustbeset"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
private void assertLineMatch(List<String> lines, String key, String expected) { |
|
|
|
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { |
|
|
|
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) |
|
|
|
|
|
|
|
.findFirst(); |
|
|
|
Writer out = mock(Writer.class); |
|
|
|
|
|
|
|
Writer nonOut = mock(Writer.class); |
|
|
|
assertTrue(found.isPresent(), "No line found with key = " + key); |
|
|
|
when(config.outWriter(anyString())).thenReturn(out, nonOut); |
|
|
|
final String line = found.get().trim(); |
|
|
|
generator.start(); |
|
|
|
int indexEquals = line.indexOf('('); |
|
|
|
verify(out).close(); |
|
|
|
String value = line.substring(indexEquals + 1).trim().substring(1); |
|
|
|
verify(nonOut, never()).close(); |
|
|
|
value = value.substring(0, value.length() - 3); |
|
|
|
verify(nonOut, never()).flush(); |
|
|
|
assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\""); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
@Test |
|
|
|
|
|
|
|
void testKeys() throws IOException, GeneratorException { |
|
|
|
private boolean keyMatches(String line, String key) { |
|
|
|
|
|
|
|
line = line.toLowerCase(); |
|
|
|
StringWriter out = new StringWriter(); |
|
|
|
key = key.toLowerCase(); |
|
|
|
when(config.outWriter(anyString())).thenReturn(out); |
|
|
|
return line.contains("\t" + key + " "); |
|
|
|
generator.start(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")) |
|
|
|
private StringReader testProperties() { |
|
|
|
.collect(Collectors.toList()); |
|
|
|
return new StringReader("\r\n" + |
|
|
|
|
|
|
|
"label = \r\n" + |
|
|
|
assertEquals(21, lines.size()); |
|
|
|
"\r\n" + |
|
|
|
assertLineMatch(lines, "label", "label"); |
|
|
|
"label.addarticle = Add Article\r\n" + |
|
|
|
assertLineMatch(lines, "label_addarticle", "label.addarticle"); |
|
|
|
"label.cancel = Cancel\r\n" + |
|
|
|
assertLineMatch(lines, "label_user_register", "label.user.register"); |
|
|
|
"label.close = Close\r\n" + |
|
|
|
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); |
|
|
|
"label.delete = Delete\r\n" + |
|
|
|
assertLineMatch(lines, "message_invoiceitem_startbeforeend", |
|
|
|
"label.discart = Discart\r\n" + |
|
|
|
"message.invoiceitem.startbeforeend"); |
|
|
|
"label.loggedin = Logged in:\r\n" + |
|
|
|
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", |
|
|
|
"label.logout = Logout\r\n" + |
|
|
|
"message.invoiceitem.allfieldsmustbeset"); |
|
|
|
"label.ok = OK\r\n" + |
|
|
|
} |
|
|
|
"label.store = Store\r\n" + |
|
|
|
|
|
|
|
"label.preview = Preview\r\n" + |
|
|
|
private void assertLineMatch(List<String> lines, String key, String expected) { |
|
|
|
"label.open = Open\r\n" + |
|
|
|
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) |
|
|
|
"label.user.register = Register\r\n" + |
|
|
|
.findFirst(); |
|
|
|
"\r\n" + |
|
|
|
assertTrue(found.isPresent(), "No line found with key = " + key); |
|
|
|
"message.article.priceerror = Please set the price.\r\n" + |
|
|
|
final String line = found.get().trim(); |
|
|
|
"message.delete.text = Delete {0}?\r\n" + |
|
|
|
int indexEquals = line.indexOf('('); |
|
|
|
"message.delete.title = Really delete?\r\n" + |
|
|
|
String value = line.substring(indexEquals + 1).trim().substring(1); |
|
|
|
"message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n" + |
|
|
|
value = value.substring(0, value.length() - 3); |
|
|
|
" empty!\r\n" + |
|
|
|
assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\""); |
|
|
|
"message.invoiceitem.startbeforeend = End must be later than start.\r\n" + |
|
|
|
|
|
|
|
"message.user.create.success = Thanks {0} created!\r\n" + |
|
|
|
} |
|
|
|
"message.user.loginfailure = Login Error! Wrong user or password?\r\n" + |
|
|
|
|
|
|
|
"message.user.passwordmissmatch = Passwords don't match.\r\n" + |
|
|
|
private boolean keyMatches(String line, String key) { |
|
|
|
""); |
|
|
|
line = line.toLowerCase(); |
|
|
|
} |
|
|
|
key = key.toLowerCase(); |
|
|
|
|
|
|
|
return line.contains("\t" + key + " "); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private StringReader testProperties() { |
|
|
|
|
|
|
|
return new StringReader("\r\n" + |
|
|
|
|
|
|
|
"label = \r\n" + |
|
|
|
|
|
|
|
"\r\n" + |
|
|
|
|
|
|
|
"label.addarticle = Add Article\r\n" + |
|
|
|
|
|
|
|
"label.cancel = Cancel\r\n" + |
|
|
|
|
|
|
|
"label.close = Close\r\n" + |
|
|
|
|
|
|
|
"label.delete = Delete\r\n" + |
|
|
|
|
|
|
|
"label.discart = Discart\r\n" + |
|
|
|
|
|
|
|
"label.loggedin = Logged in:\r\n" + |
|
|
|
|
|
|
|
"label.logout = Logout\r\n" + |
|
|
|
|
|
|
|
"label.ok = OK\r\n" + |
|
|
|
|
|
|
|
"label.store = Store\r\n" + |
|
|
|
|
|
|
|
"label.preview = Preview\r\n" + |
|
|
|
|
|
|
|
"label.open = Open\r\n" + |
|
|
|
|
|
|
|
"label.user.register = Register\r\n" + |
|
|
|
|
|
|
|
"\r\n" + |
|
|
|
|
|
|
|
"message.article.priceerror = Please set the price.\r\n" + |
|
|
|
|
|
|
|
"message.delete.text = Delete {0}?\r\n" + |
|
|
|
|
|
|
|
"message.delete.title = Really delete?\r\n" + |
|
|
|
|
|
|
|
"message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n" + |
|
|
|
|
|
|
|
" empty!\r\n" + |
|
|
|
|
|
|
|
"message.invoiceitem.startbeforeend = End must be later than start.\r\n" + |
|
|
|
|
|
|
|
"message.user.create.success = Thanks {0} created!\r\n" + |
|
|
|
|
|
|
|
"message.user.loginfailure = Login Error! Wrong user or password?\r\n" + |
|
|
|
|
|
|
|
"message.user.passwordmissmatch = Passwords don't match.\r\n" + |
|
|
|
|
|
|
|
""); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|