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

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

commit d7d9639b57399cb841109505a33fba0045bd0b37
Author: Siegfried Goeschl <[email protected]>
AuthorDate: Sat Dec 26 21:57:02 2020 +0100

    FREEMARKER-161 [freemarker-generator] Allow multiple transformations on the 
CLI
---
 .../base/template/TemplateSourceFactory.java       | 40 ----------
 .../template/TemplateTransformationsBuilder.java   | 38 +++++++---
 .../template/TemplateSourceFactoryTest.java        | 88 ----------------------
 .../TemplateTransformationsBuilderTest.java        | 48 +++++++++++-
 4 files changed, 72 insertions(+), 142 deletions(-)

diff --git 
a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSourceFactory.java
 
b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSourceFactory.java
deleted file mode 100644
index ab53ee3..0000000
--- 
a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/template/TemplateSourceFactory.java
+++ /dev/null
@@ -1,40 +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.base.template;
-
-import org.apache.freemarker.generator.base.datasource.DataSource;
-import org.apache.freemarker.generator.base.datasource.DataSourceFactory;
-import org.apache.freemarker.generator.base.util.UriUtils;
-
-import java.io.File;
-
-public abstract class TemplateSourceFactory {
-
-    public static TemplateSource create(String str) {
-        if (isTemplatePath(str)) {
-            return TemplateSource.fromPath(str);
-        } else {
-            try (DataSource dataSource = DataSourceFactory.create(str)) {
-                return TemplateSource.fromCode(dataSource.getName(), 
dataSource.getText());
-            }
-        }
-    }
-
-    private static boolean isTemplatePath(String str) {
-        return !UriUtils.isUri(str) && !new File(str).exists();
-    }
-}
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
index 261e3f4..e352173 100644
--- 
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
@@ -17,6 +17,8 @@
 package org.apache.freemarker.generator.base.template;
 
 import org.apache.freemarker.generator.base.FreeMarkerConstants.Location;
+import org.apache.freemarker.generator.base.datasource.DataSource;
+import org.apache.freemarker.generator.base.datasource.DataSourceFactory;
 import org.apache.freemarker.generator.base.file.RecursiveFileSupplier;
 import org.apache.freemarker.generator.base.util.StringUtils;
 import org.apache.freemarker.generator.base.util.Validate;
@@ -154,10 +156,12 @@ public class TemplateTransformationsBuilder {
             return resolveTemplateFile(source, output);
         } else if (isTemplateDirectoryFound(source)) {
             return resolveTemplateDirectory(source, output);
-        } else if (isTemplatePath(source)) {
-            return resolveTemplatePath(source, output);
+        } else if (isTemplateHttpUrl(source)) {
+            return resolveTemplateHttpUrl(source, output);
+        } else if (isTemplateUri(source)) {
+            return resolveTemplateUri(source, output);
         } else {
-            return resolveTemplateCode(source, output);
+            return resolveTemplatePath(source, output);
         }
     }
 
@@ -184,23 +188,29 @@ public class TemplateTransformationsBuilder {
         return templateTransformations;
     }
 
-    private List<TemplateTransformation> resolveTemplatePath(String source, 
File out) {
+    private List<TemplateTransformation> resolveTemplateHttpUrl(String source, 
File out) {
         final TemplateSource templateSource = templateSource(source);
         final TemplateOutput templateOutput = templateOutput(out);
         return singletonList(new TemplateTransformation(templateSource, 
templateOutput));
     }
 
-    private TemplateTransformation resolveInteractiveTemplate(File out) {
+    private List<TemplateTransformation> resolveTemplateUri(String source, 
File out) {
+        final TemplateSource templateSource = templateSource(source);
         final TemplateOutput templateOutput = templateOutput(out);
-        return new TemplateTransformation(interactiveTemplate, templateOutput);
+        return singletonList(new TemplateTransformation(templateSource, 
templateOutput));
     }
 
-    private List<TemplateTransformation> resolveTemplateCode(String source, 
File out) {
-        final TemplateSource templateSource = 
TemplateSource.fromCode(Location.INTERACTIVE, source);
+    private List<TemplateTransformation> resolveTemplatePath(String source, 
File out) {
+        final TemplateSource templateSource = TemplateSource.fromPath(source);
         final TemplateOutput templateOutput = templateOutput(out);
         return singletonList(new TemplateTransformation(templateSource, 
templateOutput));
     }
 
+    private TemplateTransformation resolveInteractiveTemplate(File out) {
+        final TemplateOutput templateOutput = templateOutput(out);
+        return new TemplateTransformation(interactiveTemplate, templateOutput);
+    }
+
     private TemplateOutput templateOutput(File templateOutputFile) {
         if (writer == null && templateOutputFile != null) {
             return TemplateOutput.fromFile(templateOutputFile);
@@ -210,7 +220,9 @@ public class TemplateTransformationsBuilder {
     }
 
     private TemplateSource templateSource(String source) {
-        return TemplateSourceFactory.create(source);
+        try (DataSource dataSource = DataSourceFactory.create(source)) {
+            return TemplateSource.fromCode(dataSource.getName(), 
dataSource.getText());
+        }
     }
 
     private String getInclude() {
@@ -251,8 +263,12 @@ public class TemplateTransformationsBuilder {
         return file.exists() && file.isDirectory();
     }
 
-    private static boolean isTemplatePath(String source) {
-        return !isTemplateFileFound(source) && 
!isTemplateDirectoryFound(source);
+    private static boolean isTemplateHttpUrl(String source) {
+        return source.contains("http://";) || source.contains("https://";);
+    }
+
+    private static boolean isTemplateUri(String source) {
+        return source.contains("://");
     }
 
     private static RecursiveFileSupplier templateFilesSupplier(String source, 
String include, String exclude) {
diff --git 
a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateSourceFactoryTest.java
 
b/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateSourceFactoryTest.java
deleted file mode 100644
index 2088e4b..0000000
--- 
a/freemarker-generator-base/src/test/java/org/apache/freemarker/generator/template/TemplateSourceFactoryTest.java
+++ /dev/null
@@ -1,88 +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.TemplateSource;
-import org.apache.freemarker.generator.base.template.TemplateSource.Origin;
-import org.apache.freemarker.generator.base.template.TemplateSourceFactory;
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-public class TemplateSourceFactoryTest {
-
-    private static final String ANY_TEMPLATE_PATH = "any/template/path.ftl";
-    private static final String ANY_FILE_NAME = "pom.xml";
-    private static final String ANY_URL = 
"https://raw.githubusercontent.com/apache/freemarker-generator/master/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl";;
-    private static final String ANY_ENV_VARIABLE = "JAVA_HOME";
-    private static final String ANY_ENV_URI = "env:///" + ANY_ENV_VARIABLE;
-
-    @Test
-    public void shouldCreateFromTemplatePath() {
-        final TemplateSource templateSource = 
TemplateSourceFactory.create(ANY_TEMPLATE_PATH);
-
-        assertEquals(ANY_TEMPLATE_PATH, templateSource.getName());
-        assertEquals(Origin.TEMPLATE_LOADER, templateSource.getOrigin());
-        assertEquals(ANY_TEMPLATE_PATH, templateSource.getPath());
-        assertNull(templateSource.getCode());
-    }
-
-    @Test
-    public void shouldCreateFromFile() {
-        final TemplateSource templateSource = 
TemplateSourceFactory.create(ANY_FILE_NAME);
-
-        assertEquals(ANY_FILE_NAME, templateSource.getName());
-        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
-        assertNull(templateSource.getPath());
-        assertFalse(templateSource.getCode().isEmpty());
-    }
-
-    @Test
-    public void shouldCreateFromEnvironmentVariable() {
-        final TemplateSource templateSource = 
TemplateSourceFactory.create(ANY_ENV_URI);
-
-        assertEquals(ANY_ENV_VARIABLE, templateSource.getName());
-        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
-        assertNull(templateSource.getPath());
-        assertFalse(templateSource.getCode().isEmpty());
-    }
-
-    @Test
-    // @Ignore("Requires internet access")
-    public void shouldCreateFromUrl() {
-        final TemplateSource templateSource = 
TemplateSourceFactory.create(ANY_URL);
-
-        assertNotNull(templateSource.getName());
-        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
-        assertNull(templateSource.getPath());
-        assertFalse(templateSource.getCode().isEmpty());
-    }
-
-    @Test
-    // @Ignore("Requires internet access")
-    public void shouldCreateFromNamedUri() {
-        final TemplateSource templateSource = 
TemplateSourceFactory.create("info=" + ANY_URL);
-
-        assertEquals("info", templateSource.getName());
-        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
-        assertNull(templateSource.getPath());
-        assertFalse(templateSource.getCode().isEmpty());
-    }
-}
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
index 1d86574..66d3c9a 100644
--- 
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
@@ -34,8 +34,10 @@ import java.util.List;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 public class TemplateTransformationsBuilderTest {
 
@@ -43,6 +45,8 @@ public class TemplateTransformationsBuilderTest {
     private static final String OTHER_TEMPLATE_FILE_NAME = 
"src/test/template/nginx/nginx.conf.ftl";
     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 ANY_TEMPLATE_URL = 
"https://raw.githubusercontent.com/apache/freemarker-generator/master/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl";;
+    private static final String ANY_ENV_URI = "env:///JAVA_HOME";
 
     // === Interactive Template =============================================
 
@@ -93,7 +97,7 @@ public class TemplateTransformationsBuilderTest {
 
         assertNotNull(templateSource.getName());
         assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
-        assertNotNull(templateSource.getCode());
+        assertTrue(templateSource.getCode().contains("Licensed to the Apache 
Software Foundation"));
         assertNull(templateSource.getPath());
         assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
 
@@ -129,10 +133,10 @@ public class TemplateTransformationsBuilderTest {
         final TemplateSource templateSource = 
transformations.get(0).getTemplateSource();
         final TemplateOutput templateOutput = 
transformations.get(0).getTemplateOutput();
 
-        assertNotNull(templateSource.getName());
+        assertEquals(ANY_TEMPLATE_PATH, templateSource.getName());
         assertEquals(Origin.TEMPLATE_LOADER, templateSource.getOrigin());
         assertNull(templateSource.getCode());
-        assertNotNull(templateSource.getPath());
+        assertEquals(ANY_TEMPLATE_PATH, templateSource.getPath());
         assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
 
         assertNotNull(templateOutput.getWriter());
@@ -189,6 +193,44 @@ public class TemplateTransformationsBuilderTest {
         assertEquals("application.properties", 
transformations.get(0).getTemplateSource().getName());
     }
 
+    // === Template URL ===============================================
+
+    @Test
+    public void shouldCreateFromTemplateUrl() {
+        final List<TemplateTransformation> transformations = builder()
+                .addTemplateSource(ANY_TEMPLATE_URL)
+                .setWriter(stdoutWriter())
+                .build();
+
+        final TemplateSource templateSource = 
transformations.get(0).getTemplateSource();
+
+        assertEquals(1, transformations.size());
+        assertEquals(ANY_TEMPLATE_URL, templateSource.getName());
+        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
+        assertNull(templateSource.getPath());
+        assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
+        assertTrue(templateSource.getCode().contains("<#ftl"));
+    }
+
+    // === Template ENV Variable =============================================
+
+    @Test
+    public void shouldCreateFromTemplateEnvironmentVariable() {
+        final List<TemplateTransformation> transformations = builder()
+                .addTemplateSource(ANY_ENV_URI)
+                .setWriter(stdoutWriter())
+                .build();
+
+        final TemplateSource templateSource = 
transformations.get(0).getTemplateSource();
+
+        assertEquals(1, transformations.size());
+        assertEquals("JAVA_HOME", templateSource.getName());
+        assertEquals(Origin.TEMPLATE_CODE, templateSource.getOrigin());
+        assertNull(templateSource.getPath());
+        assertEquals(StandardCharsets.UTF_8, templateSource.getEncoding());
+        assertFalse(templateSource.getCode().isEmpty());
+    }
+
     private TemplateTransformationsBuilder builder() {
         return TemplateTransformationsBuilder
                 .builder();

Reply via email to