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: package org.apache.tools.ant.taskdefs.optional.junit;
019:
020: import org.apache.tools.ant.BuildFileTest;
021: import java.io.BufferedReader;
022: import java.io.FileReader;
023: import java.io.IOException;
024:
025: public class JUnitTaskTest extends BuildFileTest {
026:
027: /**
028: * Constructor for the JUnitTaskTest object
029: */
030: public JUnitTaskTest(String name) {
031: super (name);
032: }
033:
034: /**
035: * The JUnit setup method
036: */
037: public void setUp() {
038: configureProject("src/etc/testcases/taskdefs/optional/junit.xml");
039: }
040:
041: /**
042: * The teardown method for JUnit
043: */
044: public void tearDown() {
045: executeTarget("cleanup");
046: }
047:
048: public void testCrash() {
049: expectPropertySet("crash", "crashed");
050: }
051:
052: public void testNoCrash() {
053: expectPropertyUnset("nocrash", "crashed");
054: }
055:
056: public void testTimeout() {
057: expectPropertySet("timeout", "timeout");
058: }
059:
060: public void testNoTimeout() {
061: expectPropertyUnset("notimeout", "timeout");
062: }
063:
064: public void testNonForkedCapture() throws IOException {
065: executeTarget("capture");
066: assertNoPrint(getLog(), "log");
067: assertNoPrint(getFullLog(), "debug log");
068: }
069:
070: public void testForkedCapture() throws IOException {
071: getProject().setProperty("fork", "true");
072: testNonForkedCapture();
073: // those would fail because of the way BuildFileTest captures output
074: assertNoPrint(getOutput(), "output");
075: assertNoPrint(getError(), "error output");
076: assertOutput();
077: }
078:
079: public void testBatchTestForkOnceToDir() {
080: assertResultFilesExist("testBatchTestForkOnceToDir", ".xml");
081: }
082:
083: /** Bugzilla Report 32973 */
084: public void testBatchTestForkOnceExtension() {
085: assertResultFilesExist("testBatchTestForkOnceExtension", ".foo");
086: }
087:
088: public void testBatchTestForkOnceCustomFormatter() {
089: assertResultFilesExist("testBatchTestForkOnceCustomFormatter",
090: "foo");
091: }
092:
093: private void assertResultFilesExist(String target, String extension) {
094: executeTarget(target);
095: assertResultFileExists("JUnitClassLoader", extension);
096: assertResultFileExists("JUnitTestRunner", extension);
097: assertResultFileExists("JUnitVersionHelper", extension);
098: }
099:
100: private void assertResultFileExists(String classNameFragment,
101: String ext) {
102: assertTrue("result for " + classNameFragment + "Test" + ext
103: + " exists", getProject().resolveFile(
104: "out/TEST-org.apache.tools.ant."
105: + "taskdefs.optional.junit."
106: + classNameFragment + "Test" + ext).exists());
107: }
108:
109: private void assertNoPrint(String result, String where) {
110: assertTrue(where + " '" + result
111: + "' must not contain print statement", result
112: .indexOf("print to System.") == -1);
113: }
114:
115: private void assertOutput() throws IOException {
116: FileReader inner = new FileReader(getProject().resolveFile(
117: "testlog.txt"));
118: BufferedReader reader = new BufferedReader(inner);
119: try {
120: String line = reader.readLine();
121: assertEquals(
122: "Testsuite: org.apache.tools.ant.taskdefs.optional.junit.Printer",
123: line);
124: line = reader.readLine();
125: assertNotNull(line);
126: assertTrue(line
127: .startsWith("Tests run: 1, Failures: 0, Errors: 0, Time elapsed:"));
128: line = reader.readLine();
129: assertEquals(
130: "------------- Standard Output ---------------",
131: line);
132: assertPrint(reader.readLine(), "static", "out");
133: assertPrint(reader.readLine(), "constructor", "out");
134: assertPrint(reader.readLine(), "method", "out");
135: line = reader.readLine();
136: assertEquals(
137: "------------- ---------------- ---------------",
138: line);
139: line = reader.readLine();
140: assertEquals(
141: "------------- Standard Error -----------------",
142: line);
143: assertPrint(reader.readLine(), "static", "err");
144: assertPrint(reader.readLine(), "constructor", "err");
145: assertPrint(reader.readLine(), "method", "err");
146: line = reader.readLine();
147: assertEquals(
148: "------------- ---------------- ---------------",
149: line);
150: line = reader.readLine();
151: assertEquals("", line);
152: line = reader.readLine();
153: assertNotNull(line);
154: assertTrue(line.startsWith("Testcase: testNoCrash took "));
155: } finally {
156: inner.close();
157: }
158: }
159:
160: private void assertPrint(String line, String from, String to) {
161: String search = from + " print to System." + to;
162: assertEquals(search, line);
163: }
164:
165: }
|