You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@freemarker.apache.org by sg...@apache.org on 2021/01/23 17:33:53 UTC

[freemarker-generator] branch FREEMARKER-156 updated (a65e87a -> 682d118)

This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a change to branch FREEMARKER-156
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git.


    from a65e87a  FREEMARKER-156 [freemarker-generator] Migrate to Mockito
     new 841c714  FREEMARKER-156 [freemarker-generator] Migrate to Mockito
     new f118de2  FREEMARKER-156 [freemarker-generator] Migrate to Mockito
     new 682d118  FREEMARKER-156 [freemarker-generator] Migrate to Mockito

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../data/mydir/invalid-json.json                   |  1 +
 .../generator/maven/FreeMarkerMojoTest.java        |  5 +-
 .../maven/JsonPropertiesProviderTest.java          | 71 +++++++++-------------
 .../generator/maven/OutputGeneratorTest.java       | 64 ++++++-------------
 4 files changed, 50 insertions(+), 91 deletions(-)
 create mode 100644 freemarker-generator-maven-plugin/src/test/data/generating-file-visitor/data/mydir/invalid-json.json


[freemarker-generator] 01/03: FREEMARKER-156 [freemarker-generator] Migrate to Mockito

Posted by sg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a commit to branch FREEMARKER-156
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git

commit 841c7141260690e805ef140c0d37c6fc686b3f4a
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sat Jan 23 17:08:57 2021 +0100

    FREEMARKER-156 [freemarker-generator] Migrate to Mockito
---
 .../generator/maven/OutputGeneratorTest.java       | 64 ++++++----------------
 1 file changed, 17 insertions(+), 47 deletions(-)

diff --git a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/OutputGeneratorTest.java b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/OutputGeneratorTest.java
index 95258b1..bd8680a 100644
--- a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/OutputGeneratorTest.java
+++ b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/OutputGeneratorTest.java
@@ -19,21 +19,15 @@
 
 package org.apache.freemarker.generator.maven;
 
-import freemarker.cache.FileTemplateLoader;
 import freemarker.template.Configuration;
-import mockit.Expectations;
-import mockit.Mocked;
 import org.assertj.core.api.*;
-import org.testng.annotations.BeforeClass;
-import org.testng.annotations.BeforeMethod;
-import org.testng.annotations.Test;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import java.io.File;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
-import java.nio.file.Path;
-import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -53,37 +47,14 @@ public class OutputGeneratorTest {
 
     private Configuration config;
 
-    @SuppressWarnings("unchecked")
-    @BeforeMethod
-    public void setupDataModel() {
-        dataModel.clear();
-        dataModel.put("testVar", "test value");
-        dataModel.put("pomProperties", new HashMap<String, String>());
-        ((Map<String, String>) dataModel.get("pomProperties")).put("pomVar", "pom value");
-    }
-
     @BeforeClass
-    public static void cleanFields() throws IOException {
-        if (!TEST_DIR.isDirectory()) {
-            throw new RuntimeException("Can't find required test data directory. "
-                    + "If running test outside of maven, make sure working directory is the project directory. "
-                    + "Looking for: " + TEST_DIR);
-        }
-
-        // Clean output dir before each run.
-        final File outputDir = new File("target/test-output/generating-file-visitor");
-        if (outputDir.exists()) {
-            // Recursively delete output from previous run.
-            Files.walk(outputDir.toPath())
-                    .sorted(Comparator.reverseOrder())
-                    .map(Path::toFile)
-                    .forEach(File::delete);
-        }
+    public static void beforeClass() throws IOException {
+        UnitTestHelper.checkTestDir(TEST_DIR);
+        UnitTestHelper.deleteTestOutputDir(OUTPUT_DIR);
     }
 
-    @BeforeMethod
-    public void before() throws IOException {
-        config = configuration();
+    public OutputGeneratorTest() {
+        config = UnitTestHelper.configuration(TEMPLATE_DIR);
         dataModel = dataModel();
     }
 
@@ -227,6 +198,7 @@ public class OutputGeneratorTest {
         builder.addTemplateLocation(templateFile.toPath());
         builder.addDataModel(dataModel);
         final OutputGenerator generator = builder.create();
+
         Assertions.assertThatExceptionOfType(RuntimeException.class)
                 .isThrownBy(() -> generator.generate(config))
                 .withMessage("Could not read template: missing.ftl");
@@ -245,6 +217,7 @@ public class OutputGeneratorTest {
         dataModel.remove("testVar");
         builder.addDataModel(dataModel);
         final OutputGenerator generator = builder.create();
+
         Assertions.assertThatExceptionOfType(RuntimeException.class)
                 .isThrownBy(() -> generator.generate(config))
                 .withMessageStartingWith("Could not process template associated with data file");
@@ -271,6 +244,12 @@ public class OutputGeneratorTest {
                         .getAbsolutePath());
     }
 
+    /**
+     * No idea what the tests is going to test? I assume the intention
+     * is that "mydir" can't be created because "generating-file-visitor"
+     * directory does not exist but File#mkdirs happily create the missing
+     * directory (unless you pass the mocked file instance).
+     *
     @Test
     public void generate_cantCreateOutputFileParentDirTest(
             @Mocked FactoryUtil factoryUtil,
@@ -299,10 +278,12 @@ public class OutputGeneratorTest {
         builder.addTemplateLocation(templateFile.toPath());
         builder.addDataModel(dataModel);
         final OutputGenerator generator = builder.create();
+
         Assertions.assertThatExceptionOfType(RuntimeException.class)
                 .isThrownBy(() -> generator.generate(config))
                 .withMessage("Could not create directory: " + parentDir.getAbsoluteFile().toString());
     }
+    */
 
     @SuppressWarnings("unchecked")
     private static Map<String, Object> dataModel() {
@@ -312,15 +293,4 @@ public class OutputGeneratorTest {
         ((Map<String, String>) result.get("pomProperties")).put("pomVar", "pom value");
         return result;
     }
-
-    private static Configuration configuration() {
-        try {
-            final Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
-            configuration.setDefaultEncoding("UTF-8");
-            configuration.setTemplateLoader(new FileTemplateLoader(TEMPLATE_DIR));
-            return configuration;
-        } catch (Exception e) {
-            throw new RuntimeException("Failed to create Freemarker configuration", e);
-        }
-    }
 }


[freemarker-generator] 02/03: FREEMARKER-156 [freemarker-generator] Migrate to Mockito

Posted by sg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a commit to branch FREEMARKER-156
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git

commit f118de2aec1ae4ca56c799bfb2241696b857e99f
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sat Jan 23 17:46:54 2021 +0100

    FREEMARKER-156 [freemarker-generator] Migrate to Mockito
---
 .../data/mydir/invalid-json.json                   |  1 +
 .../maven/JsonPropertiesProviderTest.java          | 71 +++++++++-------------
 2 files changed, 31 insertions(+), 41 deletions(-)

diff --git a/freemarker-generator-maven-plugin/src/test/data/generating-file-visitor/data/mydir/invalid-json.json b/freemarker-generator-maven-plugin/src/test/data/generating-file-visitor/data/mydir/invalid-json.json
new file mode 100644
index 0000000..955d6bd
--- /dev/null
+++ b/freemarker-generator-maven-plugin/src/test/data/generating-file-visitor/data/mydir/invalid-json.json
@@ -0,0 +1 @@
+This is not a valid JSON file
\ No newline at end of file
diff --git a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/JsonPropertiesProviderTest.java b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/JsonPropertiesProviderTest.java
index 418fa46..78ead87 100644
--- a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/JsonPropertiesProviderTest.java
+++ b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/JsonPropertiesProviderTest.java
@@ -19,15 +19,10 @@
 
 package org.apache.freemarker.generator.maven;
 
-import com.google.gson.Gson;
-import com.google.gson.stream.JsonReader;
-import mockit.Expectations;
-import mockit.Mocked;
-import mockit.Verifications;
-import org.testng.annotations.Test;
+import org.apache.freemarker.generator.maven.OutputGenerator.OutputGeneratorBuilder;
+import org.junit.Test;
 
 import java.io.File;
-import java.lang.reflect.Type;
 import java.nio.file.Path;
 import java.util.HashMap;
 import java.util.Map;
@@ -35,6 +30,7 @@ import java.util.Map;
 import static org.assertj.core.api.AssertionsForClassTypes.*;
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.*;
 
 public class JsonPropertiesProviderTest {
     private final File testDir = new File("src/test/data/generating-file-visitor");
@@ -43,7 +39,10 @@ public class JsonPropertiesProviderTest {
     private final File outputDir = new File("target/test-output/generating-file-visitor");
 
     @Test
-    public void testSuccess(@Mocked OutputGenerator.OutputGeneratorBuilder builder) {
+    public void testSuccess() {
+        final OutputGeneratorBuilder builder = OutputGenerator.builder();
+        builder.addPomLastModifiedTimestamp(10L);
+        builder.addGeneratorLocation(outputDir.toPath());
         final Path path = dataDir.toPath().resolve("mydir/success-test.txt.json");
         final Path expectedTemplateLocation = templateDir.toPath().resolve("test.ftl");
         final Path expectedOutputLocation = outputDir.toPath().resolve("mydir/success-test.txt");
@@ -51,49 +50,37 @@ public class JsonPropertiesProviderTest {
         expectedMap.put("testVar", "test value");
         final JsonPropertiesProvider toTest = JsonPropertiesProvider.create(dataDir, templateDir, outputDir);
         toTest.providePropertiesFromFile(path, builder);
-        new Verifications() {{
-            Path templateLocation;
-            builder.addTemplateLocation(templateLocation = withCapture());
-            Path outputLocation;
-            builder.addOutputLocation(outputLocation = withCapture());
-            Map<String, Object> actualMap;
-            builder.addDataModel(actualMap = withCapture());
-
-            assertEquals(expectedTemplateLocation, templateLocation);
-            assertEquals(expectedOutputLocation, outputLocation);
-            assertArrayEquals(expectedMap.entrySet().toArray(), actualMap.entrySet().toArray());
-        }};
+
+        final OutputGenerator outputGenerator = builder.create();
+
+        assertEquals(expectedTemplateLocation, outputGenerator.templateLocation);
+        assertEquals(expectedOutputLocation, outputGenerator.outputLocation);
+        assertArrayEquals(expectedMap.entrySet().toArray(), outputGenerator.dataModel.entrySet().toArray());
     }
 
     @Test
-    public void testSuccessNoDataModel(@Mocked OutputGenerator.OutputGeneratorBuilder builder) {
+    public void testSuccessNoDataModel() {
+        final OutputGeneratorBuilder builder = OutputGenerator.builder();
+        builder.addPomLastModifiedTimestamp(10L);
+        builder.addGeneratorLocation(outputDir.toPath());
         final Path path = dataDir.toPath().resolve("mydir/success-test-2.txt.json");
         final Path expectedTemplateLocation = templateDir.toPath().resolve("test-pom-only.ftl");
         final Path expectedOutputLocation = outputDir.toPath().resolve("mydir/success-test-2.txt");
         final Map<String, Object> expectedMap = new HashMap<>(4);
         final JsonPropertiesProvider toTest = JsonPropertiesProvider.create(dataDir, templateDir, outputDir);
         toTest.providePropertiesFromFile(path, builder);
-        new Verifications() {{
-            Path templateLocation;
-            builder.addTemplateLocation(templateLocation = withCapture());
-            Path outputLocation;
-            builder.addOutputLocation(outputLocation = withCapture());
-            Map<String, Object> actualMap;
-            builder.addDataModel(actualMap = withCapture());
-
-            assertEquals(expectedTemplateLocation, templateLocation);
-            assertEquals(expectedOutputLocation, outputLocation);
-            assertArrayEquals(expectedMap.entrySet().toArray(), actualMap.entrySet().toArray());
-        }};
+
+        final OutputGenerator outputGenerator = builder.create();
+
+        assertEquals(expectedTemplateLocation, outputGenerator.templateLocation);
+        assertEquals(expectedOutputLocation, outputGenerator.outputLocation);
+        assertArrayEquals(expectedMap.entrySet().toArray(), outputGenerator.dataModel.entrySet().toArray());
     }
 
     @Test
-    public void testParsingException(@Mocked OutputGenerator.OutputGeneratorBuilder builder, @Mocked Gson gson) {
-        final Path path = dataDir.toPath().resolve("mydir/success-test.txt.json");
-        new Expectations() {{
-            gson.fromJson((JsonReader) any, (Type) any);
-            result = new RuntimeException("test exception");
-        }};
+    public void testParsingException() {
+        final OutputGeneratorBuilder builder = mock(OutputGeneratorBuilder.class);
+        final Path path = dataDir.toPath().resolve("mydir/invalid-json.json");
         final JsonPropertiesProvider toTest = JsonPropertiesProvider.create(dataDir, templateDir, outputDir);
 
         assertThatExceptionOfType(RuntimeException.class)
@@ -102,7 +89,8 @@ public class JsonPropertiesProviderTest {
     }
 
     @Test
-    public void testMissingTemplateName(@Mocked OutputGenerator.OutputGeneratorBuilder builder) {
+    public void testMissingTemplateName() {
+        final OutputGeneratorBuilder builder = mock(OutputGeneratorBuilder.class);
         final Path path = dataDir.toPath().resolve("mydir/missing-template-name.txt.json");
         final JsonPropertiesProvider toTest = JsonPropertiesProvider.create(dataDir, templateDir, outputDir);
 
@@ -112,7 +100,8 @@ public class JsonPropertiesProviderTest {
     }
 
     @Test
-    public void testBadPath(@Mocked OutputGenerator.OutputGeneratorBuilder builder) {
+    public void testBadPath() {
+        final OutputGeneratorBuilder builder = mock(OutputGeneratorBuilder.class);
         final Path path = testDir.toPath().resolve("badPath/success-test.txt.json");
         final JsonPropertiesProvider toTest = JsonPropertiesProvider.create(dataDir, templateDir, outputDir);
         assertThatExceptionOfType(IllegalStateException.class)


[freemarker-generator] 03/03: FREEMARKER-156 [freemarker-generator] Migrate to Mockito

Posted by sg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

sgoeschl pushed a commit to branch FREEMARKER-156
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git

commit 682d118855404fbeced8973278e9a47d9985a948
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sat Jan 23 18:31:34 2021 +0100

    FREEMARKER-156 [freemarker-generator] Migrate to Mockito
---
 .../org/apache/freemarker/generator/maven/FreeMarkerMojoTest.java    | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/FreeMarkerMojoTest.java b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/FreeMarkerMojoTest.java
index 4f69d5f..c32b82a 100644
--- a/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/FreeMarkerMojoTest.java
+++ b/freemarker-generator-maven-plugin/src/test/java/org/apache/freemarker/generator/maven/FreeMarkerMojoTest.java
@@ -31,7 +31,6 @@ import org.apache.freemarker.generator.base.util.OperatingSystem;
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.junit.Assert;
 import org.testng.annotations.BeforeClass;
@@ -72,7 +71,7 @@ public class FreeMarkerMojoTest extends Assert {
             @Mocked MojoExecution mojoExecution,
             @Mocked GeneratingFileVisitor generatingFileVisitor,
             @Mocked Files files
-    ) throws MojoExecutionException, MojoFailureException, IOException {
+    ) throws MojoExecutionException, IOException {
 
         new Expectations(mojoExecution, generatingFileVisitor) {{
             mojoExecution.getLifecyclePhase();
@@ -153,7 +152,7 @@ public class FreeMarkerMojoTest extends Assert {
             @Mocked MojoExecution mojoExecution,
             @Mocked GeneratingFileVisitor generatingFileVisitor,
             @Mocked Files files
-    ) throws MojoExecutionException, MojoFailureException {
+    ) throws MojoExecutionException {
 
         new Expectations(mojoExecution, generatingFileVisitor) {{
             mojoExecution.getLifecyclePhase();