KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > iftc > AntJUnitEUTest


1 /*
2  * @(#)JUnitTestSuiteEUTest.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.junit.v1.iftc;
28
29 import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
30 import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
31
32 import javax.xml.parsers.DocumentBuilder JavaDoc;
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36 import org.w3c.dom.Node JavaDoc;
37 import org.w3c.dom.Element JavaDoc;
38
39 import java.io.*;
40
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
44 import junit.framework.TestResult;
45
46 import java.io.IOException JavaDoc;
47 import java.lang.reflect.Method JavaDoc;
48
49
50 /**
51  * Tests the functionality of the Ant JUnit optional tasks for what is
52  * expected in operation of the naming facilities.
53  *
54  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
55  * @since December 8, 2002
56  * @version $Date: 2003/02/10 22:52:21 $
57  */

58 public class AntJUnitEUTest extends TestCase
59 {
60     //-------------------------------------------------------------------------
61
// Standard JUnit Class-specific declarations
62
private static final Class JavaDoc THIS_CLASS = AntJUnitEUTest.class;
63     private static final org.apache.log4j.Logger LOG =
64         org.apache.log4j.Logger.getLogger( THIS_CLASS );
65     
66     private static class MyTest extends TestCase
67     {
68         public MyTest( String JavaDoc n ) { super( n ); }
69     }
70     
71     public AntJUnitEUTest( String JavaDoc name )
72     {
73         super( name );
74     }
75
76
77     //-------------------------------------------------------------------------
78
// Tests
79

80     
81     public void testNoTests1() throws Exception JavaDoc
82     {
83         MyTest mt[] = {};
84         assertTestGroup( "abc", mt );
85     }
86     
87     
88     public void testSet1() throws Exception JavaDoc
89     {
90         MyTest mt[] = {
91                 new MyTest( "t1" ),
92                 new MyTest( "t2" ),
93             };
94         assertTestGroup( "q.w.e.r.t.y.abc", mt );
95     }
96     
97     
98     public void testSet2() throws Exception JavaDoc
99     {
100         MyTest mt[] = {
101                 new MyTest( "A.t1[1]" ),
102                 new MyTest( "B.t2[1]" ),
103             };
104         assertTestGroup( "q.w.e.r.t.y.abc", mt );
105     }
106     
107     
108     public void testSet3() throws Exception JavaDoc
109     {
110         MyTest mt[] = {
111                 new MyTest( "A.t1[1]" ),
112                 new MyTest( "B.t2[1]" ),
113                 new MyTest( "A.t1[1]" ), // copy of first test name
114
};
115         assertTestGroup( "q.w.e.r.t.y.abc", mt );
116     }
117     
118     
119     //-------------------------------------------------------------------------
120
// Helpers
121

122     protected XMLJUnitResultFormatter createFormatter(
123             ByteArrayOutputStream baos )
124     {
125         assertNotNull( baos );
126         XMLJUnitResultFormatter rf = new XMLJUnitResultFormatter();
127         rf.setOutput( baos );
128         return rf;
129     }
130     
131     protected Document JavaDoc parseXML( String JavaDoc xmlDoc )
132             throws Exception JavaDoc
133     {
134         LOG.info("Parsing XML: "+xmlDoc);
135         InputStream is = new ByteArrayInputStream( xmlDoc.getBytes() );
136         Document JavaDoc doc = DocumentBuilderFactory.newInstance().
137             newDocumentBuilder().parse( is );
138         is.close();
139         return doc;
140     }
141     
142     protected Element JavaDoc getTestsuiteElement( String JavaDoc xmlDoc )
143             throws Exception JavaDoc
144     {
145         Document JavaDoc doc = parseXML( xmlDoc );
146         NodeList JavaDoc nl = doc.getElementsByTagName( "testsuite" );
147         assertNotNull( "null node list.", nl );
148         assertTrue( "empty node list.", nl.getLength() > 0 );
149         Node JavaDoc node = nl.item( 0 );
150         assertNotNull( "null node 0.", node );
151         return (Element JavaDoc)node;
152     }
153     
154     protected Element JavaDoc[] getTestcaseElements( Element JavaDoc suite )
155             throws Exception JavaDoc
156     {
157         NodeList JavaDoc testNodes = suite.getElementsByTagName( "testcase" );
158         if (testNodes == null)
159         {
160             LOG.warn( "Null node list of testcase elements." );
161             return new Element JavaDoc[0];
162         }
163         int len = testNodes.getLength();
164         Element JavaDoc el[] = new Element JavaDoc[ len ];
165         for (int i = 0; i < len; ++i)
166         {
167             el[ i ] = (Element JavaDoc)testNodes.item( i );
168             LOG.debug( "Found testcase node "+el[i] );
169         }
170         return el;
171     }
172     
173     protected void assertTestGroup( String JavaDoc testsuite, MyTest t[] )
174             throws Exception JavaDoc
175     {
176         assertNotNull( "Null test array", t );
177         
178         ByteArrayOutputStream baos = new ByteArrayOutputStream();
179         XMLJUnitResultFormatter rf = createFormatter( baos );
180         JUnitTest jt = new JUnitTest( testsuite );
181         rf.startTestSuite( jt );
182         for (int i = 0; i < t.length; ++i)
183         {
184             rf.startTest( t[i] );
185             rf.endTest( t[i] );
186         }
187         rf.endTestSuite( jt );
188         
189         String JavaDoc xml = new String JavaDoc( baos.toByteArray() );
190         baos.close();
191         Element JavaDoc suiteEl = getTestsuiteElement( xml );
192         
193         assertEquals(
194             "Incorrect test suite name in XML document.",
195             testsuite,
196             suiteEl.getAttribute( "name" ) );
197         
198         Element JavaDoc cases[] = getTestcaseElements( suiteEl );
199         int casesFound = 0;
200         for (int i = 0; i < t.length; ++i)
201         {
202             MyTest mt = t[i];
203             String JavaDoc mtName = mt.getName();
204             boolean found = false;
205             for (int j = 0; j < cases.length; ++j)
206             {
207                 if (cases[j] != null)
208                 {
209                     String JavaDoc name = cases[j].getAttribute( "name" );
210                     LOG.debug( "Checking test '"+mtName+
211                         "' against xml element named '"+name+"'." );
212                     if (mtName.equals( name ))
213                     {
214                         cases[j] = null;
215                         found = true;
216                         ++casesFound;
217                         break;
218                     }
219                 }
220             }
221             assertTrue(
222                 "Did not find a testcase XML element for test '"+
223                     t[i].getName()+"'.",
224                 found );
225         }
226         // check that all cases were found
227
assertEquals(
228             "There were more testcases in the XML than were registered.",
229             t.length,
230             casesFound );
231     }
232     
233     
234     //-------------------------------------------------------------------------
235
// Standard JUnit declarations
236

237     
238     public static Test suite()
239     {
240         TestSuite suite = new TestSuite( THIS_CLASS );
241         
242         return suite;
243     }
244     
245     public static void main( String JavaDoc[] args )
246     {
247         String JavaDoc[] name = { THIS_CLASS.getName() };
248         
249         // junit.textui.TestRunner.main( name );
250
// junit.swingui.TestRunner.main( name );
251

252         junit.textui.TestRunner.main( name );
253     }
254     
255     
256     /**
257      *
258      * @exception Exception thrown under any exceptional condition.
259      */

260     protected void setUp() throws Exception JavaDoc
261     {
262         super.setUp();
263         
264         // set ourself up
265
}
266     
267     
268     /**
269      *
270      * @exception Exception thrown under any exceptional condition.
271      */

272     protected void tearDown() throws Exception JavaDoc
273     {
274         // tear ourself down
275

276         
277         super.tearDown();
278     }
279 }
280
281
Popular Tags