KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JUnitQuickFixProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Sebastian Davids <sdavids@gmx.de> - bug 48696
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.ui;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashSet JavaDoc;
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 JavaDoc ASSERT_METHOD_NAMES= new HashSet JavaDoc();
60     
61     public JUnitQuickFixProcessor() {
62         ASSERT_METHOD_NAMES.add("fail"); //$NON-NLS-1$
63
ASSERT_METHOD_NAMES.add("assertTrue"); //$NON-NLS-1$
64
ASSERT_METHOD_NAMES.add("assertFalse"); //$NON-NLS-1$
65
ASSERT_METHOD_NAMES.add("assertEquals"); //$NON-NLS-1$
66
ASSERT_METHOD_NAMES.add("assertNotNull"); //$NON-NLS-1$
67
ASSERT_METHOD_NAMES.add("assertNull"); //$NON-NLS-1$
68
ASSERT_METHOD_NAMES.add("assertSame"); //$NON-NLS-1$
69
ASSERT_METHOD_NAMES.add("assertNotSame"); //$NON-NLS-1$
70
ASSERT_METHOD_NAMES.add("failNotEquals"); //$NON-NLS-1$
71
ASSERT_METHOD_NAMES.add("failSame"); //$NON-NLS-1$
72
ASSERT_METHOD_NAMES.add("failNotSame"); //$NON-NLS-1$
73
}
74
75     /* (non-Javadoc)
76      * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#hasCorrections(org.eclipse.jdt.core.ICompilationUnit, int)
77      */

78     public boolean hasCorrections(ICompilationUnit unit, int problemId) {
79         return problemId == IProblem.UndefinedType || problemId == IProblem.ImportNotFound
80         || problemId == IProblem.UndefinedMethod;
81     }
82     
83     /* (non-Javadoc)
84      * @see org.eclipse.jdt.ui.text.java.IQuickFixProcessor#getCorrections(org.eclipse.jdt.ui.text.java.IInvocationContext, org.eclipse.jdt.ui.text.java.IProblemLocation[])
85      */

86     public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations) {
87         ArrayList JavaDoc 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 JavaDoc getAddAssertImportProposals(IInvocationContext context, IProblemLocation problem, ArrayList JavaDoc proposals) {
104         String JavaDoc[] args= problem.getProblemArguments();
105         if (args.length > 1) {
106             String JavaDoc methodName= args[1];
107             if (ASSERT_METHOD_NAMES.contains(methodName) && isInsideJUnit4Test(context)) {
108                 if (proposals == null) {
109                     proposals= new ArrayList JavaDoc();
110                 }
111                 proposals.add(new AddAssertProposal(context.getASTRoot(), methodName, 9));
112                 proposals.add(new AddAssertProposal(context.getASTRoot(), "*", 10)); //$NON-NLS-1$
113
}
114         }
115         return proposals;
116     }
117
118     private ArrayList JavaDoc getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList JavaDoc proposals) {
119         try {
120             ICompilationUnit unit= context.getCompilationUnit();
121             int res= 0;
122             String JavaDoc s= unit.getBuffer().getText(location.getOffset(), location.getLength());
123             if (s.equals("org.junit")) { //$NON-NLS-1$
124
res= JUNIT4;
125             } else if (s.equals("TestCase") || s.equals("TestSuite") || s.equals("junit")) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
126
res= JUNIT3;
127             } else if (s.equals("Test")) { //$NON-NLS-1$
128
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")) { //$NON-NLS-1$
135
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 JavaDoc();
142                     }
143                     proposals.add(new JUnitAddLibraryProposal(true, context, 10));
144                 }
145                 if ((res & JUNIT3) != 0) {
146                     if (proposals == null) {
147                         proposals= new ArrayList JavaDoc();
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 JavaDoc fMethodName;
185         private final int fRelevance;
186
187         public AddAssertProposal(CompilationUnit astRoot, String JavaDoc methodName, int relevance) {
188             fAstRoot= astRoot;
189             fMethodName= methodName;
190             fRelevance= relevance;
191         }
192         
193         /* (non-Javadoc)
194          * @see org.eclipse.jdt.ui.text.java.IJavaCompletionProposal#getRelevance()
195          */

196         public int getRelevance() {
197             return fRelevance;
198         }
199
200         /* (non-Javadoc)
201          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#apply(org.eclipse.jface.text.IDocument)
202          */

203         public void apply(IDocument document) {
204             try {
205                 ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(fAstRoot, true);
206                 rewrite.addStaticImport("org.junit.Assert", fMethodName, true); //$NON-NLS-1$
207
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         /* (non-Javadoc)
216          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getAdditionalProposalInfo()
217          */

218         public String JavaDoc getAdditionalProposalInfo() {
219             return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_info, fMethodName);
220         }
221
222         /* (non-Javadoc)
223          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getContextInformation()
224          */

225         public IContextInformation getContextInformation() {
226             return null;
227         }
228
229         /* (non-Javadoc)
230          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getDisplayString()
231          */

232         public String JavaDoc getDisplayString() {
233             return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_description, fMethodName);
234         }
235
236         /* (non-Javadoc)
237          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getImage()
238          */

239         public Image getImage() {
240             return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_IMPDECL);
241         }
242
243         /* (non-Javadoc)
244          * @see org.eclipse.jface.text.contentassist.ICompletionProposal#getSelection(org.eclipse.jface.text.IDocument)
245          */

246         public Point getSelection(IDocument document) {
247             return null;
248         }
249     }
250     
251 }
252
Popular Tags