From e4cc48ec1791029f06bdcaa64eb1189fa09184a7 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Sat, 25 May 2019 23:49:51 +0200 Subject: [PATCH] Sorting properties by name --- .../java/de/kreth/property2java/Generator.java | 18 +++++++++++++++--- .../kreth/property2java/ConfigurationTest.java | 1 + .../de/kreth/property2java/GeneratorTests.java | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 508d173..7a8516d 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -5,6 +5,7 @@ import java.io.Reader; import java.io.Writer; import java.text.DateFormat; import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.Enumeration; import java.util.HashMap; @@ -68,10 +69,10 @@ public class Generator { root.put("entries", entries); @SuppressWarnings("unchecked") - Enumeration propertyNames = (Enumeration) properties.propertyNames(); + List propertyNames = Collections.list((Enumeration) properties.propertyNames()); + Collections.sort(propertyNames); - while (propertyNames.hasMoreElements()) { - final String propertyKeyString = propertyNames.nextElement(); + for (String propertyKeyString : propertyNames) { final String propertyValue = properties.getProperty(propertyKeyString); entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, @@ -85,6 +86,11 @@ public class Generator { generator.start(); } + /** + * Represents an Property Entry for the generated java class. + * @author markus + * + */ public class Entry { public final String constant; @@ -93,6 +99,12 @@ public class Generator { public final String value; + /** + * Creates Property Entry data for the generated java class. + * @param constant name for the created constant. + * @param key property key + * @param value property value + */ public Entry(String constant, String key, String value) { super(); this.constant = constant; diff --git a/src/test/java/de/kreth/property2java/ConfigurationTest.java b/src/test/java/de/kreth/property2java/ConfigurationTest.java index 97bc74f..c4f9e25 100644 --- a/src/test/java/de/kreth/property2java/ConfigurationTest.java +++ b/src/test/java/de/kreth/property2java/ConfigurationTest.java @@ -29,6 +29,7 @@ class ConfigurationTest { when(config.outWriter(anyString())).thenCallRealMethod(); when(config.getRootPath()).thenReturn(new File(".").toPath()); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); + when(config.outputCharset()).thenCallRealMethod(); Writer outWriter = config.outWriter("application.properties"); assertTrue(outWriter instanceof FileWriter); diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index 57e9cb4..12ece38 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -43,6 +43,7 @@ class GeneratorTests { config = mock(Configuration.class); when(config.getInput()).thenReturn(input); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); + when(config.outputCharset()).thenCallRealMethod(); generator = new Generator(config); }