Test fixes for freemarker template

REL-BRANCH-PropertyToJavaGenerator-0.0.1
Markus Kreth 7 years ago
parent ef5cdbfe4e
commit 9bf094d376
  1. 2
      src/main/java/de/kreth/property2java/Configuration.java
  2. 11
      src/main/java/de/kreth/property2java/FreemarkerConfig.java
  3. 19
      src/main/java/de/kreth/property2java/Generator.java
  4. 19
      src/main/java/de/kreth/property2java/GeneratorException.java
  5. 7
      src/test/java/de/kreth/property2java/ConfigurationTest.java
  6. 20
      src/test/java/de/kreth/property2java/GeneratorTests.java

@ -34,7 +34,7 @@ public interface Configuration {
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) {

@ -1,8 +1,6 @@
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.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
@ -19,14 +17,7 @@ public enum FreemarkerConfig {
private FreemarkerConfig() { private FreemarkerConfig() {
cfg = new Configuration(Configuration.VERSION_2_3_28); cfg = new Configuration(Configuration.VERSION_2_3_28);
URL url = getClass().getResource("/template/enum_template.tpl"); cfg.setClassForTemplateLoading(this.getClass(), "/template/");
try {
cfg.setDirectoryForTemplateLoading(new File(url.getFile()).getParentFile());
}
catch (IOException e) {
throw new IllegalStateException("Unable to configure freemarker", e);
}
cfg.setDefaultEncoding("UTF-8"); cfg.setDefaultEncoding("UTF-8");
} }

@ -1,5 +1,6 @@
package de.kreth.property2java; package de.kreth.property2java;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
@ -34,15 +35,22 @@ public class Generator {
} }
} }
public void start() throws IOException, TemplateException { public void start() throws IOException, GeneratorException {
for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) { for (Map.Entry<String, Reader> entry : config.getInput().entrySet()) {
String fileName = entry.getKey(); String fileName = entry.getKey();
Writer out = config.outWriter(fileName); try (Writer out = config.outWriter(new File(fileName).getName())) {
Properties properties = new Properties(); Properties properties = new Properties();
properties.load(entry.getValue()); properties.load(entry.getValue());
try {
generate(properties, out, fileName, config); generate(properties, out, fileName, config);
} }
catch (TemplateException e) {
throw new GeneratorException("Error configuring Engine", e);
}
}
}
} }
void generate(Properties properties, Writer out, String fileName, Configuration config) void generate(Properties properties, Writer out, String fileName, Configuration config)
@ -72,7 +80,7 @@ public class Generator {
template.process(root, out); template.process(root, out);
} }
public static void main(String[] args) throws IOException, TemplateException { public static void main(String[] args) throws IOException, GeneratorException {
Generator generator = new Generator(ArgumentConfiguration.parse(args)); Generator generator = new Generator(ArgumentConfiguration.parse(args));
generator.start(); generator.start();
} }
@ -104,5 +112,10 @@ public class Generator {
return value; 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);
}
}

@ -5,8 +5,10 @@ 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.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Writer;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -18,7 +20,6 @@ class ConfigurationTest {
@BeforeEach @BeforeEach
void initConfig() { void initConfig() {
config = Mockito.mock(Configuration.class); config = Mockito.mock(Configuration.class);
} }
@ -26,9 +27,11 @@ class ConfigurationTest {
void defaultWriterIsFileWriter() throws IOException { void defaultWriterIsFileWriter() throws IOException {
when(config.outWriter(anyString())).thenCallRealMethod(); when(config.outWriter(anyString())).thenCallRealMethod();
when(config.getRootPath()).thenReturn(new File(".").toPath());
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
assertTrue(config.outWriter("application.properties") instanceof FileWriter); Writer outWriter = config.outWriter("application.properties");
assertTrue(outWriter instanceof FileWriter);
} }
@Test @Test

@ -27,8 +27,6 @@ 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";
@ -50,7 +48,7 @@ class GeneratorTests {
} }
@Test @Test
void testClassDefinition() throws IOException, TemplateException { void testClassDefinition() throws IOException, GeneratorException {
when(config.getPackage()).thenReturn("de.kreth.property2java"); when(config.getPackage()).thenReturn("de.kreth.property2java");
when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod();
@ -97,7 +95,7 @@ class GeneratorTests {
} }
@Test @Test
void testOneInputGeneratesOneOutput() throws IOException, TemplateException { void testOneInputGeneratesOneOutput() throws IOException, GeneratorException {
Writer out = mock(Writer.class); Writer out = mock(Writer.class);
Writer nonOut = mock(Writer.class); Writer nonOut = mock(Writer.class);
@ -109,13 +107,13 @@ class GeneratorTests {
} }
@Test @Test
void testKeys() throws IOException, TemplateException { void testKeys() throws IOException, GeneratorException {
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
when(config.outWriter(anyString())).thenReturn(out); when(config.outWriter(anyString())).thenReturn(out);
generator.start(); generator.start();
List<String> lines = out.toString().lines().filter(line -> line.contains(" String ")) List<String> lines = out.toString().lines().filter(line -> line.contains(" (\""))
.collect(Collectors.toList()); .collect(Collectors.toList());
assertEquals(21, lines.size()); assertEquals(21, lines.size());
@ -123,7 +121,8 @@ class GeneratorTests {
assertLineMatch(lines, "label_addarticle", "label.addarticle"); assertLineMatch(lines, "label_addarticle", "label.addarticle");
assertLineMatch(lines, "label_user_register", "label.user.register"); assertLineMatch(lines, "label_user_register", "label.user.register");
assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror"); assertLineMatch(lines, "message_article_priceerror", "message.article.priceerror");
assertLineMatch(lines, "message_invoiceitem_startbeforeend", "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");
} }
@ -133,18 +132,17 @@ class GeneratorTests {
.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() - 2); value = value.substring(0, value.length() - 3);
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) { private boolean keyMatches(String line, String key) {
line = line.toLowerCase(); line = line.toLowerCase();
key = key.toLowerCase(); key = key.toLowerCase();
return line.contains(" " + key + " "); return line.contains("\t" + key + " ");
} }
private StringReader testProperties() { private StringReader testProperties() {

Loading…
Cancel
Save