freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject [1/5] incubator-freemarker git commit: AST tests and TemplateTestSuite tests remove the copyright comment from the template files on the fly.
Date Thu, 31 Dec 2015 00:13:06 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/2.3-gae 9d255d897 -> 559381f38


AST tests and TemplateTestSuite tests remove the copyright comment from the template files
on the fly.


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/9ee209e9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/9ee209e9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/9ee209e9

Branch: refs/heads/2.3-gae
Commit: 9ee209e9354d78193cc2c2c5b0a0e8135c5f4ee3
Parents: 9d255d8
Author: ddekany <ddekany@apache.org>
Authored: Wed Dec 30 23:11:23 2015 +0100
Committer: ddekany <ddekany@apache.org>
Committed: Thu Dec 31 01:11:54 2015 +0100

----------------------------------------------------------------------
 src/test/java/freemarker/core/ASTTest.java      |  4 +-
 .../CopyrightCommentRemoverTemplateLoader.java  | 63 +++++++++++++++
 src/test/java/freemarker/test/TestUtil.java     | 81 ++++++++++++++++++++
 .../test/templatesuite/TemplateTestCase.java    |  8 +-
 4 files changed, 152 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9ee209e9/src/test/java/freemarker/core/ASTTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/core/ASTTest.java b/src/test/java/freemarker/core/ASTTest.java
index 8edb85e..7124c64 100644
--- a/src/test/java/freemarker/core/ASTTest.java
+++ b/src/test/java/freemarker/core/ASTTest.java
@@ -24,6 +24,7 @@ import java.io.IOException;
 
 import freemarker.core.ASTPrinter.Options;
 import freemarker.template.utility.StringUtil;
+import freemarker.test.TestUtil;
 import freemarker.test.utility.FileTestCase;
 
 public class ASTTest extends FileTestCase {
@@ -86,7 +87,8 @@ public class ASTTest extends FileTestCase {
         final String templateName = testName + ".ftl";
         assertExpectedFileEqualsString(
                 testName + ".ast",
-                ASTPrinter.getASTAsString(templateName, normalizeLineBreaks(templateName),
ops));
+                ASTPrinter.getASTAsString(templateName,
+                        TestUtil.removeCopyrightCommentFromFTL(normalizeLineBreaks(templateName)),
ops));
     }
     
     private String normalizeLineBreaks(final String templateName) throws FileNotFoundException,
IOException {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9ee209e9/src/test/java/freemarker/test/CopyrightCommentRemoverTemplateLoader.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/test/CopyrightCommentRemoverTemplateLoader.java b/src/test/java/freemarker/test/CopyrightCommentRemoverTemplateLoader.java
new file mode 100644
index 0000000..5d336a4
--- /dev/null
+++ b/src/test/java/freemarker/test/CopyrightCommentRemoverTemplateLoader.java
@@ -0,0 +1,63 @@
+/*
+ * 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 freemarker.test;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.io.IOUtils;
+
+import freemarker.cache.TemplateLoader;
+
+public class CopyrightCommentRemoverTemplateLoader implements TemplateLoader {
+
+    private final TemplateLoader innerTemplateLoader;
+
+    public CopyrightCommentRemoverTemplateLoader(TemplateLoader innerTemplateLoader) {
+        this.innerTemplateLoader = innerTemplateLoader;
+    }
+
+    @Override
+    public Object findTemplateSource(String name) throws IOException {
+        return innerTemplateLoader.findTemplateSource(name);
+    }
+
+    @Override
+    public long getLastModified(Object templateSource) {
+        return innerTemplateLoader.getLastModified(templateSource);
+    }
+
+    @Override
+    public Reader getReader(Object templateSource, String encoding) throws IOException {
+        Reader reader = innerTemplateLoader.getReader(templateSource, encoding);
+        try {
+            String content = IOUtils.toString(reader);
+            return new StringReader(TestUtil.removeCopyrightCommentFromFTL(content));
+        } finally {
+            reader.close();
+        }
+    }
+
+    @Override
+    public void closeTemplateSource(Object templateSource) throws IOException {
+        innerTemplateLoader.closeTemplateSource(templateSource);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9ee209e9/src/test/java/freemarker/test/TestUtil.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/test/TestUtil.java b/src/test/java/freemarker/test/TestUtil.java
new file mode 100644
index 0000000..a8e9244
--- /dev/null
+++ b/src/test/java/freemarker/test/TestUtil.java
@@ -0,0 +1,81 @@
+/*
+ * 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 freemarker.test;
+
+public final class TestUtil {
+    
+    private TestUtil() {
+        // Not meant to be instantiated
+    }
+
+    public static String removeCopyrightCommentFromFTL(String ftl) {
+        if (ftl.contains("<#ftl ns_prefixes = {\"D\" : \"http://example.com/eBook\"}>"))
{
+            System.out.println();
+        }
+        
+        int copyrightIdx = ftl.indexOf("copyright");
+        if (copyrightIdx == -1) {
+            copyrightIdx = ftl.indexOf("Copyright");
+        }
+        if (copyrightIdx == -1) {
+            return ftl;
+        }
+        
+        final int commentFirstIdx;
+        final boolean squareBracketTagSyntax;
+        {
+            String ftlBeforeCopyright = ftl.substring(0, copyrightIdx);
+            int abCommentStart = ftlBeforeCopyright.lastIndexOf("<#--");
+            int sbCommentStart = ftlBeforeCopyright.lastIndexOf("[#--");
+            squareBracketTagSyntax = sbCommentStart > abCommentStart;
+            commentFirstIdx = squareBracketTagSyntax ? sbCommentStart : abCommentStart;
+            if (commentFirstIdx == -1) {
+                throw new AssertionError("Can't find copyright comment start");
+            }
+        }
+        
+        final int commentLastIdx;
+        {
+            int commentEndStart = ftl.indexOf(squareBracketTagSyntax ? "--]" : "-->",
copyrightIdx);
+            if (commentEndStart == -1) {
+                throw new AssertionError("Can't find copyright comment end");
+            }
+            commentLastIdx = commentEndStart + 2;
+        }
+        
+        final int afterCommentNLChars;
+        if (commentLastIdx + 1 < ftl.length()) {
+            char afterCommentChar = ftl.charAt(commentLastIdx + 1);
+            if (afterCommentChar == '\n' || afterCommentChar == '\r') {
+                if (afterCommentChar == '\r' && commentLastIdx + 2 < ftl.length()
&& ftl.charAt(commentLastIdx + 2) == '\n') {
+                    afterCommentNLChars = 2;
+                } else {
+                    afterCommentNLChars = 1;
+                }
+            } else {
+                afterCommentNLChars = 0;
+            }
+        } else {
+            afterCommentNLChars = 0;
+        }
+            
+        return ftl.substring(0, commentFirstIdx) + ftl.substring(commentLastIdx + afterCommentNLChars
+ 1);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/9ee209e9/src/test/java/freemarker/test/templatesuite/TemplateTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/freemarker/test/templatesuite/TemplateTestCase.java b/src/test/java/freemarker/test/templatesuite/TemplateTestCase.java
index 989e6e1..b44cf61 100644
--- a/src/test/java/freemarker/test/templatesuite/TemplateTestCase.java
+++ b/src/test/java/freemarker/test/templatesuite/TemplateTestCase.java
@@ -42,8 +42,6 @@ import java.util.TreeSet;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 
-import junit.framework.AssertionFailedError;
-
 import org.junit.Ignore;
 import org.xml.sax.InputSource;
 
@@ -51,6 +49,7 @@ import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 
+import freemarker.cache.FileTemplateLoader;
 import freemarker.core.ASTPrinter;
 import freemarker.ext.beans.BeansWrapper;
 import freemarker.ext.beans.BooleanModel;
@@ -76,6 +75,7 @@ import freemarker.template._TemplateAPI;
 import freemarker.template.utility.NullArgumentException;
 import freemarker.template.utility.NullWriter;
 import freemarker.template.utility.StringUtil;
+import freemarker.test.CopyrightCommentRemoverTemplateLoader;
 import freemarker.test.templatesuite.models.BooleanAndStringTemplateModel;
 import freemarker.test.templatesuite.models.BooleanHash1;
 import freemarker.test.templatesuite.models.BooleanHash2;
@@ -93,6 +93,7 @@ import freemarker.test.utility.AssertEqualsDirective;
 import freemarker.test.utility.AssertFailsDirective;
 import freemarker.test.utility.FileTestCase;
 import freemarker.test.utility.NoOutputDirective;
+import junit.framework.AssertionFailedError;
 
 /**
  * Instances of this are created and called by {@link TemplateTestSuite}. (It's on "Ignore"
so that Eclipse doesn't try
@@ -177,7 +178,8 @@ public class TemplateTestCase extends FileTestCase {
     @Override
     @SuppressWarnings("boxing")
     public void setUp() throws Exception {
-        conf.setDirectoryForTemplateLoading(new File(getTestClassDirectory(), "templates"));
+        conf.setTemplateLoader(new CopyrightCommentRemoverTemplateLoader(
+                new FileTemplateLoader(new File(getTestClassDirectory(), "templates"))));
         
         dataModel.put(ASSERT_VAR_NAME, AssertDirective.INSTANCE);
         dataModel.put(ASSERT_EQUALS_VAR_NAME, AssertEqualsDirective.INSTANCE);


Mime
View raw message