@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; */ }
@Override public void process(CtInvocation<?> ctInvocation) { // ignore no assert invocation // CtTypeReference<?> executableType = executable.getDeclaringType(); // List<CtExpression<?>> parameters = ctInvocation.getArguments(); // int parametersSize = parameters.size(); // Class<?>[] parameterTypes = new Class<?>[parametersSize]; // for (int i = 0; i < parametersSize; i++) { // parameterTypes[0] = parameters.get(i).getType().getActualClass(); // } // List<CtExpression<?>> arguments = ctInvocation.getArguments(); // CtIf newIf = ctInvocation.getFactory().Core().createIf(); // String body = Assert.class.getCanonicalName() + "." // + ctInvocation.getExecutable().getSimpleName() + "("; // for (int i = 0; i < arguments.size(); i++) { // CtExpression<?> ctExpression = arguments.get(i); // body += ctExpression; // if (i < arguments.size() - 1) { // body += ", "; // } // } // body += ")"; // CtCodeSnippetExpression<Boolean> condition = // ctInvocation.getFactory() // .Code().createCodeSnippetExpression(body); // newIf.setCondition(condition); // newIf.setElseStatement(createThen(ctInvocation)); // ctInvocation.replace(newIf); // createExecutionMethod(ctInvocation); // return; // CtTry ctTry = getFactory().Core().createTry(); // CtCatch ctCatch = getFactory().Core().createCatch(); CtCatchVariable<Exception> localVariable = getFactory() .Code() .createCatchVariable( getFactory().Class().createReference(Exception.class), "exception" + (++exceptionCount)); // ctCatch.setParameter(localVariable); // ctCatch.setBody(createThen(ctInvocation)); // ctTry.addCatcher(ctCatch); CtBlock<Object> tryBody = ctInvocation.getFactory().Core().createBlock(); // ctTry.setBody(tryBody); switch (ctInvocation.getExecutable().getSimpleName()) { case "assertEquals": ctInvocation.replace(replaceAssertEquals(ctInvocation)); break; case "assertFalse": ctInvocation.replace(replaceAssertTrueFalse(false, ctInvocation)); break; case "assertTrue": ctInvocation.replace(replaceAssertTrueFalse(true, ctInvocation)); break; case "assertNull": ctInvocation.replace(replaceAssertNull(ctInvocation)); break; case "assertNotNull": ctInvocation.replace(replaceAssertNotNull(ctInvocation)); break; default: // System.out.println("Assert transformation not found: " + // ctInvocation.getExecutable().getSimpleName()); return; } // insertAfterUnderSameParent(((getFactory().Code().createCodeSnippetStatement("System.out.println(\"Fianlly: \" + "+Debug.class.getCanonicalName()+".getSolvedPC())"))), // ctInvocation); /*if(ctInvocation.getParent(CtTry.class) == null) { ctInvocation.replace(ctTry); } else { ctInvocation.replace(tryBody); }*/ /* * System.out.println("REPLACE: "); System.out.println(ctInvocation); * System.out.println("BY: "); System.out.println(ctTry); */ // createExecutionMethod(ctInvocation); }