1 12 package org.eclipse.jdt.internal.junit.ui; 13 14 import java.util.ArrayList ; 15 import java.util.HashSet ; 16 17 import org.eclipse.text.edits.MalformedTreeException; 18 import org.eclipse.text.edits.TextEdit; 19 20 import org.eclipse.core.runtime.CoreException; 21 22 import org.eclipse.swt.graphics.Image; 23 import org.eclipse.swt.graphics.Point; 24 25 import org.eclipse.jface.text.BadLocationException; 26 import org.eclipse.jface.text.IDocument; 27 import org.eclipse.jface.text.contentassist.IContextInformation; 28 29 import org.eclipse.jdt.core.ICompilationUnit; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.core.JavaModelException; 32 import org.eclipse.jdt.core.compiler.IProblem; 33 import org.eclipse.jdt.core.dom.ASTNode; 34 import org.eclipse.jdt.core.dom.BodyDeclaration; 35 import org.eclipse.jdt.core.dom.CompilationUnit; 36 import org.eclipse.jdt.core.dom.IAnnotationBinding; 37 import org.eclipse.jdt.core.dom.IMethodBinding; 38 import org.eclipse.jdt.core.dom.ITypeBinding; 39 import org.eclipse.jdt.core.dom.MarkerAnnotation; 40 import org.eclipse.jdt.core.dom.MethodDeclaration; 41 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 42 43 import org.eclipse.jdt.ui.CodeStyleConfiguration; 44 import org.eclipse.jdt.ui.ISharedImages; 45 import org.eclipse.jdt.ui.JavaUI; 46 import org.eclipse.jdt.ui.text.java.IInvocationContext; 47 import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; 48 import org.eclipse.jdt.ui.text.java.IProblemLocation; 49 import org.eclipse.jdt.ui.text.java.IQuickFixProcessor; 50 51 import org.eclipse.jdt.internal.junit.Messages; 52 import org.eclipse.jdt.internal.junit.util.JUnitStubUtility; 53 54 public class JUnitQuickFixProcessor implements IQuickFixProcessor { 55 56 private static final int JUNIT3= 1; 57 private static final int JUNIT4= 2; 58 59 private static final HashSet ASSERT_METHOD_NAMES= new HashSet (); 60 61 public JUnitQuickFixProcessor() { 62 ASSERT_METHOD_NAMES.add("fail"); ASSERT_METHOD_NAMES.add("assertTrue"); ASSERT_METHOD_NAMES.add("assertFalse"); ASSERT_METHOD_NAMES.add("assertEquals"); ASSERT_METHOD_NAMES.add("assertNotNull"); ASSERT_METHOD_NAMES.add("assertNull"); ASSERT_METHOD_NAMES.add("assertSame"); ASSERT_METHOD_NAMES.add("assertNotSame"); ASSERT_METHOD_NAMES.add("failNotEquals"); ASSERT_METHOD_NAMES.add("failSame"); ASSERT_METHOD_NAMES.add("failNotSame"); } 74 75 78 public boolean hasCorrections(ICompilationUnit unit, int problemId) { 79 return problemId == IProblem.UndefinedType || problemId == IProblem.ImportNotFound 80 || problemId == IProblem.UndefinedMethod; 81 } 82 83 86 public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations) { 87 ArrayList res= null; 88 for (int i= 0; i < locations.length; i++) { 89 IProblemLocation problem= locations[i]; 90 int id= problem.getProblemId(); 91 if (IProblem.UndefinedType == id || IProblem.ImportNotFound == id) { 92 res= getAddJUnitToBuildPathProposals(context, problem, res); 93 } else if (id == IProblem.UndefinedMethod) { 94 res= getAddAssertImportProposals(context, problem, res); 95 } 96 } 97 if (res == null || res.isEmpty()) { 98 return null; 99 } 100 return (IJavaCompletionProposal[]) res.toArray(new IJavaCompletionProposal[res.size()]); 101 } 102 103 private ArrayList getAddAssertImportProposals(IInvocationContext context, IProblemLocation problem, ArrayList proposals) { 104 String [] args= problem.getProblemArguments(); 105 if (args.length > 1) { 106 String methodName= args[1]; 107 if (ASSERT_METHOD_NAMES.contains(methodName) && isInsideJUnit4Test(context)) { 108 if (proposals == null) { 109 proposals= new ArrayList (); 110 } 111 proposals.add(new AddAssertProposal(context.getASTRoot(), methodName, 9)); 112 proposals.add(new AddAssertProposal(context.getASTRoot(), "*", 10)); } 114 } 115 return proposals; 116 } 117 118 private ArrayList getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList proposals) { 119 try { 120 ICompilationUnit unit= context.getCompilationUnit(); 121 int res= 0; 122 String s= unit.getBuffer().getText(location.getOffset(), location.getLength()); 123 if (s.equals("org.junit")) { res= JUNIT4; 125 } else if (s.equals("TestCase") || s.equals("TestSuite") || s.equals("junit")) { res= JUNIT3; 127 } else if (s.equals("Test")) { ASTNode node= location.getCoveredNode(context.getASTRoot()); 129 if (node != null && node.getLocationInParent() == MarkerAnnotation.TYPE_NAME_PROPERTY) { 130 res= JUNIT4; 131 } else { 132 res= JUNIT3 | JUNIT4; 133 } 134 } else if (s.equals("RunWith")) { res= JUNIT4; 136 } 137 if (res != 0) { 138 IJavaProject javaProject= unit.getJavaProject(); 139 if (JUnitStubUtility.is50OrHigher(javaProject) && ((res & JUNIT4) != 0)) { 140 if (proposals == null) { 141 proposals= new ArrayList (); 142 } 143 proposals.add(new JUnitAddLibraryProposal(true, context, 10)); 144 } 145 if ((res & JUNIT3) != 0) { 146 if (proposals == null) { 147 proposals= new ArrayList (); 148 } 149 proposals.add(new JUnitAddLibraryProposal(false, context, 8)); 150 } 151 } 152 } catch (JavaModelException e) { 153 JUnitPlugin.log(e.getStatus()); 154 } 155 return proposals; 156 } 157 158 private boolean isInsideJUnit4Test(IInvocationContext context) { 159 if (!JUnitStubUtility.is50OrHigher(context.getCompilationUnit().getJavaProject())) { 160 return false; 161 } 162 163 ASTNode node= context.getCoveringNode(); 164 while (node != null && !(node instanceof BodyDeclaration)) { 165 node= node.getParent(); 166 } 167 if (node instanceof MethodDeclaration) { 168 IMethodBinding binding= ((MethodDeclaration) node).resolveBinding(); 169 if (binding != null) { 170 IAnnotationBinding[] annotations= binding.getAnnotations(); 171 for (int i= 0; i < annotations.length; i++) { 172 final ITypeBinding annotationType= annotations[i].getAnnotationType(); 173 if (annotationType != null && JUnitPlugin.JUNIT4_ANNOTATION_NAME.equals(annotationType.getQualifiedName())) 174 return true; 175 } 176 } 177 } 178 return false; 179 } 180 181 private static class AddAssertProposal implements IJavaCompletionProposal { 182 183 private final CompilationUnit fAstRoot; 184 private final String fMethodName; 185 private final int fRelevance; 186 187 public AddAssertProposal(CompilationUnit astRoot, String methodName, int relevance) { 188 fAstRoot= astRoot; 189 fMethodName= methodName; 190 fRelevance= relevance; 191 } 192 193 196 public int getRelevance() { 197 return fRelevance; 198 } 199 200 203 public void apply(IDocument document) { 204 try { 205 ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(fAstRoot, true); 206 rewrite.addStaticImport("org.junit.Assert", fMethodName, true); TextEdit edit= rewrite.rewriteImports(null); 208 edit.apply(document); 209 } catch (MalformedTreeException e) { 210 } catch (CoreException e) { 211 } catch (BadLocationException e) { 212 } 213 } 214 215 218 public String getAdditionalProposalInfo() { 219 return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_info, fMethodName); 220 } 221 222 225 public IContextInformation getContextInformation() { 226 return null; 227 } 228 229 232 public String getDisplayString() { 233 return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_description, fMethodName); 234 } 235 236 239 public Image getImage() { 240 return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_IMPDECL); 241 } 242 243 246 public Point getSelection(IDocument document) { 247 return null; 248 } 249 } 250 251 } 252 | Popular Tags |