ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject [4/9] ant git commit: refactor test
Date Sun, 04 Sep 2016 14:25:33 GMT
refactor test


Project: http://git-wip-us.apache.org/repos/asf/ant/repo
Commit: http://git-wip-us.apache.org/repos/asf/ant/commit/e345ccf5
Tree: http://git-wip-us.apache.org/repos/asf/ant/tree/e345ccf5
Diff: http://git-wip-us.apache.org/repos/asf/ant/diff/e345ccf5

Branch: refs/heads/master
Commit: e345ccf5393aae4b8ba9f21fc2faf288099fdc63
Parents: bdba0f5
Author: Stefan Bodewig <bodewig@apache.org>
Authored: Sun Sep 4 12:15:49 2016 +0200
Committer: Stefan Bodewig <bodewig@apache.org>
Committed: Sun Sep 4 12:15:49 2016 +0200

----------------------------------------------------------------------
 .../optional/junit/JUnitReportTest.java         | 95 +++++++-------------
 1 file changed, 32 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ant/blob/e345ccf5/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
----------------------------------------------------------------------
diff --git a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
index 1551a5b..8737a94 100644
--- a/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
+++ b/src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java
@@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
 import java.io.File;
 import java.io.FileReader;
 import java.io.InputStream;
+import java.io.IOException;
 import java.net.URL;
 
 import org.apache.tools.ant.BuildFileRule;
@@ -62,12 +63,33 @@ public class JUnitReportTest {
     }
 
     public void assertIndexCreated() {
-        if (!new File(buildRule.getProject().getProperty("output"), "html/index.html").exists())
{
-            fail("No file index file found");
+        try {
+            commonIndexFileAssertions();
+        } catch (IOException ex) {
+            throw new RuntimeException(ex);
         }
+    }
 
+    private File commonIndexFileAssertions() throws IOException {
+        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
+        commonIndexFileAssertions(reportFile);
+        return reportFile;
     }
 
+    private void commonIndexFileAssertions(File reportFile) throws IOException {
+        // tests one the file object
+        assertTrue("No index.html present. Not generated?", reportFile.exists() );
+        assertTrue("Cant read the report file.", reportFile.canRead() );
+        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
+        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
+        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
+        InputStream reportStream = reportUrl.openStream();
+        try {
+            assertTrue("This shouldn't be an empty stream.", reportStream.available() >
0);
+        } finally {
+            FileUtils.getFileUtils().close(reportStream);
+        }
+    }
 
     @Test
     public void testEmptyFile() throws Exception {
@@ -111,44 +133,21 @@ public class JUnitReportTest {
     @Test
     public void testSpecialSignsInSrcPath() throws Exception {
         buildRule.executeTarget("testSpecialSignsInSrcPath");
-        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions();
     }
 
     @Test
     public void testSpecialSignsInHtmlPath() throws Exception {
         buildRule.executeTarget("testSpecialSignsInHtmlPath");
         File reportFile = new File(buildRule.getOutputDir(), "html# $%\u00A7&-!report/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions(reportFile);
     }
 
     //Bugzilla Report 39708
     @Test
     public void testWithStyleFromDir() throws Exception {
         buildRule.executeTarget("testWithStyleFromDir");
-        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions();
     }
 
     //Bugzilla Report 40021
@@ -156,56 +155,26 @@ public class JUnitReportTest {
     public void testNoFrames() throws Exception {
         buildRule.executeTarget("testNoFrames");
         File reportFile = new File(buildRule.getOutputDir(), "html/junit-noframes.html");
-        // tests one the file object
-        assertTrue("No junit-noframes.html present. Not generated?", reportFile.exists()
);
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions(reportFile);
     }
+
     //Bugzilla Report 39708
     @Test
     public void testWithStyleFromDirAndXslImport() throws Exception {
         buildRule.executeTarget("testWithStyleFromDirAndXslImport");
-        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions();
     }
 
     @Test
     public void testWithStyleFromClasspath() throws Exception {
         buildRule.executeTarget("testWithStyleFromClasspath");
-        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions();
     }
 
     @Test
     public void testWithParams() throws Exception {
         buildRule.executeTarget("testWithParams");
         assertContains("key1=value1,key2=value2", buildRule.getLog());
-        File reportFile = new File(buildRule.getOutputDir(), "html/index.html");
-        // tests one the file object
-        assertTrue("No index.html present. Not generated?", reportFile.exists() );
-        assertTrue("Cant read the report file.", reportFile.canRead() );
-        assertTrue("File shouldn't be empty.", reportFile.length() > 0 );
-        // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested
in the bug report
-        URL reportUrl = new URL( FileUtils.getFileUtils().toURI(reportFile.getAbsolutePath())
);
-        InputStream reportStream = reportUrl.openStream();
-        assertTrue("This shouldn't be an empty stream.", reportStream.available() > 0);
+        commonIndexFileAssertions();
     }
 }


Mime
View raw message