From 055ab1a536b48fd8ed65000c2421eadbb9b4a3d5 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Sat, 30 Mar 2019 00:00:23 +0100 Subject: [PATCH] Refactoring Template Factory. --- .../de/kreth/property2java/Configuration.java | 6 ++ .../de/kreth/property2java/Generator.java | 3 +- .../property2java/GeneratorException.java | 8 --- .../config/FreemarkerConfig.java | 29 ++-------- .../config/FreemarkerConfigImpl.java | 24 ++++++++ .../kreth/property2java/GeneratorTests.java | 23 ++++++++ .../cli/ArgumentConfigurationTest.java | 55 +++++++++++++++++++ 7 files changed, 114 insertions(+), 34 deletions(-) create mode 100644 src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java create mode 100644 src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java diff --git a/src/main/java/de/kreth/property2java/Configuration.java b/src/main/java/de/kreth/property2java/Configuration.java index 2d4e2cf..500f14d 100644 --- a/src/main/java/de/kreth/property2java/Configuration.java +++ b/src/main/java/de/kreth/property2java/Configuration.java @@ -10,7 +10,9 @@ import java.util.Map; import org.apache.commons.text.WordUtils; +import de.kreth.property2java.config.FreemarkerConfigImpl; import de.kreth.property2java.config.Regex; +import freemarker.template.Template; public interface Configuration { @@ -43,4 +45,8 @@ public interface Configuration { return path; } + default Template getTemplate() throws IOException { + return FreemarkerConfigImpl.INSTANCE.getTemplate(); + } + } diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 508d173..ad9609c 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -13,7 +13,6 @@ import java.util.Map; import java.util.Properties; import de.kreth.property2java.cli.ArgumentConfiguration; -import de.kreth.property2java.config.FreemarkerConfig; import freemarker.template.Template; import freemarker.template.TemplateException; @@ -28,7 +27,7 @@ public class Generator { public Generator(Configuration config) { this.config = config; try { - template = FreemarkerConfig.INSTANCE.getTemplate(); + template = config.getTemplate(); } catch (IOException e) { throw new IllegalStateException("Unable to load freemarker template", e); diff --git a/src/main/java/de/kreth/property2java/GeneratorException.java b/src/main/java/de/kreth/property2java/GeneratorException.java index 3083d4e..0b83402 100644 --- a/src/main/java/de/kreth/property2java/GeneratorException.java +++ b/src/main/java/de/kreth/property2java/GeneratorException.java @@ -8,12 +8,4 @@ public class GeneratorException extends Exception { super(message, cause); } - public GeneratorException(String message) { - super(message); - } - - public GeneratorException(Throwable cause) { - super(cause); - } - } diff --git a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java index 4a66f9f..7e0fe10 100644 --- a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java +++ b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java @@ -1,24 +1,5 @@ -package de.kreth.property2java.config; - -import java.io.IOException; - -import freemarker.template.Configuration; -import freemarker.template.Template; - -public enum FreemarkerConfig { - - INSTANCE; - - private final Configuration cfg; - - public Template getTemplate() throws IOException { - return cfg.getTemplate("enum_template.tpl"); - } - - private FreemarkerConfig() { - cfg = new Configuration(Configuration.VERSION_2_3_28); - cfg.setClassForTemplateLoading(this.getClass(), "/template/"); - cfg.setDefaultEncoding("UTF-8"); - } - -} +package de.kreth.property2java.config; + +public interface FreemarkerConfig { + +} diff --git a/src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java b/src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java new file mode 100644 index 0000000..33ac8a1 --- /dev/null +++ b/src/main/java/de/kreth/property2java/config/FreemarkerConfigImpl.java @@ -0,0 +1,24 @@ +package de.kreth.property2java.config; + +import java.io.IOException; + +import freemarker.template.Configuration; +import freemarker.template.Template; + +public enum FreemarkerConfigImpl implements FreemarkerConfig { + + INSTANCE; + + private final Configuration cfg; + + public Template getTemplate() throws IOException { + return cfg.getTemplate("enum_template.tpl"); + } + + private FreemarkerConfigImpl() { + cfg = new Configuration(Configuration.VERSION_2_3_28); + cfg.setClassForTemplateLoading(this.getClass(), "/template/"); + cfg.setDefaultEncoding("UTF-8"); + } + +} diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index 57e9cb4..0ccc882 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -3,8 +3,11 @@ package de.kreth.property2java; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -27,6 +30,9 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import freemarker.template.Template; +import freemarker.template.TemplateException; + class GeneratorTests { private String path = "application.properties"; @@ -43,6 +49,7 @@ class GeneratorTests { config = mock(Configuration.class); when(config.getInput()).thenReturn(input); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); + when(config.getTemplate()).thenCallRealMethod(); generator = new Generator(config); } @@ -94,6 +101,22 @@ class GeneratorTests { } + @Test() + void testTemplateException() throws IOException, GeneratorException, TemplateException { + + Writer out = mock(Writer.class); + when(config.outWriter(anyString())).thenReturn(out); + + Template template = mock(Template.class); + when(config.getTemplate()).thenReturn(template); + doThrow(TemplateException.class).when(template).process(any(), any()); + + generator = new Generator(config); + + assertThrows(GeneratorException.class, () -> generator.start()); + + } + @Test void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { diff --git a/src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java b/src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java new file mode 100644 index 0000000..d0e9784 --- /dev/null +++ b/src/test/java/de/kreth/property2java/cli/ArgumentConfigurationTest.java @@ -0,0 +1,55 @@ +package de.kreth.property2java.cli; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.File; +import java.io.IOException; +import java.io.Reader; +import java.util.Map; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import de.kreth.property2java.Configuration; + +class ArgumentConfigurationTest { + + private static File target; + + private static File input; + + private static File input2; + + @BeforeAll + static void createTargetDir() throws IOException { + target = new File("testTargetDir"); + target.mkdir(); + input = new File(target, "application.properties"); + input.createNewFile(); + input2 = new File(target, "application2.properties"); + input2.createNewFile(); + } + + @AfterAll + static void deleteTestfiles() { + target.delete(); + input.delete(); + input2.delete(); + } + + @Test + void testArgumentParser() throws IOException { + + String[] args = { "-t", target.getAbsolutePath(), + "-f", + input.getAbsolutePath() + "," + input2.getAbsolutePath(), + "-p", "de.kreth.clubinvoice" }; + Configuration config = ArgumentConfiguration.parse(args); + assertEquals("de.kreth.clubinvoice", config.getPackage()); + assertEquals(target.getAbsolutePath(), config.getRootPath().toFile().getAbsolutePath()); + Map inputMap = config.getInput(); + assertEquals(2, inputMap.size()); + } + +}