From 2d87b93b4fc108f39c05a358516d0b274e5cc044 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Mon, 5 Aug 2024 20:42:31 +0200 Subject: [PATCH] Korrektur WithInnerPropertyLoader --- .../property2java/{processor => }/Format.java | 0 .../enum_template_with_inner_properties.tpl | 20 +- .../kreth/property2java/GeneratorTests.java | 20 +- .../GeneratorWithInnerPropertiesTest.java | 191 ++++++++++++++++++ .../property2java/TestPropertiesSource.java | 25 +++ 5 files changed, 229 insertions(+), 27 deletions(-) rename src/main/java/de/kreth/property2java/{processor => }/Format.java (100%) create mode 100644 src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java create mode 100644 src/test/java/de/kreth/property2java/TestPropertiesSource.java diff --git a/src/main/java/de/kreth/property2java/processor/Format.java b/src/main/java/de/kreth/property2java/Format.java similarity index 100% rename from src/main/java/de/kreth/property2java/processor/Format.java rename to src/main/java/de/kreth/property2java/Format.java diff --git a/src/main/resources/template/enum_template_with_inner_properties.tpl b/src/main/resources/template/enum_template_with_inner_properties.tpl index d3955af..99969e3 100644 --- a/src/main/resources/template/enum_template_with_inner_properties.tpl +++ b/src/main/resources/template/enum_template_with_inner_properties.tpl @@ -1,9 +1,6 @@ <#if package??>package ${package}; import java.util.Properties; -import java.util.PropertyResourceBundle; -import java.util.ResourceBundle; -import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -21,12 +18,21 @@ public enum ${classname} { ; - private final String value; - private ${classname} (String value) { this.value = value; } + private static Properties properties = new Properties(); + static { + try { + properties.load(${classname}.class.getResourceAsStream("${fileName}")); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private final String value; + /** * Represented Key in property File. * @return key @@ -35,14 +41,12 @@ public enum ${classname} { return value; } - private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}"); - /** * The Text for this Key from PropertyResourceBundle * @return human readable text */ public String getText() { - return bundle.getString(value); + return properties.getProperty(value); } } diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index bd773da..f600b7f 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -1,5 +1,6 @@ package de.kreth.property2java; +import static de.kreth.property2java.TestPropertiesSource.testProperties; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -13,7 +14,6 @@ import static org.mockito.Mockito.when; import java.io.File; import java.io.IOException; import java.io.Reader; -import java.io.StringReader; import java.io.StringWriter; import java.io.Writer; import java.util.Arrays; @@ -149,22 +149,4 @@ class GeneratorTests { 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" + ""); - } } diff --git a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java new file mode 100644 index 0000000..3e469d8 --- /dev/null +++ b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java @@ -0,0 +1,191 @@ +package de.kreth.property2java; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.StringTokenizer; +import java.util.stream.Collectors; + +import static de.kreth.property2java.TestPropertiesSource.testProperties; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import de.kreth.property2java.processor.Format; + +public class GeneratorWithInnerPropertiesTest { + + private String path = "application.properties"; + + private Configuration config; + + private Generator generator; + + @BeforeEach + void setUp() throws Exception { + Map input = new HashMap<>(); + input.put(path, testProperties()); + + config = Mockito.spy(TestImplConfig.class); + + when(config.getRootPath()).thenReturn(new File(".").toPath()); + when(config.getFormat()).thenReturn(Format.WithInnerPropertyLoader); + when(config.getInput()).thenReturn(input); + when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); + when(config.outputCharset()).thenCallRealMethod(); + + 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++; + } + } + + assertEquals(countCloseBaces, countOpenBaces, + "Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); + + assertNotNull(linePackage); + assertNotNull(lineClass); + + assertThat(linePackage, + Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); + + assertThat(lineClass, + Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); + + } + + @Test + void testResourceLoad() 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 declaration = null; + String load = null; + while (sourceTokenizer.hasMoreTokens()) { + String line = sourceTokenizer.nextToken(); + if (line.contains("Properties") + && !line.contains("import") + && !line.contains("enum") + && !line.contains("class")) { + declaration = line; + } else if (line.contains(".load")) { + load = line; + } + } + assertNotNull(declaration); + assertNotNull(load); + + assertThat(declaration, + Matchers.stringContainsInOrder(Arrays.asList("private", "static", "Properties", "properties", "=", "new Properties()", ";"))); + + assertThat(load, + Matchers.containsString("properties.load(Application_Properties.class.getResourceAsStream(\"application.properties\"));")); + + } + + @Test + void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { + + Writer out = mock(Writer.class); + Writer nonOut = mock(Writer.class); + when(config.outWriter(anyString())).thenReturn(out, nonOut); + generator.start(); + verify(out).close(); + verify(nonOut, never()).close(); + verify(nonOut, never()).flush(); + } + + @Test + void testKeys() throws IOException, GeneratorException { + + StringWriter out = new StringWriter(); + when(config.outWriter(anyString())).thenReturn(out); + generator.start(); + + List lines = out.toString().lines().filter(line -> line.contains(" (\"")).collect(Collectors.toList()); + + assertEquals(21, lines.size()); + assertLineMatch(lines, "label", "label"); + assertLineMatch(lines, "label_addarticle", "label.addarticle"); + assertLineMatch(lines, "label_user_register", "label.user.register"); + assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); + assertLineMatch(lines, "message_invoiceitem_startbeforeend", "message.invoiceitem.startbeforeend"); + assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", "message.invoiceitem.allfieldsmustbeset"); + } + + private void assertLineMatch(List lines, String key, String expected) { + Optional found = lines.stream().filter(line -> keyMatches(line, key)).findFirst(); + + assertTrue(found.isPresent(), "No line found with key = " + key); + final String line = found.get().trim(); + int indexEquals = line.indexOf('('); + String value = line.substring(indexEquals + 1).trim().substring(1); + value = value.substring(0, value.length() - 3); + assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\""); + + } + + private boolean keyMatches(String line, String key) { + line = line.toLowerCase(); + key = key.toLowerCase(); + return line.contains("\t" + key + " "); + } + +} diff --git a/src/test/java/de/kreth/property2java/TestPropertiesSource.java b/src/test/java/de/kreth/property2java/TestPropertiesSource.java new file mode 100644 index 0000000..72561b1 --- /dev/null +++ b/src/test/java/de/kreth/property2java/TestPropertiesSource.java @@ -0,0 +1,25 @@ +package de.kreth.property2java; + +import java.io.StringReader; + +public class TestPropertiesSource { + + public static 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" + ""); + } +}