diff options
author | Christian W. Damus | 2017-12-23 21:18:00 +0000 |
---|---|---|
committer | Christian W. Damus | 2017-12-23 21:27:27 +0000 |
commit | 791896186a78d125dd13fb1330c65f0067190599 (patch) | |
tree | ab6a190416f48210f6c383ab1ddec59222547298 /tests/junit/plugins | |
parent | c77068fcaf36dbf4c16f5e09a913d31066bbd8b5 (diff) | |
download | org.eclipse.papyrus-791896186a78d125dd13fb1330c65f0067190599.tar.gz org.eclipse.papyrus-791896186a78d125dd13fb1330c65f0067190599.tar.xz org.eclipse.papyrus-791896186a78d125dd13fb1330c65f0067190599.zip |
Bug 528343: [I18N] Preference checking overhead for models that don't use i18n
Fix test failures on build machine that is configured for en_US locale.
Also fix new listener API for preference changes that didn't account for
changes applied directly to the preference store (some code does this).
https://bugs.eclipse.org/bugs/show_bug.cgi?id=528343
Change-Id: I7cbe89117bb8cd410ec871dacce78e65937b6880
Diffstat (limited to 'tests/junit/plugins')
6 files changed, 133 insertions, 29 deletions
diff --git a/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/AllTests.java b/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/AllTests.java index 4c35b26bf5d..cd06c0291f7 100644 --- a/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/AllTests.java +++ b/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/AllTests.java @@ -14,15 +14,15 @@ package org.eclipse.papyrus.infra.internationalization.tests.tests; -import org.junit.runner.RunWith; import org.eclipse.papyrus.junit.framework.classification.ClassificationSuite; +import org.junit.runner.RunWith; import org.junit.runners.Suite.SuiteClasses; @RunWith(ClassificationSuite.class) -@SuiteClasses({ - InternationalizationLabelProviderTest.class, - InternationalizationChangePreferencesTest.class, - InternationalizationPreferenceChangeEventTest.class, +@SuiteClasses({ + InternationalizationLabelProviderTest.class, + InternationalizationChangePreferencesTest.class, + InternationalizationPreferenceChangeEventTest.class, }) public class AllTests { diff --git a/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/InternationalizationPreferenceChangeEventTest.java b/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/InternationalizationPreferenceChangeEventTest.java index efc8d1ae094..3c420bb756b 100644 --- a/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/InternationalizationPreferenceChangeEventTest.java +++ b/tests/junit/plugins/infra/internationalization/org.eclipse.papyrus.infra.internationalization.tests/src/org/eclipse/papyrus/infra/internationalization/tests/tests/InternationalizationPreferenceChangeEventTest.java @@ -12,13 +12,16 @@ package org.eclipse.papyrus.infra.internationalization.tests.tests; import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesConstants.LANGUAGE_PREFERENCE; +import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesConstants.USE_INTERNATIONALIZATION_PREFERENCE; import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils.getInternationalizationPreference; import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils.getPreferenceStore; import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils.setInternationalizationPreference; import static org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils.setLanguagePreference; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assume.assumeThat; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; @@ -26,6 +29,7 @@ import java.util.function.IntSupplier; import org.eclipse.emf.ecore.EObject; import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.papyrus.infra.internationalization.common.Activator; import org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferenceChangeEvent; import org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferenceListener; import org.eclipse.papyrus.infra.internationalization.common.utils.InternationalizationPreferencesUtils; @@ -94,6 +98,51 @@ public class InternationalizationPreferenceChangeEventTest extends AbstractInter } /** + * Verify the notification that enablement of internationalization changes by direct + * manipulation of the preference store. + */ + @Test + public void enablementNotificationByPreferences() { + IntSupplier count = listen(InternationalizationPreferenceChangeEvent.ENABLED, diagram, + event -> assertThat(event.isInternationalizationEnabled(), is(false))); + + IPreferenceStore prefStore = Activator.getDefault().getInternationalizationPreferenceStore(fixture.getProject().getProject(), + fixture.getModelURI().trimFileExtension().lastSegment()); + assumeThat("No preference store", prefStore, notNullValue()); + prefStore.setValue(USE_INTERNATIONALIZATION_PREFERENCE, "false"); // Most primitive access + assertThat(count.getAsInt(), is(1)); + + count = listen(InternationalizationPreferenceChangeEvent.ENABLED, diagram, + event -> assertThat(event.isInternationalizationEnabled(), is(true))); + + prefStore.setValue(USE_INTERNATIONALIZATION_PREFERENCE, "true"); // Most primitive access + assertThat(count.getAsInt(), is(1)); + } + + /** + * Verify the notification that the locale of internationalization changes by direct + * manipulation of the preference store. + */ + @Test + public void localeNotificationByPreferences() { + IntSupplier count = listen(InternationalizationPreferenceChangeEvent.LOCALE, diagram, + event -> assertThat(event.getLocale(), is(Locale.CANADA_FRENCH))); + + IPreferenceStore prefStore = Activator.getDefault().getInternationalizationPreferenceStore(fixture.getProject().getProject(), + fixture.getModelURI().trimFileExtension().lastSegment()); + assumeThat("No preference store", prefStore, notNullValue()); + prefStore.setValue(LANGUAGE_PREFERENCE, Locale.CANADA_FRENCH.toLanguageTag()); + assertThat(count.getAsInt(), is(1)); + + Locale en_US = Locale.forLanguageTag("en_US"); + count = listen(InternationalizationPreferenceChangeEvent.LOCALE, diagram, + event -> assertThat(event.getLocale(), is(en_US))); + + prefStore.setValue(LANGUAGE_PREFERENCE, "en_US"); + assertThat(count.getAsInt(), is(1)); + } + + /** * Verify that a removed listener gets no notifications. */ @Test diff --git a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/build.properties b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/build.properties index 5cf7533f370..cf5cd8e0c9d 100644 --- a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/build.properties +++ b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/build.properties @@ -3,7 +3,6 @@ bin.includes = .,\ META-INF/,\ about.html,\ - plugin.xml,\ plugin.properties,\ resources/ jars.compile.order = . diff --git a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/plugin.xml b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/plugin.xml deleted file mode 100644 index 7ac51d15095..00000000000 --- a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/plugin.xml +++ /dev/null @@ -1,9 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<?eclipse version="3.0"?> - -<!-- ---> - -<plugin> - -</plugin> diff --git a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/DefaultLocaleRule.java b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/DefaultLocaleRule.java new file mode 100644 index 00000000000..7d8b16146b1 --- /dev/null +++ b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/DefaultLocaleRule.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2017 Christian W. Damus 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Christian W. Damus - initial API and implementation + *******************************************************************************/ +package org.eclipse.papyrus.uml.internationalization.tests; + +import java.util.Locale; + +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * A JUnit rule that sets the VM's default locale for the duration of a test. + * + * @author Christian W. Damus + */ +public class DefaultLocaleRule implements TestRule { + + private final Locale locale; + + private Locale restore; + + public DefaultLocaleRule(Locale locale) { + super(); + + this.locale = locale; + } + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + restore = Locale.getDefault(); + + Locale.setDefault(locale); + + try { + base.evaluate(); + } finally { + Locale.setDefault(restore); + } + } + }; + } + +} diff --git a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/tests/InternationalizationUMLItemProviderAdapterFactoryTest.java b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/tests/InternationalizationUMLItemProviderAdapterFactoryTest.java index 9a82fdb2f59..987bb9ef05b 100644 --- a/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/tests/InternationalizationUMLItemProviderAdapterFactoryTest.java +++ b/tests/junit/plugins/uml/internationalization/org.eclipse.papyrus.uml.internationalization.tests/src/org/eclipse/papyrus/uml/internationalization/tests/tests/InternationalizationUMLItemProviderAdapterFactoryTest.java @@ -19,6 +19,8 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.hamcrest.MatcherAssert.assertThat; +import java.util.Locale; + import org.eclipse.emf.common.notify.Adapter; import org.eclipse.emf.common.notify.AdapterFactory; import org.eclipse.emf.ecore.EObject; @@ -27,7 +29,9 @@ import org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider; import org.eclipse.jface.viewers.ILabelProvider; import org.eclipse.papyrus.junit.utils.rules.PluginResource; import org.eclipse.papyrus.uml.internationalization.edit.providers.InternationalizationUMLItemProviderAdapterFactory; +import org.eclipse.papyrus.uml.internationalization.tests.DefaultLocaleRule; import org.junit.After; +import org.junit.ClassRule; import org.junit.Test; /** @@ -36,11 +40,17 @@ import org.junit.Test; * @author Christian W. Damus */ @SuppressWarnings("nls") -@PluginResource({"resources/model.di", "resources/model_en_US.properties", "resources/model_fr_FR.properties"}) +@PluginResource({ "resources/model.di", "resources/model_en_US.properties", "resources/model_fr_FR.properties" }) public class InternationalizationUMLItemProviderAdapterFactoryTest extends AbstractUMLInternationalizationTest { + // Set a locale in the VM that does not have a corresponding properties file, otherwise + // if the host system happens to be en_US then these tests will fail because the framework + // will default to enabling internationalization + @ClassRule + public static final DefaultLocaleRule defaultLocale = new DefaultLocaleRule(Locale.KOREAN); + private AdapterFactory factory; - + /** * Initializes e. */ @@ -51,7 +61,7 @@ public class InternationalizationUMLItemProviderAdapterFactoryTest extends Abstr @Test public void normalAdapters() { checkUMLNoLabels(); - + // The last adapter added should be our item-provider Adapter last = modelClass.eAdapters().get(modelClass.eAdapters().size() - 1); assertThat(last.getClass().getSimpleName(), is("ClassItemProvider")); @@ -61,20 +71,20 @@ public class InternationalizationUMLItemProviderAdapterFactoryTest extends Abstr public void rebuildAdapters() { int initialAdapterCount = modelClass.eAdapters().size(); checkUMLNoLabels(); - + int newAdapterCount = modelClass.eAdapters().size(); assertThat(newAdapterCount, greaterThan(initialAdapterCount)); - + Adapter adapter = modelClass.eAdapters().get(newAdapterCount - 1); - + setLanguagePreference(modelClass, "fr_FR"); setInternationalizationPreference(modelClass, true); - + // Purged the adapters assertThat(modelClass.eAdapters().size(), lessThan(newAdapterCount)); - + checkUMLFrenchLabels(); - + // Just replaced the adapter with a new one assertThat(modelClass.eAdapters().size(), is(newAdapterCount)); Adapter newAdapter = modelClass.eAdapters().get(newAdapterCount - 1); @@ -84,21 +94,21 @@ public class InternationalizationUMLItemProviderAdapterFactoryTest extends Abstr // // Test framework // - + @Override protected ILabelProvider initLabelProvider() { factory = new InternationalizationUMLItemProviderAdapterFactory(); - + // Initially disable the i18n setInternationalizationPreference(fixture.getModelResource(), false); - + return new AdapterFactoryLabelProvider(factory); } - + @After public void disposeFactory() { labelProvider.dispose(); - ((IDisposable)factory).dispose(); + ((IDisposable) factory).dispose(); } /** |