Sorting properties by name

REL-BRANCH-PropertyToJavaGenerator-0.0.4
Markus Kreth 7 years ago
parent f41a50093a
commit e4cc48ec17
  1. 18
      src/main/java/de/kreth/property2java/Generator.java
  2. 1
      src/test/java/de/kreth/property2java/ConfigurationTest.java
  3. 1
      src/test/java/de/kreth/property2java/GeneratorTests.java

@ -5,6 +5,7 @@ import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
@ -68,10 +69,10 @@ public class Generator {
root.put("entries", entries); root.put("entries", entries);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Enumeration<String> propertyNames = (Enumeration<String>) properties.propertyNames(); List<String> propertyNames = Collections.list((Enumeration<String>) properties.propertyNames());
Collections.sort(propertyNames);
while (propertyNames.hasMoreElements()) { for (String propertyKeyString : propertyNames) {
final String propertyKeyString = propertyNames.nextElement();
final String propertyValue = properties.getProperty(propertyKeyString); final String propertyValue = properties.getProperty(propertyKeyString);
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString,
@ -85,6 +86,11 @@ public class Generator {
generator.start(); generator.start();
} }
/**
* Represents an Property Entry for the generated java class.
* @author markus
*
*/
public class Entry { public class Entry {
public final String constant; public final String constant;
@ -93,6 +99,12 @@ public class Generator {
public final String value; 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) { public Entry(String constant, String key, String value) {
super(); super();
this.constant = constant; this.constant = constant;

@ -29,6 +29,7 @@ class ConfigurationTest {
when(config.outWriter(anyString())).thenCallRealMethod(); when(config.outWriter(anyString())).thenCallRealMethod();
when(config.getRootPath()).thenReturn(new File(".").toPath()); when(config.getRootPath()).thenReturn(new File(".").toPath());
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod();
Writer outWriter = config.outWriter("application.properties"); Writer outWriter = config.outWriter("application.properties");
assertTrue(outWriter instanceof FileWriter); assertTrue(outWriter instanceof FileWriter);

@ -43,6 +43,7 @@ class GeneratorTests {
config = mock(Configuration.class); config = mock(Configuration.class);
when(config.getInput()).thenReturn(input); when(config.getInput()).thenReturn(input);
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
when(config.outputCharset()).thenCallRealMethod();
generator = new Generator(config); generator = new Generator(config);
} }

Loading…
Cancel
Save