|
| 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 | +} |
0 commit comments