parent
ef5cdbfe4e
commit
9bf094d376
@ -1,47 +1,47 @@ |
|||||||
package de.kreth.property2java; |
package de.kreth.property2java; |
||||||
|
|
||||||
import java.io.File; |
import java.io.File; |
||||||
import java.io.FileWriter; |
import java.io.FileWriter; |
||||||
import java.io.IOException; |
import java.io.IOException; |
||||||
import java.io.Reader; |
import java.io.Reader; |
||||||
import java.io.Writer; |
import java.io.Writer; |
||||||
import java.nio.file.Path; |
import java.nio.file.Path; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
import java.util.regex.Pattern; |
import java.util.regex.Pattern; |
||||||
|
|
||||||
import org.apache.commons.text.WordUtils; |
import org.apache.commons.text.WordUtils; |
||||||
|
|
||||||
public interface Configuration { |
public interface Configuration { |
||||||
|
|
||||||
static final Pattern REGEX = Pattern.compile("_[a-z]{2}(_[A-Z]{2})?\\."); |
static final Pattern REGEX = Pattern.compile("_[a-z]{2}(_[A-Z]{2})?\\."); |
||||||
|
|
||||||
/** |
/** |
||||||
* Package for generated Java Classes eg. "de.kreth.property2java". If null - no package line is generated. |
* Package for generated Java Classes eg. "de.kreth.property2java". If null - no package line is generated. |
||||||
* @return |
* @return |
||||||
*/ |
*/ |
||||||
String getPackage(); |
String getPackage(); |
||||||
|
|
||||||
/** |
/** |
||||||
* Filename to InputReader Entries |
* Filename to InputReader Entries |
||||||
* @return |
* @return |
||||||
*/ |
*/ |
||||||
Map<String, Reader> getInput(); |
Map<String, Reader> getInput(); |
||||||
|
|
||||||
/** |
/** |
||||||
* Path of java source folder. |
* Path of java source folder. |
||||||
* @return |
* @return |
||||||
*/ |
*/ |
||||||
Path getRootPath(); |
Path getRootPath(); |
||||||
|
|
||||||
default Writer outWriter(String fileName) throws IOException { |
default Writer outWriter(String fileName) throws IOException { |
||||||
return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName))); |
return new FileWriter(new File(getRootPath().toFile(), mapFilenameToClassName(fileName) + ".java")); |
||||||
} |
} |
||||||
|
|
||||||
default String mapFilenameToClassName(String fileName) { |
default String mapFilenameToClassName(String fileName) { |
||||||
|
|
||||||
String path = REGEX.matcher(fileName).replaceAll(".").replaceAll("\\.", "_").replaceAll(" ", "_"); |
String path = REGEX.matcher(fileName).replaceAll(".").replaceAll("\\.", "_").replaceAll(" ", "_"); |
||||||
path = WordUtils.capitalize(path, '_'); |
path = WordUtils.capitalize(path, '_'); |
||||||
return path; |
return path; |
||||||
} |
} |
||||||
|
|
||||||
} |
} |
||||||
|
|||||||
@ -1,33 +1,24 @@ |
|||||||
package de.kreth.property2java; |
package de.kreth.property2java; |
||||||
|
|
||||||
import java.io.File; |
import java.io.IOException; |
||||||
import java.io.IOException; |
|
||||||
import java.net.URL; |
import freemarker.template.Configuration; |
||||||
|
import freemarker.template.Template; |
||||||
import freemarker.template.Configuration; |
|
||||||
import freemarker.template.Template; |
public enum FreemarkerConfig { |
||||||
|
|
||||||
public enum FreemarkerConfig { |
INSTANCE; |
||||||
|
|
||||||
INSTANCE; |
private final Configuration cfg; |
||||||
|
|
||||||
private final Configuration cfg; |
public Template getTemplate() throws IOException { |
||||||
|
return cfg.getTemplate("enum_template.tpl"); |
||||||
public Template getTemplate() throws IOException { |
} |
||||||
return cfg.getTemplate("enum_template.tpl"); |
|
||||||
} |
private FreemarkerConfig() { |
||||||
|
cfg = new Configuration(Configuration.VERSION_2_3_28); |
||||||
private FreemarkerConfig() { |
cfg.setClassForTemplateLoading(this.getClass(), "/template/"); |
||||||
cfg = new Configuration(Configuration.VERSION_2_3_28); |
cfg.setDefaultEncoding("UTF-8"); |
||||||
URL url = getClass().getResource("/template/enum_template.tpl"); |
} |
||||||
try { |
|
||||||
cfg.setDirectoryForTemplateLoading(new File(url.getFile()).getParentFile()); |
} |
||||||
} |
|
||||||
catch (IOException e) { |
|
||||||
throw new IllegalStateException("Unable to configure freemarker", e); |
|
||||||
} |
|
||||||
|
|
||||||
cfg.setDefaultEncoding("UTF-8"); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|||||||
@ -1,108 +1,121 @@ |
|||||||
package de.kreth.property2java; |
package de.kreth.property2java; |
||||||
|
|
||||||
import java.io.IOException; |
import java.io.File; |
||||||
import java.io.Reader; |
import java.io.IOException; |
||||||
import java.io.Writer; |
import java.io.Reader; |
||||||
import java.text.DateFormat; |
import java.io.Writer; |
||||||
import java.util.ArrayList; |
import java.text.DateFormat; |
||||||
import java.util.Date; |
import java.util.ArrayList; |
||||||
import java.util.Enumeration; |
import java.util.Date; |
||||||
import java.util.HashMap; |
import java.util.Enumeration; |
||||||
import java.util.List; |
import java.util.HashMap; |
||||||
import java.util.Map; |
import java.util.List; |
||||||
import java.util.Properties; |
import java.util.Map; |
||||||
|
import java.util.Properties; |
||||||
import de.kreth.property2java.cli.ArgumentConfiguration; |
|
||||||
import freemarker.template.Template; |
import de.kreth.property2java.cli.ArgumentConfiguration; |
||||||
import freemarker.template.TemplateException; |
import freemarker.template.Template; |
||||||
|
import freemarker.template.TemplateException; |
||||||
public class Generator { |
|
||||||
|
public class Generator { |
||||||
private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); |
|
||||||
|
private static final DateFormat dateTimeInstance = DateFormat.getDateTimeInstance(); |
||||||
private final Configuration config; |
|
||||||
|
private final Configuration config; |
||||||
private final Template template; |
|
||||||
|
private final Template template; |
||||||
public Generator(Configuration config) { |
|
||||||
this.config = config; |
public Generator(Configuration config) { |
||||||
try { |
this.config = config; |
||||||
template = FreemarkerConfig.INSTANCE.getTemplate(); |
try { |
||||||
} |
template = FreemarkerConfig.INSTANCE.getTemplate(); |
||||||
catch (IOException e) { |
} |
||||||
throw new IllegalStateException("Unable to load freemarker template", e); |
catch (IOException e) { |
||||||
} |
throw new IllegalStateException("Unable to load freemarker template", e); |
||||||
} |
} |
||||||
|
} |
||||||
public void start() throws IOException, TemplateException { |
|
||||||
|
public void start() throws IOException, GeneratorException { |
||||||
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) { |
|
||||||
String fileName = entry.getKey(); |
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) { |
||||||
Writer out = config.outWriter(fileName); |
String fileName = entry.getKey(); |
||||||
Properties properties = new Properties(); |
try (Writer out = config.outWriter(new File(fileName).getName())) { |
||||||
properties.load(entry.getValue()); |
|
||||||
generate(properties, out, fileName, config); |
Properties properties = new Properties(); |
||||||
} |
properties.load(entry.getValue()); |
||||||
} |
try { |
||||||
|
generate(properties, out, fileName, config); |
||||||
void generate(Properties properties, Writer out, String fileName, Configuration config) |
} |
||||||
throws IOException, TemplateException { |
catch (TemplateException e) { |
||||||
|
throw new GeneratorException("Error configuring Engine", e); |
||||||
Map<String, Object> root = new HashMap<>(); |
} |
||||||
root.put("generator_name", getClass().getName()); |
} |
||||||
root.put("generation_date", dateTimeInstance.format(new Date())); |
} |
||||||
root.put("package", config.getPackage()); |
} |
||||||
root.put("fileName", fileName); |
|
||||||
root.put("classname", config.mapFilenameToClassName(fileName)); |
void generate(Properties properties, Writer out, String fileName, Configuration config) |
||||||
|
throws IOException, TemplateException { |
||||||
List<Entry> entries = new ArrayList<>(); |
|
||||||
|
Map<String, Object> root = new HashMap<>(); |
||||||
root.put("entries", entries); |
root.put("generator_name", getClass().getName()); |
||||||
|
root.put("generation_date", dateTimeInstance.format(new Date())); |
||||||
@SuppressWarnings("unchecked") |
root.put("package", config.getPackage()); |
||||||
Enumeration<String> propertyNames = (Enumeration<String>) properties.propertyNames(); |
root.put("fileName", fileName); |
||||||
|
root.put("classname", config.mapFilenameToClassName(fileName)); |
||||||
while (propertyNames.hasMoreElements()) { |
|
||||||
final String propertyKeyString = propertyNames.nextElement(); |
List<Entry> entries = new ArrayList<>(); |
||||||
final String propertyValue = properties.getProperty(propertyKeyString); |
|
||||||
|
root.put("entries", entries); |
||||||
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, |
|
||||||
propertyValue)); |
@SuppressWarnings("unchecked") |
||||||
} |
Enumeration<String> propertyNames = (Enumeration<String>) properties.propertyNames(); |
||||||
template.process(root, out); |
|
||||||
} |
while (propertyNames.hasMoreElements()) { |
||||||
|
final String propertyKeyString = propertyNames.nextElement(); |
||||||
public static void main(String[] args) throws IOException, TemplateException { |
final String propertyValue = properties.getProperty(propertyKeyString); |
||||||
Generator generator = new Generator(ArgumentConfiguration.parse(args)); |
|
||||||
generator.start(); |
entries.add(new Entry(propertyKeyString.toUpperCase().replaceAll("[\\.-]", "_"), propertyKeyString, |
||||||
} |
propertyValue)); |
||||||
|
} |
||||||
public class Entry { |
template.process(root, out); |
||||||
|
} |
||||||
public final String constant; |
|
||||||
|
public static void main(String[] args) throws IOException, GeneratorException { |
||||||
public final String key; |
Generator generator = new Generator(ArgumentConfiguration.parse(args)); |
||||||
|
generator.start(); |
||||||
public final String value; |
} |
||||||
|
|
||||||
public Entry(String constant, String key, String value) { |
public class Entry { |
||||||
super(); |
|
||||||
this.constant = constant; |
public final String constant; |
||||||
this.key = key; |
|
||||||
this.value = value; |
public final String key; |
||||||
} |
|
||||||
|
public final String value; |
||||||
public String getConstant() { |
|
||||||
return constant; |
public Entry(String constant, String key, String value) { |
||||||
} |
super(); |
||||||
|
this.constant = constant; |
||||||
public String getKey() { |
this.key = key; |
||||||
return key; |
this.value = value; |
||||||
} |
} |
||||||
|
|
||||||
public String getValue() { |
public String getConstant() { |
||||||
return value; |
return constant; |
||||||
} |
} |
||||||
|
|
||||||
} |
public String getKey() { |
||||||
} |
return key; |
||||||
|
} |
||||||
|
|
||||||
|
public String getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public String toString() { |
||||||
|
return "Entry [constant=" + constant + ", key=" + key + ", value=" + value + "]"; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
} |
||||||
|
|||||||
@ -0,0 +1,19 @@ |
|||||||
|
package de.kreth.property2java; |
||||||
|
|
||||||
|
public class GeneratorException extends Exception { |
||||||
|
|
||||||
|
private static final long serialVersionUID = -7319030228448260990L; |
||||||
|
|
||||||
|
public GeneratorException(String message, Throwable cause) { |
||||||
|
super(message, cause); |
||||||
|
} |
||||||
|
|
||||||
|
public GeneratorException(String message) { |
||||||
|
super(message); |
||||||
|
} |
||||||
|
|
||||||
|
public GeneratorException(Throwable cause) { |
||||||
|
super(cause); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -1,52 +1,52 @@ |
|||||||
package de.kreth.property2java.cli; |
package de.kreth.property2java.cli; |
||||||
|
|
||||||
import java.io.IOException; |
import java.io.IOException; |
||||||
|
|
||||||
import org.apache.commons.cli.CommandLine; |
import org.apache.commons.cli.CommandLine; |
||||||
import org.apache.commons.cli.CommandLineParser; |
import org.apache.commons.cli.CommandLineParser; |
||||||
import org.apache.commons.cli.DefaultParser; |
import org.apache.commons.cli.DefaultParser; |
||||||
import org.apache.commons.cli.HelpFormatter; |
import org.apache.commons.cli.HelpFormatter; |
||||||
import org.apache.commons.cli.MissingOptionException; |
import org.apache.commons.cli.MissingOptionException; |
||||||
import org.apache.commons.cli.Option; |
import org.apache.commons.cli.Option; |
||||||
import org.apache.commons.cli.Options; |
import org.apache.commons.cli.Options; |
||||||
import org.apache.commons.cli.ParseException; |
import org.apache.commons.cli.ParseException; |
||||||
|
|
||||||
import de.kreth.property2java.cli.ArgumentConfiguration.Builder; |
import de.kreth.property2java.cli.ArgumentConfiguration.Builder; |
||||||
|
|
||||||
public class CliConfig { |
public class CliConfig { |
||||||
|
|
||||||
private final Options options = options(); |
private final Options options = options(); |
||||||
|
|
||||||
private Options options() { |
private Options options() { |
||||||
Options retVal = new Options(); |
Options retVal = new Options(); |
||||||
retVal.addOption(Option.builder("t").longOpt("targetSourcePath").hasArg().required().build()); |
retVal.addOption(Option.builder("t").longOpt("targetSourcePath").hasArg().required().build()); |
||||||
retVal.addOption(Option.builder("f").longOpt("files").hasArgs().required().valueSeparator(',').build()); |
retVal.addOption(Option.builder("f").longOpt("files").hasArgs().required().valueSeparator(',').build()); |
||||||
retVal.addOption(Option.builder("p").longOpt("package").hasArg().required(false).build()); |
retVal.addOption(Option.builder("p").longOpt("package").hasArg().required(false).build()); |
||||||
return retVal; |
return retVal; |
||||||
} |
} |
||||||
|
|
||||||
public void fill(Builder builder, String[] args) throws IOException { |
public void fill(Builder builder, String[] args) throws IOException { |
||||||
|
|
||||||
CommandLineParser parser = new DefaultParser(); |
CommandLineParser parser = new DefaultParser(); |
||||||
try { |
try { |
||||||
CommandLine cmd = parser.parse(options, args); |
CommandLine cmd = parser.parse(options, args); |
||||||
builder.setTarget(cmd.getOptionValue("t", ".")); |
builder.setTarget(cmd.getOptionValue("t", ".")); |
||||||
builder.setPackageName(cmd.getOptionValue("p")); |
builder.setPackageName(cmd.getOptionValue("p")); |
||||||
for (String value : cmd.getOptionValues("f")) { |
for (String value : cmd.getOptionValues("f")) { |
||||||
builder.addPropFile(value); |
builder.addPropFile(value); |
||||||
} |
} |
||||||
} |
} |
||||||
catch (MissingOptionException e) { |
catch (MissingOptionException e) { |
||||||
printHelp(); |
printHelp(); |
||||||
throw new IllegalStateException(e); |
throw new IllegalStateException(e); |
||||||
} |
} |
||||||
catch (ParseException e) { |
catch (ParseException e) { |
||||||
throw new IOException("Unable to parse Arguments", e); |
throw new IOException("Unable to parse Arguments", e); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
public void printHelp() { |
public void printHelp() { |
||||||
HelpFormatter formatter = new HelpFormatter(); |
HelpFormatter formatter = new HelpFormatter(); |
||||||
formatter.printHelp("Generator", options); |
formatter.printHelp("Generator", options); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|||||||
@ -1,50 +1,53 @@ |
|||||||
package de.kreth.property2java; |
package de.kreth.property2java; |
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
import static org.mockito.ArgumentMatchers.anyString; |
import static org.mockito.ArgumentMatchers.anyString; |
||||||
import static org.mockito.Mockito.when; |
import static org.mockito.Mockito.when; |
||||||
|
|
||||||
import java.io.FileWriter; |
import java.io.File; |
||||||
import java.io.IOException; |
import java.io.FileWriter; |
||||||
|
import java.io.IOException; |
||||||
import org.junit.jupiter.api.BeforeEach; |
import java.io.Writer; |
||||||
import org.junit.jupiter.api.Test; |
|
||||||
import org.mockito.Mockito; |
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
class ConfigurationTest { |
import org.mockito.Mockito; |
||||||
|
|
||||||
private Configuration config; |
class ConfigurationTest { |
||||||
|
|
||||||
@BeforeEach |
private Configuration config; |
||||||
void initConfig() { |
|
||||||
|
@BeforeEach |
||||||
config = Mockito.mock(Configuration.class); |
void initConfig() { |
||||||
} |
config = Mockito.mock(Configuration.class); |
||||||
|
} |
||||||
@Test |
|
||||||
void defaultWriterIsFileWriter() throws IOException { |
@Test |
||||||
|
void defaultWriterIsFileWriter() throws IOException { |
||||||
when(config.outWriter(anyString())).thenCallRealMethod(); |
|
||||||
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
when(config.outWriter(anyString())).thenCallRealMethod(); |
||||||
|
when(config.getRootPath()).thenReturn(new File(".").toPath()); |
||||||
assertTrue(config.outWriter("application.properties") instanceof FileWriter); |
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
||||||
} |
|
||||||
|
Writer outWriter = config.outWriter("application.properties"); |
||||||
@Test |
assertTrue(outWriter instanceof FileWriter); |
||||||
void testPathMapping() { |
} |
||||||
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
||||||
String className = config.mapFilenameToClassName("application.properties"); |
@Test |
||||||
assertEquals("Application_Properties", className); |
void testPathMapping() { |
||||||
} |
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
||||||
|
String className = config.mapFilenameToClassName("application.properties"); |
||||||
@Test |
assertEquals("Application_Properties", className); |
||||||
void testPathMappingLocalized() { |
} |
||||||
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
|
||||||
String className = config.mapFilenameToClassName("application_de_DE.properties"); |
@Test |
||||||
assertEquals("Application_Properties", className); |
void testPathMappingLocalized() { |
||||||
className = config.mapFilenameToClassName("application_en_US.properties"); |
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
||||||
assertEquals("Application_Properties", className); |
String className = config.mapFilenameToClassName("application_de_DE.properties"); |
||||||
} |
assertEquals("Application_Properties", className); |
||||||
|
className = config.mapFilenameToClassName("application_en_US.properties"); |
||||||
} |
assertEquals("Application_Properties", className); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|||||||
@ -1,178 +1,176 @@ |
|||||||
package de.kreth.property2java; |
package de.kreth.property2java; |
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat; |
import static org.hamcrest.MatcherAssert.assertThat; |
||||||
import static org.junit.jupiter.api.Assertions.assertEquals; |
import static org.junit.jupiter.api.Assertions.assertEquals; |
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull; |
import static org.junit.jupiter.api.Assertions.assertNotNull; |
||||||
import static org.junit.jupiter.api.Assertions.assertTrue; |
import static org.junit.jupiter.api.Assertions.assertTrue; |
||||||
import static org.mockito.ArgumentMatchers.anyString; |
import static org.mockito.ArgumentMatchers.anyString; |
||||||
import static org.mockito.Mockito.mock; |
import static org.mockito.Mockito.mock; |
||||||
import static org.mockito.Mockito.never; |
import static org.mockito.Mockito.never; |
||||||
import static org.mockito.Mockito.verify; |
import static org.mockito.Mockito.verify; |
||||||
import static org.mockito.Mockito.when; |
import static org.mockito.Mockito.when; |
||||||
|
|
||||||
import java.io.IOException; |
import java.io.IOException; |
||||||
import java.io.Reader; |
import java.io.Reader; |
||||||
import java.io.StringReader; |
import java.io.StringReader; |
||||||
import java.io.StringWriter; |
import java.io.StringWriter; |
||||||
import java.io.Writer; |
import java.io.Writer; |
||||||
import java.util.Arrays; |
import java.util.Arrays; |
||||||
import java.util.HashMap; |
import java.util.HashMap; |
||||||
import java.util.List; |
import java.util.List; |
||||||
import java.util.Map; |
import java.util.Map; |
||||||
import java.util.Optional; |
import java.util.Optional; |
||||||
import java.util.StringTokenizer; |
import java.util.StringTokenizer; |
||||||
import java.util.stream.Collectors; |
import java.util.stream.Collectors; |
||||||
|
|
||||||
import org.hamcrest.Matchers; |
import org.hamcrest.Matchers; |
||||||
import org.junit.jupiter.api.BeforeEach; |
import org.junit.jupiter.api.BeforeEach; |
||||||
import org.junit.jupiter.api.Test; |
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
import freemarker.template.TemplateException; |
class GeneratorTests { |
||||||
|
|
||||||
class GeneratorTests { |
private String path = "application.properties"; |
||||||
|
|
||||||
private String path = "application.properties"; |
private Configuration config; |
||||||
|
|
||||||
private Configuration config; |
private Generator generator; |
||||||
|
|
||||||
private Generator generator; |
@BeforeEach |
||||||
|
void setUp() throws Exception { |
||||||
@BeforeEach |
Map<String, Reader> input = new HashMap<>(); |
||||||
void setUp() throws Exception { |
input.put(path, testProperties()); |
||||||
Map<String, Reader> input = new HashMap<>(); |
|
||||||
input.put(path, testProperties()); |
config = mock(Configuration.class); |
||||||
|
when(config.getInput()).thenReturn(input); |
||||||
config = mock(Configuration.class); |
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
||||||
when(config.getInput()).thenReturn(input); |
|
||||||
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
generator = new Generator(config); |
||||||
|
} |
||||||
generator = new Generator(config); |
|
||||||
} |
@Test |
||||||
|
void testClassDefinition() throws IOException, GeneratorException { |
||||||
@Test |
|
||||||
void testClassDefinition() throws IOException, TemplateException { |
when(config.getPackage()).thenReturn("de.kreth.property2java"); |
||||||
|
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
||||||
when(config.getPackage()).thenReturn("de.kreth.property2java"); |
|
||||||
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); |
StringWriter out = new StringWriter(); |
||||||
|
when(config.outWriter(anyString())).thenReturn(out); |
||||||
StringWriter out = new StringWriter(); |
|
||||||
when(config.outWriter(anyString())).thenReturn(out); |
generator.start(); |
||||||
|
|
||||||
generator.start(); |
String sourceCode = out.toString().trim(); |
||||||
|
StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n"); |
||||||
String sourceCode = out.toString().trim(); |
String linePackage = null; |
||||||
StringTokenizer sourceTokenizer = new StringTokenizer(sourceCode, "\n"); |
String lineClass = null; |
||||||
String linePackage = null; |
int countOpenBaces = 0; |
||||||
String lineClass = null; |
int countCloseBaces = 0; |
||||||
int countOpenBaces = 0; |
while (sourceTokenizer.hasMoreTokens()) { |
||||||
int countCloseBaces = 0; |
String line = sourceTokenizer.nextToken(); |
||||||
while (sourceTokenizer.hasMoreTokens()) { |
if (line.trim().startsWith("package")) { |
||||||
String line = sourceTokenizer.nextToken(); |
linePackage = line; |
||||||
if (line.trim().startsWith("package")) { |
} |
||||||
linePackage = line; |
else if (line.trim().startsWith("public enum")) { |
||||||
} |
lineClass = line; |
||||||
else if (line.trim().startsWith("public enum")) { |
} |
||||||
lineClass = line; |
if (line.contains("{")) { |
||||||
} |
countOpenBaces++; |
||||||
if (line.contains("{")) { |
} |
||||||
countOpenBaces++; |
if (line.contains("}")) { |
||||||
} |
countCloseBaces++; |
||||||
if (line.contains("}")) { |
} |
||||||
countCloseBaces++; |
} |
||||||
} |
|
||||||
} |
assertEquals(countCloseBaces, countOpenBaces, |
||||||
|
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); |
||||||
assertEquals(countCloseBaces, countOpenBaces, |
|
||||||
"Count of Braces doesn't match. Open = " + countOpenBaces + ", Close = " + countCloseBaces); |
assertNotNull(linePackage); |
||||||
|
assertNotNull(lineClass); |
||||||
assertNotNull(linePackage); |
|
||||||
assertNotNull(lineClass); |
assertThat(linePackage, |
||||||
|
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); |
||||||
assertThat(linePackage, |
|
||||||
Matchers.stringContainsInOrder(Arrays.asList("package", "de.kreth.property2java", ";"))); |
assertThat(lineClass, |
||||||
|
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); |
||||||
assertThat(lineClass, |
|
||||||
Matchers.stringContainsInOrder(Arrays.asList("public", "enum", "Application_Properties"))); |
} |
||||||
|
|
||||||
} |
@Test |
||||||
|
void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { |
||||||
@Test |
|
||||||
void testOneInputGeneratesOneOutput() throws IOException, TemplateException { |
Writer out = mock(Writer.class); |
||||||
|
Writer nonOut = mock(Writer.class); |
||||||
Writer out = mock(Writer.class); |
when(config.outWriter(anyString())).thenReturn(out, nonOut); |
||||||
Writer nonOut = mock(Writer.class); |
generator.start(); |
||||||
when(config.outWriter(anyString())).thenReturn(out, nonOut); |
verify(out).close(); |
||||||
generator.start(); |
verify(nonOut, never()).close(); |
||||||
verify(out).close(); |
verify(nonOut, never()).flush(); |
||||||
verify(nonOut, never()).close(); |
} |
||||||
verify(nonOut, never()).flush(); |
|
||||||
} |
@Test |
||||||
|
void testKeys() throws IOException, GeneratorException { |
||||||
@Test |
|
||||||
void testKeys() throws IOException, TemplateException { |
StringWriter out = new StringWriter(); |
||||||
|
when(config.outWriter(anyString())).thenReturn(out); |
||||||
StringWriter out = new StringWriter(); |
generator.start(); |
||||||
when(config.outWriter(anyString())).thenReturn(out); |
|
||||||
generator.start(); |
List<String> lines = out.toString().lines().filter(line -> line.contains(" (\"")) |
||||||
|
.collect(Collectors.toList()); |
||||||
List<String> lines = out.toString().lines().filter(line -> line.contains(" String ")) |
|
||||||
.collect(Collectors.toList()); |
assertEquals(21, lines.size()); |
||||||
|
assertLineMatch(lines, "label", "label"); |
||||||
assertEquals(21, lines.size()); |
assertLineMatch(lines, "label_addarticle", "label.addarticle"); |
||||||
assertLineMatch(lines, "label", "label"); |
assertLineMatch(lines, "label_user_register", "label.user.register"); |
||||||
assertLineMatch(lines, "label_addarticle", "label.addarticle"); |
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); |
||||||
assertLineMatch(lines, "label_user_register", "label.user.register"); |
assertLineMatch(lines, "message_invoiceitem_startbeforeend", |
||||||
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); |
"message.invoiceitem.startbeforeend"); |
||||||
assertLineMatch(lines, "message_invoiceitem_startbeforeend", "message.invoiceitem.startbeforeend"); |
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", |
||||||
assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", |
"message.invoiceitem.allfieldsmustbeset"); |
||||||
"message.invoiceitem.allfieldsmustbeset"); |
} |
||||||
} |
|
||||||
|
private void assertLineMatch(List<String> lines, String key, String expected) { |
||||||
private void assertLineMatch(List<String> lines, String key, String expected) { |
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) |
||||||
Optional<String> found = lines.stream().filter(line -> keyMatches(line, key)) |
.findFirst(); |
||||||
.findFirst(); |
assertTrue(found.isPresent(), "No line found with key = " + key); |
||||||
assertTrue(found.isPresent(), "No line found with key = " + key); |
final String line = found.get().trim(); |
||||||
final String line = found.get().trim(); |
int indexEquals = line.indexOf('('); |
||||||
int indexEquals = line.indexOf('='); |
String value = line.substring(indexEquals + 1).trim().substring(1); |
||||||
String value = line.substring(indexEquals + 1).trim().substring(1); |
value = value.substring(0, value.length() - 3); |
||||||
value = value.substring(0, value.length() - 2); |
assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\""); |
||||||
assertEquals(expected, value, "Line \"" + line + "\" don't match expected Value \"" + expected + "\""); |
|
||||||
|
} |
||||||
assertEquals(';', line.charAt(line.length() - 1), "Line \"" + line + "\" don't end with ;"); |
|
||||||
} |
private boolean keyMatches(String line, String key) { |
||||||
|
line = line.toLowerCase(); |
||||||
private boolean keyMatches(String line, String key) { |
key = key.toLowerCase(); |
||||||
line = line.toLowerCase(); |
return line.contains("\t" + key + " "); |
||||||
key = key.toLowerCase(); |
} |
||||||
return line.contains(" " + key + " "); |
|
||||||
} |
private StringReader testProperties() { |
||||||
|
return new StringReader("\r\n" + |
||||||
private StringReader testProperties() { |
"label = \r\n" + |
||||||
return new StringReader("\r\n" + |
"\r\n" + |
||||||
"label = \r\n" + |
"label.addarticle = Add Article\r\n" + |
||||||
"\r\n" + |
"label.cancel = Cancel\r\n" + |
||||||
"label.addarticle = Add Article\r\n" + |
"label.close = Close\r\n" + |
||||||
"label.cancel = Cancel\r\n" + |
"label.delete = Delete\r\n" + |
||||||
"label.close = Close\r\n" + |
"label.discart = Discart\r\n" + |
||||||
"label.delete = Delete\r\n" + |
"label.loggedin = Logged in:\r\n" + |
||||||
"label.discart = Discart\r\n" + |
"label.logout = Logout\r\n" + |
||||||
"label.loggedin = Logged in:\r\n" + |
"label.ok = OK\r\n" + |
||||||
"label.logout = Logout\r\n" + |
"label.store = Store\r\n" + |
||||||
"label.ok = OK\r\n" + |
"label.preview = Preview\r\n" + |
||||||
"label.store = Store\r\n" + |
"label.open = Open\r\n" + |
||||||
"label.preview = Preview\r\n" + |
"label.user.register = Register\r\n" + |
||||||
"label.open = Open\r\n" + |
"\r\n" + |
||||||
"label.user.register = Register\r\n" + |
"message.article.priceerror = Please set the price.\r\n" + |
||||||
"\r\n" + |
"message.delete.text = Delete {0}?\r\n" + |
||||||
"message.article.priceerror = Please set the price.\r\n" + |
"message.delete.title = Really delete?\r\n" + |
||||||
"message.delete.text = Delete {0}?\r\n" + |
"message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n" + |
||||||
"message.delete.title = Really delete?\r\n" + |
" empty!\r\n" + |
||||||
"message.invoiceitem.allfieldsmustbeset = Start, end and article must not be \\r\\n" + |
"message.invoiceitem.startbeforeend = End must be later than start.\r\n" + |
||||||
" empty!\r\n" + |
"message.user.create.success = Thanks {0} created!\r\n" + |
||||||
"message.invoiceitem.startbeforeend = End must be later than start.\r\n" + |
"message.user.loginfailure = Login Error! Wrong user or password?\r\n" + |
||||||
"message.user.create.success = Thanks {0} created!\r\n" + |
"message.user.passwordmissmatch = Passwords don't match.\r\n" + |
||||||
"message.user.loginfailure = Login Error! Wrong user or password?\r\n" + |
""); |
||||||
"message.user.passwordmissmatch = Passwords don't match.\r\n" + |
} |
||||||
""); |
} |
||||||
} |
|
||||||
} |
|
||||||
|
|||||||
Loading…
Reference in new issue