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:54 UTC
[freemarker-generator] 01/03: FREEMARKER-156 [freemarker-generator]
Migrate to Mockito
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);
- }
- }
}