Real files generated, formats optimized.

pull/11/head
Markus Kreth 1 year ago
parent 7a2cbd108c
commit f0a9b158c4
  1. 6
      src/main/resources/template/enum_template_with_inner_properties.tpl
  2. 8
      src/main/resources/template/enum_template_with_inner_propertyresourcebundle.tpl
  3. 19
      src/test/java/de/kreth/property2java/TestPropertiesSource.java
  4. 124
      src/test/java/de/kreth/property2java/generated/GenerateTheTest.java
  5. 132
      src/test/java/de/kreth/property2java/generated/Property_Loader_Properties.java
  6. 126
      src/test/java/de/kreth/property2java/generated/Resource_Bundle_Properties.java
  7. 19
      src/test/java/de/kreth/property2java/generated/TestRealGenerated.java
  8. 127
      src/test/java/de/kreth/property2java/generated/Unary_Operator_Properties.java
  9. 24
      src/test/resources/property_loader.properties
  10. 24
      src/test/resources/resource_bundle.properties
  11. 24
      src/test/resources/unary_operator.properties

@ -1,6 +1,8 @@
<#if package??>package ${package};
</#if>import java.util.Properties;
</#if>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);
}

@ -1,9 +1,7 @@
<#if package??>package ${package};
</#if>import java.util.Properties;
import java.util.PropertyResourceBundle;
</#if>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} {
</#sep>
</#list>;
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

@ -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);
}
}
}

@ -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<String, Reader> 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<String, Reader> getInput() {
return input;
}
};
Generator generator = new Generator(config);
generator.start();
}
private static void withUnaryOperatorParameter(Path current) throws IOException, GeneratorException {
Format format = Format.WithUnaryOperatorParameter;
Map<String, Reader> 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<String, Reader> getInput() {
return input;
}
};
Generator generator = new Generator(config);
generator.start();
}
private static void withInnerPropertyLoader(Path current) throws IOException, GeneratorException {
Format format = Format.WithInnerPropertyLoader;
Map<String, Reader> 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<String, Reader> getInput() {
return input;
}
};
Generator generator = new Generator(config);
generator.start();
}
}

@ -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);
}
}

@ -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);
}
}

@ -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());
}
}

@ -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<String>)}
* 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.
* <p>
* e.g. <code>Unary_Operator_Properties.getString(resBundle::getString)</code>
* @param resourceFunction {@link Properties#getProperty(String)} or {@link ResourceBundle#getString(String)}
* @return
*/
public String getString(UnaryOperator<String> resourceFunction) {
return resourceFunction.apply(value);
}
}

@ -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.

@ -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.

@ -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.
Loading…
Cancel
Save