public static CtMethod cloneMethodTest(CtMethod method, String suffix, int timeOut) { CtMethod cloned_method = cloneMethod(method, suffix); CtAnnotation testAnnotation = cloned_method .getAnnotations() .stream() .filter(annotation -> annotation.toString().contains("Test")) .findFirst() .orElse(null); if (testAnnotation != null) { cloned_method.removeAnnotation(testAnnotation); } testAnnotation = method.getFactory().Core().createAnnotation(); CtTypeReference<Object> ref = method.getFactory().Core().createTypeReference(); ref.setSimpleName("Test"); CtPackageReference refPackage = method.getFactory().Core().createPackageReference(); refPackage.setSimpleName("org.junit"); ref.setPackage(refPackage); testAnnotation.setAnnotationType(ref); Map<String, Object> elementValue = new HashMap<>(); elementValue.put("timeout", timeOut); testAnnotation.setElementValues(elementValue); cloned_method.addAnnotation(testAnnotation); return cloned_method; }
@Override public boolean isToBeProcessed(CtInvocation<?> candidate) { if (!candidate.getExecutable().getSimpleName().contains("assert")) { return false; } CtExecutableReference<?> executable = candidate.getExecutable(); CtPackageReference executablePackage = executable.getDeclaringType().getPackage(); if (executablePackage == null || !executablePackage.getSimpleName().contains("junit")) { return false; } CtMethod<?> parentMethod = candidate.getParent(CtMethod.class); if (parentMethod == null) { return false; } createExecutionMethod(candidate); return super.isToBeProcessed(candidate); /* * boolean found = false; for (TestCase testCase : faillingTest) { if * (testCase.className().equals(parentClass.getQualifiedName())) { if * (testCase.testName().equals(parentMethod.getSimpleName())) { found = * true; break; } } } * * return found; */ }
/** * Creates a reference to a package. * * @param name full name of the package to reference */ public CtPackageReference createReference(String name) { CtPackageReference ref = factory.Core().createPackageReference(); ref.setSimpleName(name); return ref; }