() {
+
+ @Override
+ public String getKey() {
+ return propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_");
+ }
+
+ @Override
+ public String getValue() {
+ return propertyValue;
+ }
+
+ @Override
+ public String setValue(String value) {
+ throw new UnsupportedOperationException("Set Value not supported!");
+ }
+ };
+ entries.add(entry);
}
- out.write(" }\n");
- out.flush();
- out.close();
+ template.process(root, out);
}
- public static void main(String[] args) throws IOException {
+ public static void main(String[] args) throws IOException, TemplateException {
Generator generator = new Generator(ArgumentConfiguration.parse(args));
generator.start();
}
diff --git a/src/main/resources/template/enum_template.tpl b/src/main/resources/template/enum_template.tpl
new file mode 100644
index 0000000..19b5379
--- /dev/null
+++ b/src/main/resources/template/enum_template.tpl
@@ -0,0 +1,44 @@
+<#if package??>package ${package};
+
+#if>import java.util.Properties;
+import java.util.ResourceBundle;
+import java.util.function.UnaryOperator;
+
+/**
+ * Property key from ${fileName}
+ */
+public enum ${classname} {
+
+<#list entries as entry>
+ /**
+ * "${entry.value}"
+ */
+ ${entry.key} ("${entry.value}")<#sep>,
+#sep>
+#list>;
+
+ private final String value;
+
+ private ${classname} (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. ${classname}.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/java/de/kreth/property2java/ConfigurationTest.java b/src/test/java/de/kreth/property2java/ConfigurationTest.java
index 5df1040..996f21b 100644
--- a/src/test/java/de/kreth/property2java/ConfigurationTest.java
+++ b/src/test/java/de/kreth/property2java/ConfigurationTest.java
@@ -33,12 +33,14 @@ class ConfigurationTest {
@Test
void testPathMapping() {
+ when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
String className = config.mapFilenameToClassName("application.properties");
assertEquals("Application_Properties", className);
}
@Test
void testPathMappingLocalized() {
+ when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
String className = config.mapFilenameToClassName("application_de_DE.properties");
assertEquals("Application_Properties", className);
className = config.mapFilenameToClassName("application_en_US.properties");
diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java
index 1429197..cd339b9 100644
--- a/src/test/java/de/kreth/property2java/GeneratorTests.java
+++ b/src/test/java/de/kreth/property2java/GeneratorTests.java
@@ -27,6 +27,8 @@ import org.hamcrest.Matchers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import freemarker.template.TemplateException;
+
class GeneratorTests {
private String path = "application.properties";
@@ -48,9 +50,10 @@ class GeneratorTests {
}
@Test
- void testClassDefinition() throws IOException {
+ void testClassDefinition() throws IOException, TemplateException {
when(config.getPackage()).thenReturn("de.kreth.property2java");
+ when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
StringWriter out = new StringWriter();
when(config.outWriter(anyString())).thenReturn(out);
@@ -68,7 +71,7 @@ class GeneratorTests {
if (line.trim().startsWith("package")) {
linePackage = line;
}
- else if (line.trim().startsWith("public interface")) {
+ else if (line.trim().startsWith("public enum")) {
lineClass = line;
}
if (line.contains("{")) {
@@ -89,12 +92,12 @@ class GeneratorTests {
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";")));
assertThat(lineClass,
- Matchers.stringContainsInOrder(Arrays.asList("public", "interface", "Application_Properties")));
+ Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties")));
}
@Test
- void testOneInputGeneratesOneOutput() throws IOException {
+ void testOneInputGeneratesOneOutput() throws IOException, TemplateException {
Writer out = mock(Writer.class);
Writer nonOut = mock(Writer.class);
@@ -106,7 +109,7 @@ class GeneratorTests {
}
@Test
- void testKeys() throws IOException {
+ void testKeys() throws IOException, TemplateException {
StringWriter out = new StringWriter();
when(config.outWriter(anyString())).thenReturn(out);