|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Carsten Hammer and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Carsten Hammer using github copilot - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.internal.junit.ui; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.core.runtime.CoreException; |
| 20 | + |
| 21 | +import org.eclipse.jdt.core.dom.ASTNode; |
| 22 | +import org.eclipse.jdt.core.dom.IAnnotationBinding; |
| 23 | +import org.eclipse.jdt.core.dom.IMethodBinding; |
| 24 | +import org.eclipse.jdt.core.dom.ITypeBinding; |
| 25 | +import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 26 | + |
| 27 | +import org.eclipse.jdt.internal.junit.JUnitCorePlugin; |
| 28 | + |
| 29 | +import org.eclipse.jdt.ui.text.java.IInvocationContext; |
| 30 | +import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; |
| 31 | +import org.eclipse.jdt.ui.text.java.IProblemLocation; |
| 32 | +import org.eclipse.jdt.ui.text.java.IQuickAssistProcessor; |
| 33 | + |
| 34 | +public class JUnitQuickAssistProcessor implements IQuickAssistProcessor { |
| 35 | + |
| 36 | + private static final String JUNIT4_IGNORE_ANNOTATION = "org.junit.Ignore"; //$NON-NLS-1$ |
| 37 | + private static final String JUNIT5_DISABLED_ANNOTATION = "org.junit.jupiter.api.Disabled"; //$NON-NLS-1$ |
| 38 | + private static final String JUNIT5_TEST_ANNOTATION = "org.junit.jupiter.api.Test"; //$NON-NLS-1$ |
| 39 | + private static final String JUNIT5_PARAMETERIZED_TEST_ANNOTATION = "org.junit.jupiter.params.ParameterizedTest"; //$NON-NLS-1$ |
| 40 | + private static final String JUNIT5_REPEATED_TEST_ANNOTATION = "org.junit.jupiter.api.RepeatedTest"; //$NON-NLS-1$ |
| 41 | + private static final String JUNIT5_TEST_FACTORY_ANNOTATION = "org.junit.jupiter.api.TestFactory"; //$NON-NLS-1$ |
| 42 | + private static final String JUNIT5_TEST_TEMPLATE_ANNOTATION = "org.junit.jupiter.api.TestTemplate"; //$NON-NLS-1$ |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean hasAssists(IInvocationContext context) throws CoreException { |
| 46 | + ASTNode coveringNode = context.getCoveringNode(); |
| 47 | + if (coveringNode == null) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + MethodDeclaration methodDecl = getMethodDeclaration(coveringNode); |
| 52 | + if (methodDecl == null) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + return isJUnitTestMethod(methodDecl); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public IJavaCompletionProposal[] getAssists(IInvocationContext context, IProblemLocation[] locations) throws CoreException { |
| 61 | + ASTNode coveringNode = context.getCoveringNode(); |
| 62 | + if (coveringNode == null) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + MethodDeclaration methodDecl = getMethodDeclaration(coveringNode); |
| 67 | + if (methodDecl == null) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + if (!isJUnitTestMethod(methodDecl)) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + List<IJavaCompletionProposal> proposals = new ArrayList<>(); |
| 76 | + |
| 77 | + boolean hasDisabledAnnotation = hasAnnotation(methodDecl, JUNIT5_DISABLED_ANNOTATION); |
| 78 | + boolean hasIgnoreAnnotation = hasAnnotation(methodDecl, JUNIT4_IGNORE_ANNOTATION); |
| 79 | + |
| 80 | + if (hasDisabledAnnotation || hasIgnoreAnnotation) { |
| 81 | + // Offer to remove the annotation |
| 82 | + String annotationToRemove = hasDisabledAnnotation ? JUNIT5_DISABLED_ANNOTATION : JUNIT4_IGNORE_ANNOTATION; |
| 83 | + proposals.add(new RemoveAnnotationProposal(context, methodDecl, annotationToRemove)); |
| 84 | + } else { |
| 85 | + // Offer to add the appropriate annotation based on JUnit version |
| 86 | + if (isJUnit5TestMethod(methodDecl)) { |
| 87 | + // JUnit 5 test |
| 88 | + proposals.add(new AddAnnotationProposal(context, methodDecl, JUNIT5_DISABLED_ANNOTATION, "Disabled")); //$NON-NLS-1$ |
| 89 | + } else if (hasAnnotation(methodDecl, JUnitCorePlugin.JUNIT4_ANNOTATION_NAME)) { |
| 90 | + // JUnit 4 test |
| 91 | + proposals.add(new AddAnnotationProposal(context, methodDecl, JUNIT4_IGNORE_ANNOTATION, "Ignore")); //$NON-NLS-1$ |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + if (proposals.isEmpty()) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + return proposals.toArray(new IJavaCompletionProposal[proposals.size()]); |
| 100 | + } |
| 101 | + |
| 102 | + private MethodDeclaration getMethodDeclaration(ASTNode node) { |
| 103 | + while (node != null && !(node instanceof MethodDeclaration)) { |
| 104 | + node = node.getParent(); |
| 105 | + } |
| 106 | + return (MethodDeclaration) node; |
| 107 | + } |
| 108 | + |
| 109 | + private boolean isJUnitTestMethod(MethodDeclaration methodDecl) { |
| 110 | + IMethodBinding binding = methodDecl.resolveBinding(); |
| 111 | + if (binding == null) { |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + IAnnotationBinding[] annotations = binding.getAnnotations(); |
| 116 | + for (IAnnotationBinding annotation : annotations) { |
| 117 | + ITypeBinding annotationType = annotation.getAnnotationType(); |
| 118 | + if (annotationType != null) { |
| 119 | + String qualifiedName = annotationType.getQualifiedName(); |
| 120 | + if (JUnitCorePlugin.JUNIT4_ANNOTATION_NAME.equals(qualifiedName) || |
| 121 | + isJUnit5TestAnnotation(qualifiedName)) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return false; |
| 128 | + } |
| 129 | + |
| 130 | + private boolean isJUnit5TestMethod(MethodDeclaration methodDecl) { |
| 131 | + IMethodBinding binding = methodDecl.resolveBinding(); |
| 132 | + if (binding == null) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + IAnnotationBinding[] annotations = binding.getAnnotations(); |
| 137 | + for (IAnnotationBinding annotation : annotations) { |
| 138 | + ITypeBinding annotationType = annotation.getAnnotationType(); |
| 139 | + if (annotationType != null) { |
| 140 | + String qualifiedName = annotationType.getQualifiedName(); |
| 141 | + if (isJUnit5TestAnnotation(qualifiedName)) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + private boolean isJUnit5TestAnnotation(String qualifiedName) { |
| 151 | + return JUNIT5_TEST_ANNOTATION.equals(qualifiedName) || |
| 152 | + JUNIT5_PARAMETERIZED_TEST_ANNOTATION.equals(qualifiedName) || |
| 153 | + JUNIT5_REPEATED_TEST_ANNOTATION.equals(qualifiedName) || |
| 154 | + JUNIT5_TEST_FACTORY_ANNOTATION.equals(qualifiedName) || |
| 155 | + JUNIT5_TEST_TEMPLATE_ANNOTATION.equals(qualifiedName); |
| 156 | + } |
| 157 | + |
| 158 | + private boolean hasAnnotation(MethodDeclaration methodDecl, String annotationQualifiedName) { |
| 159 | + IMethodBinding binding = methodDecl.resolveBinding(); |
| 160 | + if (binding == null) { |
| 161 | + return false; |
| 162 | + } |
| 163 | + |
| 164 | + IAnnotationBinding[] annotations = binding.getAnnotations(); |
| 165 | + for (IAnnotationBinding annotation : annotations) { |
| 166 | + ITypeBinding annotationType = annotation.getAnnotationType(); |
| 167 | + if (annotationType != null && annotationQualifiedName.equals(annotationType.getQualifiedName())) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return false; |
| 173 | + } |
| 174 | +} |
0 commit comments