001: /*
002: * @(#)JUnitTestSuiteEUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.junit.v1.iftc;
028:
029: import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
030: import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
031:
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.NodeList;
036: import org.w3c.dom.Node;
037: import org.w3c.dom.Element;
038:
039: import java.io.*;
040:
041: import junit.framework.Test;
042: import junit.framework.TestCase;
043: import junit.framework.TestSuite;
044: import junit.framework.TestResult;
045:
046: import java.io.IOException;
047: import java.lang.reflect.Method;
048:
049: /**
050: * Tests the functionality of the Ant JUnit optional tasks for what is
051: * expected in operation of the naming facilities.
052: *
053: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
054: * @since December 8, 2002
055: * @version $Date: 2003/02/10 22:52:21 $
056: */
057: public class AntJUnitEUTest extends TestCase {
058: //-------------------------------------------------------------------------
059: // Standard JUnit Class-specific declarations
060: private static final Class THIS_CLASS = AntJUnitEUTest.class;
061: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
062: .getLogger(THIS_CLASS);
063:
064: private static class MyTest extends TestCase {
065: public MyTest(String n) {
066: super (n);
067: }
068: }
069:
070: public AntJUnitEUTest(String name) {
071: super (name);
072: }
073:
074: //-------------------------------------------------------------------------
075: // Tests
076:
077: public void testNoTests1() throws Exception {
078: MyTest mt[] = {};
079: assertTestGroup("abc", mt);
080: }
081:
082: public void testSet1() throws Exception {
083: MyTest mt[] = { new MyTest("t1"), new MyTest("t2"), };
084: assertTestGroup("q.w.e.r.t.y.abc", mt);
085: }
086:
087: public void testSet2() throws Exception {
088: MyTest mt[] = { new MyTest("A.t1[1]"), new MyTest("B.t2[1]"), };
089: assertTestGroup("q.w.e.r.t.y.abc", mt);
090: }
091:
092: public void testSet3() throws Exception {
093: MyTest mt[] = { new MyTest("A.t1[1]"), new MyTest("B.t2[1]"),
094: new MyTest("A.t1[1]"), // copy of first test name
095: };
096: assertTestGroup("q.w.e.r.t.y.abc", mt);
097: }
098:
099: //-------------------------------------------------------------------------
100: // Helpers
101:
102: protected XMLJUnitResultFormatter createFormatter(
103: ByteArrayOutputStream baos) {
104: assertNotNull(baos);
105: XMLJUnitResultFormatter rf = new XMLJUnitResultFormatter();
106: rf.setOutput(baos);
107: return rf;
108: }
109:
110: protected Document parseXML(String xmlDoc) throws Exception {
111: LOG.info("Parsing XML: " + xmlDoc);
112: InputStream is = new ByteArrayInputStream(xmlDoc.getBytes());
113: Document doc = DocumentBuilderFactory.newInstance()
114: .newDocumentBuilder().parse(is);
115: is.close();
116: return doc;
117: }
118:
119: protected Element getTestsuiteElement(String xmlDoc)
120: throws Exception {
121: Document doc = parseXML(xmlDoc);
122: NodeList nl = doc.getElementsByTagName("testsuite");
123: assertNotNull("null node list.", nl);
124: assertTrue("empty node list.", nl.getLength() > 0);
125: Node node = nl.item(0);
126: assertNotNull("null node 0.", node);
127: return (Element) node;
128: }
129:
130: protected Element[] getTestcaseElements(Element suite)
131: throws Exception {
132: NodeList testNodes = suite.getElementsByTagName("testcase");
133: if (testNodes == null) {
134: LOG.warn("Null node list of testcase elements.");
135: return new Element[0];
136: }
137: int len = testNodes.getLength();
138: Element el[] = new Element[len];
139: for (int i = 0; i < len; ++i) {
140: el[i] = (Element) testNodes.item(i);
141: LOG.debug("Found testcase node " + el[i]);
142: }
143: return el;
144: }
145:
146: protected void assertTestGroup(String testsuite, MyTest t[])
147: throws Exception {
148: assertNotNull("Null test array", t);
149:
150: ByteArrayOutputStream baos = new ByteArrayOutputStream();
151: XMLJUnitResultFormatter rf = createFormatter(baos);
152: JUnitTest jt = new JUnitTest(testsuite);
153: rf.startTestSuite(jt);
154: for (int i = 0; i < t.length; ++i) {
155: rf.startTest(t[i]);
156: rf.endTest(t[i]);
157: }
158: rf.endTestSuite(jt);
159:
160: String xml = new String(baos.toByteArray());
161: baos.close();
162: Element suiteEl = getTestsuiteElement(xml);
163:
164: assertEquals("Incorrect test suite name in XML document.",
165: testsuite, suiteEl.getAttribute("name"));
166:
167: Element cases[] = getTestcaseElements(suiteEl);
168: int casesFound = 0;
169: for (int i = 0; i < t.length; ++i) {
170: MyTest mt = t[i];
171: String mtName = mt.getName();
172: boolean found = false;
173: for (int j = 0; j < cases.length; ++j) {
174: if (cases[j] != null) {
175: String name = cases[j].getAttribute("name");
176: LOG.debug("Checking test '" + mtName
177: + "' against xml element named '" + name
178: + "'.");
179: if (mtName.equals(name)) {
180: cases[j] = null;
181: found = true;
182: ++casesFound;
183: break;
184: }
185: }
186: }
187: assertTrue("Did not find a testcase XML element for test '"
188: + t[i].getName() + "'.", found);
189: }
190: // check that all cases were found
191: assertEquals(
192: "There were more testcases in the XML than were registered.",
193: t.length, casesFound);
194: }
195:
196: //-------------------------------------------------------------------------
197: // Standard JUnit declarations
198:
199: public static Test suite() {
200: TestSuite suite = new TestSuite(THIS_CLASS);
201:
202: return suite;
203: }
204:
205: public static void main(String[] args) {
206: String[] name = { THIS_CLASS.getName() };
207:
208: // junit.textui.TestRunner.main( name );
209: // junit.swingui.TestRunner.main( name );
210:
211: junit.textui.TestRunner.main(name);
212: }
213:
214: /**
215: *
216: * @exception Exception thrown under any exceptional condition.
217: */
218: protected void setUp() throws Exception {
219: super .setUp();
220:
221: // set ourself up
222: }
223:
224: /**
225: *
226: * @exception Exception thrown under any exceptional condition.
227: */
228: protected void tearDown() throws Exception {
229: // tear ourself down
230:
231: super.tearDown();
232: }
233: }
|