diff --git a/.gitignore b/.gitignore index f1f6662..cc16de6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .project .factorypath .settings +.idea target diff --git a/Application_Properties.java b/Application_Properties.java index 139597f..e69de29 100644 --- a/Application_Properties.java +++ b/Application_Properties.java @@ -1,2 +0,0 @@ - - diff --git a/src/main/java/de/kreth/property2java/GeneratorException.java b/src/main/java/de/kreth/property2java/GeneratorException.java index 1529503..05a302e 100644 --- a/src/main/java/de/kreth/property2java/GeneratorException.java +++ b/src/main/java/de/kreth/property2java/GeneratorException.java @@ -11,12 +11,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/GeneratorOptions.java b/src/main/java/de/kreth/property2java/GeneratorOptions.java index c990b50..2fe4637 100644 --- a/src/main/java/de/kreth/property2java/GeneratorOptions.java +++ b/src/main/java/de/kreth/property2java/GeneratorOptions.java @@ -14,7 +14,7 @@ public enum GeneratorOptions { private final String[] additionalImport; - private GeneratorOptions(String... additionalImport) { + GeneratorOptions(String... additionalImport) { this.additionalImport = additionalImport!= null ? additionalImport : new String[]{}; } diff --git a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java index ff8877c..ebef6c6 100644 --- a/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java +++ b/src/main/java/de/kreth/property2java/config/FreemarkerConfig.java @@ -30,7 +30,7 @@ public enum FreemarkerConfig { } - private FreemarkerConfig() { + FreemarkerConfig() { cfg = new Configuration(Configuration.VERSION_2_3_28); cfg.setClassForTemplateLoading(this.getClass(), "/template/"); cfg.setDefaultEncoding("UTF-8"); diff --git a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java index 9bc318a..52dda7e 100644 --- a/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java +++ b/src/main/java/de/kreth/property2java/processor/GenerateProperty2Java.java @@ -6,7 +6,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - import de.kreth.property2java.Format; import de.kreth.property2java.GeneratorOptions; diff --git a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java index 48aadd3..492a01e 100644 --- a/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java +++ b/src/main/java/de/kreth/property2java/processor/Property2JavaGenerator.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; import javax.annotation.processing.AbstractProcessor; @@ -15,7 +16,6 @@ import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; import javax.tools.Diagnostic.Kind; - import de.kreth.property2java.Format; import de.kreth.property2java.GeneratorException; import de.kreth.property2java.GeneratorOptions; @@ -64,14 +64,14 @@ public class Property2JavaGenerator extends AbstractProcessor { GenerateResourceBundleProperty2Java[] value = element .getAnnotation(GenerateResourceBundleProperty2Javas.class).value(); for (GenerateResourceBundleProperty2Java generateResourceBundleProperty2Java : value) { - List resources = Arrays.asList(generateResourceBundleProperty2Java.resource()); + List resources = Collections.singletonList(generateResourceBundleProperty2Java.resource()); generateElementProperties(element, resources, generateResourceBundleProperty2Java.format(), generateResourceBundleProperty2Java.options()); } } } private void generateElementProperties(Element element, List resources, Format format, GeneratorOptions[] options) { - processingEnv.getMessager().printMessage(Kind.NOTE, "Generating Java for " + Arrays.asList(resources)); + processingEnv.getMessager().printMessage(Kind.NOTE, "Generating Java for " + Collections.singletonList(resources)); try { ProcessorConfiguration .builder(processingEnv.getFiler(), element) @@ -83,7 +83,7 @@ public class Property2JavaGenerator extends AbstractProcessor { StringWriter out = new StringWriter(); e.printStackTrace(new PrintWriter(out)); out.flush(); - processingEnv.getMessager().printMessage(Kind.ERROR, "Exception " + e + "\n" + out.toString(), element); + processingEnv.getMessager().printMessage(Kind.ERROR, "Exception " + e + "\n" + out, element); } } diff --git a/src/test/java/de/kreth/property2java/ConfigurationTest.java b/src/test/java/de/kreth/property2java/ConfigurationTest.java index bae8019..ece2f85 100644 --- a/src/test/java/de/kreth/property2java/ConfigurationTest.java +++ b/src/test/java/de/kreth/property2java/ConfigurationTest.java @@ -1,7 +1,6 @@ package de.kreth.property2java; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.when; @@ -31,7 +30,7 @@ class ConfigurationTest { when(config.outputCharset()).thenCallRealMethod(); Writer outWriter = config.outWriter("application.properties"); - assertTrue(outWriter instanceof FileWriter); + assertInstanceOf(FileWriter.class, outWriter); } @Test diff --git a/src/test/java/de/kreth/property2java/GeneratorTests.java b/src/test/java/de/kreth/property2java/GeneratorTests.java index c0bfa4b..4cb7388 100644 --- a/src/test/java/de/kreth/property2java/GeneratorTests.java +++ b/src/test/java/de/kreth/property2java/GeneratorTests.java @@ -6,6 +6,7 @@ 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.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -16,6 +17,10 @@ import java.io.IOException; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.EnumSet; import java.util.HashMap; @@ -25,7 +30,7 @@ import java.util.Optional; import java.util.StringTokenizer; import java.util.stream.Collectors; - +import org.apache.commons.cli.MissingOptionException; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -43,7 +48,7 @@ import freemarker.template.TemplateException; @MockitoSettings(strictness = Strictness.LENIENT) class GeneratorTests { - private String path = "application.properties"; + private final String path = "application.properties"; @Mock private Configuration config; @@ -187,6 +192,38 @@ class GeneratorTests { assertLineMatch(lines, "message_invoiceitem_allfieldsmustbeset", "message.invoiceitem.allfieldsmustbeset"); } + @Test + void testTemplateException() throws TemplateException, IOException { + Template template = mock(Template.class); + + when(config.outWriter(anyString())).thenReturn(mock(Writer.class)); + doThrow(new TemplateException(null)).when(template).process(any(Map.class), any(Writer.class)); + + Generator generator = new Generator(config, template); + GeneratorException ex = assertThrows(GeneratorException.class, () -> generator.start()); + assertThat(ex.getCause()).isInstanceOf(TemplateException.class); + } + + @Test + void testMainMethod() throws IOException, GeneratorException { + Path source = Files.createTempFile(getClass().getSimpleName(), ".properties"); + Generator.main(new String[] {"-t", "target", "-f", source.toString()}); + } + + @Test + void testMainMethodMissingOption() throws IOException, GeneratorException { + IllegalStateException e = assertThrows(IllegalStateException.class, () -> Generator.main(new String[] {})); + assertThat(e.getCause()).isInstanceOf(MissingOptionException.class); + } + + @Test + void testGenerateFor() throws IOException, GeneratorException { + Class locationClass = getClass(); + List rescources = new ArrayList<>(); + String relativeTargetDir = "target"; + Generator.generateFor(locationClass, rescources, relativeTargetDir); + } + private void assertLineMatch(List lines, String key, String expected) { Optional found = lines.stream().filter(line -> keyMatches(line, key)).findFirst(); diff --git a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java index 5fc9052..5f5d6ec 100644 --- a/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java +++ b/src/test/java/de/kreth/property2java/GeneratorWithInnerPropertiesTest.java @@ -34,7 +34,7 @@ import org.mockito.quality.Strictness; @MockitoSettings(strictness = Strictness.LENIENT) public class GeneratorWithInnerPropertiesTest { - private String path = "application.properties"; + private final String path = "application.properties"; @Mock private Configuration config; diff --git a/src/test/java/de/kreth/property2java/generated/Property_Loader_Format_Properties.java b/src/test/java/de/kreth/property2java/generated/Property_Loader_Format_Properties.java index 7b72cac..59934fc 100644 --- a/src/test/java/de/kreth/property2java/generated/Property_Loader_Format_Properties.java +++ b/src/test/java/de/kreth/property2java/generated/Property_Loader_Format_Properties.java @@ -5,7 +5,6 @@ import java.util.ResourceBundle; import javax.annotation.processing.Generated; import java.text.MessageFormat; -; /** * Property keys from property_loader_format.properties diff --git a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java index 9f8f3b3..75f53c6 100644 --- a/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java +++ b/src/test/java/de/kreth/property2java/parts/ReplaceLogicForTemplate.java @@ -5,7 +5,7 @@ public final class ReplaceLogicForTemplate { static String doReplacements(String property, Object...objects) { StringBuilder text = new StringBuilder(); int index = property.indexOf('{'); - text.append(property.substring(0, index)); + text.append(property, 0, index); while (index >= 0) { index++; @@ -22,7 +22,7 @@ public final class ReplaceLogicForTemplate { if (index <0) { text.append(property.substring(endIndex + 1)); } else { - text.append(property.substring(endIndex + 1, index)); + text.append(property, endIndex + 1, index); } } else { endIndex = index; diff --git a/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java b/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java index 59f5676..d0ea0be 100644 --- a/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java +++ b/src/test/java/de/kreth/property2java/processor/Property2JavaGeneratorTest.java @@ -1,12 +1,16 @@ package de.kreth.property2java.processor; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.lang.annotation.Annotation; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.annotation.processing.Messager; import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; +import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; @@ -38,10 +42,40 @@ public class Property2JavaGeneratorTest { when(processingEnv.getMessager()).thenReturn(messanger); } - @SuppressWarnings("unchecked") + @SuppressWarnings({ "unchecked", "rawtypes" }) @Test void testGeneratorInitializedCorrectly() { + + GenerateProperty2Java an = mock(GenerateProperty2Java.class); + when(an.resources()).thenReturn(new String[] {}); + TypeElement element = mock(TypeElement.class); + when(element.getAnnotation(GenerateProperty2Java.class)).thenReturn(an); + + annotations.add(element); + when(roundEnv.getElementsAnnotatedWith(ArgumentMatchers.any(Class.class))).thenReturn(annotations); + Element annotatedElement = mock(Element.class); + GenerateResourceBundleProperty2Javas value = new GenerateResourceBundleProperty2Javas() { + + @Override + public Class annotationType() { + return null; + } + + @Override + public GenerateResourceBundleProperty2Java[] value() { + return new GenerateResourceBundleProperty2Java[0]; + } + }; + when(annotatedElement.getAnnotation(GenerateResourceBundleProperty2Javas.class)).thenReturn(value ); + Set elements = new HashSet<>(List.of(annotatedElement)); + + when(roundEnv.getElementsAnnotatedWith(GenerateResourceBundleProperty2Javas.class)) + .thenReturn((Set)elements); + processor.process(annotations, roundEnv); + + } + }