Skip to content

Commit d11ddab

Browse files
author
Fabrice Tiercelin
committed
New cleanup: refactors to a proper use of JUnit 5 assertions.
1 parent aafe748 commit d11ddab

File tree

6 files changed

+1448
-1
lines changed

6 files changed

+1448
-1
lines changed

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/AllCleanUpRules.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public static List<RefactoringRule> getAllCleanUpRules() {
138138
new AnnotationCleanUp(), new TryWithResourceCleanUp(),
139139
// FIXME it would be nice if it was only enabled when testng jar is detected for
140140
// the project
141-
new TestNGAssertCleanUp(), new JUnitAssertCleanUp(), new AssertJCleanUp(),
141+
new TestNGAssertCleanUp(),
142+
new JupiterAssertCleanUp(),
143+
new JUnitAssertCleanUp(), new AssertJCleanUp(),
142144
new SeparateAssertionsRatherThanBooleanExpressionCleanUp(),
143145
new RemoveEmptyLinesCleanUp(),
144146
new RemoveEmptySuperConstrInvocationCleanUp(),
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* AutoRefactor - Eclipse plugin to automatically refactor Java code bases.
3+
*
4+
* Copyright (C) 2015 Jean-Noël Rouvignac - initial API and implementation
5+
* Copyright (C) 2016 Fabrice Tiercelin - Adapt for JUnit
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program under LICENSE-GNUGPL. If not, see
19+
* <http://www.gnu.org/licenses/>.
20+
*
21+
*
22+
* All rights reserved. This program and the accompanying materials
23+
* are made available under the terms of the Eclipse Public License v1.0
24+
* which accompanies this distribution under LICENSE-ECLIPSE, and is
25+
* available at http://www.eclipse.org/legal/epl-v10.html
26+
*/
27+
package org.autorefactor.jdt.internal.ui.fix;
28+
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import java.util.Set;
32+
33+
import org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory;
34+
import org.autorefactor.jdt.internal.corext.dom.ASTNodes;
35+
import org.autorefactor.util.Pair;
36+
import org.eclipse.jdt.core.dom.Expression;
37+
import org.eclipse.jdt.core.dom.IfStatement;
38+
import org.eclipse.jdt.core.dom.MethodInvocation;
39+
import org.eclipse.jdt.core.dom.Statement;
40+
41+
/**
42+
* See {@link #getDescription()} method.
43+
*/
44+
public class JupiterAssertCleanUp extends AbstractUnitTestCleanUp {
45+
private static final String JUPITER_CLASS= "org.junit.jupiter.api.Assertions"; //$NON-NLS-1$
46+
47+
/**
48+
* Initialize canUseAssertNotEquals.
49+
*/
50+
public JupiterAssertCleanUp() {
51+
canUseAssertNotEquals= true;
52+
}
53+
54+
@Override
55+
public String getName() {
56+
return MultiFixMessages.CleanUpRefactoringWizard_JupiterAssertCleanUp_name;
57+
}
58+
59+
@Override
60+
public String getDescription() {
61+
return MultiFixMessages.CleanUpRefactoringWizard_JupiterAssertCleanUp_description;
62+
}
63+
64+
@Override
65+
public String getReason() {
66+
return MultiFixMessages.CleanUpRefactoringWizard_JupiterAssertCleanUp_reason;
67+
}
68+
69+
@Override
70+
protected Pair<Expression, Expression> getActualAndExpected(final Expression leftValue,
71+
final Expression rightValue) {
72+
return Pair.of(leftValue, rightValue);
73+
}
74+
75+
@Override
76+
public boolean maybeRefactorMethodInvocation(final MethodInvocation node, final Set<String> classesToUseWithImport,
77+
final Set<String> importsToAdd) {
78+
@SuppressWarnings("unchecked")
79+
List<Expression> args= node.arguments();
80+
81+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertTrue", boolean.class.getSimpleName())) { //$NON-NLS-1$
82+
return maybeRefactorStatement(classesToUseWithImport, importsToAdd, node, node, true, args.get(0), null, false);
83+
}
84+
85+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertTrue", boolean.class.getSimpleName(), String.class.getCanonicalName())) { //$NON-NLS-1$
86+
return maybeRefactorStatement(classesToUseWithImport, importsToAdd, node, node, true, args.get(0), args.get(1), false);
87+
}
88+
89+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertFalse", boolean.class.getSimpleName())) { //$NON-NLS-1$
90+
return maybeRefactorStatement(classesToUseWithImport, importsToAdd, node, node, false, args.get(0), null, false);
91+
}
92+
93+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertFalse", boolean.class.getSimpleName(), String.class.getCanonicalName())) { //$NON-NLS-1$
94+
return maybeRefactorStatement(classesToUseWithImport, importsToAdd, node, node, false, args.get(0), args.get(1), false);
95+
}
96+
97+
for (Class<?> clazz : new Class<?>[]{boolean.class, int.class, long.class, double.class, float.class, short.class, char.class, byte.class, String.class, Object.class}) {
98+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertEquals", clazz.getCanonicalName(), clazz.getCanonicalName())) { //$NON-NLS-1$
99+
return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, node, node, true, args.get(0), args.get(1), null);
100+
}
101+
102+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertEquals", clazz.getCanonicalName(), clazz.getCanonicalName(), String.class.getCanonicalName())) { //$NON-NLS-1$
103+
return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, node, node, true, args.get(0), args.get(1), args.get(2));
104+
}
105+
106+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertNotEquals", clazz.getCanonicalName(), clazz.getCanonicalName())) { //$NON-NLS-1$
107+
return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, node, node, false, args.get(0), args.get(1), null);
108+
}
109+
110+
if (ASTNodes.usesGivenSignature(node, JUPITER_CLASS, "assertNotEquals", clazz.getCanonicalName(), clazz.getCanonicalName(), String.class.getCanonicalName())) { //$NON-NLS-1$
111+
return maybeRefactorToEquality(classesToUseWithImport, importsToAdd, node, node, false, args.get(0), args.get(1), args.get(2));
112+
}
113+
}
114+
115+
return true;
116+
}
117+
118+
@SuppressWarnings("unchecked")
119+
@Override
120+
public boolean maybeRefactorIfStatement(final IfStatement node, final Set<String> classesToUseWithImport,
121+
final Set<String> importsToAdd) {
122+
List<Statement> statements= ASTNodes.asList(node.getThenStatement());
123+
124+
if (node.getElseStatement() == null && statements.size() == 1) {
125+
MethodInvocation methodInvocation= ASTNodes.asExpression(statements.get(0), MethodInvocation.class);
126+
127+
if (ASTNodes.usesGivenSignature(methodInvocation, JUPITER_CLASS, "fail")) { //$NON-NLS-1$
128+
return maybeRefactorStatement(classesToUseWithImport, importsToAdd, node, methodInvocation, false, node.getExpression(), null, true);
129+
}
130+
131+
if (ASTNodes.usesGivenSignature(methodInvocation, JUPITER_CLASS, "fail", String.class.getCanonicalName())) { //$NON-NLS-1$
132+
return maybeRefactorIfObjectsAreNotUsed(classesToUseWithImport, importsToAdd, node, methodInvocation, false, node.getExpression(), ((List<Expression>) methodInvocation.arguments()).get(0));
133+
}
134+
}
135+
136+
return true;
137+
}
138+
139+
@Override
140+
protected MethodInvocation invokeQualifiedMethod(final Set<String> classesToUseWithImport, final Set<String> importsToAdd,
141+
final Expression copyOfExpression, final String methodName, final Expression copyOfActual,
142+
final Expression copyOfExpected, final Expression delta, final Expression failureMessage) {
143+
ASTNodeFactory ast= cuRewrite.getASTBuilder();
144+
145+
List<Expression> arguments= new ArrayList<>(4);
146+
147+
if (copyOfActual != null) {
148+
arguments.add(copyOfActual);
149+
}
150+
151+
if (copyOfExpected != null) {
152+
arguments.add(copyOfExpected);
153+
}
154+
155+
if (delta != null) {
156+
arguments.add(delta);
157+
}
158+
159+
if (failureMessage != null) {
160+
arguments.add(ASTNodes.createMoveTarget(cuRewrite.getASTRewrite(), ASTNodes.getUnparenthesedExpression(failureMessage)));
161+
}
162+
163+
return ast.newMethodInvocation(copyOfExpression, methodName, arguments.toArray(new Expression[0]));
164+
}
165+
}

plugin/src/main/java/org/autorefactor/jdt/internal/ui/fix/MultiFixMessages.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,18 @@ private MultiFixMessages() {
12981298
* Automatically filled.
12991299
*/
13001300
public static String CleanUpRefactoringWizard_TestNGAssertCleanUp_reason;
1301+
/**
1302+
* Automatically filled.
1303+
*/
1304+
public static String CleanUpRefactoringWizard_JupiterAssertCleanUp_name;
1305+
/**
1306+
* Automatically filled.
1307+
*/
1308+
public static String CleanUpRefactoringWizard_JupiterAssertCleanUp_description;
1309+
/**
1310+
* Automatically filled.
1311+
*/
1312+
public static String CleanUpRefactoringWizard_JupiterAssertCleanUp_reason;
13011313
/**
13021314
* Automatically filled.
13031315
*/

plugin/src/main/resources/org/autorefactor/jdt/internal/ui/fix/MultiFixMessages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ CleanUpRefactoringWizard_TryWithResourceCleanUp_reason=It improves the readabili
313313
CleanUpRefactoringWizard_TestNGAssertCleanUp_name=TestNG asserts
314314
CleanUpRefactoringWizard_TestNGAssertCleanUp_description=Refactors to a proper use of TestNG assertions.
315315
CleanUpRefactoringWizard_TestNGAssertCleanUp_reason=It improves the readability of the code and the report.
316+
CleanUpRefactoringWizard_JupiterAssertCleanUp_name=Jupiter asserts
317+
CleanUpRefactoringWizard_JupiterAssertCleanUp_description=Refactors to a proper use of JUnit 5 assertions.
318+
CleanUpRefactoringWizard_JupiterAssertCleanUp_reason=It improves the readability of the code and the report.
316319
CleanUpRefactoringWizard_JUnitAssertCleanUp_name=JUnit asserts
317320
CleanUpRefactoringWizard_JUnitAssertCleanUp_description=Refactors to a proper use of JUnit assertions.
318321
CleanUpRefactoringWizard_JUnitAssertCleanUp_reason=It improves the readability of the code and the report.

0 commit comments

Comments
 (0)