001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.junit;
020:
021: import java.io.File;
022: import java.io.FileReader;
023: import java.io.InputStream;
024: import java.net.URL;
025: import org.apache.tools.ant.BuildFileTest;
026: import org.apache.tools.ant.util.FileUtils;
027:
028: /**
029: * Small testcase for the junitreporttask.
030: * First test added to reproduce an fault, still a lot to improve
031: *
032: */
033: public class JUnitReportTest extends BuildFileTest {
034:
035: public JUnitReportTest(String name) {
036: super (name);
037: }
038:
039: protected void setUp() {
040: configureProject("src/etc/testcases/taskdefs/optional/junitreport.xml");
041: }
042:
043: protected void tearDown() {
044: executeTarget("clean");
045: }
046:
047: /**
048: * Verifies that no empty junit-noframes.html is generated when frames
049: * output is selected via the default.
050: * Needs reports1 task from junitreport.xml.
051: */
052: public void testNoFileJUnitNoFrames() {
053: executeTarget("reports1");
054: if (new File(System.getProperty("root"),
055: "src/etc/testcases/taskdefs/optional/junitreport/test/html/junit-noframes.html")
056: .exists()) {
057: fail("No file junit-noframes.html expected");
058: }
059: }
060:
061: public void assertIndexCreated() {
062: if (!new File(System.getProperty("root"),
063: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html")
064: .exists()) {
065: fail("No file index file found");
066: }
067:
068: }
069:
070: /**
071: * run a target, assert the index file is there, look for text in the log
072: * @param targetName target
073: * @param text optional text to look for
074: */
075: private void expectReportWithText(String targetName, String text) {
076: executeTarget(targetName);
077: assertIndexCreated();
078: if (text != null) {
079: assertLogContaining(text);
080: }
081: }
082:
083: public void testEmptyFile() throws Exception {
084: expectReportWithText("testEmptyFile",
085: XMLResultAggregator.WARNING_EMPTY_FILE);
086: }
087:
088: public void testIncompleteFile() throws Exception {
089: expectReportWithText("testIncompleteFile",
090: XMLResultAggregator.WARNING_IS_POSSIBLY_CORRUPTED);
091: }
092:
093: public void testWrongElement() throws Exception {
094: expectReportWithText("testWrongElement",
095: XMLResultAggregator.WARNING_INVALID_ROOT_ELEMENT);
096: }
097:
098: // Bugzilla Report 34963
099: public void testStackTraceLineBreaks() throws Exception {
100: expectReportWithText("testStackTraceLineBreaks", null);
101: FileReader r = null;
102: try {
103: r = new FileReader(
104: new File(
105: System.getProperty("root"),
106: "src/etc/testcases/taskdefs/optional/junitreport/test/html/sampleproject/coins/0_CoinTest.html"));
107: String report = FileUtils.readFully(r);
108: assertTrue(
109: "output must contain <br>",
110: report
111: .indexOf("junit.framework.AssertionFailedError: DOEG<br/>") > -1);
112: } finally {
113: FileUtils.close(r);
114: }
115: }
116:
117: // Bugzilla Report 38477
118: public void testSpecialSignsInSrcPath() throws Exception {
119: executeTarget("testSpecialSignsInSrcPath");
120: File reportFile = new File(System.getProperty("root"),
121: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html");
122: // tests one the file object
123: assertTrue("No index.html present. Not generated?", reportFile
124: .exists());
125: assertTrue("Cant read the report file.", reportFile.canRead());
126: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
127: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
128: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
129: reportFile.getAbsolutePath()));
130: InputStream reportStream = reportUrl.openStream();
131: assertTrue("This shouldnt be an empty stream.", reportStream
132: .available() > 0);
133: }
134:
135: public void testSpecialSignsInHtmlPath() throws Exception {
136: executeTarget("testSpecialSignsInHtmlPath");
137: File reportFile = new File(
138: System.getProperty("root"),
139: "src/etc/testcases/taskdefs/optional/junitreport/test/html# $%\u00A7&-!report/index.html");
140: // tests one the file object
141: assertTrue("No index.html present. Not generated?", reportFile
142: .exists());
143: assertTrue("Cant read the report file.", reportFile.canRead());
144: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
145: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
146: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
147: reportFile.getAbsolutePath()));
148: InputStream reportStream = reportUrl.openStream();
149: assertTrue("This shouldnt be an empty stream.", reportStream
150: .available() > 0);
151: }
152:
153: //Bugzilla Report 39708
154: public void testWithStyleFromDir() throws Exception {
155: executeTarget("testWithStyleFromDir");
156: File reportFile = new File(System.getProperty("root"),
157: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html");
158: // tests one the file object
159: assertTrue("No index.html present. Not generated?", reportFile
160: .exists());
161: assertTrue("Cant read the report file.", reportFile.canRead());
162: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
163: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
164: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
165: reportFile.getAbsolutePath()));
166: InputStream reportStream = reportUrl.openStream();
167: assertTrue("This shouldnt be an empty stream.", reportStream
168: .available() > 0);
169: }
170:
171: //Bugzilla Report 40021
172: public void testNoFrames() throws Exception {
173: executeTarget("testNoFrames");
174: File reportFile = new File(System.getProperty("root"),
175: "src/etc/testcases/taskdefs/optional/junitreport/test/html/junit-noframes.html");
176: // tests one the file object
177: assertTrue("No junit-noframes.html present. Not generated?",
178: reportFile.exists());
179: assertTrue("Cant read the report file.", reportFile.canRead());
180: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
181: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
182: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
183: reportFile.getAbsolutePath()));
184: InputStream reportStream = reportUrl.openStream();
185: assertTrue("This shouldnt be an empty stream.", reportStream
186: .available() > 0);
187: }
188:
189: //Bugzilla Report 39708
190: public void testWithStyleFromDirAndXslImport() throws Exception {
191: executeTarget("testWithStyleFromDirAndXslImport");
192: File reportFile = new File(System.getProperty("root"),
193: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html");
194: // tests one the file object
195: assertTrue("No index.html present. Not generated?", reportFile
196: .exists());
197: assertTrue("Cant read the report file.", reportFile.canRead());
198: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
199: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
200: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
201: reportFile.getAbsolutePath()));
202: InputStream reportStream = reportUrl.openStream();
203: assertTrue("This shouldnt be an empty stream.", reportStream
204: .available() > 0);
205: }
206:
207: public void testWithStyleFromClasspath() throws Exception {
208: executeTarget("testWithStyleFromClasspath");
209: File reportFile = new File(System.getProperty("root"),
210: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html");
211: // tests one the file object
212: assertTrue("No index.html present. Not generated?", reportFile
213: .exists());
214: assertTrue("Cant read the report file.", reportFile.canRead());
215: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
216: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
217: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
218: reportFile.getAbsolutePath()));
219: InputStream reportStream = reportUrl.openStream();
220: assertTrue("This shouldnt be an empty stream.", reportStream
221: .available() > 0);
222: }
223:
224: public void testWithParams() throws Exception {
225: expectLogContaining("testWithParams", "key1=value1,key2=value2");
226: File reportFile = new File(System.getProperty("root"),
227: "src/etc/testcases/taskdefs/optional/junitreport/test/html/index.html");
228: // tests one the file object
229: assertTrue("No index.html present. Not generated?", reportFile
230: .exists());
231: assertTrue("Cant read the report file.", reportFile.canRead());
232: assertTrue("File shouldnt be empty.", reportFile.length() > 0);
233: // conversion to URL via FileUtils like in XMLResultAggregator, not as suggested in the bug report
234: URL reportUrl = new URL(FileUtils.getFileUtils().toURI(
235: reportFile.getAbsolutePath()));
236: InputStream reportStream = reportUrl.openStream();
237: assertTrue("This shouldnt be an empty stream.", reportStream
238: .available() > 0);
239: }
240: }
|