Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae323e80883c61dea79f9998537ed881bc26d4f1 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*******************************************************************************
 * Copyright (c) 2006, 2009 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.unit.diff;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.compare.FactoryException;
import org.eclipse.emf.compare.diff.metamodel.DiffElement;
import org.eclipse.emf.compare.diff.metamodel.DiffGroup;
import org.eclipse.emf.compare.diff.metamodel.DiffModel;
import org.eclipse.emf.compare.diff.metamodel.ModelElementChangeLeftTarget;
import org.eclipse.emf.compare.diff.service.DiffService;
import org.eclipse.emf.compare.match.MatchOptions;
import org.eclipse.emf.compare.match.metamodel.MatchModel;
import org.eclipse.emf.compare.match.service.MatchService;
import org.eclipse.emf.compare.tests.util.EcoreModelUtils;
import org.eclipse.emf.compare.util.EFactory;
import org.eclipse.emf.compare.util.ModelUtils;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;

// TODO testing : these tests are not covering conflicting changes
/**
 * Tests the behavior of the generic diff engine. Be aware that these tests will take a while to be executed.
 * 
 * @author <a href="mailto:laurent.goubet@obeo.fr">Laurent Goubet</a>
 */
@SuppressWarnings("nls")
public class ThreeWayDiffTest extends TestCase {
	/** This is the resource holding the first model we'll use to test the differencing process. */
	private Resource testResource1;

	/** This is the resource holding the second model we'll use to test the differencing process. */
	private Resource testResource2;

	/**
	 * Tests the behavior of the GenericDiffEngine with two distinct EObjects (a model and its deep copy
	 * slightly modified). The left model will be used as common ancestor.
	 * <p>
	 * We're assuming that the matching process is returning the expected result as it has been tested through
	 * the org.eclipse.emf.compare.tests.unit.match.* tests.
	 * </p>
	 * 
	 * @throws FactoryException
	 *             Thrown if the comparison fails somehow.
	 */
	public void test3WayDiffDifferentModelsLocalChange() throws Exception {
		final int writerCount = 3;
		final int bookPerWriterCount = 5;
		final long seed = System.nanoTime();
		testResource1 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		testResource2 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		internalTest3WayDistinctModelsLocalChange();
	}

	/**
	 * Tests the behavior of the GenericDiffEngine with two distinct EObjects (a model and its deep copy
	 * slightly modified). The right model will be used as common ancestor.
	 * <p>
	 * We're assuming that the matching process is returning the expected result as it has been tested through
	 * the org.eclipse.emf.compare.tests.unit.match.* tests.
	 * </p>
	 * 
	 * @throws FactoryException
	 *             Thrown if the comparison fails somehow.
	 */
	public void test3WayDiffDifferentModelsRemoteChange() throws FactoryException {
		final int writerCount = 3;
		final int bookPerWriterCount = 5;
		final long seed = System.nanoTime();
		testResource1 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		testResource2 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		internalTest3WayDistinctModelsRemoteChange();
	}

	/**
	 * Tests the behavior of the GenericDiffEngine with two equal EObjects (a model and its deep copy). The
	 * left model will be used as common ancestor.
	 * <p>
	 * We're assuming that the matching process is returning the expected result as it has been tested through
	 * the org.eclipse.emf.compare.tests.unit.match.* tests.
	 * </p>
	 * 
	 * @throws FactoryException
	 *             Thrown if the comparison fails somehow.
	 */
	public void test3WayDiffEqualModelsLocalChange() throws FactoryException {
		final int writerCount = 3;
		final int bookPerWriterCount = 5;
		final long seed = System.nanoTime();
		testResource1 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		testResource2 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();

		MatchModel match = null;
		try {
			match = MatchService.doResourceMatch(testResource1, testResource2, testResource1, getOptions());
		} catch (final InterruptedException e) {
			fail("modelMatch() threw an unexpected Exception.");
		}
		assertNotNull("Failed to match the models.", match);

		final DiffModel diff = DiffService.doDiff(match, true);
		assertNotNull("Failed to compute the models' diff.", diff);

		final TreeIterator<EObject> diffIterator = diff.eAllContents();
		int elementCount = 0;
		while (diffIterator.hasNext()) {
			final DiffElement aDiff = (DiffElement)diffIterator.next();
			if (!(aDiff instanceof DiffGroup)) {
				elementCount++;
			}
		}

		assertEquals("There shouldn't have been a differences.", 0, elementCount);
	}

	/**
	 * Tests the behavior of the GenericDiffEngine with two equal EObjects (a model and its deep copy). The
	 * right model will be used as common ancestor.
	 * <p>
	 * We're assuming that the matching process is returning the expected result as it has been tested through
	 * the org.eclipse.emf.compare.tests.unit.match.* tests.
	 * </p>
	 * 
	 * @throws FactoryException
	 *             Thrown if the comparison fails somehow.
	 */
	public void test3WayDiffEqualModelsRemoteChange() throws FactoryException {
		final int writerCount = 3;
		final int bookPerWriterCount = 5;
		final long seed = System.nanoTime();
		testResource1 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();
		testResource2 = EcoreModelUtils.createModel(writerCount, bookPerWriterCount, seed).eResource();

		MatchModel match = null;
		try {
			match = MatchService.doResourceMatch(testResource1, testResource2, testResource1, getOptions());
		} catch (final InterruptedException e) {
			fail("modelMatch threw an unexpected InterruptedException.");
		}
		assertNotNull("Failed to match the two models.", match);

		final DiffModel diff = DiffService.doDiff(match, true);
		assertNotNull("Failed to compute the two models' diff.", diff);

		final TreeIterator<EObject> diffIterator = diff.eAllContents();
		int elementCount = 0;
		while (diffIterator.hasNext()) {
			final DiffElement aDiff = (DiffElement)diffIterator.next();
			if (!(aDiff instanceof DiffGroup)) {
				elementCount++;
			}
		}

		assertEquals("There shouldn't have been a single difference.", 0, elementCount);
	}

	/**
	 * Tests the behavior of the GenericDiffEngine with a <code>null</code> match model.
	 * <p>
	 * Expects a {@link NullPointerException} to be thrown.
	 * </p>
	 */
	public void test3WayDiffNullEObjects() {
		final String failNPE = "The differencing process did not throw the expected NullPointerException.";
		try {
			DiffService.doDiff((MatchModel)null, true);
			fail(failNPE);
		} catch (final NullPointerException e) {
			// This was expected behavior
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() {
		// voids the testResources (and hopes gc passes by ... should we hint at it here with System.gc?)
		if (testResource1 != null) {
			testResource1.getContents().clear();
		}
		if (testResource2 != null) {
			testResource2.getContents().clear();
		}
		testResource1 = null;
		testResource2 = null;
	}

	/**
	 * This will return the map of options to be used for comparisons within this test class.
	 * 
	 * @return Default options for matching.
	 */
	private Map<String, Object> getOptions() {
		final Map<String, Object> options = new HashMap<String, Object>();
		options.put(MatchOptions.OPTION_DISTINCT_METAMODELS, Boolean.TRUE);
		return options;
	}

	/**
	 * This handles the modification of the given model.
	 * <p>
	 * We'll retrieve the first Writer we find (see javadoc of {@link EcoreModelUtils#createMetaModel()}).
	 * First we copy this element, modify its name and void its "writtenBooks" reference to have a new element
	 * to add to the model, then we'll modify the original's "name" attribute to see if we can still match it.
	 * </p>
	 * 
	 * @param copyModel
	 *            The model to alter.
	 */
	private void internalModifyModel(EObject copyModel) {
		try {
			EObject originalWriter = null;
			EObject newElement = null;
			for (final EObject element : copyModel.eContents()) {
				if ("Writer".equals(element.eClass().getName())) {
					originalWriter = element;
					newElement = EcoreUtil.copy(element);
					break;
				}
			}
			// Change name
			EFactory.eSet(newElement, "name", "ThisNameShouldntHaveBeenUsedYet");
			// void books
			final List<Object> values = new ArrayList<Object>();
			values.addAll(EFactory.eGetAsList(newElement, "writtenBooks"));
			for (final Object aValue : values) {
				EFactory.eRemove(newElement, "writtenBooks", aValue);
			}
			// add this new element to model
			EFactory.eAdd(copyModel, "authors", newElement);
			// modify existing element
			EFactory.eSet(originalWriter, "name", "ModifiedWriterName");
		} catch (final FactoryException e) {
			/*
			 * Shouldn't have happened if we had found a Writer as expected. Consider it a failure
			 */
			fail("Couldn't modify original model to test differencing.");
		}
	}

	/**
	 * Handles the modification and comparison of the test models for distinct objects comparison.
	 * <p>
	 * Match failures should have been tested in the match engine test suite. As for the differencing
	 * failures, we expect to find an AddModelElement and an UpdateAttribute in the result. Externalized here
	 * to avoid copy/pasting within the two tests making use of it.
	 * </p>
	 */
	private void internalTest3WayDistinctModelsRemoteChange() {
		internalModifyModel(testResource2.getContents().get(0));

		MatchModel match = null;
		try {
			match = MatchService.doResourceMatch(testResource1, testResource2, testResource1, getOptions());
		} catch (final InterruptedException e) {
			fail("modelMatch() threw an unexpected InterruptedException.");
		}
		assertNotNull("Failed to match the three models.", match);

		final DiffModel diff = DiffService.doDiff(match, true);
		assertNotNull("Failed to compute the three models' diff.", diff);

		final TreeIterator<EObject> diffIterator = diff.eAllContents();
		int elementCount = 0;
		int deletionCount = 0;
		while (diffIterator.hasNext()) {
			final DiffElement aDiff = (DiffElement)diffIterator.next();
			if (aDiff instanceof ModelElementChangeLeftTarget) {
				deletionCount++;
			}
			if (!(aDiff instanceof DiffGroup)) {
				elementCount++;
			}
		}

		// We're expecting two changes, one of which being a removal
		assertEquals("Unexpected count of differences.", 2, elementCount);
		assertEquals("Unexpected count of deletions in the DiffModel.", 1, deletionCount);
	}

	/**
	 * Handles the modification and comparison of the test models for distinct objects comparison.
	 * <p>
	 * Match failures should have been tested in the match engine test suite. As for the differencing
	 * failures, we expect to find a RemoteRemoveModelElement and a RemoteUpdateAttribute in the result.
	 * Externalized here to avoid copy/pasting within the two tests making use of it.
	 * </p>
	 * 
	 * @throws IOException
	 */
	private void internalTest3WayDistinctModelsLocalChange() throws IOException {
		internalModifyModel(testResource2.getContents().get(0));

		MatchModel match = null;
		try {
			match = MatchService.doResourceMatch(testResource1, testResource2, testResource2, getOptions());
		} catch (final InterruptedException e) {
			fail("modelMatch() threw an unexpected InterruptedException.");
		}
		assertNotNull("Failed to match the three models.", match);

		final DiffModel diff = DiffService.doDiff(match, true);
		assertNotNull("Failed to compute the three models' diff.", diff);

		final TreeIterator<EObject> diffIterator = diff.eAllContents();
		int elementCount = 0;
		int additionCount = 0;
		while (diffIterator.hasNext()) {
			final DiffElement aDiff = (DiffElement)diffIterator.next();
			if (aDiff instanceof ModelElementChangeLeftTarget) {
				additionCount++;
			}
			if (!(aDiff instanceof DiffGroup)) {
				elementCount++;
			}
		}

		String v1 = ModelUtils.serialize(testResource1.getContents().get(0));
		String v2 = ModelUtils.serialize(testResource2.getContents().get(0));

		// We're expecting two changes, one of which being an addition
		assertEquals("Unexpected count of differences.", 2, elementCount);
		assertEquals("Unexpected count of additions in the DiffModel.", 1, additionCount);
	}
}

Back to the top