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 2020/05/23 16:22:17 UTC
[freemarker-generator] 01/02: FREEMARKER-142 First draft of
supporting multiple templates on the command line
This is an automated email from the ASF dual-hosted git repository.
sgoeschl pushed a commit to branch FREEMARKER-142
in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit ec6fefb4b90b0418b5c69456e4c768fb8acc702a
Author: Siegfried Goeschl <si...@gmail.com>
AuthorDate: Sat May 23 15:19:10 2020 +0200
FREEMARKER-142 First draft of supporting multiple templates on the command line
---
.../generator/base/template/TemplateOutput.java | 24 +-
.../template/TemplateProcessingInfoSupplier.java | 153 -------------
.../generator/base/template/TemplateSource.java | 6 +-
...essingInfo.java => TemplateTransformation.java} | 7 +-
.../base/template/TemplateTransformations.java | 28 +++
.../template/TemplateTransformationsBuilder.java | 247 +++++++++++++++++++++
.../TemplateProcessingInfoSupplierTest.java | 77 -------
.../TemplateTransformationsBuilderTest.java | 141 ++++++++++++
8 files changed, 434 insertions(+), 249 deletions(-)
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateOutput.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateOutput.java
index 53001f4..e0efc42 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateOutput.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateOutput.java
@@ -1,37 +1,34 @@
package org.apache.freemarker.generator.base.template;
-import org.apache.freemarker.generator.base.util.CloseableReaper;
import org.apache.freemarker.generator.base.util.Validate;
-import java.io.Closeable;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
+import static java.util.Objects.requireNonNull;
+
/**
* Information about where to write the ouput of a template. Initially we
* wanted to use a <code>FileWriter</code> but it requires actually an
* existing output file (otherwise a FileNotFound exception is thrown).
+ *
+ * TODO what to do with created FileWriter?
*/
-public class TemplateOutput implements Closeable {
+public class TemplateOutput {
private final Writer writer;
private final File file;
- /** Collect all closables handed out to the caller to be closed when the instance is closed */
- private final CloseableReaper closables;
-
private TemplateOutput(File file) {
this.writer = null;
- this.file = file;
- this.closables = new CloseableReaper();
+ this.file = requireNonNull(file);
}
private TemplateOutput(Writer writer) {
- this.writer = writer;
+ this.writer = requireNonNull(writer);
this.file = null;
- this.closables = new CloseableReaper();
}
public static TemplateOutput fromWriter(Writer writer) {
@@ -62,16 +59,11 @@ public class TemplateOutput implements Closeable {
return writer != null ? writer : fileWriter();
}
- @Override
- public void close() throws IOException {
- closables.close();
- }
-
private FileWriter fileWriter() {
Validate.notNull(file, "Output file is null");
try {
- return closables.add(new FileWriter(file));
+ return new FileWriter(file);
} catch (IOException e) {
throw new RuntimeException("Failed to create FileWriter: " + file.getAbsolutePath(), e);
}
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfoSupplier.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfoSupplier.java
deleted file mode 100644
index 4c6a42e..0000000
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfoSupplier.java
+++ /dev/null
@@ -1,153 +0,0 @@
-package org.apache.freemarker.generator.base.template;
-
-import org.apache.freemarker.generator.base.file.RecursiveFileSupplier;
-import org.apache.freemarker.generator.base.util.Validate;
-
-import java.io.File;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.function.Supplier;
-
-import static java.util.Collections.singletonList;
-import static java.util.stream.Collectors.toList;
-
-public class TemplateProcessingInfoSupplier implements Supplier<List<TemplateProcessingInfo>> {
-
- /** List of templates and/or template directories to be rendered */
- private final Collection<String> sources;
-
- /** Optional include pattern for resolving source templates or directory */
- private final String include;
-
- /** Optional exclude pattern for resolving source templates or directory */
- private final String exclude;
-
- /** Optional output file or directory */
- private final File out;
-
- /** Optional user-supplied writer */
- private final Writer writer;
-
- public TemplateProcessingInfoSupplier(Collection<String> sources, String include, String exclude, File out) {
- this.sources = new ArrayList<>(sources);
- this.include = include;
- this.exclude = exclude;
- this.out = out;
- this.writer = null;
- }
-
- public TemplateProcessingInfoSupplier(Collection<String> sources, String include, String exclude, Writer writer) {
- this.sources = new ArrayList<>(sources);
- this.include = include;
- this.exclude = exclude;
- this.out = null;
- this.writer = writer;
- }
-
- @Override
- public List<TemplateProcessingInfo> get() {
- return sources.stream()
- .map(this::get)
- .flatMap(Collection::stream)
- .collect(toList());
- }
-
- /**
- * Resolve a <code>source</code> to a list of <code>TemplateProcessingInfo</code>.
- *
- * @param source the source being a file name, an URI or <code>NamedUri</code>
- * @return list of <code>TemplateProcessingInfo</code>
- */
- private List<TemplateProcessingInfo> get(String source) {
- if (isTemplateFile(source)) {
- return resolveTemplateFile(source);
- } else if (isTemplateDirectory(source)) {
- return resolveTemplateDirectory(source);
- } else if (isTemplatePath(source)) {
- return resolveTemplatePath(source);
- } else {
- throw new RuntimeException("Don't know how to resolve: " + source);
- }
- }
-
- private List<TemplateProcessingInfo> resolveTemplateFile(String source) {
- final TemplateSource templateSource = templateSource(source);
- final TemplateOutput templateOutput = templateOutput(out);
- return singletonList(new TemplateProcessingInfo(templateSource, templateOutput));
- }
-
- private List<TemplateProcessingInfo> resolveTemplateDirectory(String source) {
- Validate.fileExists(new File(source), "Template directory does not exist: " + source);
-
- final File templateDirectory = new File(source);
- final List<File> templateFiles = templateFilesSupplier(source, include, exclude).get();
- final List<TemplateProcessingInfo> templateProcessingInfos = new ArrayList<>();
-
- for (File templateFile : templateFiles) {
- final TemplateSource templateSource = templateSource(templateFile.getAbsolutePath());
- final File outputFile = getTemplateOutputFile(templateDirectory, templateFile, out);
- final TemplateOutput templateOutput = templateOutput(outputFile);
- templateProcessingInfos.add(new TemplateProcessingInfo(templateSource, templateOutput));
- }
-
- return templateProcessingInfos;
- }
-
- private List<TemplateProcessingInfo> resolveTemplatePath(String source) {
- final TemplateSource templateSource = templateSource(source);
- final TemplateOutput templateOutput = templateOutput(out);
- return singletonList(new TemplateProcessingInfo(templateSource, templateOutput));
- }
-
- private TemplateOutput templateOutput(File templateOutputFile) {
- if (writer != null) {
- return TemplateOutput.fromWriter(writer);
- } else {
- return TemplateOutput.fromFile(templateOutputFile);
- }
- }
-
- private TemplateSource templateSource(String source) {
- return TemplateSourceFactory.create(source);
- }
-
- private static File getTemplateOutputFile(File templateDirectory, File templateFile, File outputDirectory) {
- final String relativePath = relativePath(templateDirectory, templateFile);
- final String relativeOutputFileName = mapExtension(relativePath);
- return new File(outputDirectory, relativeOutputFileName);
- }
-
- private static boolean isTemplateFile(String source) {
- final File file = new File(source);
- return file.exists() && file.isFile();
- }
-
- private static boolean isTemplateDirectory(String source) {
- final File file = new File(source);
- return file.exists() && file.isDirectory();
- }
-
- private static boolean isTemplatePath(String source) {
- return !isTemplateFile(source) && !isTemplateDirectory(source);
- }
-
- private static RecursiveFileSupplier templateFilesSupplier(String source, String include, String exclude) {
- return new RecursiveFileSupplier(singletonList(source), singletonList(include), singletonList(exclude));
- }
-
- private static String relativePath(File directory, File file) {
- return file.getAbsolutePath()
- .substring(directory.getAbsolutePath().length())
- .substring(1);
- }
-
- private static String mapExtension(String fileName) {
- if (fileName.toLowerCase().endsWith(".ftl")) {
- return fileName.substring(0, fileName.length() - 4);
- } else {
- return fileName;
- }
- }
-}
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSource.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSource.java
index f9f7f10..0659d22 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSource.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSource.java
@@ -37,7 +37,7 @@ public class TemplateSource {
this.origin = Origin.CODE;
this.code = code;
this.path = null;
- this.encoding = null;
+ this.encoding = StandardCharsets.UTF_8;
}
private TemplateSource(String name, String path, Charset encoding) {
@@ -81,6 +81,10 @@ public class TemplateSource {
return name;
}
+ public Charset getEncoding() {
+ return encoding;
+ }
+
@Override
public String toString() {
return "TemplateSource{" +
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfo.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformation.java
similarity index 77%
rename from freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfo.java
rename to freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformation.java
index fb94e96..8840e50 100644
--- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateProcessingInfo.java
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformation.java
@@ -2,7 +2,10 @@ package org.apache.freemarker.generator.base.template;
import static java.util.Objects.requireNonNull;
-public class TemplateProcessingInfo {
+/**
+ * Information about loading templates and writing their output.
+ */
+public class TemplateTransformation {
/** Source of template */
private final TemplateSource templateSource;
@@ -10,7 +13,7 @@ public class TemplateProcessingInfo {
/** Output of template */
private final TemplateOutput templateOutput;
- public TemplateProcessingInfo(TemplateSource templateSource, TemplateOutput templateOutput) {
+ public TemplateTransformation(TemplateSource templateSource, TemplateOutput templateOutput) {
this.templateSource = requireNonNull(templateSource);
this.templateOutput = requireNonNull(templateOutput);
}
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformations.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformations.java
new file mode 100644
index 0000000..380e76a
--- /dev/null
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformations.java
@@ -0,0 +1,28 @@
+package org.apache.freemarker.generator.base.template;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static java.util.Objects.requireNonNull;
+
+public class TemplateTransformations {
+
+ private final List<TemplateTransformation> templateTransformations;
+
+ public TemplateTransformations(Collection<? extends TemplateTransformation> templateTransformations) {
+ this.templateTransformations = new ArrayList<>(requireNonNull(templateTransformations));
+ }
+
+ public List<? extends TemplateTransformation> getList() {
+ return templateTransformations;
+ }
+
+ public TemplateTransformation get(int index) {
+ return templateTransformations.get(index);
+ }
+
+ public int size() {
+ return templateTransformations.size();
+ }
+}
diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformationsBuilder.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformationsBuilder.java
new file mode 100644
index 0000000..834e3aa
--- /dev/null
+++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateTransformationsBuilder.java
@@ -0,0 +1,247 @@
+package org.apache.freemarker.generator.base.template;
+
+import org.apache.freemarker.generator.base.file.RecursiveFileSupplier;
+import org.apache.freemarker.generator.base.util.NonClosableWriterWrapper;
+import org.apache.freemarker.generator.base.util.StringUtils;
+import org.apache.freemarker.generator.base.util.Validate;
+
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Collections.singletonList;
+
+public class TemplateTransformationsBuilder {
+
+ /** List of templates and/or template directories to be rendered */
+ private final List<String> sources;
+
+ /** Optional include patterns for resolving source templates or template directories */
+ private final List<String> includes;
+
+ /** Optional exclude patterns for resolving source templates or template directories */
+ private final List<String> excludes;
+
+ /** Optional output file or directory */
+ private final List<File> outputs;
+
+ /** Optional user-supplied writer */
+ private Writer writer;
+
+ private TemplateTransformationsBuilder() {
+ this.sources = new ArrayList<>();
+ this.includes = new ArrayList<>();
+ this.excludes = new ArrayList<>();
+ this.outputs = new ArrayList<>();
+ this.writer = null;
+ }
+
+ public static TemplateTransformationsBuilder builder() {
+ return new TemplateTransformationsBuilder();
+ }
+
+ public TemplateTransformations build() {
+ final List<TemplateTransformation> result = new ArrayList<>();
+
+ for (int i = 0; i < sources.size(); i++) {
+ final String source = sources.get(i);
+ final File output = i < outputs.size() ? outputs.get(i) : null;
+ result.addAll(resolve(source, output));
+ }
+
+ return new TemplateTransformations(result);
+ }
+
+ public TemplateTransformationsBuilder addSource(String source) {
+ if (StringUtils.isNotEmpty(source)) {
+ this.sources.add(source);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addSources(Collection<String> sources) {
+ if (sources != null) {
+ this.sources.addAll(sources);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addInclude(String include) {
+ if (StringUtils.isNotEmpty(include)) {
+ this.includes.add(include);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addIncludes(Collection<String> includes) {
+ if (includes != null) {
+ this.includes.addAll(includes);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addExclude(String exclude) {
+ if (StringUtils.isNotEmpty(exclude)) {
+ this.excludes.add(exclude);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addExcludes(Collection<String> excludes) {
+ if (excludes != null) {
+ this.excludes.addAll(excludes);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addOutput(String output) {
+ if (StringUtils.isNotEmpty(output)) {
+ this.outputs.add(new File(output));
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder addOutputs(Collection<String> outputs) {
+ if (outputs != null) {
+ outputs.forEach(this::addOutput);
+ }
+ return this;
+ }
+
+ public TemplateTransformationsBuilder setWriter(Writer writer) {
+ this.writer = writer;
+ return this;
+ }
+
+ public TemplateTransformationsBuilder setStdOut() {
+ this.writer = new NonClosableWriterWrapper(new BufferedWriter(new OutputStreamWriter(System.out, UTF_8)));
+ return this;
+ }
+
+ /**
+ * Resolve a <code>source</code> to a list of <code>TemplateTransformation</code>.
+ *
+ * @param source the source being a file name, an URI or <code>NamedUri</code>
+ * @param output Optional output file or directory
+ * @return list of <code>TemplateTransformation</code>
+ */
+ private List<TemplateTransformation> resolve(String source, File output) {
+ if (isTemplateFile(source)) {
+ return resolveTemplateFile(source, output);
+ } else if (isTemplateDirectory(source)) {
+ return resolveTemplateDirectory(source, output);
+ } else if (isTemplatePath(source)) {
+ return resolveTemplatePath(source, output);
+ } else {
+ throw new RuntimeException("Don't know how to resolve: " + source);
+ }
+ }
+
+ private List<TemplateTransformation> resolveTemplateFile(String source, File outputFile) {
+ final TemplateSource templateSource = templateSource(source);
+ final TemplateOutput templateOutput = templateOutput(outputFile);
+ return singletonList(new TemplateTransformation(templateSource, templateOutput));
+ }
+
+ private List<TemplateTransformation> resolveTemplateDirectory(String source, File outputDirectory) {
+ Validate.fileExists(new File(source), "Template directory does not exist: " + source);
+
+ final File templateDirectory = new File(source);
+ final List<File> templateFiles = templateFilesSupplier(source, getInclude(), getExclude()).get();
+ final List<TemplateTransformation> templateTransformations = new ArrayList<>();
+
+ for (File templateFile : templateFiles) {
+ final TemplateSource templateSource = templateSource(templateFile.getAbsolutePath());
+ final File outputFile = getTemplateOutputFile(templateDirectory, templateFile, outputDirectory);
+ final TemplateOutput templateOutput = templateOutput(outputFile);
+ templateTransformations.add(new TemplateTransformation(templateSource, templateOutput));
+ }
+
+ return templateTransformations;
+ }
+
+ private List<TemplateTransformation> resolveTemplatePath(String source, File out) {
+ final TemplateSource templateSource = templateSource(source);
+ final TemplateOutput templateOutput = templateOutput(out);
+ return singletonList(new TemplateTransformation(templateSource, templateOutput));
+ }
+
+ private TemplateOutput templateOutput(File templateOutputFile) {
+ if (templateOutputFile != null) {
+ return TemplateOutput.fromFile(templateOutputFile);
+ } else {
+ return TemplateOutput.fromWriter(writer);
+ }
+ }
+
+ private TemplateSource templateSource(String source) {
+ return TemplateSourceFactory.create(source);
+ }
+
+ private String getInclude() {
+ return includes.isEmpty() ? null : includes.get(0);
+ }
+
+ private String getExclude() {
+ return excludes.isEmpty() ? null : excludes.get(0);
+ }
+
+ private Writer writer(String outputFile, String outputEncoding) {
+ try {
+ if (writer != null) {
+ return writer;
+ } else if (!StringUtils.isEmpty(outputFile)) {
+ return new BufferedWriter(new FileWriter(outputFile));
+ } else {
+ return new BufferedWriter(new OutputStreamWriter(System.out, outputEncoding));
+ }
+ } catch (IOException e) {
+ throw new RuntimeException("Unable to create writer", e);
+ }
+ }
+
+ private static File getTemplateOutputFile(File templateDirectory, File templateFile, File outputDirectory) {
+ final String relativePath = relativePath(templateDirectory, templateFile);
+ final String relativeOutputFileName = mapExtension(relativePath);
+ return new File(outputDirectory, relativeOutputFileName);
+ }
+
+ private static boolean isTemplateFile(String source) {
+ final File file = new File(source);
+ return file.exists() && file.isFile();
+ }
+
+ private static boolean isTemplateDirectory(String source) {
+ final File file = new File(source);
+ return file.exists() && file.isDirectory();
+ }
+
+ private static boolean isTemplatePath(String source) {
+ return !isTemplateFile(source) && !isTemplateDirectory(source);
+ }
+
+ private static RecursiveFileSupplier templateFilesSupplier(String source, String include, String exclude) {
+ return new RecursiveFileSupplier(singletonList(source), singletonList(include), singletonList(exclude));
+ }
+
+ private static String relativePath(File directory, File file) {
+ return file.getAbsolutePath()
+ .substring(directory.getAbsolutePath().length())
+ .substring(1);
+ }
+
+ private static String mapExtension(String fileName) {
+ if (fileName.toLowerCase().endsWith(".ftl")) {
+ return fileName.substring(0, fileName.length() - 4);
+ } else {
+ return fileName;
+ }
+ }
+}
diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateProcessingInfoSupplierTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateProcessingInfoSupplierTest.java
deleted file mode 100644
index 129fd71..0000000
--- a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateProcessingInfoSupplierTest.java
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.freemarker.generator.template;
-
-import org.apache.freemarker.generator.base.template.TemplateProcessingInfo;
-import org.apache.freemarker.generator.base.template.TemplateProcessingInfoSupplier;
-import org.junit.Test;
-
-import java.io.File;
-import java.util.Collections;
-import java.util.List;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-
-public class TemplateProcessingInfoSupplierTest {
-
- private static final File ROOT_DIR = new File("./target/out");
- private static final String ANY_TEMPLATE_FILE_NAME = "src/test/template/application.properties";
- private static final String ANY_TEMPLATE_PATH = "template/info.ftl";
- private static final String ANY_TEMPLATE_DIRECTORY_NAME = "src/test/template";
-
- private static final String NO_INCLUDE = null;
- private static final String NO_EXCLUDE = null;
-
- @Test
- public void shouldCreateFromTemplateFile() {
- final TemplateProcessingInfoSupplier supplier = supplier(ANY_TEMPLATE_FILE_NAME);
-
- final List<TemplateProcessingInfo> templateProcessingInfos = supplier.get();
-
- assertNotNull(templateProcessingInfos);
- assertEquals(1, templateProcessingInfos.size());
- }
-
- @Test
- public void shouldCreateFromTemplatePath() {
- final TemplateProcessingInfoSupplier supplier = supplier(ANY_TEMPLATE_PATH);
-
- final List<TemplateProcessingInfo> templateProcessingInfos = supplier.get();
-
- assertNotNull(templateProcessingInfos);
- assertEquals(1, templateProcessingInfos.size());
- }
-
- @Test
- public void shouldCreateFromTemplateDirectory() {
- final TemplateProcessingInfoSupplier supplier = supplier(ANY_TEMPLATE_DIRECTORY_NAME);
-
- final List<TemplateProcessingInfo> templateProcessingInfos = supplier.get();
-
- assertNotNull(templateProcessingInfos);
- assertEquals(2, templateProcessingInfos.size());
- }
-
- private TemplateProcessingInfoSupplier supplier(String source) {
- return new TemplateProcessingInfoSupplier(
- Collections.singleton(source),
- NO_INCLUDE,
- NO_EXCLUDE,
- ROOT_DIR);
- }
-}
diff --git a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateTransformationsBuilderTest.java b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateTransformationsBuilderTest.java
new file mode 100644
index 0000000..f1e4067
--- /dev/null
+++ b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateTransformationsBuilderTest.java
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.freemarker.generator.template;
+
+import org.apache.freemarker.generator.base.template.TemplateOutput;
+import org.apache.freemarker.generator.base.template.TemplateSource;
+import org.apache.freemarker.generator.base.template.TemplateSource.Origin;
+import org.apache.freemarker.generator.base.template.TemplateTransformations;
+import org.apache.freemarker.generator.base.template.TemplateTransformationsBuilder;
+import org.junit.Test;
+
+import java.io.File;
+import java.nio.charset.StandardCharsets;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TemplateTransformationsBuilderTest {
+
+ private static final String ANY_TEMPLATE_FILE_NAME = "src/test/template/application.properties";
+ private static final String ANY_TEMPLATE_PATH = "template/info.ftl";
+ private static final String ANY_TEMPLATE_DIRECTORY_NAME = "src/test/template";
+
+ // === Template File ====================================================
+
+ @Test
+ public void shouldCreateFromTemplateFile() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_FILE_NAME)
+ .setStdOut()
+ .build();
+
+ assertEquals(1, transformations.size());
+
+ final TemplateSource templateSource = transformations.get(0).getTemplateSource();
+ final TemplateOutput templateOutput = transformations.get(0).getTemplateOutput();
+
+ assertNotNull(templateSource.getName());
+ assertEquals(Origin.CODE, templateSource.getOrigin());
+ assertNotNull(templateSource.getCode());
+ assertNull(templateSource.getPath());
+ assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
+
+ assertNotNull(templateOutput.getWriter());
+ assertNull(templateOutput.getFile());
+ }
+
+ // === Template Path ====================================================
+
+ @Test
+ public void shouldCreateFromTemplatePath() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_PATH)
+ .setStdOut()
+ .build();
+
+ assertEquals(1, transformations.size());
+
+ final TemplateSource templateSource = transformations.get(0).getTemplateSource();
+ final TemplateOutput templateOutput = transformations.get(0).getTemplateOutput();
+
+ assertNotNull(templateSource.getName());
+ assertEquals(Origin.PATH, templateSource.getOrigin());
+ assertNull(templateSource.getCode());
+ assertNotNull(templateSource.getPath());
+ assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
+
+ assertNotNull(templateOutput.getWriter());
+ assertNull(templateOutput.getFile());
+ }
+
+ // === Template Directory ===============================================
+
+ @Test
+ public void shouldCreateFromTemplateDirectory() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_DIRECTORY_NAME)
+ .setStdOut()
+ .build();
+
+ assertEquals(2, transformations.size());
+ assertEquals(new File("nginx/nginx.conf"), transformations.get(0).getTemplateOutput().getFile());
+ assertEquals(new File("application.properties"), transformations.get(1).getTemplateOutput().getFile());
+ }
+
+ @Test
+ public void shouldCreateFromTemplateDirectoryWithOutputDirectory() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_DIRECTORY_NAME)
+ .addOutput("/tmp")
+ .build();
+
+ assertEquals(2, transformations.size());
+ assertEquals(new File("/tmp/nginx/nginx.conf"), transformations.get(0).getTemplateOutput().getFile());
+ assertEquals(new File("/tmp/application.properties"), transformations.get(1).getTemplateOutput().getFile());
+ }
+
+ @Test
+ public void shouldCreateFromTemplateDirectoryWithInclude() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_DIRECTORY_NAME)
+ .addInclude("*.properties")
+ .setStdOut()
+ .build();
+
+ assertEquals(1, transformations.size());
+ assertEquals(new File("application.properties"), transformations.get(0).getTemplateOutput().getFile());
+ }
+
+ @Test
+ public void shouldCreateFromTemplateDirectoryWithExclude() {
+ final TemplateTransformations transformations = builder()
+ .addSource(ANY_TEMPLATE_DIRECTORY_NAME)
+ .addExclude("*.ftl")
+ .setStdOut()
+ .build();
+
+ assertEquals(1, transformations.size());
+ assertEquals(new File("application.properties"), transformations.get(0).getTemplateOutput().getFile());
+ }
+
+ private TemplateTransformationsBuilder builder() {
+ return TemplateTransformationsBuilder
+ .builder();
+ }
+}