Tests enhanced

pull/11/head
Markus Kreth 10 months ago
parent effd3017bc
commit dceda9dd3c
  1. 11
      src/main/java/de/kreth/property2java/Generator.java
  2. 14
      src/test/java/de/kreth/property2java/ConfigurationTest.java
  3. 74
      src/test/java/de/kreth/property2java/GeneratorTests.java
  4. 22
      src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java
  5. 27
      src/test/java/de/kreth/property2java/TestImplConfig.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);
}

@ -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 {

@ -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<String, Reader> 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);
@SuppressWarnings("unchecked")
ArgumentCaptor<Map<String, Object>> rootCaptior = ArgumentCaptor.forClass(Map.class);
generator.start();
verify(template).process(rootCaptior.capture(), any(Writer.class));
Map<String, Object> root = rootCaptior.getValue();
@SuppressWarnings("unchecked")
EnumSet<GeneratorOptions> options = (EnumSet<GeneratorOptions>) root.get("options");
assertThat(options).contains(GeneratorOptions.WithMessageFormatter, GeneratorOptions.WithSubstitutors);
@SuppressWarnings("unchecked")
List<String> imports = (List<String>) 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<Map<String, Object>> rootCaptior = ArgumentCaptor.forClass(Map.class);
generator.start();
verify(template).process(rootCaptior.capture(), any(Writer.class));
Map<String, Object> root = rootCaptior.getValue();
@SuppressWarnings("unchecked")
EnumSet<GeneratorOptions> options = (EnumSet<GeneratorOptions>) root.get("options");
assertThat(options).contains(GeneratorOptions.WithSubstitutors);
generator = new Generator(config);
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();

@ -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<String, Reader> 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);
}

@ -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<String, Reader> getInput() {
// TODO Auto-generated method stub
return null;
}
@Override
public Path getRootPath() {
// TODO Auto-generated method stub
return null;
}
}
Loading…
Cancel
Save