AST tests and TemplateTestSuite tests remove the copyright comment from the template files on the fly.
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 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 @@
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 {
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);
+ }
+
+}
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);
+ }
+
+}
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 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.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.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.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 @@
@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);