1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* @(#)InterfaceTestSuite.java
*
* Copyright (C) 2002-2003 Matt Albrecht
* groboclown@users.sourceforge.net
* http://groboutils.sourceforge.net
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package net.sourceforge.groboutils.junit.v1.iftc;
import junit.framework.Test;
import junit.framework.TestSuite;
import java.util.Enumeration;
import java.util.Vector;
import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
import net.sourceforge.groboutils.junit.v1.parser.TestClassParser;
import net.sourceforge.groboutils.junit.v1.parser.TestClassCreator;
import net.sourceforge.groboutils.junit.v1.parser.ITestCreator;
import net.sourceforge.groboutils.junit.v1.parser.DelegateTestCreator;
import net.sourceforge.groboutils.junit.v1.parser.JUnitOrigCreator;
import net.sourceforge.groboutils.junit.v1.parser.JUnit3_8Creator;
import net.sourceforge.groboutils.junit.v1.parser.IftcOrigCreator;
import org.apache.log4j.Logger;
/**
* Allows for tests to be written on interfaces or abstract classes. These
* must be run through an InterfaceTestSuite to have the implemented object
* be set correctly.
* <P>
* This class extends <tt>TestSuite</tt> only for the purpose of being a testing
* repository. The act of parsing TestCase classes is delegated to
* new <tt>TestSuite</tt> instances. A new instance will be created for each
* test method (just as <tt>TestSuite</tt> does), If a <tt>TestCase</tt> class
* has a constructor which is of the form <tt>( String, ImplFactory )</tt>,
* then each test method instance will be created
* once for each known <tt>ImplFactory</tt> object; these will be
* stored and executed through the <tt>ImplFactory</tt> class. All other
* classes will be added just as TestSuite does (the standard method).
* <P>
* The creation of test instances is delayed until the tests are actually
* retrieved via the <tt>testAt()</tt>, <tt>tests()</tt>, and
* <tt>testCount()</tt> methods. Therefore, adding new Classes and
* ImplFactory instances after the creation time will cause an error, due to
* problems with <tt>addTest()</tt> (they cannot be removed).
* <P>
* Currently, this class is slow: it does not do smart things like cache
* results from inspection on the same class object.
*
* @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
* @version $Date: 2003/02/10 22:52:20 $
* @since March 2, 2002
* @see InterfaceTestCase
* @see ImplFactory
* @see junit.framework.TestSuite
*/
public class InterfaceTestSuite extends TestSuite
{
private static final Logger LOG = Logger.getLogger(
InterfaceTestSuite.class );
// these are not private for test-case usage.
Vector creators = new Vector();
Vector classes = new Vector();
/**
* Constructs a TestSuite from the given class, and sets the initial
* set of creators. Adds all the methods
* starting with "test" as test cases to the suite.
*/
public InterfaceTestSuite()
{
// do nothing
}
/**
* Constructs a TestSuite from the given class, and sets the initial
* set of creators. Adds all the methods
* starting with "test" as test cases to the suite.
*
* @param theClass the class under inspection
*/
public InterfaceTestSuite( Class theClass )
{
addTestSuite( theClass );
}
/**
* Constructs a TestSuite from the given class, and sets the initial
* set of creators. Adds all the methods
* starting with "test" as test cases to the suite.
*
* @param theClass the class under inspection
* @param f a factory to add to this suite.
*/
public InterfaceTestSuite( Class theClass, ImplFactory f )
{
addTestSuite( theClass );
addFactory( f );
}
/**
* Add a new Implementation factory to the suite. This should only be
* called before any tests are extracted from this suite. If it is
* called after, then an IllegalStateException will be generated.
*
* @param f a factory to add to this suite.
* @exception IllegalArgumentException if <tt>f</tt> is <tt>null</tt>
* @exception IllegalStateException if the tests have already been generated
*/
public void addFactory( ImplFactory f )
{
if (f == null)
{
throw new IllegalArgumentException("no null args");
}
if (creators == null)
{
throw new IllegalStateException("Already created TestSuites.");
}
this.creators.addElement( f );
}
/**
* Add an array of new Implementation factories to the suite.
* This should only be
* called before any tests are extracted from this suite.
*
* @param f a set of factories to add to this suite.
* @exception IllegalArgumentException if <tt>f</tt> is <tt>null</tt>, or
* any element in the list is <tt>null</tt>
* @exception IllegalStateException if the tests have already been generated
*/
public void addFactories( ImplFactory f[] )
{
if (f == null)
{
throw new IllegalArgumentException("no null args");
}
for (int i = 0; i < f.length; ++i)
{
addFactory( f[i] );
}
}
/**
* Add an InterfaceTestSuite to this suite. If an interface extends
* another interface, it should add it's super interface's test suite
* through this method. The same goes for any abstract or base class.
* Adding the parent suite through this method will cause both suites to
* share creators. In fact, the parent suite <b>cannot</b> have any
* factories when passed into this method, because they will be ignored.
* <P>
* This allows for the flexibility of determining whether to add a full
* test suite, without sharing factories, or not.
*
* @param t a test to add to the suite. It can be <tt>null</tt>.
*/
public void addInterfaceTestSuite( InterfaceTestSuite t )
{
if (t != null)
{
if (t.creators != null && t.classes != null && t.classes.size() > 0)
{
if (t.creators.size() > 0)
{
LOG.warn( "Passed in InterfaceTestSuite "+t+
" with factories registered. This is a no-no. "+
"You need to pass it in through addTest(), or not add "+
"factories to it." );
}
else
{
Enumeration enum = t.classes.elements();
while (enum.hasMoreElements())
{
addTestSuite( (Class)enum.nextElement() );
}
}
}
}
}
/**
* Add an array of tests to the suite.
*
* @param t a set of tests to add to this suite.
* @param IllegalArgumentException if <tt>t</tt> is <tt>null</tt>
*/
public void addTests( Test[] t )
{
if (t == null)
{
throw new IllegalArgumentException("no null arguments");
}
for (int i = 0; i < t.length; ++i)
{
addTest( t[i] );
}
}
/**
* Adds all the methods
* starting with "test" as test cases to the suite.
* <P>
* Overrides the parent implementation to allow for InterfaceTests.
*
* @param theClass the class under inspection
* @exception IllegalArgumentException if <tt>theClass</tt> is <tt>null</tt>
* @exception IllegalStateException if the tests have already been generated
*/
public void addTestSuite( Class theClass )
{
if (theClass == null)
{
throw new IllegalArgumentException("no null arguments");
}
if (this.classes == null)
{
throw new IllegalStateException("Class "+theClass.getName()+
" added after the load time. See JavaDoc for proper usage.");
}
this.classes.addElement( theClass );
}
// from parent
public Test testAt(int index)
{
loadTestSuites();
return super.testAt( index );
}
// from parent
public int testCount()
{
loadTestSuites();
return super.testCount();
}
// from parent
public Enumeration tests()
{
loadTestSuites();
return super.tests();
}
/**
* Load all the tests from the cache of classes and factories.
*/
protected void loadTestSuites()
{
// if either of these Vectors are null, then the loading has
// already been done.
if (this.creators == null || this.classes == null)
{
return;
}
ITestCreator tc = createTestCreator( this.creators );
TestClassCreator tcc = new TestClassCreator( tc );
for (Enumeration enum = this.classes.elements();
enum.hasMoreElements();)
{
Class c = (Class)enum.nextElement();
loadTestSuite( c, tcc );
}
// tell the instance to not load test suites again, and not allow
// new factories to be registered.
this.creators = null;
this.classes = null;
}
/**
* Load all the tests and warnings from the class and the creator
* type into this instance's suite of tests.
*
* @param testClass the class being inspected for test instance
* creation.
* @param tcc the creator type that will be used to create new tests.
*/
protected void loadTestSuite( Class testClass, TestClassCreator tcc )
{
TestClassParser tcp = new TestClassParser( testClass );
// ensure that all unwanted warnings are removed.
tcc.clearWarnings();
Test t[] = tcc.createTests( tcp );
if (t == null || t.length <= 0)
{
// no discovered tests, so create an error test
LOG.info( "No tests for class discovered." );
addTest( TestClassCreator.createWarningTest(
"No tests found in test class " + testClass.getName() ) );
}
else
{
addTests( t );
}
addTests( tcc.createWarningTests( tcp ) );
// be a nice citizen and clean up after ourself.
tcc.clearWarnings();
}
/**
* Create a TestCreator that contains the knowledge of how to properly
* parse and generate tests for all types of supported test classes.
*
* @param factories a vector of ImplFactory instances to load Interface
* test class instances.
* @return the new creator.
*/
protected ITestCreator createTestCreator( Vector vf )
{
ImplFactory factories[] = new ImplFactory[ vf.size() ];
vf.copyInto( factories );
// Order matters!!!
//
// Use the original version before the new technique for backwards
// compatibility.
ITestCreator tc = new DelegateTestCreator( new ITestCreator[] {
new IftcOrigCreator( factories ),
//new Iftc3_8Creator( factories ),
new JUnitOrigCreator(),
new JUnit3_8Creator()
} );
return tc;
}
}
|