From 286c8af9cd4992a33779149fef0c455e655af690 Mon Sep 17 00:00:00 2001 From: Laurent Goubet Date: Thu, 13 Mar 2014 13:04:17 +0100 Subject: normalize EOLs in the core (first pass) Change-Id: Id5b351cc3c89777401f672c85abb46560ee461b3 --- .../emf/compare/tests/diff/ComparisonUtilTest.java | 240 +++++++-------- .../emf/compare/tests/diff/LCSPerformanceTest.java | 322 ++++++++++----------- .../emf/compare/tests/diff/URIDistanceTest.java | 316 ++++++++++---------- 3 files changed, 439 insertions(+), 439 deletions(-) (limited to 'plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff') diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/ComparisonUtilTest.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/ComparisonUtilTest.java index 098875513..c7b3e85df 100644 --- a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/ComparisonUtilTest.java +++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/ComparisonUtilTest.java @@ -1,120 +1,120 @@ -/******************************************************************************* - * Copyright (c) 2012, 2013 Obeo. - * 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: - * Obeo - initial API and implementation - *******************************************************************************/ -package org.eclipse.emf.compare.tests.diff; - -import static com.google.common.base.Predicates.and; -import static junit.framework.Assert.assertEquals; -import static org.eclipse.emf.compare.utils.EMFComparePredicates.fromSide; -import static org.eclipse.emf.compare.utils.EMFComparePredicates.ofKind; -import static org.eclipse.emf.compare.utils.EMFComparePredicates.referenceValueMatch; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Iterators; - -import java.io.IOException; -import java.util.List; - -import org.eclipse.emf.compare.Comparison; -import org.eclipse.emf.compare.Diff; -import org.eclipse.emf.compare.DifferenceKind; -import org.eclipse.emf.compare.DifferenceSource; -import org.eclipse.emf.compare.EMFCompare; -import org.eclipse.emf.compare.internal.utils.ComparisonUtil; -import org.eclipse.emf.compare.scope.DefaultComparisonScope; -import org.eclipse.emf.compare.scope.IComparisonScope; -import org.eclipse.emf.compare.tests.fullcomparison.data.identifier.IdentifierMatchInputData; -import org.eclipse.emf.ecore.resource.Resource; -import org.junit.Test; - -@SuppressWarnings("all") -public class ComparisonUtilTest { - @Test - public void testSubDiffs() throws IOException { - IdentifierMatchInputData inputData = new IdentifierMatchInputData(); - - final Resource left = inputData.getExtlibraryLeft(); - final Resource origin = inputData.getExtlibraryOrigin(); - final Resource right = inputData.getExtlibraryRight(); - - // 2-way - IComparisonScope scope = new DefaultComparisonScope(left, right, null); - Comparison comparison = EMFCompare.builder().build().compare(scope); - List differences = comparison.getDifferences(); - - // Right to left on a deleted element - final Predicate leftPeriodical = and(fromSide(DifferenceSource.LEFT), - ofKind(DifferenceKind.DELETE), referenceValueMatch("eClassifiers", "extlibrary.Periodical", - true)); - final Diff leftPeriodicalDiff = Iterators.find(differences.iterator(), leftPeriodical); - boolean leftToRight = false; - Iterable subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff); - - assertEquals(7, Iterables.size(subDiffs)); - - // Left to right on a deleted element - leftToRight = true; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff); - - assertEquals(4, Iterables.size(subDiffs)); - - // Right to left on an added element - final Predicate leftMagazine = and(fromSide(DifferenceSource.LEFT), - ofKind(DifferenceKind.ADD), referenceValueMatch("eClassifiers", "extlibrary.Magazine", true)); - final Diff leftMagazineDiff = Iterators.find(differences.iterator(), leftMagazine); - leftToRight = false; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff); - - assertEquals(5, Iterables.size(subDiffs)); - - // Left to right on an added element - leftToRight = true; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff); - - assertEquals(5, Iterables.size(subDiffs)); - - // 3-way - scope = new DefaultComparisonScope(left, right, origin); - comparison = EMFCompare.builder().build().compare(scope); - differences = comparison.getDifferences(); - - // Right to left on a deleted element - final Predicate leftPeriodical3Way = and(fromSide(DifferenceSource.LEFT), - ofKind(DifferenceKind.DELETE), referenceValueMatch("eClassifiers", "extlibrary.Periodical", - true)); - final Diff leftPeriodicalDiff3Way = Iterators.find(differences.iterator(), leftPeriodical3Way); - leftToRight = false; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff3Way); - - assertEquals(11, Iterables.size(subDiffs)); - - // Left to right on a deleted element - leftToRight = true; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff3Way); - - assertEquals(8, Iterables.size(subDiffs)); - - // Right to left on a added element - final Predicate leftMagazine3Way = and(fromSide(DifferenceSource.LEFT), - ofKind(DifferenceKind.ADD), referenceValueMatch("eClassifiers", "extlibrary.Magazine", true)); - final Diff leftMagazineDiff3Way = Iterators.find(differences.iterator(), leftMagazine3Way); - leftToRight = false; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff3Way); - - assertEquals(5, Iterables.size(subDiffs)); - - // Left to right on an added element - leftToRight = true; - subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff3Way); - - assertEquals(5, Iterables.size(subDiffs)); - } -} +/******************************************************************************* + * Copyright (c) 2012, 2013 Obeo. + * 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.emf.compare.tests.diff; + +import static com.google.common.base.Predicates.and; +import static junit.framework.Assert.assertEquals; +import static org.eclipse.emf.compare.utils.EMFComparePredicates.fromSide; +import static org.eclipse.emf.compare.utils.EMFComparePredicates.ofKind; +import static org.eclipse.emf.compare.utils.EMFComparePredicates.referenceValueMatch; + +import com.google.common.base.Predicate; +import com.google.common.collect.Iterables; +import com.google.common.collect.Iterators; + +import java.io.IOException; +import java.util.List; + +import org.eclipse.emf.compare.Comparison; +import org.eclipse.emf.compare.Diff; +import org.eclipse.emf.compare.DifferenceKind; +import org.eclipse.emf.compare.DifferenceSource; +import org.eclipse.emf.compare.EMFCompare; +import org.eclipse.emf.compare.internal.utils.ComparisonUtil; +import org.eclipse.emf.compare.scope.DefaultComparisonScope; +import org.eclipse.emf.compare.scope.IComparisonScope; +import org.eclipse.emf.compare.tests.fullcomparison.data.identifier.IdentifierMatchInputData; +import org.eclipse.emf.ecore.resource.Resource; +import org.junit.Test; + +@SuppressWarnings("all") +public class ComparisonUtilTest { + @Test + public void testSubDiffs() throws IOException { + IdentifierMatchInputData inputData = new IdentifierMatchInputData(); + + final Resource left = inputData.getExtlibraryLeft(); + final Resource origin = inputData.getExtlibraryOrigin(); + final Resource right = inputData.getExtlibraryRight(); + + // 2-way + IComparisonScope scope = new DefaultComparisonScope(left, right, null); + Comparison comparison = EMFCompare.builder().build().compare(scope); + List differences = comparison.getDifferences(); + + // Right to left on a deleted element + final Predicate leftPeriodical = and(fromSide(DifferenceSource.LEFT), + ofKind(DifferenceKind.DELETE), referenceValueMatch("eClassifiers", "extlibrary.Periodical", + true)); + final Diff leftPeriodicalDiff = Iterators.find(differences.iterator(), leftPeriodical); + boolean leftToRight = false; + Iterable subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff); + + assertEquals(7, Iterables.size(subDiffs)); + + // Left to right on a deleted element + leftToRight = true; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff); + + assertEquals(4, Iterables.size(subDiffs)); + + // Right to left on an added element + final Predicate leftMagazine = and(fromSide(DifferenceSource.LEFT), + ofKind(DifferenceKind.ADD), referenceValueMatch("eClassifiers", "extlibrary.Magazine", true)); + final Diff leftMagazineDiff = Iterators.find(differences.iterator(), leftMagazine); + leftToRight = false; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff); + + assertEquals(5, Iterables.size(subDiffs)); + + // Left to right on an added element + leftToRight = true; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff); + + assertEquals(5, Iterables.size(subDiffs)); + + // 3-way + scope = new DefaultComparisonScope(left, right, origin); + comparison = EMFCompare.builder().build().compare(scope); + differences = comparison.getDifferences(); + + // Right to left on a deleted element + final Predicate leftPeriodical3Way = and(fromSide(DifferenceSource.LEFT), + ofKind(DifferenceKind.DELETE), referenceValueMatch("eClassifiers", "extlibrary.Periodical", + true)); + final Diff leftPeriodicalDiff3Way = Iterators.find(differences.iterator(), leftPeriodical3Way); + leftToRight = false; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff3Way); + + assertEquals(11, Iterables.size(subDiffs)); + + // Left to right on a deleted element + leftToRight = true; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftPeriodicalDiff3Way); + + assertEquals(8, Iterables.size(subDiffs)); + + // Right to left on a added element + final Predicate leftMagazine3Way = and(fromSide(DifferenceSource.LEFT), + ofKind(DifferenceKind.ADD), referenceValueMatch("eClassifiers", "extlibrary.Magazine", true)); + final Diff leftMagazineDiff3Way = Iterators.find(differences.iterator(), leftMagazine3Way); + leftToRight = false; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff3Way); + + assertEquals(5, Iterables.size(subDiffs)); + + // Left to right on an added element + leftToRight = true; + subDiffs = ComparisonUtil.getSubDiffs(leftToRight).apply(leftMagazineDiff3Way); + + assertEquals(5, Iterables.size(subDiffs)); + } +} diff --git a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/LCSPerformanceTest.java b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/LCSPerformanceTest.java index ca7cbc688..56a728ec0 100644 --- a/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/LCSPerformanceTest.java +++ b/plugins/org.eclipse.emf.compare.tests/src/org/eclipse/emf/compare/tests/diff/LCSPerformanceTest.java @@ -1,161 +1,161 @@ -/** - * Copyright (c) 2013, 2014 Obeo. - * 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: - * Obeo - initial API and implementation - */ -package org.eclipse.emf.compare.tests.diff; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -import com.google.common.collect.Lists; - -import java.util.List; - -import org.eclipse.emf.common.util.BasicMonitor; -import org.eclipse.emf.common.util.URI; -import org.eclipse.emf.compare.Comparison; -import org.eclipse.emf.compare.Diff; -import org.eclipse.emf.compare.DifferenceKind; -import org.eclipse.emf.compare.EMFCompare; -import org.eclipse.emf.compare.merge.BatchMerger; -import org.eclipse.emf.compare.merge.IBatchMerger; -import org.eclipse.emf.compare.merge.IMerger; -import org.eclipse.emf.compare.scope.DefaultComparisonScope; -import org.eclipse.emf.compare.scope.IComparisonScope; -import org.eclipse.emf.compare.tests.nodes.Node; -import org.eclipse.emf.compare.tests.nodes.NodesFactory; -import org.eclipse.emf.ecore.xmi.XMIResource; -import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - -/** - * This will specifically make sure that merging two large lists never takes more than a set amount of time. - *

- * I have a list (S1) on the left side that contains 2000 elements. Its counterpart on the right side (S2) has - * 200 elements, only 100 of which are not differences (i.e. : also present in S1). We'll make sure that - * merging these two lists is fast enough (through test timeouts). - *

- * - * @author Laurent Goubet - */ -@SuppressWarnings("nls") -public class LCSPerformanceTest { - private XMIResource left; - - private XMIResource right; - - @Before - public void setup() { - left = new XMIResourceImpl(URI.createURI("left.xmi")); - right = new XMIResourceImpl(URI.createURI("right.xmi")); - - createNode(left, 2000, 0, 1); - createNode(right, 200, 0, 1901); - } - - @Test - public void checkTestData() { - assertTrue(left.getContents().size() == 1); - final Node leftRoot = (Node)left.getContents().get(0); - assertTrue(leftRoot.eContents().size() == 2000); - - assertTrue(right.getContents().size() == 1); - final Node rightRoot = (Node)right.getContents().get(0); - assertTrue(rightRoot.eContents().size() == 200); - - IComparisonScope scope = new DefaultComparisonScope(left, right, null); - Comparison comparison = EMFCompare.builder().build().compare(scope); - - final List additions = Lists.newArrayList(); - final List deletions = Lists.newArrayList(); - - for (Diff difference : comparison.getDifferences()) { - if (difference.getKind() == DifferenceKind.ADD) { - additions.add(difference); - } else { - deletions.add(difference); - } - } - - assertEquals(Integer.valueOf(1900), Integer.valueOf(additions.size())); - assertEquals(Integer.valueOf(100), Integer.valueOf(deletions.size())); - } - - /** - * Will fail if {@link #checkTestData()} does. - *

- * The real assertion here is that this should never take more than 40 seconds to execute. We have 1900 - * additions to merge, accounting for as many LCS computations. - *

- *

- * Note that this test should run in less than 30 seconds... we give it a little leeway with a 40 seconds - * time out. - *

- *

- * Ignored for now : the build machine is much too slow and does not execute this even when left 60s to do - * so. - *

- */ - @Ignore - @Test(timeout = 40000) - public void copyLeftToRight() { - IComparisonScope scope = new DefaultComparisonScope(left, right, null); - Comparison comparison = EMFCompare.builder().build().compare(scope); - - final IMerger.Registry registry = IMerger.RegistryImpl.createStandaloneInstance(); - final IBatchMerger merger = new BatchMerger(registry); - merger.copyAllLeftToRight(comparison.getDifferences(), new BasicMonitor()); - - comparison = EMFCompare.builder().build().compare(scope); - assertTrue(comparison.getDifferences().isEmpty()); - } - - /** - * Will fail if {@link #checkTestData()} does. - *

- * The real assertion here is that this should never take more than 3 seconds to execute : we're resetting - * all differences so there are only 100 "slow" ones : resetting deletions need the LCS computation. - *

- *

- * Ignored for now : the build machine is much too slow. - *

- */ - @Ignore - @Test(timeout = 3000) - public void copyRightToLeft() { - IComparisonScope scope = new DefaultComparisonScope(left, right, null); - Comparison comparison = EMFCompare.builder().build().compare(scope); - - final IMerger.Registry registry = IMerger.RegistryImpl.createStandaloneInstance(); - final IBatchMerger merger = new BatchMerger(registry); - merger.copyAllRightToLeft(comparison.getDifferences(), new BasicMonitor()); - - comparison = EMFCompare.builder().build().compare(scope); - assertTrue(comparison.getDifferences().isEmpty()); - } - - private void createNode(XMIResource resource, int childCount, int nodeId, int idGap) { - resource.eSetDeliver(false); - Node node = NodesFactory.eINSTANCE.createNode(); - node.setName("node" + nodeId); - resource.getContents().add(node); - resource.setID(node, Integer.toString(nodeId)); - int childId = nodeId + Math.max(1, idGap); - for (int i = 0; i < childCount; i++) { - Node child = NodesFactory.eINSTANCE.createNode(); - child.setName("node" + childId); - node.getContainmentRef1().add(child); - resource.setID(child, Integer.toString(childId)); - childId++; - } - resource.eSetDeliver(true); - } -} +/** + * Copyright (c) 2013, 2014 Obeo. + * 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: + * Obeo - initial API and implementation + */ +package org.eclipse.emf.compare.tests.diff; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.Lists; + +import java.util.List; + +import org.eclipse.emf.common.util.BasicMonitor; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.compare.Comparison; +import org.eclipse.emf.compare.Diff; +import org.eclipse.emf.compare.DifferenceKind; +import org.eclipse.emf.compare.EMFCompare; +import org.eclipse.emf.compare.merge.BatchMerger; +import org.eclipse.emf.compare.merge.IBatchMerger; +import org.eclipse.emf.compare.merge.IMerger; +import org.eclipse.emf.compare.scope.DefaultComparisonScope; +import org.eclipse.emf.compare.scope.IComparisonScope; +import org.eclipse.emf.compare.tests.nodes.Node; +import org.eclipse.emf.compare.tests.nodes.NodesFactory; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +/** + * This will specifically make sure that merging two large lists never takes more than a set amount of time. + *

+ * I have a list (S1) on the left side that contains 2000 elements. Its counterpart on the right side (S2) has + * 200 elements, only 100 of which are not differences (i.e. : also present in S1). We'll make sure that + * merging these two lists is fast enough (through test timeouts). + *

+ * + * @author Laurent Goubet + */ +@SuppressWarnings("nls") +public class LCSPerformanceTest { + private XMIResource left; + + private XMIResource right; + + @Before + public void setup() { + left = new XMIResourceImpl(URI.createURI("left.xmi")); + right = new XMIResourceImpl(URI.createURI("right.xmi")); + + createNode(left, 2000, 0, 1); + createNode(right, 200, 0, 1901); + } + + @Test + public void checkTestData() { + assertTrue(left.getContents().size() == 1); + final Node leftRoot = (Node)left.getContents().get(0); + assertTrue(leftRoot.eContents().size() == 2000); + + assertTrue(right.getContents().size() == 1); + final Node rightRoot = (Node)right.getContents().get(0); + assertTrue(rightRoot.eContents().size() == 200); + + IComparisonScope scope = new DefaultComparisonScope(left, right, null); + Comparison comparison = EMFCompare.builder().build().compare(scope); + + final List additions = Lists.newArrayList(); + final List deletions = Lists.newArrayList(); + + for (Diff difference : comparison.getDifferences()) { + if (difference.getKind() == DifferenceKind.ADD) { + additions.add(difference); + } else { + deletions.add(difference); + } + } + + assertEquals(Integer.valueOf(1900), Integer.valueOf(additions.size())); + assertEquals(Integer.valueOf(100), Integer.valueOf(deletions.size())); + } + + /** + * Will fail if {@link #checkTestData()} does. + *

+ * The real assertion here is that this should never take more than 40 seconds to execute. We have 1900 + * additions to merge, accounting for as many LCS computations. + *

+ *

+ * Note that this test should run in less than 30 seconds... we give it a little leeway with a 40 seconds + * time out. + *

+ *

+ * Ignored for now : the build machine is much too slow and does not execute this even when left 60s to do + * so. + *

+ */ + @Ignore + @Test(timeout = 40000) + public void copyLeftToRight() { + IComparisonScope scope = new DefaultComparisonScope(left, right, null); + Comparison comparison = EMFCompare.builder().build().compare(scope); + + final IMerger.Registry registry = IMerger.RegistryImpl.createStandaloneInstance(); + final IBatchMerger merger = new BatchMerger(registry); + merger.copyAllLeftToRight(comparison.getDifferences(), new BasicMonitor()); + + comparison = EMFCompare.builder().build().compare(scope); + assertTrue(comparison.getDifferences().isEmpty()); + } + + /** + * Will fail if {@link #checkTestData()} does. + *

+ * The real assertion here is that this should never take more than 3 seconds to execute : we're resetting + * all differences so there are only 100 "slow" ones : resetting deletions need the LCS computation. + *

+ *

+ * Ignored for now : the build machine is much too slow. + *

+ */ + @Ignore + @Test(timeout = 3000) + public void copyRightToLeft() { + IComparisonScope scope = new DefaultComparisonScope(left, right, null); + Comparison comparison = EMFCompare.builder().build().compare(scope); + + final IMerger.Registry registry = IMerger.RegistryImpl.createStandaloneInstance(); + final IBatchMerger merger = new BatchMerger(registry); + merger.copyAllRightToLeft(comparison.getDifferences(), new BasicMonitor()); + + comparison = EMFCompare.builder().build().compare(scope); + assertTrue(comparison.getDifferences().isEmpty()); + } + + private void createNode(XMIResource resource, int childCount, int nodeId, int idGap) { + resource.eSetDeliver(false); + Node node = NodesFactory.eINSTANCE.createNode(); + node.setName("node" + nodeId); + resource.getContents().add(node); + resource.setID(node, Integer.toString(nodeId)); + int childId = nodeId + Math.max(1, idGap); + for (int i = 0; i < childCount; i++) { + Node child = NodesFactory.eINSTANCE.createNode(); + child.setName("node" + childId); + node.getContainmentRef1().add(child); + resource.setID(child, Integer.toString(childId)); + childId++; + } + resource.eSetDeliver(true); + } +} 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 b172370df..a0548fb94 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 @@ -1,158 +1,158 @@ -/** - * Copyright (c) 2012, 2014 Obeo. - * 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: - * Obeo - initial API and implementation - */ -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; - -@SuppressWarnings("nls") -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(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, proximity("/root/a/b/", "/root/a/b/")); - assertEquals(0, proximity("/root/", "/root/")); - assertEquals(0, proximity("", "")); - } - - @Test - public void limitCases() throws Exception { - 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, 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, proximity("/c/d/e/f", "/f/d/c/e")); - } - - @Test - public void idLikeURIs() throws Exception { - assertEquals(10, proximity("#131233", "#azeazezae")); - assertEquals(2, proximity("/c/d/e/f", "/c/d/e/f?#azeaze")); - } - - @Test - public void traillingSlashes() throws Exception { - 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 it = EcorePackage.eINSTANCE.eAllContents(); - Iterator 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('/'); - String actualAPath = aPath; - String actualBPath = bPath; - if (slash.indexIn(actualAPath) == 0) { - actualAPath = aPath.substring(1); - } - if (slash.indexIn(actualBPath) == 0) { - actualBPath = bPath.substring(1); - } - Iterable aString = splitter.split(slash.trimTrailingFrom(actualAPath)); - Iterable bString = splitter.split(slash.trimTrailingFrom(actualBPath)); - return meter.proximity(ImmutableList.copyOf(aString), ImmutableList.copyOf(bString)); - } - } -} +/** + * Copyright (c) 2012, 2014 Obeo. + * 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: + * Obeo - initial API and implementation + */ +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; + +@SuppressWarnings("nls") +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(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, proximity("/root/a/b/", "/root/a/b/")); + assertEquals(0, proximity("/root/", "/root/")); + assertEquals(0, proximity("", "")); + } + + @Test + public void limitCases() throws Exception { + 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, 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, proximity("/c/d/e/f", "/f/d/c/e")); + } + + @Test + public void idLikeURIs() throws Exception { + assertEquals(10, proximity("#131233", "#azeazezae")); + assertEquals(2, proximity("/c/d/e/f", "/c/d/e/f?#azeaze")); + } + + @Test + public void traillingSlashes() throws Exception { + 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 it = EcorePackage.eINSTANCE.eAllContents(); + Iterator 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('/'); + String actualAPath = aPath; + String actualBPath = bPath; + if (slash.indexIn(actualAPath) == 0) { + actualAPath = aPath.substring(1); + } + if (slash.indexIn(actualBPath) == 0) { + actualBPath = bPath.substring(1); + } + Iterable aString = splitter.split(slash.trimTrailingFrom(actualAPath)); + Iterable bString = splitter.split(slash.trimTrailingFrom(actualBPath)); + return meter.proximity(ImmutableList.copyOf(aString), ImmutableList.copyOf(bString)); + } + } +} -- cgit v1.2.3