public Object getTestToStart(String[] args) { final Request request = JUnit4TestRunnerUtil.buildRequest(args, false); if (request == null) return null; final Runner testRunner = request.getRunner(); Description description = null; try { description = testRunner.getDescription(); if (request instanceof ClassRequest) { description = getSuiteMethodDescription(request, description); } else if (request instanceof FilterRequest) { description = getFilteredDescription(request, description); } } catch (Exception e) { //noinspection HardCodedStringLiteral System.err.println("Internal Error occured."); e.printStackTrace(System.err); } return description; }
private static Description getDescription(Request request, Runner testRunner) throws NoSuchFieldException, IllegalAccessException { Description description = testRunner.getDescription(); if (description == null) { System.err.println("Nothing found to run. Runner " + testRunner.getClass().getName() + " provides no description."); return null; } if (request instanceof ClassRequest) { description = getSuiteMethodDescription(request, description); } else if (request instanceof FilterRequest) { description = getFilteredDescription(request, description); } return description; }
private static Description getFilteredDescription(Request request, Description description) throws NoSuchFieldException, IllegalAccessException { final Field field = FilterRequest.class.getDeclaredField("fFilter"); field.setAccessible(true); final Filter filter = (Filter)field.get(request); final String filterDescription = filter.describe(); if (filterDescription != null) { boolean isMethodFilter = filterDescription.startsWith("Method"); if (isMethodFilter && canCompress(description)) return (Description)description.getChildren().get(0); try { final Description failedTestsDescription = Description.createSuiteDescription(filterDescription, null); if (filterDescription.startsWith("Tests") || filterDescription.startsWith("Ignored")) { for (Iterator iterator = description.getChildren().iterator(); iterator.hasNext(); ) { final Description childDescription = (Description)iterator.next(); if (filter.shouldRun(childDescription)) { failedTestsDescription.addChild(childDescription); } } description = failedTestsDescription; } else if (isMethodFilter && canCompress(failedTestsDescription)) { description = (Description)failedTestsDescription.getChildren().get(0); } } catch (NoSuchMethodError e) { //junit 4.0 doesn't have method createSuite(String, Annotation...) : skip it } } return description; }
/** * Returns a Request that only contains those tests that should run when * <code>filter</code> is applied * * @param filter The {@link Filter} to apply to this Request * @return the filtered Request */ public Request filterWith(Filter filter) { return new FilterRequest(this, filter); }