freemarker-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ddek...@apache.org
Subject incubator-freemarker git commit: Changed FileTestCase (the superclass of some tests) to use URL-s instead of File-s. This allows the test suite to run from unextracted jar (among others).
Date Sat, 06 May 2017 21:09:38 GMT
Repository: incubator-freemarker
Updated Branches:
  refs/heads/3 7d61a45d9 -> 7486e23b3


Changed FileTestCase (the superclass of some tests) to use URL-s instead of File-s. This allows
the test suite to run from unextracted jar (among others).


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

Branch: refs/heads/3
Commit: 7486e23b31014d3451dd9e794165549aaf53a834
Parents: 7d61a45
Author: ddekany <ddekany@apache.org>
Authored: Sat May 6 23:09:26 2017 +0200
Committer: ddekany <ddekany@apache.org>
Committed: Sat May 6 23:09:26 2017 +0200

----------------------------------------------------------------------
 .../org/apache/freemarker/core/ASTTest.java     |  6 +-
 .../test/templatesuite/TemplateTestCase.java    | 19 +++--
 .../freemarker/test/util/FileTestCase.java      | 88 ++++++++------------
 3 files changed, 52 insertions(+), 61 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7486e23b/src/test/java/org/apache/freemarker/core/ASTTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/freemarker/core/ASTTest.java b/src/test/java/org/apache/freemarker/core/ASTTest.java
index d99c62c..ff67cfb 100644
--- a/src/test/java/org/apache/freemarker/core/ASTTest.java
+++ b/src/test/java/org/apache/freemarker/core/ASTTest.java
@@ -21,6 +21,7 @@ package org.apache.freemarker.core;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.net.URL;
 
 import org.apache.freemarker.core.ASTPrinter.Options;
 import org.apache.freemarker.core.util._StringUtil;
@@ -88,7 +89,10 @@ public class ASTTest extends FileTestCase {
         assertExpectedFileEqualsString(
                 testName + ".ast",
                 ASTPrinter.getASTAsString(templateName,
-                        TestUtil.removeFTLCopyrightComment(normalizeLineBreaks(loadResource(templateName))),
ops));
+                        TestUtil.removeFTLCopyrightComment(
+                                normalizeLineBreaks(
+                                        loadTestTextResource(new URL(getTestClassDirectory(),
templateName))
+                        )), ops));
     }
     
     private String normalizeLineBreaks(final String s) throws FileNotFoundException, IOException
{

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7486e23b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java
b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java
index 9dbccc7..2a24b5b 100644
--- a/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java
+++ b/src/test/java/org/apache/freemarker/test/templatesuite/TemplateTestCase.java
@@ -19,12 +19,12 @@
 
 package org.apache.freemarker.test.templatesuite;
 
-import java.io.File;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -62,7 +62,7 @@ import org.apache.freemarker.core.model.impl.ResourceBundleModel;
 import org.apache.freemarker.core.model.impl.SimpleCollection;
 import org.apache.freemarker.core.model.impl.SimpleDate;
 import org.apache.freemarker.core.model.impl.SimpleNumber;
-import org.apache.freemarker.core.templateresolver.impl.FileTemplateLoader;
+import org.apache.freemarker.core.templateresolver.impl.ClassTemplateLoader;
 import org.apache.freemarker.core.util._NullArgumentException;
 import org.apache.freemarker.core.util._NullWriter;
 import org.apache.freemarker.core.util._StringUtil;
@@ -174,8 +174,9 @@ public class TemplateTestCase extends FileTestCase {
     @Override
     @SuppressWarnings("boxing")
     public void setUp() throws Exception {
-        confB.setTemplateLoader(new CopyrightCommentRemoverTemplateLoader(
-                new FileTemplateLoader(new File(getTestClassDirectory(), "templates"))));
+        confB.setTemplateLoader(
+                new CopyrightCommentRemoverTemplateLoader(
+                        new ClassTemplateLoader(TemplateTestCase.class, "templates")));
         
         DefaultObjectWrapper dow = new DefaultObjectWrapper.Builder(Configuration.VERSION_3_0_0).build();
         
@@ -423,18 +424,18 @@ public class TemplateTestCase extends FileTestCase {
     }
 
     @Override
-    protected File getExpectedFileDirectory() throws IOException {
-        return new File(super.getExpectedFileDirectory(), "expected");
+    protected URL getExpectedFileDirectory() throws IOException {
+        return new URL(super.getExpectedFileDirectory(), "expected/");
     }
 
     @Override
-    protected Charset getFileCharset() {
+    protected Charset getTestResourceCharset() {
         return confB.getOutputEncoding() != null ? confB.getOutputEncoding() : StandardCharsets.UTF_8;
     }
     
     @Override
-    protected File getExpectedFileFor(String testCaseFileName) throws IOException {
-        return new File(getExpectedFileDirectory(), expectedFileName);
+    protected URL getExpectedFileFor(String testCaseFileName) throws IOException {
+        return new URL(getExpectedFileDirectory(), expectedFileName);
     }
 
     static class TestBoolean implements TemplateBooleanModel, TemplateScalarModel {

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/7486e23b/src/test/java/org/apache/freemarker/test/util/FileTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/freemarker/test/util/FileTestCase.java b/src/test/java/org/apache/freemarker/test/util/FileTestCase.java
index c0990ba..144592a 100644
--- a/src/test/java/org/apache/freemarker/test/util/FileTestCase.java
+++ b/src/test/java/org/apache/freemarker/test/util/FileTestCase.java
@@ -20,8 +20,6 @@
 package org.apache.freemarker.test.util;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -33,6 +31,8 @@ import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
 import org.apache.freemarker.core.util._StringUtil;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -48,41 +48,20 @@ public abstract class FileTestCase extends TestCase {
         super(name);
     }
 
-    protected void assertFilesEqual(String testCaseFileName) throws IOException {
-        try {
-            multilineAssertEquals(
-                    loadFile(getExpectedFileFor(testCaseFileName)),
-                    loadFile(getActualFileFor(testCaseFileName)));
-        } catch (IOException e) {
-            throw new RuntimeException("Failed to do assertion", e);
-        }
-    }
-
     protected void assertExpectedFileEqualsString(String expectedFileName, String actualContent)
{
         try {
-            final File expectedFile = getExpectedFileFor(expectedFileName);
+            final URL expectedFile = getExpectedFileFor(expectedFileName);
             
-            AssertionFailedError assertionException = null;
-            boolean successful = false;
             try {
-                if (expectedFile.exists()) {
-                    multilineAssertEquals(loadFile(expectedFile), actualContent);
-                    successful = true;
-                }
+                multilineAssertEquals(loadTestTextResource(expectedFile), actualContent);
             } catch (AssertionFailedError e) {
-                assertionException = e;
-            }
-            
-            if (!successful) {
                 File actualFile = getActualFileFor(expectedFileName);
-                saveString(actualFile, actualContent);
-                reportActualFileSaved(actualFile);
-                
-                if (assertionException != null) {
-                    throw assertionException;
-                } else {
-                    throw new FileNotFoundException(expectedFile.getPath());
+                if (actualFile == null) {
+                    saveString(actualFile, actualContent);
+                    reportActualFileSaved(actualFile);
                 }
+
+                throw e;
             }
         } catch (IOException e) {
             throw new RuntimeException("Failed to do assertion", e);
@@ -116,12 +95,19 @@ public abstract class FileTestCase extends TestCase {
         }
     }
 
-    protected File getExpectedFileFor(String testCaseFileName) throws IOException {
-        return new File(getExpectedFileDirectory(), testCaseFileName);
+    protected URL getExpectedFileFor(String testCaseFileName) throws IOException {
+        return new URL(getExpectedFileDirectory(), testCaseFileName);
     }
-    
+
+    /**
+     * @return {@code null} if there's no place to write the actual files to
+     */
     protected File getActualFileFor(String testCaseFileName) throws IOException {
-        return new File(getActualFileDirectory(), deduceActualFileName(testCaseFileName));
+        File actualFileDirectory = getActualFileDirectory();
+        if (actualFileDirectory == null) {
+            return null;
+        }
+        return new File(actualFileDirectory, deduceActualFileName(testCaseFileName));
     }
     
     private String deduceActualFileName(String testCaseFileName) {
@@ -131,38 +117,38 @@ public abstract class FileTestCase extends TestCase {
                 : testCaseFileName.substring(0, lastDotIdx) + "-actual" + testCaseFileName.substring(lastDotIdx);
     }
 
-    protected File getExpectedFileDirectory() throws IOException {
+    /**
+     * The URL of the directory that contains the expected files; must end with "/" or "/."
or else relative paths won't
+     * be resolved correctly.
+     */
+    protected URL getExpectedFileDirectory() throws IOException {
         return getTestClassDirectory();
     }
 
+    /**
+     * @return {@code null} if there's no directory to write the actual files to
+     */
     protected File getActualFileDirectory() throws IOException {
-        return getExpectedFileDirectory();
+        return FileUtils.toFile(getExpectedFileDirectory());
     }
 
     @SuppressFBWarnings(value="UI_INHERITANCE_UNSAFE_GETRESOURCE", justification="By design
relative to subclass")
-    protected final File getTestClassDirectory() throws IOException {
+    protected final URL getTestClassDirectory() throws IOException {
         URL url = getClass().getResource(".");
         if (url == null) throw new IOException("Couldn't get resource URL for \".\"");
-        return new File(url.getFile());
-    }
-
-    protected String loadFile(File f) throws IOException {
-        return TestUtil.removeTxtCopyrightComment(loadFile(f, getFileCharset()));
-    }
-    
-    protected String loadFile(File f, Charset charset) throws IOException {
-        return loadString(new FileInputStream(f), charset);
+        return url;
     }
 
-    protected String loadResource(String resourceName) throws IOException {
-        return loadResource(resourceName, getFileCharset());
+    protected String loadTestTextResource(URL resource) throws IOException {
+        return loadTestTextResource(resource, getTestResourceCharset());
     }
     
-    protected String loadResource(String resourceName, Charset charset) throws IOException
{
-        return loadString(new FileInputStream(new File(getTestClassDirectory(), resourceName)),
charset);
+    protected String loadTestTextResource(URL resource, Charset charset) throws IOException
{
+        return TestUtil.removeTxtCopyrightComment(
+                IOUtils.toString(resource, charset.name()));
     }
     
-    protected Charset getFileCharset() {
+    protected Charset getTestResourceCharset() {
         return StandardCharsets.UTF_8;
     }
     


Mime
View raw message