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.junit4;
 
 import java.util.List;
 import java.util.Map;

Author(s):
Henrik Kaipe
 
 abstract class RunWrapper extends Runner {
 
     final Map<String,StringnoargProxyMethodMap = new HashMap<String,String>();
     Class<?> reloadedTestClass;
 
     static RunWrapper newInstance(Class<?> testClass)
     throws ClassNotFoundException {
         Junit3RunWrapper wrapper38 = new Junit3RunWrapper();
         if (wrapper38.tryInitTestClass(testClass)) {
             return wrapper38;
         } else {
             return new Junit4RunWrapper(testClass);
         }
     }
 
     public final Description getDescription() {
         return createRunner().getDescription();
     }
 
     public final void run(final RunNotifier notifier) {
         CallbackRunListener callbackRunListener =
                 Version.newRunListener(notifier);
         callbackRunListener.setNoargProxyMethods();
         callbackRunListener.setTestClass();
         run(callbackRunListener);
     }
 
     void run(final CallbackRunListener callbackRunListener) {
         CallbackTestClassReloader classReloader = (CallbackTestClassReloader)
                 .getClassLoader();
 
         RunNotifier outerNotifier = new RunNotifier();
         outerNotifier.addListener(callbackRunListener);
 
         Collection callbackRecords = CallbackRecordsFactory.getInstance()
                 .collectCallbackRecordsReflectively();
         for (final Object uncastedSingleRecord : callbackRecords) {
             final List currentRecord;
             if (uncastedSingleRecord instanceof Object[]) {
                 currentRecord = new ArrayList(
                         ((Object[])uncastedSingleRecord).length);
                 Collections.addAll(currentRecord,
                         (Object[])uncastedSingleRecord);
             } else {
                 currentRecord = new ArrayList((Collection)uncastedSingleRecord);
             }
 
             classReloader.setupTestRunCallbackRecord(currentRecord);
             callbackRunListener.setCurrentRecordString(currentRecord.toString());
             runWithCtxClassLoader(outerNotifier);
         }
     }
 
     private void runWithCtxClassLoader(final RunNotifier outerNotifier) {
         try {
 
             ThreadContext.runWithCtxClassLoader(
                     .getClassLoader(),
                     new ThreadContext.Testrun() {
 
                 public void executeTestrun() throws Throwable {
                     createRunner().run(outerNotifier);
                }
            });
        } catch (Throwable x) {
            if (x instanceof RuntimeException) {
                throw (RuntimeException)x;
            } else if (x instanceof Error) {
                throw (Error)x;
            } else {
                throw new Error(x);
            }
        }
    }
    final Runner createRunner() {
        WrappedRunner wrappedRunner =
                .getAnnotation(WrappedRunner.class);
        if (null == wrappedRunner) {
            return createDefaultRunner();
        } else {
            try {
                return wrappedRunner.value().getConstructor(Class.class)
                        .newInstance();
            } catch (InvocationTargetException ex) {
                Throwable t = ex.getTargetException();
                if (t instanceof RuntimeException) {
                    throw (RuntimeException)t;
                } else if (t instanceof Error) {
                    throw (Error)t;
                } else {
                    throw new Error(t);
                }
            } catch (Exception x) {
                throw new Error(x);
            }
        }
    }
    abstract Runner createDefaultRunner();
New to GrepCode? Check out our FAQ X