-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add junit 3 test (disabled) #134
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,283 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Carsten Hammer. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Carsten Hammer | ||
*******************************************************************************/ | ||
package org.sandbox.jdt.internal.corext.fix.helper; | ||
|
||
import java.util.List; | ||
|
||
/*- | ||
* #%L | ||
* Sandbox junit cleanup | ||
* %% | ||
* Copyright (C) 2024 hammer | ||
* %% | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License, v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is | ||
* available at https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
* #L% | ||
*/ | ||
|
||
import java.util.Set; | ||
|
||
import org.eclipse.jdt.core.dom.AST; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.ASTVisitor; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
import org.eclipse.jdt.core.dom.Expression; | ||
import org.eclipse.jdt.core.dom.MarkerAnnotation; | ||
import org.eclipse.jdt.core.dom.MethodDeclaration; | ||
import org.eclipse.jdt.core.dom.MethodInvocation; | ||
import org.eclipse.jdt.core.dom.Modifier; | ||
import org.eclipse.jdt.core.dom.SimpleName; | ||
import org.eclipse.jdt.core.dom.Type; | ||
import org.eclipse.jdt.core.dom.TypeDeclaration; | ||
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | ||
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | ||
import org.eclipse.jdt.core.dom.rewrite.ListRewrite; | ||
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperationWithSourceRange; | ||
import org.eclipse.text.edits.TextEditGroup; | ||
import org.sandbox.jdt.internal.common.HelperVisitor; | ||
import org.sandbox.jdt.internal.common.ReferenceHolder; | ||
import org.sandbox.jdt.internal.corext.fix.JUnitCleanUpFixCore; | ||
|
||
/** | ||
* | ||
* | ||
*/ | ||
public class TestJUnit3Plugin extends AbstractTool<ReferenceHolder<Integer, JunitHolder>> { | ||
|
||
@Override | ||
public void find(JUnitCleanUpFixCore fixcore, CompilationUnit compilationUnit, | ||
Set<CompilationUnitRewriteOperationWithSourceRange> operations, Set<ASTNode> nodesprocessed) { | ||
ReferenceHolder<Integer, JunitHolder> dataholder= new ReferenceHolder<>(); | ||
HelperVisitor.callTypeDeclarationVisitor("junit.framework.TestCase", compilationUnit, dataholder, | ||
nodesprocessed, | ||
(visited, aholder) -> processFoundNode(fixcore, operations, visited, aholder, nodesprocessed)); | ||
} | ||
|
||
private boolean processFoundNode(JUnitCleanUpFixCore fixcore, | ||
Set<CompilationUnitRewriteOperationWithSourceRange> operations, TypeDeclaration node, | ||
ReferenceHolder<Integer, JunitHolder> dataholder, Set<ASTNode> nodesprocessed) { | ||
if (!nodesprocessed.contains(node)) { | ||
boolean nothingtochange= true; | ||
for (MethodDeclaration method : node.getMethods()) { | ||
if (!isTestMethod(method)) { | ||
nothingtochange= false; | ||
} | ||
} | ||
if (nothingtochange) { | ||
return false; | ||
} | ||
|
||
nodesprocessed.add(node); | ||
JunitHolder mh= new JunitHolder(); | ||
mh.minv= node; | ||
dataholder.put(dataholder.size(), mh); | ||
operations.add(fixcore.rewrite(dataholder)); | ||
} | ||
return false; | ||
} | ||
|
||
private void correctAssertionOrder(MethodInvocation node, ASTRewrite rewriter, AST ast, TextEditGroup group) { | ||
Check warning Code scanning / PMD Avoid unused method parameters such as 'holder'. Warning
Avoid unused method parameters such as 'ast'.
Check notice Code scanning / Pmd (reported by Codacy) Avoid unused private methods such as 'correctAssertionOrder(MethodInvocation,ASTRewrite,AST,TextEditGroup)'. Note
Avoid unused private methods such as 'correctAssertionOrder(MethodInvocation,ASTRewrite,AST,TextEditGroup)'.
Check notice Code scanning / Pmd (reported by Codacy) Avoid unused method parameters such as 'ast'. Note
Avoid unused method parameters such as 'ast'.
|
||
String methodName = node.getName().getIdentifier(); | ||
|
||
// Prüfe, ob es sich um eine bekannte Assertion handelt | ||
if ("assertEquals".equals(methodName) || "assertArrayEquals".equals(methodName)) { | ||
List<?> arguments = node.arguments(); | ||
if (arguments.size() == 3) { | ||
// Reorganisiere die Reihenfolge: message, expected, actual -> expected, actual, message | ||
Expression expected = (Expression) arguments.get(1); | ||
Expression actual = (Expression) arguments.get(2); | ||
Expression message = (Expression) arguments.get(0); | ||
|
||
ListRewrite listRewrite = rewriter.getListRewrite(node, MethodInvocation.ARGUMENTS_PROPERTY); | ||
listRewrite.replace((ASTNode) arguments.get(0), expected, group); | ||
listRewrite.replace((ASTNode) arguments.get(1), actual, group); | ||
listRewrite.replace((ASTNode) arguments.get(2), message, group); | ||
} | ||
} | ||
} | ||
|
||
private boolean isTestMethod(MethodDeclaration method) { | ||
// Konstruktoren ausschließen | ||
if (method.isConstructor()) { | ||
return false; | ||
} | ||
|
||
String methodName = method.getName().getIdentifier(); | ||
|
||
// Prüfen auf typische JUnit 3-Testmethoden | ||
if (methodName.startsWith("test")) { | ||
return true; | ||
} | ||
|
||
// Prüfen auf alternative Namensschemata | ||
if (methodName.endsWith("_test") || methodName.startsWith("should") || methodName.contains("Test")) { | ||
return true; | ||
} | ||
|
||
// Zusätzliche Bedingungen: public, void, keine Parameter | ||
return Modifier.isPublic(method.getModifiers()) && "void".equals(method.getReturnType2().toString()) | ||
&& method.parameters().isEmpty(); | ||
} | ||
|
||
|
||
@Override | ||
void process2Rewrite(TextEditGroup group, ASTRewrite rewriter, AST ast, ImportRewrite importRewriter, | ||
JunitHolder mh) { | ||
TypeDeclaration node= mh.getTypeDeclaration(); | ||
// Remove `extends TestCase` | ||
Type superclass= node.getSuperclassType(); | ||
if (superclass != null && "TestCase".equals(superclass.toString())) { | ||
rewriter.remove(node.getSuperclassType(), group); | ||
} | ||
|
||
for (MethodDeclaration method : node.getMethods()) { | ||
if (isSetupMethod(method)) { | ||
convertToAnnotation(method, "BeforeEach", importRewriter, rewriter, ast, group); | ||
} else if (isTeardownMethod(method)) { | ||
convertToAnnotation(method, "AfterEach", importRewriter, rewriter, ast, group); | ||
} else if (isTestMethod(method)) { | ||
addAnnotationToMethod(method, "Test", importRewriter, rewriter, ast, group); | ||
} | ||
|
||
// Bearbeite Assertions und Assumptions in allen relevanten Methoden | ||
if (method.getBody() != null) { | ||
rewriteAssertionsAndAssumptions(method, rewriter, ast, group,importRewriter); | ||
} | ||
} | ||
|
||
} | ||
|
||
private void rewriteAssertionsAndAssumptions(MethodDeclaration method, ASTRewrite rewriter, AST ast, TextEditGroup group, ImportRewrite importRewriter) { | ||
method.accept(new ASTVisitor() { | ||
@Override | ||
public boolean visit(MethodInvocation node) { | ||
// Überprüfen, ob das MethodBinding aufgelöst werden kann | ||
if (node.resolveMethodBinding() != null) { | ||
String fullyQualifiedName = node.resolveMethodBinding().getDeclaringClass().getQualifiedName(); | ||
|
||
if ("junit.framework.Assert".equals(fullyQualifiedName) || "junit.framework.Assume".equals(fullyQualifiedName)) { | ||
// correctAssertionOrder(node, rewriter, ast, group); | ||
|
||
reorderParameters(node, rewriter, group, oneparam, twoparam); | ||
// SimpleName newQualifier= ast.newSimpleName(ASSERTIONS); | ||
// Expression expression= assertexpression; | ||
// if (expression != null) { | ||
// ASTNodes.replaceButKeepComment(rewriter, expression, newQualifier, group); | ||
// } | ||
|
||
|
||
|
||
|
||
// Ändere den Qualifier (z.B. Assert.assertEquals -> Assertions.assertEquals) | ||
rewriter.set(node.getExpression(), SimpleName.IDENTIFIER_PROPERTY, "Assertions", group); | ||
// ASTNodes.replaceButKeepComment(rewriter, expression, newQualifier, group); | ||
|
||
// Passe Importe an | ||
addImportForAssertion(node.getName().getIdentifier(), ast, rewriter, group, importRewriter); | ||
} | ||
} | ||
|
||
return super.visit(node); | ||
} | ||
}); | ||
} | ||
|
||
|
||
private void addImportForAssertion(String assertionMethod, AST ast, ASTRewrite rewriter, TextEditGroup group, ImportRewrite importRewriter) { | ||
Check warning Code scanning / PMD Avoid unused method parameters such as 'holder'. Warning
Avoid unused method parameters such as 'ast'.
Check warning Code scanning / PMD Avoid unused method parameters such as 'holder'. Warning
Avoid unused method parameters such as 'rewriter'.
Check warning Code scanning / PMD Avoid unused method parameters such as 'holder'. Warning
Avoid unused method parameters such as 'group'.
Check notice Code scanning / Pmd (reported by Codacy) Avoid unused method parameters such as 'rewriter'. Note
Avoid unused method parameters such as 'rewriter'.
Check notice Code scanning / Pmd (reported by Codacy) Avoid unused method parameters such as 'group'. Note
Avoid unused method parameters such as 'group'.
Check notice Code scanning / Pmd (reported by Codacy) Avoid unused method parameters such as 'ast'. Note
Avoid unused method parameters such as 'ast'.
|
||
String importToAdd = null; | ||
|
||
switch (assertionMethod) { | ||
Check notice Code scanning / Pmd (reported by Codacy) Switch statements should be exhaustive, add a default case (or missing enum branches) Note
Switch statements should be exhaustive, add a default case (or missing enum branches)
|
||
case "assertEquals": | ||
case "assertArrayEquals": | ||
case "assertTrue": | ||
case "assertFalse": | ||
case "assertNull": | ||
case "assertNotNull": | ||
importToAdd = "org.junit.jupiter.api.Assertions"; | ||
break; | ||
case "assumeTrue": | ||
case "assumeFalse": | ||
case "assumeNotNull": | ||
importToAdd = "org.junit.jupiter.api.Assumptions"; | ||
break; | ||
case "assertThat": | ||
importToAdd = "org.hamcrest.MatcherAssert"; | ||
break; | ||
} | ||
Comment on lines
+209
to
+226
Check warning Code scanning / PMD Switch statements or expressions should be exhaustive, add a default case (or missing enum branches) Warning
Switch statements or expressions should be exhaustive, add a default case (or missing enum branches)
|
||
|
||
if (importToAdd != null) { | ||
importRewriter.addImport(importToAdd); | ||
} | ||
} | ||
|
||
|
||
private boolean isSetupMethod(MethodDeclaration method) { | ||
return "setUp".equals(method.getName().getIdentifier()) && method.parameters().isEmpty() | ||
&& method.getReturnType2() == null; | ||
} | ||
|
||
private boolean isTeardownMethod(MethodDeclaration method) { | ||
return "tearDown".equals(method.getName().getIdentifier()) && method.parameters().isEmpty() | ||
&& method.getReturnType2() == null; | ||
} | ||
|
||
private void convertToAnnotation(MethodDeclaration method, String annotation, ImportRewrite importRewrite, | ||
ASTRewrite rewrite, AST ast, TextEditGroup group) { | ||
// Add annotation | ||
ListRewrite modifiers= rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); | ||
MarkerAnnotation newMarkerAnnotation= ast.newMarkerAnnotation(); | ||
newMarkerAnnotation.setTypeName(ast.newSimpleName(annotation)); | ||
modifiers.insertFirst(newMarkerAnnotation, group); | ||
importRewrite.addImport("org.junit.jupiter.api." + annotation.substring(1)); | ||
} | ||
|
||
private void addAnnotationToMethod(MethodDeclaration method, String annotation, ImportRewrite importRewrite, | ||
ASTRewrite rewrite, AST ast, TextEditGroup group) { | ||
// Füge die Annotation zum Methodenkopf hinzu | ||
ListRewrite modifiers= rewrite.getListRewrite(method, MethodDeclaration.MODIFIERS2_PROPERTY); | ||
MarkerAnnotation newMarkerAnnotation= ast.newMarkerAnnotation(); | ||
newMarkerAnnotation.setTypeName(ast.newSimpleName(annotation)); // annotation sollte nur den Namen enthalten, z. | ||
// B. "Test" | ||
modifiers.insertFirst(newMarkerAnnotation, group); | ||
|
||
// Import der Annotation hinzufügen | ||
importRewrite.addImport("org.junit.jupiter.api." + annotation); | ||
} | ||
|
||
@Override | ||
public String getPreview(boolean afterRefactoring) { | ||
if (afterRefactoring) { | ||
return """ | ||
import org.junit.jupiter.api.Test; | ||
"""; //$NON-NLS-1$ | ||
} | ||
return """ | ||
import junit.framework.TestCase; | ||
"""; //$NON-NLS-1$ | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "TestCase"; //$NON-NLS-1$ | ||
} | ||
} |
Check warning
Code scanning / PMD
Avoid unused private methods such as 'correctAssertionOrder(MethodInvocation, ASTRewrite, AST, TextEditGroup)'. Warning