Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1ac97f3488618c0278b1203938c94c0f967e4f66 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
 * Copyright (c) 2016 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.merge;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.junit.Assert.assertEquals;

import java.util.List;

import org.eclipse.emf.common.util.BasicMonitor;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.merge.IMerger;
import org.eclipse.emf.compare.merge.IMerger.Registry;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.ecore.resource.Resource;
import org.junit.Test;

/**
 * Abstract test class for merge tests. The class create a comparison for the given resources, merge given
 * diffs to merge in the correct side then assert that the result is equal to a given resource. The
 * responsibility of finding the diffs to merge is given to concrete classes since this is specific to each
 * test.
 * 
 * @author <a href="mailto:mathieu.cartaud@obeo.fr">Mathieu Cartaud</a>
 */
public abstract class AbstractMergerTest {

	protected Resource origin;

	protected Resource left;

	protected Resource right;

	protected Resource expected;

	protected boolean rightToLeft;

	protected Registry mergerRegistry;

	protected Comparison comparison;

	public AbstractMergerTest(final Resource origin, final Resource left, final Resource right,
			final boolean rightToLeft, final Resource expected, IMerger.Registry mergerRegistry) {
		this.origin = checkNotNull(origin);
		this.left = checkNotNull(left);
		this.right = checkNotNull(right);
		this.expected = checkNotNull(expected);
		this.rightToLeft = rightToLeft;
		this.mergerRegistry = checkNotNull(mergerRegistry);
	}

	/**
	 * This class is implemented by client to get the diffs to merge for each specific test.
	 * 
	 * @return the list of diffs to merge
	 */
	protected abstract List<Diff> getDiffsToMerge();

	@Test
	public void runMergeTest() {
		createComparison();
		List<Diff> diffsToMerge = getDiffsToMerge();
		mergeAndCompare(diffsToMerge);
	}

	/**
	 * Create the initial comparison.
	 */
	protected void createComparison() {
		IComparisonScope scope = new DefaultComparisonScope(left, right, origin);
		comparison = EMFCompare.builder().build().compare(scope);
	}

	/**
	 * Merge the given diffs and assert that the result is identical with the expected result resource.
	 * 
	 * @param diffsToMerge
	 *            The list of diffs to merge
	 */
	protected void mergeAndCompare(List<Diff> diffsToMerge) {
		for (Diff diff : diffsToMerge) {
			if (rightToLeft) {
				mergerRegistry.getHighestRankingMerger(diff).copyRightToLeft(diff, new BasicMonitor());
			} else {
				mergerRegistry.getHighestRankingMerger(diff).copyLeftToRight(diff, new BasicMonitor());
			}
		}

		IComparisonScope scope;
		if (rightToLeft) {
			scope = new DefaultComparisonScope(left, expected, null);
		} else {
			scope = new DefaultComparisonScope(right, expected, null);
		}

		comparison = EMFCompare.builder().build().compare(scope);
		assertEquals(0, comparison.getDifferences().size());
	}

}

Back to the top