Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcbrun2013-01-08 09:14:49 -0500
committercbrun2013-01-08 09:14:49 -0500
commit9914ac93cf20a2082a36c6509fbee0c7c5858ea4 (patch)
treebf72279af026ded88a04bf87d908e292a7be35a6 /plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff
parent038fc87c93acda1b6692398fd360da33753d5698 (diff)
downloadorg.eclipse.emf.compare-9914ac93cf20a2082a36c6509fbee0c7c5858ea4.tar.gz
org.eclipse.emf.compare-9914ac93cf20a2082a36c6509fbee0c7c5858ea4.tar.xz
org.eclipse.emf.compare-9914ac93cf20a2082a36c6509fbee0c7c5858ea4.zip
394668: Version 2.1 results 'compare' versus 1.3
https://bugs.eclipse.org/bugs/show_bug.cgi?id=394668 Fix match by content regression introduced by the recent changes in the URIDistance computation. We were using a "simili uri" in the form of /someRef/indexInRef/otherRef . Problems are rising when some elements are added/removed in a collection the indexInRef is shifted and if you're 'unlucky' you'll ends up matching something (let's say an EAttribute) with another EAttribute which happens to be at the previous index. To lower this effect the weight of the location is kept minimum and the URIDistance is more fine grained (it used to be 0 : same URI, 10 : different URI) We are now leveraging the classical fragment mechanism of EMF (available in InternalEObject) to retrieve the fragment while supporting eKeys which matters a lot in those case. The URI Distance is also now more fine grained as it is counting the number of "hops" required to go from one location to another, leading to values which will go from 0 to 10 and not 0 or 10 exclusively.
Diffstat (limited to 'plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff')
-rw-r--r--plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/URIDistanceTest.java130
1 files changed, 105 insertions, 25 deletions
diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/URIDistanceTest.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/URIDistanceTest.java
index 4b29dc7ed..a1947ce21 100644
--- a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/URIDistanceTest.java
+++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/URIDistanceTest.java
@@ -11,8 +11,17 @@
package org.eclipse.emf.compare.tests.diff;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.base.CharMatcher;
+import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
+
+import java.util.Iterator;
import org.eclipse.emf.compare.match.eobject.URIDistance;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecore.EcorePackage;
import org.junit.Test;
public class URIDistanceTest {
@@ -20,56 +29,127 @@ public class URIDistanceTest {
private URIDistance meter = new URIDistance();
@Test
+ public void moreOrLessTheSame() throws Exception {
+ String origin = "/root/a/b";
+ String closest = "/root/a/c";
+ String farthest = "/root/d/c";
+ assertTrue(proximity(origin, closest) < proximity(origin, farthest));
+ }
+
+ @Test
+ public void moreOrLessTheSame2() throws Exception {
+ String origin = "/root/a/b";
+ String closest = "/root/a/b/c/d";
+ String farthest = "/root/d/c/d";
+ assertTrue(proximity(origin, closest) < proximity(origin, farthest));
+ }
+
+ @Test
+ public void moreOrLessTheSame3() throws Exception {
+ String origin = "/root/a/b";
+ String closest = "/root/";
+ String farthest = "/";
+ assertTrue(proximity(origin, closest) < proximity(origin, farthest));
+ }
+
+ @Test
+ public void moreOrLessTheSame4() throws Exception {
+ String origin = "/root/a/b";
+ String closest = "/root/";
+ String farthest = "/otherRoot/";
+ assertTrue(proximity(origin, closest) < proximity(origin, farthest));
+ }
+
+ @Test
public void sameNumberOfFragments() throws Exception {
- assertEquals(10, meter.proximity("/root/a/b", "/root/a/b/c"));
- assertEquals(10, meter.proximity("/root/a/b/", "/root/a/b/c/"));
- assertEquals(0, meter.proximity("/root/a/b", "/root/a/b"));
- assertEquals(10, meter.proximity("/root/a/a2/a3", "/root/b/b2/a3"));
- assertEquals(10, meter.proximity("/root/a/a2/a3", "/root/b/b2/b3"));
+ assertEquals(1, proximity("/root/a/b", "/root/a/b/c"));
+ assertEquals(1, proximity("/root/a/b/", "/root/a/b/c/"));
+ assertEquals(2, proximity("/root/a/b/", "/root/a/b/c/d/"));
+ assertEquals(0, proximity("/root/a/b", "/root/a/b"));
+ assertEquals(7, proximity("/root/a/a2/a3", "/root/b/b2/a3"));
+ assertEquals(7, proximity("/root/a/a2/a3", "/root/b/a2/b3"));
}
@Test
public void identics() throws Exception {
- assertEquals(0, meter.proximity("/root/a/b/", "/root/a/b/"));
- assertEquals(0, meter.proximity("/root/", "/root/"));
- assertEquals(0, meter.proximity("", ""));
+ assertEquals(0, proximity("/root/a/b/", "/root/a/b/"));
+ assertEquals(0, proximity("/root/", "/root/"));
+ assertEquals(0, proximity("", ""));
}
@Test
public void limitCases() throws Exception {
- assertEquals(0, meter.proximity("", ""));
- assertEquals(10, meter.proximity("",
- "/a/very/long/path/just/to/check/we/wont/ends/up/with/a/weird/thing"));
- assertEquals(10, meter.proximity(
- "/a/very/long/path/just/to/check/we/wont/ends/up/with/a/weird/thing", ""));
+ assertEquals(0, proximity("", ""));
+ assertEquals(10, proximity("/", "/a/very/long/path/just/to/check/we/wont/ends/up/with/a/weird/thing"));
+ assertEquals(10, proximity("/a/very/long/path/just/to/check/we/wont/ends/up/with/a/weird/thing", "/"));
}
@Test
public void completelyDifferent() throws Exception {
- assertEquals(10, meter.proximity("/c/d/e/", "/root/a/b/"));
- assertEquals(10, meter.proximity("/c/", "/root/a/b/"));
- assertEquals(10, meter.proximity("/c/d/e", "/root/"));
- assertEquals(10, meter.proximity("/c/d/e/f", "/a/b/e/f"));
- assertEquals(10, meter.proximity("/a", "/b"));
+ assertEquals(10, proximity("/c/d/e/", "/root/a/b/"));
+ assertEquals(10, proximity("/c/", "/root/a/b/"));
+ assertEquals(10, proximity("/c/d/e", "/root/"));
+ assertEquals(10, proximity("/c/d/e/f", "/a/b/e/f"));
+ assertEquals(10, proximity("/a", "/b"));
}
@Test
public void orderMatters() throws Exception {
- assertEquals(10, meter.proximity("/c/d/e/f", "/f/d/c/e"));
+ assertEquals(10, proximity("/c/d/e/f", "/f/d/c/e"));
}
@Test
public void idLikeURIs() throws Exception {
- assertEquals(10, meter.proximity("#131233", "#azeazezae"));
- assertEquals(10, meter.proximity("/c/d/e/f", "/c/d/e/f?#azeaze"));
+ assertEquals(10, proximity("#131233", "#azeazezae"));
+ assertEquals(2, proximity("/c/d/e/f", "/c/d/e/f?#azeaze"));
}
@Test
public void traillingSlashes() throws Exception {
- assertEquals(10, meter.proximity("/root/a/b/", "/root/a/b/c/"));
- assertEquals(10, meter.proximity("root/a/b/", "/root/a/b/c/"));
- assertEquals(10, meter.proximity("/root/a/b/", "/root/a/b/c"));
- assertEquals(10, meter.proximity("///root/a/b/", "/root/a/b/c"));
+ assertEquals(1, proximity("/root/a/b/", "/root/a/b/c/"));
+ assertEquals(1, proximity("root/a/b/", "/root/a/b/c/"));
+ assertEquals(1, proximity("/root/a/b/", "/root/a/b/c"));
+ assertEquals(10, proximity("///root/a/b/", "/root/a/b/c"));
}
+ @Test
+ public void nullDistanceForSameModel() throws Exception {
+ Iterator<? extends EObject> it = EcorePackage.eINSTANCE.eAllContents();
+ Iterator<? extends EObject> it2 = EcorePackage.eINSTANCE.eAllContents();
+ while (it.hasNext() && it2.hasNext()) {
+ EObject a = it.next();
+ EObject b = it2.next();
+ // System.out.println(meter.apply(a));
+ assertEquals(0, meter.proximity(a, b));
+ }
+ }
+
+ /**
+ * Return a metric result URI similarities. It compares 2 strings splitting those by "/" and return an int
+ * representing the level of similarity. 0 - they are exactly the same to 10 - they are completely
+ * different. "adding a fragment", "removing a fragment".
+ *
+ * @param aPath
+ * First of the two {@link String}s to compare.
+ * @param bPath
+ * Second of the two {@link String}s to compare.
+ * @return The number of changes to transform one uri to another one.
+ */
+ public int proximity(String aPath, String bPath) {
+ if (aPath.equals(bPath)) {
+ return 0;
+ } else {
+ CharMatcher slash = CharMatcher.is('/');
+ Splitter splitter = Splitter.on('/');
+ if (slash.indexIn(aPath) == 0) {
+ aPath = aPath.substring(1);
+ }
+ if (slash.indexIn(bPath) == 0) {
+ bPath = bPath.substring(1);
+ }
+ Iterable<String> aString = splitter.split(slash.trimTrailingFrom(aPath));
+ Iterable<String> bString = splitter.split(slash.trimTrailingFrom(bPath));
+ return meter.proximity(ImmutableList.copyOf(aString), ImmutableList.copyOf(bString));
+ }
+ }
}

Back to the top