Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2011 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.version;
 
Works as factory for creating instances whose implementation depends on the JUnit-version currently in use.

Author(s):
Henrik Kaipe
 
 public class Version {
 
     private enum JUnitVersion {
         JUNIT_4_5_OR_LATER, JUNIT_4_4, JUNIT_4_3_OR_EARLIER;
     }
 
     /* The initial value is supposed to be the best but will be changed in
      * case it does not work properly */
 
     private static final Constructor<? extends Runnerjunit4Runner = runConstr(
             "org.junit.runners.JUnit4",
             .,
             "org.junit.internal.runners.TestClassRunner");
     private static final Constructor<? extends Runnerjunit38RunnerrunConstr(
             "org.junit.internal.runners.JUnit38ClassRunner",
             .,
             "org.junit.internal.runners.OldTestClassRunner");
     private static final Constructor<? extends Runnerjunit38SuiteRunner =
             runConstr("org.junit.runners.AllTests"nullnull);
 
     private static final Constructor<? extends RunnerenumRunner =
              != .
             ? null
             : runConstr(EnumRunner.class.getName(), nullnull);
 
     private static Constructor<? extends RunnerrunConstr(String desiredClass,
             JUnitVersion backupVersionString backupClass) {
         try {
             try {
                 return Class.forName(desiredClass).asSubclass(Runner.class)
                         .getConstructor(Class.class);
             } catch (ClassNotFoundException ex) {
                  = backupVersion;
                 try {
                     return Class.forName(backupClass).asSubclass(Runner.class)
                             .getConstructor(Class.class);
                 } catch (ClassNotFoundException ex1) {
                     throw new Error(ex1);
                 }
             }
         } catch (NoSuchMethodException ex) {
             throw new Error(ex);
         }
     }
 
     private static Runner newRunner(
             Constructor<? extends RunnerconstrClass<?> testClass) {
         try {
             try {
                 return constr.newInstance(testClass);
             } catch (InvocationTargetException x) {
                 throw x.getTargetException();
             }
         } catch (Throwable x) {
             if (x instanceof RuntimeException) {
                 throw (RuntimeException)x;
             } else if (x instanceof Error) {
                 throw (Error)x;
             } else {
                 throw new Error(x);
             }
         }
     }
 
     public static Runner newJunit4Runner(Class<?> testClass) {
         return newRunner(testClass);
     }
    public static Runner newJunit38Runner(Class<?> testClass) {
        return newRunner(testClass);
    }
    public static Runner newJunit38SuiteRunner(Class<?> testClass) {
        return newRunner(testClass);
    }
    public static Runner newEnumRunner(Class<? extends EnumtestClass) {
        switch () {
            case :
                return newRunner(testClass);
            default:
                String runnerName = testClass
                        .getAnnotation(RunWith.class).value().getName();
                throw new Error(
                        runnerName.substring(runnerName.lastIndexOf('.') + 1) +
                        " needs JUnit-4.5 or later to run an enum test-class");
        }
    }
    public static CallbackRunListener newRunListener(RunNotifier notifier) {
        switch () {
            default:
                return new CallbackRunListener_4_4_OrEarlier(notifier);
            case :
                return new CallbackRunListener_4_5(notifier);
        }
    }
New to GrepCode? Check out our FAQ X