diff --git a/Application_Properties.java b/Application_Properties.java index 8b13789..e69de29 100644 --- a/Application_Properties.java +++ b/Application_Properties.java @@ -1 +0,0 @@ - diff --git a/src/main/java/de/kreth/property2java/Generator.java b/src/main/java/de/kreth/property2java/Generator.java index 218f15f..20484bd 100644 --- a/src/main/java/de/kreth/property2java/Generator.java +++ b/src/main/java/de/kreth/property2java/Generator.java @@ -69,7 +69,6 @@ public class Generator { void generate(Properties properties, Writer out, String fileName, Configuration config) throws IOException, TemplateException { - Map root = new HashMap<>(); root.put("generator_name", getClass().getName()); root.put("generation_date", dateTimeInstance.format(new Date())); diff --git a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java index fe51ab9..92ca472 100644 --- a/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java +++ b/src/main/java/de/kreth/property2java/processor/ProcessorConfiguration.java @@ -91,7 +91,7 @@ public class ProcessorConfiguration implements Configuration { } static final class Builder { - public GeneratorOptions[] options; + private GeneratorOptions[] options; private final Filer filer; private final Element element; private final List resourcenames; @@ -113,7 +113,7 @@ public class ProcessorConfiguration implements Configuration { return this; } - public Builder addAll(String[] resourceNames) { + public Builder addAll(String... resourceNames) { this.resourcenames.addAll(Arrays.asList(resourceNames)); return this; } diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index 2f7987b..421b136 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -27,6 +27,7 @@ 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; @@ -205,6 +206,24 @@ class GeneratorTests { assertThat(ex.getCause()).isInstanceOf(TemplateException.class); } + @Test + void testGenerateProp() throws IOException, TemplateException { + Template template = mock(Template.class); + Properties properties = new Properties(); + StringWriter out = new StringWriter(); + Generator generator = new Generator(config, template); + String fileName = "de.kreth.messages.properties"; + generator.generate(properties, out, fileName , config); + + @SuppressWarnings("unchecked") + ArgumentCaptor> dataModelCaptor = ArgumentCaptor.forClass(Map.class); + verify(template).process(dataModelCaptor.capture(), any(Writer.class)); + assertThat(dataModelCaptor.getValue()) + .containsEntry("bundle_base_name", "de.kreth.messages") + .containsEntry("fileName", fileName) + .containsEntry("classname", "De_Kreth_Messages_Properties"); + } + @Test void testMainMethod() throws IOException, GeneratorException { Path source = Files.createTempFile(getClass().getSimpleName(), ".properties"); @@ -243,4 +262,5 @@ class GeneratorTests { return line.contains("\t" + key + " "); } + } diff --git a/src/test/java/de/kreth/property2java/processor/ProcessorConfigurationTest.java b/src/test/java/de/kreth/property2java/processor/ProcessorConfigurationTest.java new file mode 100644 index 0000000..ea0bc65 --- /dev/null +++ b/src/test/java/de/kreth/property2java/processor/ProcessorConfigurationTest.java @@ -0,0 +1,55 @@ +package de.kreth.property2java.processor; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.StringReader; + +import javax.annotation.processing.Filer; +import javax.lang.model.element.Name; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.tools.FileObject; +import javax.tools.StandardLocation; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import de.kreth.property2java.Format; + +@ExtendWith(MockitoExtension.class) +public class ProcessorConfigurationTest { + + @Mock + private Filer filer; + @Mock + private TypeElement element; + + @Test + void testPackage() throws IOException { + PackageElement packageElement = mock(PackageElement.class); + Name name = mock(Name.class); + String packageName = "de.kreth.pack.name"; + String resourceName = "localization.properties"; + FileObject fileObject = mock(FileObject.class); + StringReader input = new StringReader("key1=value1\n" + + "key2=value2"); + + when(element.getEnclosingElement()).thenReturn(packageElement); + when(packageElement.getQualifiedName()).thenReturn(name); + when(name.toString()).thenReturn(packageName); + when(filer.getResource(StandardLocation.CLASS_PATH, "", resourceName)).thenReturn(fileObject); + when(fileObject.openReader(false)).thenReturn(input); + + ProcessorConfiguration config = new ProcessorConfiguration( + ProcessorConfiguration.builder(filer, element) + .withFormat(Format.WithInnerPropertyLoader) + .addAll(resourceName)); + + assertThat(config.getPackage()).isEqualTo(packageName); + } +}