Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 511443 | Differences between
and this patch

Collapse All | Expand All

(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitPlugin.java (-7 / +28 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
2
 * Copyright (c) 2000, 2017 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 16-22 Link Here
16
16
17
import java.net.URL;
17
import java.net.URL;
18
import java.util.ArrayList;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.HashSet;
19
import java.util.List;
21
import java.util.List;
22
import java.util.Set;
23
import java.util.stream.Collectors;
20
24
21
import org.osgi.framework.Bundle;
25
import org.osgi.framework.Bundle;
22
import org.osgi.framework.BundleContext;
26
import org.osgi.framework.BundleContext;
Lines 45-50 Link Here
45
import org.eclipse.ui.IWorkbenchWindow;
49
import org.eclipse.ui.IWorkbenchWindow;
46
import org.eclipse.ui.PartInitException;
50
import org.eclipse.ui.PartInitException;
47
import org.eclipse.ui.plugin.AbstractUIPlugin;
51
import org.eclipse.ui.plugin.AbstractUIPlugin;
52
53
import org.eclipse.jdt.ui.PreferenceConstants;
48
54
49
/**
55
/**
50
 * The plug-in runtime class for the JUnit plug-in.
56
 * The plug-in runtime class for the JUnit plug-in.
Lines 187-204 Link Here
187
		return null;
193
		return null;
188
	}
194
	}
189
195
190
	/**
191
	 * @see AbstractUIPlugin#start(BundleContext)
192
	 */
193
	@Override
196
	@Override
194
	public void start(BundleContext context) throws Exception {
197
	public void start(BundleContext context) throws Exception {
195
		super.start(context);
198
		super.start(context);
196
		fBundleContext= context;
199
		fBundleContext= context;
200
		setDefaultFavoriteStaticMembers();
197
	}
201
	}
198
202
199
	/**
203
	private void setDefaultFavoriteStaticMembers() {
200
	 * @see AbstractUIPlugin#stop(BundleContext)
204
		Set<String> favorites= new HashSet<>();
201
	 */
205
		favorites.add("org.junit.Assert.*"); //$NON-NLS-1$
206
		favorites.add("org.junit.Assume.*"); //$NON-NLS-1$
207
		favorites.add("org.junit.jupiter.api.Assertions.*"); //$NON-NLS-1$
208
		favorites.add("org.junit.jupiter.api.Assumptions.*"); //$NON-NLS-1$
209
		favorites.add("org.junit.jupiter.api.DynamicTest.*"); //$NON-NLS-1$
210
211
		String currentPreferenceValue= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
212
		if (currentPreferenceValue != null && currentPreferenceValue.length() > 0) {
213
			Set<String> currentFavorites= new HashSet<>(Arrays.asList(currentPreferenceValue.split(";"))); //$NON-NLS-1$
214
			if (currentFavorites.size() > 0) {
215
				favorites.addAll(currentFavorites);
216
			}
217
		}
218
219
		String newPreferenceValue= favorites.stream().collect(Collectors.joining(";")); //$NON-NLS-1$
220
		PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, newPreferenceValue);
221
	}
222
202
	@Override
223
	@Override
203
	public void stop(BundleContext context) throws Exception {
224
	public void stop(BundleContext context) throws Exception {
204
		fIsStopped= true;
225
		fIsStopped= true;
(-)a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java (-117 / +3 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2016 IBM Corporation and others.
2
 * Copyright (c) 2000, 2017 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 25-36 Link Here
25
25
26
import org.eclipse.core.resources.IFile;
26
import org.eclipse.core.resources.IFile;
27
27
28
import org.eclipse.text.edits.MalformedTreeException;
29
import org.eclipse.text.edits.TextEdit;
30
31
import org.eclipse.jface.operation.IRunnableWithProgress;
28
import org.eclipse.jface.operation.IRunnableWithProgress;
32
29
33
import org.eclipse.jface.text.BadLocationException;
34
import org.eclipse.jface.text.IDocument;
30
import org.eclipse.jface.text.IDocument;
35
import org.eclipse.jface.text.contentassist.IContextInformation;
31
import org.eclipse.jface.text.contentassist.IContextInformation;
36
32
Lines 47-70 Link Here
47
import org.eclipse.jdt.core.JavaModelException;
43
import org.eclipse.jdt.core.JavaModelException;
48
import org.eclipse.jdt.core.compiler.IProblem;
44
import org.eclipse.jdt.core.compiler.IProblem;
49
import org.eclipse.jdt.core.dom.ASTNode;
45
import org.eclipse.jdt.core.dom.ASTNode;
50
import org.eclipse.jdt.core.dom.BodyDeclaration;
51
import org.eclipse.jdt.core.dom.CompilationUnit;
46
import org.eclipse.jdt.core.dom.CompilationUnit;
52
import org.eclipse.jdt.core.dom.IAnnotationBinding;
53
import org.eclipse.jdt.core.dom.IMethodBinding;
54
import org.eclipse.jdt.core.dom.ITypeBinding;
55
import org.eclipse.jdt.core.dom.MarkerAnnotation;
47
import org.eclipse.jdt.core.dom.MarkerAnnotation;
56
import org.eclipse.jdt.core.dom.MethodDeclaration;
57
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
48
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
58
49
59
import org.eclipse.jdt.internal.junit.BasicElementLabels;
60
import org.eclipse.jdt.internal.junit.JUnitCorePlugin;
61
import org.eclipse.jdt.internal.junit.Messages;
62
import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
50
import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
63
import org.eclipse.jdt.internal.junit.util.JUnitStubUtility;
64
51
65
import org.eclipse.jdt.ui.CodeStyleConfiguration;
52
import org.eclipse.jdt.ui.CodeStyleConfiguration;
66
import org.eclipse.jdt.ui.ISharedImages;
67
import org.eclipse.jdt.ui.JavaUI;
68
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor;
53
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor;
69
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal;
54
import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal;
70
import org.eclipse.jdt.ui.text.java.IInvocationContext;
55
import org.eclipse.jdt.ui.text.java.IInvocationContext;
Lines 92-130 Link Here
92
77
93
	@Override
78
	@Override
94
	public boolean hasCorrections(ICompilationUnit unit, int problemId) {
79
	public boolean hasCorrections(ICompilationUnit unit, int problemId) {
95
		return problemId == IProblem.UndefinedType || problemId ==  IProblem.UndefinedMethod;
80
		return problemId == IProblem.UndefinedType;
96
	}
81
	}
97
82
98
	@Override
83
	@Override
99
	public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations)  {
84
	public IJavaCompletionProposal[] getCorrections(final IInvocationContext context, IProblemLocation[] locations)  {
100
		ArrayList<IJavaCompletionProposal> res= null;
85
		ArrayList<IJavaCompletionProposal> res= null;
101
		for (IProblemLocation problem : locations) {
86
		for (IProblemLocation problem : locations) {
102
			int id= problem.getProblemId();
87
			res= getAddJUnitToBuildPathProposals(context, problem, res);
103
			if (IProblem.UndefinedType == id) {
104
				res= getAddJUnitToBuildPathProposals(context, problem, res);
105
			} else if (id == IProblem.UndefinedMethod) {
106
				res= getAddAssertImportProposals(context, problem, res);
107
			}
108
		}
88
		}
109
		if (res == null || res.isEmpty()) {
89
		if (res == null || res.isEmpty()) {
110
			return null;
90
			return null;
111
		}
91
		}
112
		return res.toArray(new IJavaCompletionProposal[res.size()]);
92
		return res.toArray(new IJavaCompletionProposal[res.size()]);
113
	}
114
115
	private ArrayList<IJavaCompletionProposal> getAddAssertImportProposals(IInvocationContext context, IProblemLocation problem, ArrayList<IJavaCompletionProposal> proposals) {
116
		String[] args= problem.getProblemArguments();
117
		if (args.length > 1) {
118
			String methodName= args[1];
119
			if (ASSERT_METHOD_NAMES.contains(methodName) && isInsideJUnit4Test(context)) {
120
				if (proposals == null) {
121
					proposals= new ArrayList<>();
122
				}
123
				proposals.add(new AddAssertProposal(context.getASTRoot(), methodName, 9));
124
				proposals.add(new AddAssertProposal(context.getASTRoot(), "*", 10)); //$NON-NLS-1$
125
			}
126
		}
127
		return proposals;
128
	}
93
	}
129
94
130
	private ArrayList<IJavaCompletionProposal> getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList<IJavaCompletionProposal> proposals) {
95
	private ArrayList<IJavaCompletionProposal> getAddJUnitToBuildPathProposals(IInvocationContext context, IProblemLocation location, ArrayList<IJavaCompletionProposal> proposals) {
Lines 189-217 Link Here
189
			return importRewrite;
154
			return importRewrite;
190
		}
155
		}
191
		return null;
156
		return null;
192
	}
193
194
	private boolean isInsideJUnit4Test(IInvocationContext context) {
195
		if (!JUnitStubUtility.is50OrHigher(context.getCompilationUnit().getJavaProject())) {
196
			return false;
197
		}
198
199
		ASTNode node= context.getCoveringNode();
200
		while (node != null && !(node instanceof BodyDeclaration)) {
201
			node= node.getParent();
202
		}
203
		if (node instanceof MethodDeclaration) {
204
			IMethodBinding binding= ((MethodDeclaration) node).resolveBinding();
205
			if (binding != null) {
206
				IAnnotationBinding[] annotations= binding.getAnnotations();
207
				for (IAnnotationBinding annotation : annotations) {
208
					final ITypeBinding annotationType= annotation.getAnnotationType();
209
					if (annotationType != null && JUnitCorePlugin.JUNIT4_ANNOTATION_NAME.equals(annotationType.getQualifiedName()))
210
						return true;
211
				}
212
			}
213
		}
214
		return false;
215
	}
157
	}
216
158
217
	private static class JUnitClasspathFixCorrectionProposal implements IJavaCompletionProposal {
159
	private static class JUnitClasspathFixCorrectionProposal implements IJavaCompletionProposal {
Lines 290-351 Link Here
290
		@Override
232
		@Override
291
		public Image getImage() {
233
		public Image getImage() {
292
			return fClasspathFixProposal.getImage();
234
			return fClasspathFixProposal.getImage();
293
		}
294
295
		@Override
296
		public Point getSelection(IDocument document) {
297
			return null;
298
		}
299
	}
300
301
	private static class AddAssertProposal implements IJavaCompletionProposal {
302
303
		private final CompilationUnit fAstRoot;
304
		private final String fMethodName;
305
		private final int fRelevance;
306
307
		public AddAssertProposal(CompilationUnit astRoot, String methodName, int relevance) {
308
			fAstRoot= astRoot;
309
			fMethodName= methodName;
310
			fRelevance= relevance;
311
		}
312
313
		@Override
314
		public int getRelevance() {
315
			return fRelevance;
316
		}
317
318
		@Override
319
		public void apply(IDocument document) {
320
			try {
321
				ImportRewrite rewrite= CodeStyleConfiguration.createImportRewrite(fAstRoot, true);
322
				rewrite.addStaticImport("org.junit.Assert", fMethodName, true); //$NON-NLS-1$
323
				TextEdit edit= rewrite.rewriteImports(null);
324
				edit.apply(document);
325
			} catch (MalformedTreeException e) {
326
			} catch (CoreException e) {
327
			} catch (BadLocationException e) {
328
			}
329
		}
330
331
		@Override
332
		public String getAdditionalProposalInfo() {
333
			return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_info, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$
334
		}
335
336
		@Override
337
		public IContextInformation getContextInformation() {
338
			return null;
339
		}
340
341
		@Override
342
		public String getDisplayString() {
343
			return Messages.format(JUnitMessages.JUnitQuickFixProcessor_add_assert_description, BasicElementLabels.getJavaElementName("org.junit.Assert." + fMethodName)); //$NON-NLS-1$
344
		}
345
346
		@Override
347
		public Image getImage() {
348
			return JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_IMPDECL);
349
		}
235
		}
350
236
351
		@Override
237
		@Override

Return to bug 511443