From f0a9b158c43cc373adc6f5fc2760643414366845 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Mon, 5 Aug 2024 22:18:10 +0200 Subject: [PATCH] Real files generated, formats optimized. --- .../enum_template_with_inner_properties.tpl | 6 +- ...late_with_inner_propertyresourcebundle.tpl | 8 +- .../property2java/TestPropertiesSource.java | 19 +++ .../generated/GenerateTheTest.java | 124 ++++++++++++++++ .../generated/Property_Loader_Properties.java | 132 ++++++++++++++++++ .../generated/Resource_Bundle_Properties.java | 126 +++++++++++++++++ .../generated/TestRealGenerated.java | 19 +++ .../generated/Unary_Operator_Properties.java | 127 +++++++++++++++++ src/test/resources/property_loader.properties | 24 ++++ src/test/resources/resource_bundle.properties | 24 ++++ src/test/resources/unary_operator.properties | 24 ++++ 11 files changed, 626 insertions(+), 7 deletions(-) create mode 100644 src/test/java/de/kreth/property2java/generated/GenerateTheTest.java create mode 100644 src/test/java/de/kreth/property2java/generated/Property_Loader_Properties.java create mode 100644 src/test/java/de/kreth/property2java/generated/Resource_Bundle_Properties.java create mode 100644 src/test/java/de/kreth/property2java/generated/TestRealGenerated.java create mode 100644 src/test/java/de/kreth/property2java/generated/Unary_Operator_Properties.java create mode 100644 src/test/resources/property_loader.properties create mode 100644 src/test/resources/resource_bundle.properties create mode 100644 src/test/resources/unary_operator.properties 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 99969e3..13020c3 100644 --- a/src/main/resources/template/enum_template_with_inner_properties.tpl +++ b/src/main/resources/template/enum_template_with_inner_properties.tpl @@ -1,6 +1,8 @@ <#if package??>package ${package}; -import java.util.Properties; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Properties; import javax.annotation.processing.Generated; @@ -25,7 +27,7 @@ public enum ${classname} { private static Properties properties = new Properties(); static { try { - properties.load(${classname}.class.getResourceAsStream("${fileName}")); + properties.load(${classname}.class.getResourceAsStream("/${fileName}")); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl b/src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl index 0ddfc7d..a1a2df0 100644 --- a/src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl +++ b/src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl @@ -1,9 +1,7 @@ <#if package??>package ${package}; -import java.util.Properties; -import java.util.PropertyResourceBundle; +import java.util.PropertyResourceBundle; import java.util.ResourceBundle; -import java.util.function.UnaryOperator; import javax.annotation.processing.Generated; @@ -23,6 +21,8 @@ public enum ${classname} { ; + private static ResourceBundle bundle = PropertyResourceBundle.getBundle("${bundle_base_name}"); + private final String value; private ${classname} (String value) { @@ -37,8 +37,6 @@ 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 diff --git a/src/test/java/de/kreth/property2java/TestPropertiesSource.java b/src/test/java/de/kreth/property2java/TestPropertiesSource.java index be716f8..6cf7e93 100644 --- a/src/test/java/de/kreth/property2java/TestPropertiesSource.java +++ b/src/test/java/de/kreth/property2java/TestPropertiesSource.java @@ -1,7 +1,12 @@ package de.kreth.property2java; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; import java.io.StringReader; +import de.kreth.property2java.generated.GenerateTheTest; + public class TestPropertiesSource { public static StringReader testProperties() { @@ -23,4 +28,18 @@ public class TestPropertiesSource { + "message.user.passwordmissmatch = Passwords don't match.\r\n" + ""); } + public static void main(String[] args) throws IOException { + File dir = new File("D:\\Markus\\programmierung\\workspace_clubhelper\\PropertyToJavaGenerator\\src\\test\\resources"); + + try (FileWriter out = new FileWriter(new File(dir, GenerateTheTest.PROPERTY_LOADER_PROPERTIES))) { + testProperties().transferTo(out); + } + + try (FileWriter out = new FileWriter(new File(dir, GenerateTheTest.UNARY_OPERATOR_PROPERTIES))) { + testProperties().transferTo(out); + } + try (FileWriter out = new FileWriter(new File(dir, GenerateTheTest.RESOURCE_BUNDLE))) { + testProperties().transferTo(out); + } + } } diff --git a/src/test/java/de/kreth/property2java/generated/GenerateTheTest.java b/src/test/java/de/kreth/property2java/generated/GenerateTheTest.java new file mode 100644 index 0000000..77789b9 --- /dev/null +++ b/src/test/java/de/kreth/property2java/generated/GenerateTheTest.java @@ -0,0 +1,124 @@ +package de.kreth.property2java.generated; + +import java.io.IOException; +import java.io.Reader; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +import de.kreth.property2java.Configuration; +import de.kreth.property2java.Format; +import de.kreth.property2java.Generator; +import de.kreth.property2java.GeneratorException; +import de.kreth.property2java.TestPropertiesSource; + +public class GenerateTheTest { + + public static final String PROPERTY_LOADER_PROPERTIES = "property_loader.properties"; + public static final String UNARY_OPERATOR_PROPERTIES = "unary_operator.properties"; + public static final String RESOURCE_BUNDLE = "resource_bundle.properties"; + + public static void main(String[] args) throws IOException, GeneratorException { + Path current = Paths.get(".", "src", "test", "java", "de", "kreth", "property2java", "generated").toAbsolutePath().normalize(); + System.out.println(current); + + withUnaryOperatorParameter(current); + withInnerPropertyLoader(current); + withResourceBundle(current); + } + + private static void withResourceBundle(Path current) throws IOException, GeneratorException { + Format format = Format.WithInnerPropertyResourceBundle; + Map input = new HashMap<>(); + input.put(RESOURCE_BUNDLE, TestPropertiesSource.testProperties()); + + Configuration config = new Configuration() { + + @Override + public Path getRootPath() { + return current; + } + + @Override + public String getPackage() { + return "de.kreth.property2java.generated"; + } + + @Override + public Format getFormat() { + return format; + } + + @Override + public Map getInput() { + return input; + } + }; + Generator generator = new Generator(config); + generator.start(); + } + + private static void withUnaryOperatorParameter(Path current) throws IOException, GeneratorException { + Format format = Format.WithUnaryOperatorParameter; + Map input = new HashMap<>(); + input.put(UNARY_OPERATOR_PROPERTIES, TestPropertiesSource.testProperties()); + + Configuration config = new Configuration() { + + @Override + public Path getRootPath() { + return current; + } + + @Override + public String getPackage() { + return "de.kreth.property2java.generated"; + } + + @Override + public Format getFormat() { + return format; + } + + @Override + public Map getInput() { + return input; + } + }; + Generator generator = new Generator(config); + generator.start(); + } + + private static void withInnerPropertyLoader(Path current) throws IOException, GeneratorException { + Format format = Format.WithInnerPropertyLoader; + Map input = new HashMap<>(); + input.put(PROPERTY_LOADER_PROPERTIES, TestPropertiesSource.testProperties()); + + Configuration config = new Configuration() { + + @Override + public Path getRootPath() { + return current; + } + + @Override + public String getPackage() { + return "de.kreth.property2java.generated"; + } + + @Override + public Format getFormat() { + return format; + } + + @Override + public Map getInput() { + return input; + } + }; + Generator generator = new Generator(config); + generator.start(); + } + +} diff --git a/src/test/java/de/kreth/property2java/generated/Property_Loader_Properties.java b/src/test/java/de/kreth/property2java/generated/Property_Loader_Properties.java new file mode 100644 index 0000000..b9026da --- /dev/null +++ b/src/test/java/de/kreth/property2java/generated/Property_Loader_Properties.java @@ -0,0 +1,132 @@ +package de.kreth.property2java.generated; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.Properties; + +import javax.annotation.processing.Generated; + +/** + * Property keys from property_loader.properties + */ +@Generated(date = "05.08.2024, 22:08:26", value = "de.kreth.property2java.Generator") +public enum Property_Loader_Properties { + + /** + * label = "" + */ + LABEL ("label"), + /** + * label.addarticle = "Add Article" + */ + LABEL_ADDARTICLE ("label.addarticle"), + /** + * label.cancel = "Cancel" + */ + LABEL_CANCEL ("label.cancel"), + /** + * label.close = "Close" + */ + LABEL_CLOSE ("label.close"), + /** + * label.delete = "Delete" + */ + LABEL_DELETE ("label.delete"), + /** + * label.discart = "Discart" + */ + LABEL_DISCART ("label.discart"), + /** + * label.loggedin = "Logged in:" + */ + LABEL_LOGGEDIN ("label.loggedin"), + /** + * label.logout = "Logout" + */ + LABEL_LOGOUT ("label.logout"), + /** + * label.ok = "OK" + */ + LABEL_OK ("label.ok"), + /** + * label.open = "Open" + */ + LABEL_OPEN ("label.open"), + /** + * label.preview = "Preview" + */ + LABEL_PREVIEW ("label.preview"), + /** + * label.store = "Store" + */ + LABEL_STORE ("label.store"), + /** + * label.user.register = "Register" + */ + LABEL_USER_REGISTER ("label.user.register"), + /** + * message.article.priceerror = "Please set the price." + */ + MESSAGE_ARTICLE_PRICEERROR ("message.article.priceerror"), + /** + * message.delete.text = "Delete {0}?" + */ + MESSAGE_DELETE_TEXT ("message.delete.text"), + /** + * message.delete.title = "Really delete?" + */ + MESSAGE_DELETE_TITLE ("message.delete.title"), + /** + * message.invoiceitem.allfieldsmustbeset = "Start, end and article must not be + empty!" + */ + MESSAGE_INVOICEITEM_ALLFIELDSMUSTBESET ("message.invoiceitem.allfieldsmustbeset"), + /** + * message.invoiceitem.startbeforeend = "End must be later than start." + */ + MESSAGE_INVOICEITEM_STARTBEFOREEND ("message.invoiceitem.startbeforeend"), + /** + * message.user.create.success = "Thanks {0} created!" + */ + MESSAGE_USER_CREATE_SUCCESS ("message.user.create.success"), + /** + * message.user.loginfailure = "Login Error! Wrong user or password?" + */ + MESSAGE_USER_LOGINFAILURE ("message.user.loginfailure"), + /** + * message.user.passwordmissmatch = "Passwords don't match." + */ + MESSAGE_USER_PASSWORDMISSMATCH ("message.user.passwordmissmatch"); + + private Property_Loader_Properties (String value) { + this.value = value; + } + + private static Properties properties = new Properties(); + static { + try { + properties.load(Property_Loader_Properties.class.getResourceAsStream("/property_loader.properties")); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + + private final String value; + + /** + * Represented Key in property File. + * @return key + */ + public String getValue() { + return value; + } + + /** + * The Text for this Key from PropertyResourceBundle + * @return human readable text + */ + public String getText() { + return properties.getProperty(value); + } + +} diff --git a/src/test/java/de/kreth/property2java/generated/Resource_Bundle_Properties.java b/src/test/java/de/kreth/property2java/generated/Resource_Bundle_Properties.java new file mode 100644 index 0000000..c9e426a --- /dev/null +++ b/src/test/java/de/kreth/property2java/generated/Resource_Bundle_Properties.java @@ -0,0 +1,126 @@ +package de.kreth.property2java.generated; + +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; + +import javax.annotation.processing.Generated; + +/** + * Property keys from resource_bundle.properties + * {@link #getValue()} gives the key for the entry, with {@link #getText()} the value for the key is given directly. + * Initializationis generated also. + */ +@Generated(date = "05.08.2024, 22:08:26", value = "de.kreth.property2java.Generator") +public enum Resource_Bundle_Properties { + + /** + * label = "" + */ + LABEL ("label"), + /** + * label.addarticle = "Add Article" + */ + LABEL_ADDARTICLE ("label.addarticle"), + /** + * label.cancel = "Cancel" + */ + LABEL_CANCEL ("label.cancel"), + /** + * label.close = "Close" + */ + LABEL_CLOSE ("label.close"), + /** + * label.delete = "Delete" + */ + LABEL_DELETE ("label.delete"), + /** + * label.discart = "Discart" + */ + LABEL_DISCART ("label.discart"), + /** + * label.loggedin = "Logged in:" + */ + LABEL_LOGGEDIN ("label.loggedin"), + /** + * label.logout = "Logout" + */ + LABEL_LOGOUT ("label.logout"), + /** + * label.ok = "OK" + */ + LABEL_OK ("label.ok"), + /** + * label.open = "Open" + */ + LABEL_OPEN ("label.open"), + /** + * label.preview = "Preview" + */ + LABEL_PREVIEW ("label.preview"), + /** + * label.store = "Store" + */ + LABEL_STORE ("label.store"), + /** + * label.user.register = "Register" + */ + LABEL_USER_REGISTER ("label.user.register"), + /** + * message.article.priceerror = "Please set the price." + */ + MESSAGE_ARTICLE_PRICEERROR ("message.article.priceerror"), + /** + * message.delete.text = "Delete {0}?" + */ + MESSAGE_DELETE_TEXT ("message.delete.text"), + /** + * message.delete.title = "Really delete?" + */ + MESSAGE_DELETE_TITLE ("message.delete.title"), + /** + * message.invoiceitem.allfieldsmustbeset = "Start, end and article must not be + empty!" + */ + MESSAGE_INVOICEITEM_ALLFIELDSMUSTBESET ("message.invoiceitem.allfieldsmustbeset"), + /** + * message.invoiceitem.startbeforeend = "End must be later than start." + */ + MESSAGE_INVOICEITEM_STARTBEFOREEND ("message.invoiceitem.startbeforeend"), + /** + * message.user.create.success = "Thanks {0} created!" + */ + MESSAGE_USER_CREATE_SUCCESS ("message.user.create.success"), + /** + * message.user.loginfailure = "Login Error! Wrong user or password?" + */ + MESSAGE_USER_LOGINFAILURE ("message.user.loginfailure"), + /** + * message.user.passwordmissmatch = "Passwords don't match." + */ + MESSAGE_USER_PASSWORDMISSMATCH ("message.user.passwordmissmatch"); + + private static ResourceBundle bundle = PropertyResourceBundle.getBundle("resource_bundle"); + + private final String value; + + private Resource_Bundle_Properties (String value) { + this.value = value; + } + + /** + * Represented Key in property File. + * @return key + */ + public String getValue() { + return value; + } + + /** + * The Text for this Key from PropertyResourceBundle + * @return human readable text + */ + public String getText() { + return bundle.getString(value); + } + +} diff --git a/src/test/java/de/kreth/property2java/generated/TestRealGenerated.java b/src/test/java/de/kreth/property2java/generated/TestRealGenerated.java new file mode 100644 index 0000000..7d2d156 --- /dev/null +++ b/src/test/java/de/kreth/property2java/generated/TestRealGenerated.java @@ -0,0 +1,19 @@ +package de.kreth.property2java.generated; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class TestRealGenerated { + + @Test + void propLoader() { + assertEquals("Please set the price.", Property_Loader_Properties.MESSAGE_ARTICLE_PRICEERROR.getText()); + } + + @Test + void resourceBundle() { + assertEquals("Login Error! Wrong user or password?", Resource_Bundle_Properties.MESSAGE_USER_LOGINFAILURE.getText()); + } + +} diff --git a/src/test/java/de/kreth/property2java/generated/Unary_Operator_Properties.java b/src/test/java/de/kreth/property2java/generated/Unary_Operator_Properties.java new file mode 100644 index 0000000..4cf86ba --- /dev/null +++ b/src/test/java/de/kreth/property2java/generated/Unary_Operator_Properties.java @@ -0,0 +1,127 @@ +package de.kreth.property2java.generated; + +import java.util.Properties; +import java.util.ResourceBundle; +import java.util.function.UnaryOperator; + +import javax.annotation.processing.Generated; + +/** + * Property keys from unary_operator.properties + * {@link #getValue()} gives the key for the entry, with {@link #getString(UnaryOperator)} + * the value is given directly. + */ +@Generated(date = "05.08.2024, 22:08:26", value = "de.kreth.property2java.Generator") +public enum Unary_Operator_Properties { + + /** + * label = "" + */ + LABEL ("label"), + /** + * label.addarticle = "Add Article" + */ + LABEL_ADDARTICLE ("label.addarticle"), + /** + * label.cancel = "Cancel" + */ + LABEL_CANCEL ("label.cancel"), + /** + * label.close = "Close" + */ + LABEL_CLOSE ("label.close"), + /** + * label.delete = "Delete" + */ + LABEL_DELETE ("label.delete"), + /** + * label.discart = "Discart" + */ + LABEL_DISCART ("label.discart"), + /** + * label.loggedin = "Logged in:" + */ + LABEL_LOGGEDIN ("label.loggedin"), + /** + * label.logout = "Logout" + */ + LABEL_LOGOUT ("label.logout"), + /** + * label.ok = "OK" + */ + LABEL_OK ("label.ok"), + /** + * label.open = "Open" + */ + LABEL_OPEN ("label.open"), + /** + * label.preview = "Preview" + */ + LABEL_PREVIEW ("label.preview"), + /** + * label.store = "Store" + */ + LABEL_STORE ("label.store"), + /** + * label.user.register = "Register" + */ + LABEL_USER_REGISTER ("label.user.register"), + /** + * message.article.priceerror = "Please set the price." + */ + MESSAGE_ARTICLE_PRICEERROR ("message.article.priceerror"), + /** + * message.delete.text = "Delete {0}?" + */ + MESSAGE_DELETE_TEXT ("message.delete.text"), + /** + * message.delete.title = "Really delete?" + */ + MESSAGE_DELETE_TITLE ("message.delete.title"), + /** + * message.invoiceitem.allfieldsmustbeset = "Start, end and article must not be + empty!" + */ + MESSAGE_INVOICEITEM_ALLFIELDSMUSTBESET ("message.invoiceitem.allfieldsmustbeset"), + /** + * message.invoiceitem.startbeforeend = "End must be later than start." + */ + MESSAGE_INVOICEITEM_STARTBEFOREEND ("message.invoiceitem.startbeforeend"), + /** + * message.user.create.success = "Thanks {0} created!" + */ + MESSAGE_USER_CREATE_SUCCESS ("message.user.create.success"), + /** + * message.user.loginfailure = "Login Error! Wrong user or password?" + */ + MESSAGE_USER_LOGINFAILURE ("message.user.loginfailure"), + /** + * message.user.passwordmissmatch = "Passwords don't match." + */ + MESSAGE_USER_PASSWORDMISSMATCH ("message.user.passwordmissmatch"); + + private final String value; + + private Unary_Operator_Properties (String value) { + this.value = value; + } + + /** + * Represented Key in property File. + * @return key + */ + public String getValue() { + return value; + } + + /** + * Resolves the value for this key from the parameter function. + *

+ * e.g. Unary_Operator_Properties.getString(resBundle::getString) + * @param resourceFunction {@link Properties#getProperty(String)} or {@link ResourceBundle#getString(String)} + * @return + */ + public String getString(UnaryOperator resourceFunction) { + return resourceFunction.apply(value); + } +} diff --git a/src/test/resources/property_loader.properties b/src/test/resources/property_loader.properties new file mode 100644 index 0000000..84cf35a --- /dev/null +++ b/src/test/resources/property_loader.properties @@ -0,0 +1,24 @@ + +label = + +label.addarticle = Add Article +label.cancel = Cancel +label.close = Close +label.delete = Delete +label.discart = Discart +label.loggedin = Logged in: +label.logout = Logout +label.ok = OK +label.store = Store +label.preview = Preview +label.open = Open +label.user.register = Register + +message.article.priceerror = Please set the price. +message.delete.text = Delete {0}? +message.delete.title = Really delete? +message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \r\n empty! +message.invoiceitem.startbeforeend = End must be later than start. +message.user.create.success = Thanks {0} created! +message.user.loginfailure = Login Error! Wrong user or password? +message.user.passwordmissmatch = Passwords don't match. diff --git a/src/test/resources/resource_bundle.properties b/src/test/resources/resource_bundle.properties new file mode 100644 index 0000000..84cf35a --- /dev/null +++ b/src/test/resources/resource_bundle.properties @@ -0,0 +1,24 @@ + +label = + +label.addarticle = Add Article +label.cancel = Cancel +label.close = Close +label.delete = Delete +label.discart = Discart +label.loggedin = Logged in: +label.logout = Logout +label.ok = OK +label.store = Store +label.preview = Preview +label.open = Open +label.user.register = Register + +message.article.priceerror = Please set the price. +message.delete.text = Delete {0}? +message.delete.title = Really delete? +message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \r\n empty! +message.invoiceitem.startbeforeend = End must be later than start. +message.user.create.success = Thanks {0} created! +message.user.loginfailure = Login Error! Wrong user or password? +message.user.passwordmissmatch = Passwords don't match. diff --git a/src/test/resources/unary_operator.properties b/src/test/resources/unary_operator.properties new file mode 100644 index 0000000..84cf35a --- /dev/null +++ b/src/test/resources/unary_operator.properties @@ -0,0 +1,24 @@ + +label = + +label.addarticle = Add Article +label.cancel = Cancel +label.close = Close +label.delete = Delete +label.discart = Discart +label.loggedin = Logged in: +label.logout = Logout +label.ok = OK +label.store = Store +label.preview = Preview +label.open = Open +label.user.register = Register + +message.article.priceerror = Please set the price. +message.delete.text = Delete {0}? +message.delete.title = Really delete? +message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \r\n empty! +message.invoiceitem.startbeforeend = End must be later than start. +message.user.create.success = Thanks {0} created! +message.user.loginfailure = Login Error! Wrong user or password? +message.user.passwordmissmatch = Passwords don't match.