diff options
| author | Noopur Gupta | 2017-03-16 13:02:36 +0000 |
|---|---|---|
| committer | Noopur Gupta | 2017-03-16 13:02:36 +0000 |
| commit | b0675f3a1449aa34e74ef66f86af7a061f5182cf (patch) | |
| tree | 228f770c4ce9bbce682d1c3d48aeb264879fa3a6 | |
| parent | 2bc40c9df4decaa1b17c16a14cfb2d354ea32136 (diff) | |
| download | eclipse.jdt.ui-b0675f3a1449aa34e74ef66f86af7a061f5182cf.tar.gz eclipse.jdt.ui-b0675f3a1449aa34e74ef66f86af7a061f5182cf.tar.xz eclipse.jdt.ui-b0675f3a1449aa34e74ef66f86af7a061f5182cf.zip | |
Fixed bug 511443: [JUnit 5][content assist][quick fix] Set default
favorites for static imports
Change-Id: If54774880edc1b76084b0383bae0d9d7235c0b14
4 files changed, 72 insertions, 32 deletions
diff --git a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/JunitPreferenceInitializer.java b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/JunitPreferenceInitializer.java index 02312ffd24..0dc750c15c 100644 --- a/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/JunitPreferenceInitializer.java +++ b/org.eclipse.jdt.junit.core/src/org/eclipse/jdt/internal/junit/JunitPreferenceInitializer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -13,12 +13,9 @@ package org.eclipse.jdt.internal.junit; import java.util.List; -import org.osgi.service.prefs.BackingStoreException; - import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; -import org.eclipse.core.runtime.preferences.InstanceScope; /** * Default preference value initialization for the @@ -47,25 +44,5 @@ public class JunitPreferenceInitializer extends AbstractPreferenceInitializer { prefs.put(JUnitPreferencesConstants.JUNIT3_JAVADOC, "http://junit.sourceforge.net/junit3.8.1/javadoc/"); //$NON-NLS-1$ prefs.put(JUnitPreferencesConstants.JUNIT4_JAVADOC, "http://junit-team.github.io/junit/javadoc/latest/"); //$NON-NLS-1$ prefs.put(JUnitPreferencesConstants.HAMCREST_CORE_JAVADOC, "http://hamcrest.org/JavaHamcrest/javadoc/1.3/"); //$NON-NLS-1$ - - // migrate old instance scope prefs - try { - IEclipsePreferences newInstancePrefs= InstanceScope.INSTANCE.getNode(JUnitCorePlugin.CORE_PLUGIN_ID); - - if (newInstancePrefs.keys().length == 0) { - IEclipsePreferences oldInstancePrefs= InstanceScope.INSTANCE.getNode(JUnitCorePlugin.PLUGIN_ID); - - String[] oldKeys= oldInstancePrefs.keys(); - for (int i= 0; i < oldKeys.length; i++ ) { - String key= oldKeys[i]; - newInstancePrefs.put(key, oldInstancePrefs.get(key, null)); - oldInstancePrefs.remove(key); - } - newInstancePrefs.flush(); - oldInstancePrefs.flush(); - } - } catch (BackingStoreException e) { - JUnitCorePlugin.log(e); - } } } diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitPlugin.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitPlugin.java index 8257678f0d..732be2d42f 100644 --- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitPlugin.java +++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitPlugin.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -16,7 +16,11 @@ package org.eclipse.jdt.internal.junit.ui; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; @@ -46,6 +50,8 @@ import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PartInitException; import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.eclipse.jdt.ui.PreferenceConstants; + /** * The plug-in runtime class for the JUnit plug-in. */ @@ -187,18 +193,55 @@ public class JUnitPlugin extends AbstractUIPlugin { return null; } - /** - * @see AbstractUIPlugin#start(BundleContext) - */ @Override public void start(BundleContext context) throws Exception { super.start(context); fBundleContext= context; + setCodeassistFavoriteStaticMembers(); } /** - * @see AbstractUIPlugin#stop(BundleContext) + * Add the new default static import favorites in old workspaces that already have non-default + * favorites. Only do this once, so that users have a way to opt-out if they don't want the new + * favorites. */ + private void setCodeassistFavoriteStaticMembers() { + Set<String> favoritesToAdd= new LinkedHashSet<>(); + favoritesToAdd.add("org.junit.Assert.*"); //$NON-NLS-1$ + favoritesToAdd.add("org.junit.Assume.*"); //$NON-NLS-1$ + favoritesToAdd.add("org.junit.jupiter.api.Assertions.*"); //$NON-NLS-1$ + favoritesToAdd.add("org.junit.jupiter.api.Assumptions.*"); //$NON-NLS-1$ + favoritesToAdd.add("org.junit.jupiter.api.DynamicTest.*"); //$NON-NLS-1$ + + // default value + Set<String> defaultFavorites= new LinkedHashSet<>(); + String defaultPreferenceValue= PreferenceConstants.getPreferenceStore().getDefaultString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS); + if (defaultPreferenceValue != null && defaultPreferenceValue.length() > 0) { + defaultFavorites.addAll(Arrays.asList(defaultPreferenceValue.split(";"))); //$NON-NLS-1$ + } + defaultFavorites.addAll(favoritesToAdd); + String newDefaultPreferenceValue= defaultFavorites.stream().collect(Collectors.joining(";")); //$NON-NLS-1$ + PreferenceConstants.getPreferenceStore().setDefault(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, newDefaultPreferenceValue); + + // current value + if (JUnitUIPreferencesConstants.isCodeassistFavoriteStaticMembersMigrated()) { + return; + } + Set<String> currentFavorites= new LinkedHashSet<>(); + String currentPreferenceValue= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS); + if (currentPreferenceValue != null && currentPreferenceValue.length() > 0) { + currentFavorites.addAll(Arrays.asList(currentPreferenceValue.split(";"))); //$NON-NLS-1$ + } + favoritesToAdd.removeAll(currentFavorites); + if (!favoritesToAdd.isEmpty()) { + String newPreferenceValue= currentPreferenceValue + ";" + favoritesToAdd.stream().collect(Collectors.joining(";")); //$NON-NLS-1$ //$NON-NLS-2$ + PreferenceConstants.getPreferenceStore().setValue(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, newPreferenceValue); + } + + // set as migrated + JUnitUIPreferencesConstants.setCodeassistFavoriteStaticMembersMigrated(true); + } + @Override public void stop(BundleContext context) throws Exception { fIsStopped= true; diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java index 3009998aa2..a85676f174 100644 --- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java +++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitQuickFixProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -65,6 +65,7 @@ import org.eclipse.jdt.internal.junit.util.JUnitStubUtility; import org.eclipse.jdt.ui.CodeStyleConfiguration; import org.eclipse.jdt.ui.ISharedImages; import org.eclipse.jdt.ui.JavaUI; +import org.eclipse.jdt.ui.PreferenceConstants; import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor; import org.eclipse.jdt.ui.text.java.ClasspathFixProcessor.ClasspathFixProposal; import org.eclipse.jdt.ui.text.java.IInvocationContext; @@ -103,7 +104,10 @@ public class JUnitQuickFixProcessor implements IQuickFixProcessor { if (IProblem.UndefinedType == id) { res= getAddJUnitToBuildPathProposals(context, problem, res); } else if (id == IProblem.UndefinedMethod) { - res= getAddAssertImportProposals(context, problem, res); + String currentPreferenceValue= PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS); + if (!currentPreferenceValue.contains("org.junit.Assert.*")) { //$NON-NLS-1$ + res= getAddAssertImportProposals(context, problem, res); + } } } if (res == null || res.isEmpty()) { diff --git a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitUIPreferencesConstants.java b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitUIPreferencesConstants.java index e1079681bd..c730610916 100644 --- a/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitUIPreferencesConstants.java +++ b/org.eclipse.jdt.junit/src/org/eclipse/jdt/internal/junit/ui/JUnitUIPreferencesConstants.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2016 IBM Corporation and others. + * Copyright (c) 2010, 2017 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,6 +30,8 @@ public class JUnitUIPreferencesConstants { public static final boolean SHOW_IN_ALL_VIEWS_DEFAULT= false; // would need a PreferenceInitializer if this was changed to true! + private static final String CODEASSIST_FAVORITE_STATIC_MEMBERS_MIGRATED= JUnitPlugin.PLUGIN_ID + ".content_assist_favorite_static_members_migrated"; //$NON-NLS-1$ + private JUnitUIPreferencesConstants() { // no instance } @@ -47,4 +49,18 @@ public class JUnitUIPreferencesConstants { JUnitPlugin.log(e); } } + + public static boolean isCodeassistFavoriteStaticMembersMigrated() { + return Platform.getPreferencesService().getBoolean(JUnitPlugin.PLUGIN_ID, CODEASSIST_FAVORITE_STATIC_MEMBERS_MIGRATED, false, null); + } + + public static void setCodeassistFavoriteStaticMembersMigrated(boolean migrated) { + IEclipsePreferences preferences= InstanceScope.INSTANCE.getNode(JUnitPlugin.PLUGIN_ID); + preferences.putBoolean(CODEASSIST_FAVORITE_STATIC_MEMBERS_MIGRATED, migrated); + try { + preferences.flush(); + } catch (BackingStoreException e) { + JUnitPlugin.log(e); + } + } } |
