Browse Source

refactor test

master
Stefan Bodewig 8 years ago
parent
commit
e345ccf539
1 changed files with 32 additions and 63 deletions
  1. +32
    -63
      src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java

+ 32
- 63
src/tests/junit/org/apache/tools/ant/taskdefs/optional/junit/JUnitReportTest.java View File

@@ -26,6 +26,7 @@ import static org.junit.Assert.fail;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException;
import java.net.URL; import java.net.URL;


import org.apache.tools.ant.BuildFileRule; import org.apache.tools.ant.BuildFileRule;
@@ -62,12 +63,33 @@ public class JUnitReportTest {
} }


public void assertIndexCreated() { 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 @Test
public void testEmptyFile() throws Exception { public void testEmptyFile() throws Exception {
@@ -111,44 +133,21 @@ public class JUnitReportTest {
@Test @Test
public void testSpecialSignsInSrcPath() throws Exception { public void testSpecialSignsInSrcPath() throws Exception {
buildRule.executeTarget("testSpecialSignsInSrcPath"); 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 @Test
public void testSpecialSignsInHtmlPath() throws Exception { public void testSpecialSignsInHtmlPath() throws Exception {
buildRule.executeTarget("testSpecialSignsInHtmlPath"); buildRule.executeTarget("testSpecialSignsInHtmlPath");
File reportFile = new File(buildRule.getOutputDir(), "html# $%\u00A7&-!report/index.html"); 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 //Bugzilla Report 39708
@Test @Test
public void testWithStyleFromDir() throws Exception { public void testWithStyleFromDir() throws Exception {
buildRule.executeTarget("testWithStyleFromDir"); 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 //Bugzilla Report 40021
@@ -156,56 +155,26 @@ public class JUnitReportTest {
public void testNoFrames() throws Exception { public void testNoFrames() throws Exception {
buildRule.executeTarget("testNoFrames"); buildRule.executeTarget("testNoFrames");
File reportFile = new File(buildRule.getOutputDir(), "html/junit-noframes.html"); 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 //Bugzilla Report 39708
@Test @Test
public void testWithStyleFromDirAndXslImport() throws Exception { public void testWithStyleFromDirAndXslImport() throws Exception {
buildRule.executeTarget("testWithStyleFromDirAndXslImport"); 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 @Test
public void testWithStyleFromClasspath() throws Exception { public void testWithStyleFromClasspath() throws Exception {
buildRule.executeTarget("testWithStyleFromClasspath"); 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 @Test
public void testWithParams() throws Exception { public void testWithParams() throws Exception {
buildRule.executeTarget("testWithParams"); buildRule.executeTarget("testWithParams");
assertContains("key1=value1,key2=value2", buildRule.getLog()); 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();
} }
} }

Loading…
Cancel
Save