Skip to content

Commit

Permalink
Merge pull request #312 from cbeust/feautre/154_favorite_static_import
Browse files Browse the repository at this point in the history
Add Eclipse Favorites - for static import of 'org.testng.Assert.*'
  • Loading branch information
missedone authored Jan 31, 2017
2 parents f60bc7a + 72f4532 commit 7f31dbc
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 29 deletions.
6 changes: 6 additions & 0 deletions testng-eclipse-plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,11 @@ public void ${tearDownMethod}() throws Exception {
sectionTitle="TestNG for Eclipse Details">
</section>
</extension>
<extension
point="org.eclipse.ui.startup">
<startup
class="org.testng.eclipse.ui.preferences.JdtPreferenceInjector">
</startup>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public abstract class TestNGPluginConstants {
public static final String S_PRE_DEFINED_LISTENERS = "preDefinedListeners";
public static final String S_SHOW_VIEW_WHEN_TESTS_COMPLETE = "showViewWhenTestComplete";
public static final String S_VIEW_TITLE_SHOW_CASE_NAME = "showCaseNameOnViewTitle";
public static final String S_APPEND_FAVORITE_STATIC_IMPORT = "appendFavoriteStaticImport";

private TestNGPluginConstants() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.testng.eclipse.ui.preferences;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IStartup;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.testng.eclipse.TestNGPlugin;
import org.testng.eclipse.TestNGPluginConstants;

import com.google.common.base.Joiner;

public class JdtPreferenceInjector implements IStartup, IPropertyChangeListener {

private static final String assertClassName = "org.testng.Assert";

@Override
public void earlyStartup() {
initializeFavoriteStatic();
}

@Override
public void propertyChange(PropertyChangeEvent event) {
if (PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS.equals(event.getProperty())) {
// check if user manully removed testng assert
if (event.getOldValue().toString().contains(assertClassName)
&& !event.getNewValue().toString().contains(assertClassName)) {
IPreferenceStore store = TestNGPlugin.getDefault().getPreferenceStore();
store.setValue(TestNGPluginConstants.S_APPEND_FAVORITE_STATIC_IMPORT, false);
try {
((ScopedPreferenceStore) store).save();
} catch (IOException e) {
}
}
}
}

private void initializeFavoriteStatic() {
IPreferenceStore store = TestNGPlugin.getDefault().getPreferenceStore();
boolean appendFavorite = store.getBoolean(TestNGPluginConstants.S_APPEND_FAVORITE_STATIC_IMPORT);
if (!appendFavorite) {
return;
}

IPreferenceStore jdtPrefStore = PreferenceConstants.getPreferenceStore();
jdtPrefStore.addPropertyChangeListener(this);

Set<String> favorites = new LinkedHashSet<>();
String existingFavorites = jdtPrefStore.getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
if (!existingFavorites.isEmpty()) {
favorites.addAll(Arrays.asList(existingFavorites.split(";")));
}

favorites.add(assertClassName + ".*");

jdtPrefStore.setValue(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS,
Joiner.on(";").join(favorites));
try {
((ScopedPreferenceStore) jdtPrefStore).save();
} catch (IOException e) {
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
package org.testng.eclipse.ui.preferences;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
import org.eclipse.jdt.ui.PreferenceConstants;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.ui.preferences.ScopedPreferenceStore;
import org.testng.eclipse.TestNGPlugin;
import org.testng.eclipse.TestNGPluginConstants;

import com.google.common.base.Joiner;

/**
* Class used to initialize default preference values.
*/
Expand All @@ -38,26 +29,8 @@ public void initializeDefaultPreferences() {
true);
store.setDefault(TestNGPluginConstants.S_VIEW_TITLE_SHOW_CASE_NAME,
true);

initializeFavoriteStatic();
store.setDefault(TestNGPluginConstants.S_APPEND_FAVORITE_STATIC_IMPORT,
true);
}

private void initializeFavoriteStatic() {
IPreferenceStore jdtPrefStore = PreferenceConstants.getPreferenceStore();

Set<String> favorites = new LinkedHashSet<>();
String existingFavorites = jdtPrefStore.getString(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS);
if (!existingFavorites.isEmpty()) {
favorites.addAll(Arrays.asList(existingFavorites.split(";")));
}

favorites.add("org.testng.Assert.*");

jdtPrefStore.setValue(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS,
Joiner.on(";").join(favorites));
try {
((ScopedPreferenceStore) jdtPrefStore).save();
} catch (IOException e) {
}
}
}

0 comments on commit 7f31dbc

Please sign in to comment.