@Override public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { List<TestStatement> commomStatements = new ArrayList<TestStatement>(); List<TestStatement> temp = new ArrayList<TestStatement>(); commomStatements.addAll(targetMethod.getStatements()); List<Integer> asserts = new ArrayList<Integer>(); boolean lastWasAssert = false; for (int i = 0; i < commomStatements.size(); i++) { if (commomStatements.get(i) instanceof Assertion) { asserts.add(i); lastWasAssert = true; } else { if (lastWasAssert) { createMethod(temp); } lastWasAssert = false; } temp.add(commomStatements.get(i)); } if (lastWasAssert) { createMethod(commomStatements); } TestSuite ts = targetMethod.getParent(); ts.removeTestMethod(targetMethod); return new RefactoringStatus(); }
private void createMethod(List<TestStatement> statements) { boolean canRemove = false; for (int i = statements.size() - 1; i >= 0; i--) { if (statements.get(i) instanceof Assertion) { if (canRemove) { statements.remove(i); } } else { canRemove = true; } } List<TestStatement> copied = new ArrayList<TestStatement>(); copied.addAll(statements); String newMethodName = null; List<? extends TestMethod> methods = targetMethod.getParent().getAllTestMethodList(); for (int i = index; i < Integer.MAX_VALUE; ) { index++; newMethodName = targetMethod.getName() + i; for (TestMethod tm : methods) { if (tm.getName().equals(newMethodName)) { continue; } } break; } TestMethod tm = targetMethod.getParent().createNewTestMethod(newMethodName); tm.addStatements(copied, 0); }
@Override public void checkForPresence(TestMethod method, MarkerManager markerManager) throws JavaModelException, CoreException { Block code = method.getCodeElement().getBody(); for (Statement statement : code.getStatementList()) { if (statement.isBranchStatement()) { markerManager.addMarker( statement, "Control flow instruction in test method", this.getClass()); } } }