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;
Parameterized tests for JUnit-3.8.x can be created by subclassing this class instead of junit.framework.TestCase.
Upgrade to JUnit-4.x and usage of CallbackParamsRunner is strongly recommended! However, that does not necessarily mean quit using the JUnit-3.x API since JUnit-4.x is backward compatible and CallbackParamsRunner also can run JUnit-3.x tests.

Thou somewhat ugly, having the test-class subclass this class does not conflict with using CallbackParamsRunner to run the test. The only differences would be occasionally better performance and that only @CallbackField-annotated fields are callback-injected while the alternative combine API that is unlocked by overriding getCallbackRecords() or getCallbackRecordsFactory() would still be available.

Author(s):
Henrik Kaipe
 
 public abstract class CallbackTestCase extends TestCase {
 
     private static final Collection dummyRecord = Arrays.asList(new Object[0]);

    
Instantiation of rebyted test-class will fail if no callback-record is set right away.
 
     {
         if (isRebytedTestClass()
                 && null == TestrunCallbacks.getCurrentCallbackRecord()) {
             TestrunCallbacks.setCallbackRecord();
         }
     }
 
     protected CallbackTestCase(String name) {super(name);}
     protected CallbackTestCase() {}
 
     private boolean isRebytedTestClass() {
         return TestrunCallbacks.class.isInstance(this);
     }
 
     private boolean isTestrun() {
         return isRebytedTestClass()
                 &&  != TestrunCallbacks.getCurrentCallbackRecord();
     }

    
Dummy test-method that does nothing. When the test-runner tries to run this test it is intercepted by the callback framework and test-methods with callback parameters are run instead.
 
     final public void testSpecialDummyTestMethodThatIsUsedToTrickJunitOnly() {
         throw new AssertionError("This method is not supposed to be called!");
     }
     
     private boolean isInterceptionOfCallbackTestRun() {
         if ("testSpecialDummyTestMethodThatIsUsedToTrickJunitOnly"
                 .equals(getName())) {
             return true;
 
         } else {
             return false;
         }
     }
 
     private Class rebyteTestClass(
            final Map/*String,String*/ newMethodNameMappings) {
        return CallbackTestSuite.rebyteTestClass(
                getClass(), newMethodNameMappings);
    }
    public void run(final TestResult result) {
        if (isTestrun()) {
            super.run(result);
        } else if (isRebytedTestClass()) {
            if (isInterceptionOfCallbackTestRun()) {
                /* This can occur if a CallbackTestCase-test has been annotated
                 * with @RunWith(CallbackParamsRunner.class), which happens to
                 * be the recommended way to run tests.
                 * It happens when the test-runner tries to run the test-method
                 * testSpecialDummyTestMethodThatIsUsedToTrickJunitOnly(), which
                 * for known reasons is not a test-method to be run ...
                 */
                return;
            }
            TestInstanceFactory factory = new TestInstanceFactory(getClass());
            for (Iterator i = getCallbackRecords().iterator() ; i.hasNext() ;) {
                final Object testRunCallbackRecord = i.next();
                try {
                    factory.newTestrun(getName(), testRunCallbackRecord)
                            .run(result);
                } finally {
                    TestrunCallbacks.setCallbackRecord();
                }
            }
        } else if (isInterceptionOfCallbackTestRun()) {
            Map/*String,String*/ newTestNamesMap = new HashMap();
            final Class rebytedTestClass = rebyteTestClass(newTestNamesMap);
            for (Iterator i = newTestNamesMap.entrySet().iterator()
                    ; i.hasNext() ;) {
                final Map.Entry/*String,String*/ proxyMethodEntr =
                        (Map.Entryi.next();
                String proxyMethodTestName =
                        TestInstanceFactory.asTestName(proxyMethodEntr);
                TestSuite.createTest(rebytedTestClassproxyMethodTestName)
                        .run(result);
            }
        } else {
            TestSuite.createTest(rebyteTestClass(null), getName()).run(result);
        }
    }
    public int countTestCases() {
        final int countBase = super.countTestCases();
        if (isTestrun()) {
            return countBase;
        } else if (isRebytedTestClass()) {
            return countBase * getCallbackRecords().size();
        } else if (isInterceptionOfCallbackTestRun()) {
            Map tmp = new HashMap();
            rebyteTestClass(tmp);
            return countBase * tmp.size() * getCallbackRecords().size();
        } else {
            return countBase * getCallbackRecords().size();
        }
    }

    
Returns a collection of callback-records, i.e. Object[] or Collection instances that contain all callback-values for a particular test-run. This method can be overridden to test specific callback-records.
The default implementation uses the org.callbackparams.combine.reflect.CallbackRecordsFactory supplied by the overridable method getCallbackRecordsFactory() to create the callback-records.

Please note that overriding this method is a non-static and JDK-1.4 compatible alternative to the using the annotation @CallbackRecords.

    protected Collection getCallbackRecords() {
        return getCallbackRecordsFactory()
                .collectCallbackRecordsReflectively(getClass());
    }
    
    
Returns the org.callbackparams.combine.reflect.CallbackRecordsFactory instance that will be used by the default implementation of getCallbackRecords() to create the callback-records. The default CallbackRecordsFactory instance returned is the one supplied by the factory method org.callbackparams.combine.reflect.CallbackRecordsFactory.getInstance(). If this default factory won't fit your purposes you can override this method.
Overriding this method is a particularly interesting alternative if you are stuck with JDK-1.4 and therefore cannot use annotations to instruct the default factory. E.g. you could then have your factory implementation override the method org.callbackparams.combine.reflect.CallbackRecordsFactory.retrieveCombineStrategy(java.lang.Class) and let it return a org.callbackparams.combine.CombineCompletely instance instead of the default org.callbackparams.combine.CombineAllPossible2Combinations instance.

Overriding this method is a non-static and JDK-1.4 compatible alternative to configuring a combine-strategy by using the annotation @CombineConfig. An interesting JDK-1.4 oppertunity is to override org.callbackparams.combine.reflect.CallbackRecordsFactory.isCallbackValuesFactoryClass(java.lang.Class) and org.callbackparams.combine.reflect.CallbackRecordsFactory.retrieveCallbackValuesArray(java.lang.Class), e.g. to make it recognize Enum-classes a'la commons-lang ...

        return CallbackRecordsFactory.getInstance();
    }
New to GrepCode? Check out our FAQ X