From dceda9dd3c2f28144eb93400a3e3ac2f2c2fa2c3 Mon Sep 17 00:00:00 2001 From: Markus Kreth Date: Sun, 9 Feb 2025 22:58:08 +0100 Subject: [PATCH] Tests enhanced --- .../de/kreth/property2java/Generator.java | 11 ++- .../property2java/ConfigurationTest.java | 14 ++-- .../kreth/property2java/GeneratorTests.java | 74 +++++++++++++++++-- .../GeneratorWithInnerPropertiesTest.java | 22 +++--- .../kreth/property2java/TestImplConfig.java | 27 ------- 5 files changed, 96 insertions(+), 52 deletions(-) delete mode 100644 src/test/java/de/kreth/property2java/TestImplConfig.java diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 29cc63c..07ac05d 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -31,9 +31,18 @@ public class Generator { private final Template template; public Generator(Configuration config) { + this(config, createTemplate(config)); + } + + public Generator(Configuration config, Template template) { + super(); this.config = config; + this.template = template; + } + + private static Template createTemplate(Configuration config) { try { - template = FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); + return FreemarkerConfig.INSTANCE.getTemplate(config.getFormat()); } catch (IOException e) { throw new IllegalStateException("Unable to load freemarker template", e); } diff --git a/src/test/java/de/kreth/property2java/ConfigurationTest.java b/src/test/java/de/kreth/property2java/ConfigurationTest.java index cdcafe3..4d8f16f 100644 --- a/src/test/java/de/kreth/property2java/ConfigurationTest.java +++ b/src/test/java/de/kreth/property2java/ConfigurationTest.java @@ -10,18 +10,16 @@ import java.io.FileWriter; import java.io.IOException; import java.io.Writer; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +@ExtendWith(MockitoExtension.class) class ConfigurationTest { - private TestImplConfig config; - - @BeforeEach - void initConfig() { - config = Mockito.spy(TestImplConfig.class); - } + @Mock + private Configuration config; @Test void defaultWriterIsFileWriter() throws IOException { diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index f6ee6a0..81b0d6f 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -1,10 +1,13 @@ package de.kreth.property2java; import static de.kreth.property2java.TestPropertiesSource.testProperties; +import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; 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.mock; import static org.mockito.Mockito.never; @@ -17,6 +20,7 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.util.Arrays; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -27,35 +31,89 @@ import java.util.stream.Collectors; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +import freemarker.template.Template; +import freemarker.template.TemplateException; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) class GeneratorTests { private String path = "application.properties"; + @Mock private Configuration config; - private Generator generator; - @BeforeEach void setUp() throws Exception { Map input = new HashMap<>(); input.put(path, testProperties()); - config = Mockito.spy(TestImplConfig.class); - when(config.getRootPath()).thenReturn(new File(".").toPath()); when(config.getFormat()).thenReturn(Format.WithUnaryOperatorParameter); when(config.getInput()).thenReturn(input); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.outputCharset()).thenCallRealMethod(); + when(config.getOptions()).thenReturn(EnumSet.noneOf(GeneratorOptions.class)); + + } + + @Test + void testAllOptionsConfiguration() throws IOException, GeneratorException, TemplateException { + + Template template = mock(Template.class); + Generator generator = new Generator(config, template); + when(config.getOptions()).thenReturn(EnumSet.allOf(GeneratorOptions.class)); + + StringWriter out = new StringWriter(); + when(config.outWriter(anyString())).thenReturn(out); - generator = new Generator(config); + @SuppressWarnings("unchecked") + ArgumentCaptor> rootCaptior = ArgumentCaptor.forClass(Map.class); + generator.start(); + verify(template).process(rootCaptior.capture(), any(Writer.class)); + Map root = rootCaptior.getValue(); + @SuppressWarnings("unchecked") + EnumSet options = (EnumSet) root.get("options"); + assertThat(options).contains(GeneratorOptions.WithMessageFormatter, GeneratorOptions.WithSubstitutors); + + @SuppressWarnings("unchecked") + List imports = (List) root.get("imports"); + assertThat(imports).contains("java.text.MessageFormat"); } + @Test + void testWithSubstitutorsNoImportsConfiguration() throws IOException, GeneratorException, TemplateException { + + Template template = mock(Template.class); + Generator generator = new Generator(config, template); + when(config.getOptions()).thenReturn(EnumSet.of(GeneratorOptions.WithSubstitutors)); + + StringWriter out = new StringWriter(); + when(config.outWriter(anyString())).thenReturn(out); + + @SuppressWarnings("unchecked") + ArgumentCaptor> rootCaptior = ArgumentCaptor.forClass(Map.class); + generator.start(); + verify(template).process(rootCaptior.capture(), any(Writer.class)); + Map root = rootCaptior.getValue(); + @SuppressWarnings("unchecked") + EnumSet options = (EnumSet) root.get("options"); + assertThat(options).contains(GeneratorOptions.WithSubstitutors); + + assertFalse(root.containsKey("imports")); + } + @Test void testClassDefinition() throws IOException, GeneratorException { + Generator generator = new Generator(config); when(config.getPackage()).thenReturn("de.kreth.property2java"); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); @@ -102,6 +160,7 @@ class GeneratorTests { @Test void testOneInputGeneratesOneOutput() throws IOException, GeneratorException { + Generator generator = new Generator(config); Writer out = mock(Writer.class); Writer nonOut = mock(Writer.class); when(config.outWriter(anyString())).thenReturn(out, nonOut); @@ -114,6 +173,7 @@ class GeneratorTests { @Test void testKeys() throws IOException, GeneratorException { + Generator generator = new Generator(config); StringWriter out = new StringWriter(); when(config.outWriter(anyString())).thenReturn(out); generator.start(); diff --git a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java index 658210d..cb321d5 100644 --- a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java +++ b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java @@ -1,5 +1,6 @@ package de.kreth.property2java; +import static de.kreth.property2java.TestPropertiesSource.testProperties; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -16,27 +17,31 @@ import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.util.Arrays; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.Properties; import java.util.StringTokenizer; import java.util.stream.Collectors; -import static de.kreth.property2java.TestPropertiesSource.testProperties; - import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) public class GeneratorWithInnerPropertiesTest { private String path = "application.properties"; + @Mock private Configuration config; - private Generator generator; @BeforeEach @@ -44,15 +49,14 @@ public class GeneratorWithInnerPropertiesTest { Map input = new HashMap<>(); input.put(path, testProperties()); - config = Mockito.spy(TestImplConfig.class); - when(config.getRootPath()).thenReturn(new File(".").toPath()); when(config.getFormat()).thenReturn(Format.WithInnerPropertyLoader); when(config.getInput()).thenReturn(input); when(config.mapFilenameToClassName(anyString())).thenCallRealMethod(); when(config.outputCharset()).thenCallRealMethod(); + when(config.getOptions()).thenReturn(EnumSet.noneOf(GeneratorOptions.class)); - generator = new Generator(config); + this.generator = new Generator(config); } diff --git a/src/test/java/de/kreth/property2java/TestImplConfig.java b/src/test/java/de/kreth/property2java/TestImplConfig.java deleted file mode 100644 index a2bb819..0000000 --- a/src/test/java/de/kreth/property2java/TestImplConfig.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.kreth.property2java; - -import java.io.Reader; -import java.nio.file.Path; -import java.util.Map; - -class TestImplConfig implements Configuration { - - @Override - public String getPackage() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Map getInput() { - // TODO Auto-generated method stub - return null; - } - - @Override - public Path getRootPath() { - // TODO Auto-generated method stub - return null; - } - -} \ No newline at end of file