1 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 ; 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.NodeList ; 36 import org.w3c.dom.Node ; 37 import org.w3c.dom.Element ; 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 ; 47 import java.lang.reflect.Method ; 48 49 50 58 public class AntJUnitEUTest extends TestCase 59 { 60 private static final Class 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 n ) { super( n ); } 69 } 70 71 public AntJUnitEUTest( String name ) 72 { 73 super( name ); 74 } 75 76 77 80 81 public void testNoTests1() throws Exception 82 { 83 MyTest mt[] = {}; 84 assertTestGroup( "abc", mt ); 85 } 86 87 88 public void testSet1() throws Exception 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 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 109 { 110 MyTest mt[] = { 111 new MyTest( "A.t1[1]" ), 112 new MyTest( "B.t2[1]" ), 113 new MyTest( "A.t1[1]" ), }; 115 assertTestGroup( "q.w.e.r.t.y.abc", mt ); 116 } 117 118 119 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 parseXML( String xmlDoc ) 132 throws Exception 133 { 134 LOG.info("Parsing XML: "+xmlDoc); 135 InputStream is = new ByteArrayInputStream( xmlDoc.getBytes() ); 136 Document doc = DocumentBuilderFactory.newInstance(). 137 newDocumentBuilder().parse( is ); 138 is.close(); 139 return doc; 140 } 141 142 protected Element getTestsuiteElement( String xmlDoc ) 143 throws Exception 144 { 145 Document doc = parseXML( xmlDoc ); 146 NodeList nl = doc.getElementsByTagName( "testsuite" ); 147 assertNotNull( "null node list.", nl ); 148 assertTrue( "empty node list.", nl.getLength() > 0 ); 149 Node node = nl.item( 0 ); 150 assertNotNull( "null node 0.", node ); 151 return (Element )node; 152 } 153 154 protected Element [] getTestcaseElements( Element suite ) 155 throws Exception 156 { 157 NodeList testNodes = suite.getElementsByTagName( "testcase" ); 158 if (testNodes == null) 159 { 160 LOG.warn( "Null node list of testcase elements." ); 161 return new Element [0]; 162 } 163 int len = testNodes.getLength(); 164 Element el[] = new Element [ len ]; 165 for (int i = 0; i < len; ++i) 166 { 167 el[ i ] = (Element )testNodes.item( i ); 168 LOG.debug( "Found testcase node "+el[i] ); 169 } 170 return el; 171 } 172 173 protected void assertTestGroup( String testsuite, MyTest t[] ) 174 throws Exception 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 xml = new String ( baos.toByteArray() ); 190 baos.close(); 191 Element suiteEl = getTestsuiteElement( xml ); 192 193 assertEquals( 194 "Incorrect test suite name in XML document.", 195 testsuite, 196 suiteEl.getAttribute( "name" ) ); 197 198 Element cases[] = getTestcaseElements( suiteEl ); 199 int casesFound = 0; 200 for (int i = 0; i < t.length; ++i) 201 { 202 MyTest mt = t[i]; 203 String 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 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 assertEquals( 228 "There were more testcases in the XML than were registered.", 229 t.length, 230 casesFound ); 231 } 232 233 234 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 [] args ) 246 { 247 String [] name = { THIS_CLASS.getName() }; 248 249 252 junit.textui.TestRunner.main( name ); 253 } 254 255 256 260 protected void setUp() throws Exception 261 { 262 super.setUp(); 263 264 } 266 267 268 272 protected void tearDown() throws Exception 273 { 274 276 277 super.tearDown(); 278 } 279 } 280 281 | Popular Tags |