Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.callbackparams.junit3;
 
 import java.util.Map;
TestSuite implementation that could be useful under JUnit-3.8.x when CallbackTestCase cannot be subclassed, such as when it is desired to subclass another third-party TestCase implementation, e.g. MockObjectTestCase in JMock-1.x.

Example usage ...
 public class MyTest extends MockObjectTestCase {

     private CallbackInterface foo;
 
     //
     // ... Test-logic and param-values goes here ...
     //

     public static Test suite() {
         return new CallbackTestSuite(MyTest.class);
     }
 }
 
To get an idea of how the above actually works under JUnit-3.8.x this article might shed some light.

Upgrade to JUnit-4.x and usage of CallbackParamsRunner is strongly recommended! Thereafter the above test-example should be refactored to ...
 @RunWith(CallbackParamsRunner.class)
 public class MyTest extends MockObjectTestCase {

     @CallbackField
     private CallbackInterface foo;

     //
     // ... Test-logic and param-values goes here ...
     //
 }
 
I.e. method suite() is replaced by the class-annotation @RunWith(CallbackParamsRunner.class) and the callback-injected field foo is annotated with @CallbackField. (Trying to use RunWith(CallbackParamsRunner.class) while still having suite() return a CallbackTestSuite instance will result in undefined incorrect behaviour.)

Author(s):
Henrik Kaipe
 
 public class CallbackTestSuite extends TestSuite {

    
Special constructor that allows the user to specify the org.callbackparams.combine.reflect.CallbackRecordsFactory instance. This API is pretty much CallbackTestSuite's equivalent for CallbackTestCase.getCallbackRecordsFactory().

 
     public CallbackTestSuite(
             final Class testClassfinal CallbackRecordsFactory factory) {
         Map/*String,String*/ newTestNamesMap = new HashMap();
         Class rebytedTestClass = rebyteTestClass(testClassnewTestNamesMap);
 
         TestInstanceFactory tif = new TestInstanceFactory(rebytedTestClass);
         for (Iterator iter = factory
                 .collectCallbackRecordsReflectively(rebytedTestClass).iterator()
                 ; iter.hasNext() ;) {
             final Object record = iter.next();
            final Method[] publicMethods = rebytedTestClass.getMethods();
            for (int i = 0 ; i < publicMethods.length ; ++i) {
                final Method m = publicMethods[i];
                if (void.class != m.getReturnType()
                        || Modifier.isStatic(m.getModifiers())
                        || 0 != m.getParameterTypes().length
                        || false == m.getName().startsWith("test")) {
                    continue;
                }
                String noargTestMethodName = m.getName();
                String actualTestMethodName =
                        newTestNamesMap.containsKey(noargTestMethodName)
                        ? (StringnewTestNamesMap.get(noargTestMethodName)
                        : noargTestMethodName;
                addTest(tif.newTestrun(
                        noargTestMethodNameactualTestMethodNamerecord));
            }
        }
    }
    public CallbackTestSuite(final Class testClass) {
        this(testClass, CallbackRecordsFactory.getInstance());
    }
    static Class rebyteTestClass(
            final Class testClassfinal Map newTestNamesMap) {
        RebyteInfo crInfo = new RebyteInfo() {
            public boolean isCallbackProxiedMethod(Method m) {
                return null != newTestNamesMap
                        && super.isCallbackProxiedMethod(m);
            }
            public void putNoargProxyMethod(
                    String proxyMethodNameMethod callbackProxiedMethod) {
                newTestNamesMap.put(proxyMethodName,
                        callbackProxiedMethod.getName());
            }
        };
        CallbackTestClassReloader classReloader = CallbackTestClassReloader
                .newInstance(testClassnullcrInfo);
        try {
            Class reloadedClass = classReloader.loadClass(testClass.getName());
            classReloader.getCallbackContext();
            return reloadedClass;
        } catch (ClassNotFoundException x) {
            throw new Error(x);
        }
    }
New to GrepCode? Check out our FAQ X