Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Fleck2016-10-24 03:50:55 -0400
committerLaurent Delaigue2017-01-13 05:54:04 -0500
commita6cb5f1bd528d972fc41cc14907375aeb5ea13dc (patch)
tree41418caf321337aa34566e918cade551b5b81278 /plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare
parent9a46d2ecdbb0f059ebaef665edb1671de246e327 (diff)
downloadorg.eclipse.emf.compare-a6cb5f1bd528d972fc41cc14907375aeb5ea13dc.tar.gz
org.eclipse.emf.compare-a6cb5f1bd528d972fc41cc14907375aeb5ea13dc.tar.xz
org.eclipse.emf.compare-a6cb5f1bd528d972fc41cc14907375aeb5ea13dc.zip
[483798] Introduce and use context in adapter factories
- Provide context tester for fine-grained selection of adapter factory - Extend extension point with optional context tester - Allow context to be given during creation of adapter factory registry - Fire event on adapter factory change in compare configuration - Add listeners to propagate new adapter factory after context change Includes tests. Bug: 483798 Change-Id: Ib0a1a3538a8cf705ea82620bde625cebfd68636c Signed-off-by: Martin Fleck <mfleck@eclipsesource.com> Signed-off-by: Philip Langer <planger@eclipsesource.com>
Diffstat (limited to 'plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare')
-rw-r--r--plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/adapterfactory/EMFCompareAdapterFactoryTest.java8
-rw-r--r--plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/registry/RankedAdapterFactoryRegistryTest.java340
-rw-r--r--plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/suite/AllTests.java3
3 files changed, 349 insertions, 2 deletions
diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/adapterfactory/EMFCompareAdapterFactoryTest.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/adapterfactory/EMFCompareAdapterFactoryTest.java
index 95718cb3f..7c5179bcc 100644
--- a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/adapterfactory/EMFCompareAdapterFactoryTest.java
+++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/adapterfactory/EMFCompareAdapterFactoryTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Obeo.
+ * Copyright (c) 2013, 2016 Obeo 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
@@ -7,6 +7,7 @@
*
* Contributors:
* Obeo - initial API and implementation
+ * Martin Fleck - bug 483798
*******************************************************************************/
package org.eclipse.emf.compare.tests.adapterfactory;
@@ -24,6 +25,7 @@ import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.compare.CompareFactory;
import org.eclipse.emf.compare.Comparison;
+import org.eclipse.emf.compare.adapterfactory.context.IContextTester;
import org.eclipse.emf.compare.internal.adapterfactory.RankedAdapterFactoryDescriptor;
import org.eclipse.emf.compare.internal.adapterfactory.RankedAdapterFactoryDescriptorRegistryImpl;
import org.eclipse.emf.compare.provider.IItemStyledLabelProvider;
@@ -98,6 +100,10 @@ public class EMFCompareAdapterFactoryTest {
return ranking;
}
+ public IContextTester getContextTester() {
+ return null;
+ }
+
public String getId() {
return this.adapterFactory.getClass().getName();
}
diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/registry/RankedAdapterFactoryRegistryTest.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/registry/RankedAdapterFactoryRegistryTest.java
new file mode 100644
index 000000000..c5d961cc2
--- /dev/null
+++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/registry/RankedAdapterFactoryRegistryTest.java
@@ -0,0 +1,340 @@
+/*******************************************************************************
+ * Copyright (c) 2016 EclipseSource Muenchen GmbH 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:
+ * Martin Fleck - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.emf.compare.tests.registry;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.emf.common.notify.Adapter;
+import org.eclipse.emf.common.notify.AdapterFactory;
+import org.eclipse.emf.compare.CompareFactory;
+import org.eclipse.emf.compare.Comparison;
+import org.eclipse.emf.compare.adapterfactory.context.AbstractContextTester;
+import org.eclipse.emf.compare.adapterfactory.context.IContextTester;
+import org.eclipse.emf.compare.internal.adapterfactory.RankedAdapterFactoryDescriptor;
+import org.eclipse.emf.compare.internal.adapterfactory.RankedAdapterFactoryDescriptorRegistryImpl;
+import org.eclipse.emf.compare.provider.IItemStyledLabelProvider;
+import org.eclipse.emf.compare.provider.spec.CompareItemProviderAdapterFactorySpec;
+import org.eclipse.emf.compare.provider.spec.ComparisonItemProviderSpec;
+import org.eclipse.emf.compare.provider.utils.ComposedStyledString;
+import org.eclipse.emf.compare.provider.utils.IStyledString.IComposedStyledString;
+import org.eclipse.emf.edit.provider.ComposedAdapterFactory;
+import org.junit.Test;
+
+@SuppressWarnings({"restriction", "nls" })
+public class RankedAdapterFactoryRegistryTest {
+
+ /** registry map */
+ private Multimap<Collection<?>, RankedAdapterFactoryDescriptor> registry;
+
+ /** ranked registry */
+ private RankedAdapterFactoryDescriptor.Registry rankedRegistry;
+
+ private void setUpAdapterFactoryRegistry(Comparison comparison) {
+ registry = ArrayListMultimap.create();
+ HashMap<Object, Object> context = Maps.newLinkedHashMap();
+ context.put(IContextTester.CTX_COMPARISON, comparison);
+ rankedRegistry = new RankedAdapterFactoryDescriptorRegistryImpl(
+ ComposedAdapterFactory.Descriptor.Registry.INSTANCE, registry, context);
+ }
+
+ private Comparison createNullComparison() {
+ return null;
+ }
+
+ private Comparison createComparison() {
+ final Comparison comparison = CompareFactory.eINSTANCE.createComparison();
+ comparison.setThreeWay(true);
+ return comparison;
+ }
+
+ private IContextTester createMatchingContextTester() {
+ return new AbstractContextTester() {
+ public boolean apply(Map<Object, Object> context) {
+ Comparison comparison = getComparison(context);
+ return comparison != null && comparison.isThreeWay();
+ }
+ };
+ }
+
+ private IContextTester createNonMatchingContextTester() {
+ return new AbstractContextTester() {
+ public boolean apply(Map<Object, Object> context) {
+ Comparison comparison = getComparison(context);
+ return comparison == null || !comparison.isThreeWay();
+ }
+ };
+ }
+
+ /**
+ * Tests that the registry returns the adapter factory with the highest rank when no context tester is
+ * present.
+ */
+ @Test
+ public void testNoContextTesterRankConsidered() {
+ final Comparison comparison = CompareFactory.eINSTANCE.createComparison();
+ setUpAdapterFactoryRegistry(createNullComparison());
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-NoContextTester", 50, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-NoContextTester", 70, null));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank70-NoContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that adapter factories with matching and non-matching context testers are ignored if no context
+ * is provided.
+ */
+ @Test
+ public void testNoContextContextTesterIgnored() {
+ final Comparison comparison = CompareFactory.eINSTANCE.createComparison();
+ setUpAdapterFactoryRegistry(createNullComparison());
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-MatchingContextTester", 50,
+ createMatchingContextTester()));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-NonMatchingContextTester", 70,
+ createNonMatchingContextTester()));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank20-NoContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that the registry returns the adapter factory with a lower rank if the context tester for a
+ * higher-ranking factory does not match.
+ */
+ @Test
+ public void testNonMatchingContextTesterSkipped() {
+ final Comparison comparison = createComparison();
+ setUpAdapterFactoryRegistry(comparison);
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-NoContextTester", 50, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-NonMatchingContextTester", 70,
+ createNonMatchingContextTester()));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank50-NoContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that the registry returns the adapter factory with the highest rank if the context tester for the
+ * highest-ranking factory does match.
+ */
+ @Test
+ public void testMatchingContextTesterConsidered() {
+ final Comparison comparison = createComparison();
+ setUpAdapterFactoryRegistry(comparison);
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-NoContextTester", 50, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-MatchingContextTester", 70,
+ createMatchingContextTester()));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank70-MatchingContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that the registry returns the adapter factory with the highest rank and context tester if
+ * multiple factories with context testers exist.
+ */
+ @Test
+ public void testContextTesterRanksConsidered() {
+ final Comparison comparison = createComparison();
+ setUpAdapterFactoryRegistry(comparison);
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-MatchingContextTester", 50,
+ createMatchingContextTester()));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-MatchingContextTester", 70,
+ createMatchingContextTester()));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank70-MatchingContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that the registry favors higher rank over matching context tester.
+ */
+ @Test
+ public void testHigherRankBeatsContextTester() {
+ final Comparison comparison = createComparison();
+ setUpAdapterFactoryRegistry(comparison);
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank50-MatchingContextTester", 50,
+ createMatchingContextTester()));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-NoContextTester", 70, null));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank70-NoContextTester", styledText.getString());
+ }
+
+ /**
+ * Tests that the registry favors a factory with context tester when factories have an equal rank.
+ */
+ @Test
+ public void testEqualRankContextTesterPreferred() {
+ final Comparison comparison = createComparison();
+ setUpAdapterFactoryRegistry(comparison);
+
+ final Collection<String> key = new ArrayList<String>();
+ key.add("http://www.eclipse.org/emf/compare");
+ key.add("org.eclipse.emf.compare.provider.IItemStyledLabelProvider");
+
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank20-NoContextTester", 20, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-NoContextTester", 70, null));
+ registry.put(key, new TestAdapterFactoryDescriptor("Rank70-MatchingContextTester", 70,
+ createMatchingContextTester()));
+
+ final AdapterFactory fAdapterFactory = new ComposedAdapterFactory(rankedRegistry);
+ Adapter adapter = fAdapterFactory.adapt(comparison, IItemStyledLabelProvider.class);
+ IComposedStyledString styledText = ((IItemStyledLabelProvider)adapter).getStyledText(comparison);
+
+ assertEquals("Rank70-MatchingContextTester", styledText.getString());
+ }
+
+ /**
+ * Descriptor used for test ranking.
+ */
+ public class TestAdapterFactoryDescriptor implements RankedAdapterFactoryDescriptor {
+
+ AdapterFactory adapterFactory;
+
+ int ranking;
+
+ IContextTester contextTester;
+
+ public TestAdapterFactoryDescriptor(String text, int ranking, IContextTester contextTester) {
+ this.adapterFactory = new TestCompareItemProviderAdapterFactorySpec(text);
+ this.ranking = ranking;
+ this.contextTester = contextTester;
+ }
+
+ public AdapterFactory createAdapterFactory() {
+ return adapterFactory;
+ }
+
+ public int getRanking() {
+ return ranking;
+ }
+
+ public IContextTester getContextTester() {
+ return contextTester;
+ }
+
+ public String getId() {
+ return this.adapterFactory.getClass().getName();
+ }
+
+ public String getLabel() {
+ return "";
+ }
+
+ public String getDescription() {
+ return null;
+ }
+
+ public boolean isOptional() {
+ return false;
+ }
+
+ }
+
+ /**
+ * Specialized CompareItemProviderAdapterFactorySpec used for testing.
+ */
+ public class TestCompareItemProviderAdapterFactorySpec extends CompareItemProviderAdapterFactorySpec {
+
+ private String text;
+
+ public TestCompareItemProviderAdapterFactorySpec(String text) {
+ this.text = text;
+ }
+
+ @Override
+ public Adapter createComparisonAdapter() {
+ return new TestComparisonItemProviderSpec(this, text);
+ }
+ }
+
+ /**
+ * Specialized ComparisonItemProviderSpec used for testing.
+ */
+ public class TestComparisonItemProviderSpec extends ComparisonItemProviderSpec {
+
+ private String text;
+
+ public TestComparisonItemProviderSpec(AdapterFactory adapterFactory, String text) {
+ super(adapterFactory);
+ this.text = text;
+ }
+
+ @Override
+ public IComposedStyledString getStyledText(Object object) {
+ return new ComposedStyledString(text);
+ }
+ }
+}
diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/suite/AllTests.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/suite/AllTests.java
index 4a1003a90..8e093a4b3 100644
--- a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/suite/AllTests.java
+++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/suite/AllTests.java
@@ -58,6 +58,7 @@ import org.eclipse.emf.compare.tests.monitor.MonitorCancelTest;
import org.eclipse.emf.compare.tests.nodes.NodesPackage;
import org.eclipse.emf.compare.tests.nodes.util.NodesResourceFactoryImpl;
import org.eclipse.emf.compare.tests.postprocess.PostProcessorTest;
+import org.eclipse.emf.compare.tests.registry.RankedAdapterFactoryRegistryTest;
import org.eclipse.emf.compare.tests.req.ReqComputingTest;
import org.eclipse.emf.compare.tests.scope.ComparisonScopeAdapterTest;
import org.eclipse.emf.compare.tests.scope.DefaultComparisonScopeTest;
@@ -97,7 +98,7 @@ import junit.textui.TestRunner;
MultiLineAttributeMergeTest.class, MonitorCancelTest.class, IdentifierEObjectMatcherTest.class,
MatchUtilFeatureContainsTest.class, RefineMergeTest.class, Bug484557ConflictTest.class,
Bug485266_MoveDeleteConflict_Test.class, ResourceAttachmentChangeBug492261.class,
- ComparisonScopeAdapterTest.class, EMFComparePredicatesTest.class, })
+ ComparisonScopeAdapterTest.class, EMFComparePredicatesTest.class, RankedAdapterFactoryRegistryTest.class })
public class AllTests {
/**
* Standalone launcher for all of compare's tests.

Back to the top