@Override public void testRunFinished(Result result) throws Exception { super.testRunFinished(result); JCodeModel codeModel = new JCodeModel(); JDefinedClass resultClass = codeModel._class(getSuiteClassName()); JClass suiteClazz = codeModel.ref(Suite.class); resultClass.annotate(RunWith.class).param(VALUE_PARAM, suiteClazz); JClass suiteClasses = codeModel.ref(Suite.SuiteClasses.class); JAnnotationArrayMember testClassArray = resultClass.annotate(suiteClasses).paramArray(VALUE_PARAM); testClassesAndTheirTests.keySet().forEach(className -> addClassToSuite(codeModel, testClassArray, className)); resultClass.javadoc().add(getJavaDocComment()); File file = new File(getTargetDirectory()); if ( !file.exists() && !file.mkdirs() ) { throw new RuntimeException("Cannot create folder " + file.getAbsolutePath()); } codeModel.build(file); }
private boolean isTestClass(Class<?> c) { if (Modifier.isAbstract(c.getModifiers())) { return false; } if (c.getAnnotation(Suite.SuiteClasses.class) != null) { return true; } for (Method met : c.getMethods()) { if (met.getAnnotation(Test.class) != null) { return true; } } return false; }
/** * @return true if {@param cls} is {@link JUnit4} annotated. */ protected boolean isJUnit4TestClass(Class cls) { // Need to find test classes, otherwise crashes with b/11790448. if (!cls.getName().endsWith("Test")) { return false; } // Check the annotations. Annotation annotation = cls.getAnnotation(RunWith.class); if (annotation != null) { RunWith runWith = (RunWith) annotation; Object value = runWith.value(); if (value.equals(JUnit4.class) || value.equals(Suite.class)) { return true; } } return false; }
@SuppressWarnings({"rawtypes", "LoopStatementThatDoesntLoop"}) private boolean assertTestRunner(String testClass) { try { RunWith runWith = Class.forName(testClass).getAnnotation(RunWith.class); if (runWith == null) { throw new RuntimeException("Missing [@" + RunWith.class.getCanonicalName() + "(" + TestRunner.class.getCanonicalName() + ".class)] on class [" + testClass + "]"); } if (runWith.value().equals(Suite.class)) { SuiteClasses suiteClasses = Class.forName(testClass).getAnnotation(SuiteClasses.class); for (Class suiteTestClass : suiteClasses.value()) { return assertTestRunner(suiteTestClass.getCanonicalName()); } } else if (!runWith.value().equals(TestRunner.class)) { throw new RuntimeException("Unsupported run with [" + runWith.value().getCanonicalName() + "] on class [" + testClass + "]"); } } catch (Exception exception) { String message = "The test [" + testClass + "] included a rule [" + getClass().getCanonicalName() + "] but did not include a [@" + RunWith.class.getCanonicalName() + "(" + TestRunner.class.getCanonicalName() + ".class)] class annotation"; if (LOG.isErrorEnabled()) { LOG.error(message, exception); } throw new RuntimeException(message, exception); } return true; }
public static void run(Configuration configuration, Class<?> clazz, String methodName) { try { Request request = null; if (clazz!=null && methodName!=null) { request = Request.method(clazz, methodName); } else { Suite suite = new Suite(new JUnit4Builder(), API_TEST_CLASSES); request = Request.runner(suite); } Configuration originalConfiguration = WorkflowTest.cachedConfiguration; WorkflowTest.cachedConfiguration = configuration; JUnitCore junitCore = new JUnitCore(); Result result = junitCore.run(request); WorkflowTest.cachedConfiguration = originalConfiguration; checkResult(result); } catch (Exception e) { throw new RuntimeException(e); } }
private Request constructLeafRequest(List<Description> leaves) { final List<Runner> runners = new ArrayList<Runner>(); for (Description each : leaves) { runners.add(buildRunner(each)); } return new Request() { @Override public Runner getRunner() { try { return new Suite((Class<?>) null, runners) { }; } catch (InitializationError e) { return new ErrorReportingRunner(null, e); } } }; }
private Runner buildRunner(Description each) { if (each.toString().equals("TestSuite with 0 tests")) { return Suite.emptySuite(); } if (each.toString().startsWith(MALFORMED_JUNIT_3_TEST_CLASS_PREFIX)) { // This is cheating, because it runs the whole class // to get the warning for this method, but we can't do better, // because JUnit 3.8's // thrown away which method the warning is for. return new JUnit38ClassRunner(new TestSuite(getMalformedTestClass(each))); } Class<?> type = each.getTestClass(); if (type == null) { throw new RuntimeException("Can't locate a runner from description [" + each + "]"); } String methodName = each.getMethodName(); if (methodName == null) { return Request.aClass(type).getRunner(); } return Request.method(type, methodName).getRunner(); }
private Runner buildRunner(Description each) { if (each.toString().equals("TestSuite with 0 tests")) { return Suite.emptySuite(); } if (each.toString().startsWith(MALFORMED_JUNIT_3_TEST_CLASS_PREFIX)) { // This is cheating, because it runs the whole class // to get the warning for this method, but we can't do better, // because JUnit 3.8's // thrown away which method the warning is for. return new JUnit38ClassRunner(new TestSuite(getMalformedTestClass(each))); } Class<?> type = each.getTestClass(); if (type == null) { throw new RuntimeException("Can't build a runner from description [" + each + "]"); } String methodName = each.getMethodName(); if (methodName == null) { return Request.aClass(type).getRunner(); } return Request.method(type, methodName).getRunner(); }
private static Result runTests(Injector injector, Class<?>[] testClasses, Optional<Description> testFilter) throws InitializationError { final JUnitCore junit = new JUnitCore(); junit.addListener(new JUnitRunListener(LOGGER)); final Request testRequest = Request.runner(new Suite(new GuiceInjectionJUnitRunner(injector), testClasses)); if (testFilter.isPresent()) { return junit.run(testRequest.filterWith(testFilter.get())); } else { return junit.run(testRequest); } }
/** * Determine if this class contains JUnit tests * @deprecated use {@code org.evosuite.junit.CoverageAnalysis.isTest(Class<?> cls)} * * @param className * @return */ @Deprecated public static boolean isTest(String className) { // TODO-JRO Identifying tests should be done differently: // If the class either contains methods // annotated with @Test (> JUnit 4.0) // or contains Test or Suite in it's inheritance structure try { Class<?> clazz = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(className); Class<?> superClazz = clazz.getSuperclass(); while (!superClazz.equals(Object.class)) { if (superClazz.equals(Suite.class)) return true; if (superClazz.equals(Test.class)) return true; superClazz = clazz.getSuperclass(); } for (Method method : clazz.getMethods()) { if (method.isAnnotationPresent(Test.class)) { return true; } } } catch (ClassNotFoundException e) { logger.info("Could not load class: ", className); } return false; }
@Override public void addTests(TestSuite suite) { //$JUnit-BEGIN$ Suite.SuiteClasses suiteClasses = getClass().getAnnotation(Suite.SuiteClasses.class); if (suiteClasses == null) { Logger.getLogger(getClass()).warn("No @SuiteClasses annotation found"); return; } for (Class testClass : suiteClasses.value()) suite.addTest(new JUnit4TestAdapter(testClass)); //$JUnit-END$ }
/** * Create a suite for {@code classes}, building Runners with {@code builder}. * Throws an InitializationError if Runner construction fails */ public Runner getSuite(final RunnerBuilder builder, Class<?>[] classes) throws InitializationError { return new Suite(new RunnerBuilder() { @Override public Runner runnerForClass(Class<?> testClass) throws Throwable { return getRunner(builder, testClass); } }, classes); }
private boolean hasSuitableRunnner(final Class<?> clazz) { final RunWith runWith = clazz.getAnnotation(RunWith.class); if (runWith != null) { return (runWith.value().equals(Suite.class)); } return false; }
/** * @see junitconverter.stages.TestConversionStage#convertClass(junitconverter.testcase.TestCaseClass) */ public void convertClass(TestCaseClass testCase) { codeEditor.importClass(testCase, Suite.class); codeEditor.importClass(testCase, RunWith.class); codeEditor.importClass(testCase, SuiteClasses.class); // FIXME Doesn't import it properly. Adds a $ instead of . between SUite and SuiteClasses }
private static boolean isSuite(Object child) { return child instanceof Suite && !(child instanceof Parameterized) || child instanceof SuiteMethod; }
public JicUnitRunner(Class<?> testClass) throws Throwable { Class<? extends Runner> runnerClass; RunInContainerWith runInContainerWith = findAnnotation(testClass, RunInContainerWith.class); // figure out if this is happening locally or in the JEE container String containerUrl = getContainerUrl(); if (containerUrl == null) { // this code is executed in the JEE container if (runInContainerWith != null) { runnerClass = runInContainerWith.value(); } else { runnerClass = JUnit4.class; } try { Constructor<? extends Runner> constructor = runnerClass .getDeclaredConstructor(Class.class); mRunner = constructor.newInstance(testClass); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { Throwable cause = (e.getCause() != null ? e.getCause() : e); String msg = "Unable to create instance of " + runnerClass + " using test class " + testClass.getName() + " Reason: " + cause; if (cause instanceof InitializationError) { InitializationError initializationError = (InitializationError) cause; msg = msg + " " + initializationError.getCauses(); } sLog.log(Level.SEVERE, msg); throw new RuntimeException(msg, cause); } } else { // this code is executed locally so create a ProxyRunner which will // forward the execution to the container if (runInContainerWith != null) { runnerClass = runInContainerWith.value(); if (Parameterized.class.isAssignableFrom(runnerClass)) { mRunner = new ParameterizedProxyRunner(testClass, containerUrl); } else if (Suite.class.isAssignableFrom(runnerClass)) { throw new IllegalArgumentException( RunInContainerWith.class.getSimpleName() + " annotation does not support Suite runner or any subclass of Suite except Parameterized"); } else { Runner runInContainerRunner = runnerClass.getDeclaredConstructor(Class.class) .newInstance(testClass); Description desc = runInContainerRunner.getDescription(); mRunner = new BasicProxyRunner(testClass, containerUrl, desc); } } else { mRunner = new BasicProxyRunner(testClass, containerUrl); } } }
private static boolean isSuite(Object child) { return child instanceof Suite || child instanceof SuiteMethod; }
/** * Determines if the given {@link Description} is describing a {@link Suite}. * * @param description * the {@link Description} to check, must not be {@code null} * @return whether the description is describing a Suite */ public static boolean isSuite(final Description description) { final RunWith runWithAnnotation = description.getAnnotation(RunWith.class); return runWithAnnotation != null && Suite.class.isAssignableFrom(runWithAnnotation.value()); }