Skip to main content
summaryrefslogtreecommitdiffstats
blob: 277ca91cfa456863687924d9814f81d5b713a093 (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
346
347
348
349
350
/*******************************************************************************
 * Copyright (c) 2015 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:
 *     Stefan Dirix - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.compare.tests.match;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.Lists;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.eclipse.emf.compare.MatchResource;
import org.eclipse.emf.compare.match.resource.RootIDMatchingStrategy;
import org.eclipse.emf.compare.tests.match.data.MatchInputData;
import org.eclipse.emf.ecore.resource.Resource;
import org.junit.Test;

@SuppressWarnings("nls")
public class RootIDMatchingTest {

	private MatchInputData input = new MatchInputData();

	/**
	 * Tests a scenario in which all root ids are different and no match should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA1() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA1Left();
		List<Resource> right = input.getRootIDTwoWayA1Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which one pair of root ids intersect and one match should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA2() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA2Left();
		List<Resource> right = input.getRootIDTwoWayA2Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(1, matchResources.size());
	}

	/**
	 * Tests a scenario in which each file has at least one root id which intersects and therefore two matches
	 * should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA3() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA3Left();
		List<Resource> right = input.getRootIDTwoWayA3Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(2, matchResources.size());
	}

	/**
	 * Tests a scenario in which one resource from the left intersects with all files from the right,
	 * therefore no match should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA4() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA4Left();
		List<Resource> right = input.getRootIDTwoWayA4Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which one resource from the left intersects with both files from the right, while
	 * the other left file intersects with only one. Still no match should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA5() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA5Left();
		List<Resource> right = input.getRootIDTwoWayA5Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which one resource from the right intersects with both files from the left, while
	 * the other right file intersects with only one. Still no match should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDTwoWayA6() throws IOException {
		List<Resource> left = input.getRootIDTwoWayA6Left();
		List<Resource> right = input.getRootIDTwoWayA6Right();
		List<Resource> origin = Lists.newLinkedList();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which no ids intersect, therefore no matches should be returned.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA1() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA1Left();
		List<Resource> right = input.getRootIDThreeWayA1Right();
		List<Resource> origin = input.getRootIDThreeWayA1Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which a left resource intersects with an origin resource, a right resource
	 * intersects with another origin resource and no left and right resource intersects each other.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA2() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA2Left();
		List<Resource> right = input.getRootIDThreeWayA2Right();
		List<Resource> origin = input.getRootIDThreeWayA2Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(2, matchResources.size());
		assertContainsMatching(matchResources, "left.nodes", null, "origin2.nodes");
		assertContainsMatching(matchResources, null, "right2.nodes", "origin.nodes");
	}

	/**
	 * Tests a scenario in which a left resource intersects with an origin resource, a right resource
	 * intersects with the same origin resource and these resources are therefore matched together, although
	 * the left resource does not intersect the right resource.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA3() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA3Left();
		List<Resource> right = input.getRootIDThreeWayA3Right();
		List<Resource> origin = input.getRootIDThreeWayA3Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(1, matchResources.size());
		assertContainsMatching(matchResources, "left.nodes", "right.nodes", "origin.nodes");
	}

	/**
	 * Tests a scenario in which a left resource does not intersect with an origin resource, a right resource
	 * intersects with one origin resource while the left and right resource also intersect. These resources
	 * should therefore be matched together, although the left resource does not intersect the origin
	 * resource.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA4() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA4Left();
		List<Resource> right = input.getRootIDThreeWayA4Right();
		List<Resource> origin = input.getRootIDThreeWayA4Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(1, matchResources.size());
		assertContainsMatching(matchResources, "left.nodes", "right2.nodes", "origin.nodes");
	}

	/**
	 * Tests a scenario in which a left resource does intersect with an origin resource, a right resource does
	 * not intersect with an origin resource while the left and right resource also intersect. These resources
	 * should therefore be matched together, although the right resource does not intersect the origin
	 * resource.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA5() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA5Left();
		List<Resource> right = input.getRootIDThreeWayA5Right();
		List<Resource> origin = input.getRootIDThreeWayA5Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(1, matchResources.size());
		assertContainsMatching(matchResources, "left2.nodes", "right.nodes", "origin.nodes");
	}

	/**
	 * Tests a scenario in which resources intersect left, right and origin. Therefore they should be matched
	 * together.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA6() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA6Left();
		List<Resource> right = input.getRootIDThreeWayA6Right();
		List<Resource> origin = input.getRootIDThreeWayA6Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(2, matchResources.size());
		assertContainsMatching(matchResources, "left.nodes", "right.nodes", "origin.nodes");
		assertContainsMatching(matchResources, "left2.nodes", "right2.nodes", "origin2.nodes");
	}

	/**
	 * Tests a scenario in which three resources intersect left, right and origin but the origin intersects
	 * with multiple resources left. Therefore there should be no matches.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA7() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA7Left();
		List<Resource> right = input.getRootIDThreeWayA7Right();
		List<Resource> origin = input.getRootIDThreeWayA7Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which three resources intersect left, right and origin but the origin intersects
	 * with multiple resources from right. Therefore there should be no matches.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA8() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA8Left();
		List<Resource> right = input.getRootIDThreeWayA8Right();
		List<Resource> origin = input.getRootIDThreeWayA8Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	/**
	 * Tests a scenario in which three resources intersect left, right and origin but the right intersects
	 * with multiple resources from left. Therefore there should be no matches.
	 * 
	 * @throws IOException
	 *             if {@link MatchInputData} fails to load the test models.
	 */
	@Test
	public void testRootIDThreeWayA9() throws IOException {
		List<Resource> left = input.getRootIDThreeWayA9Left();
		List<Resource> right = input.getRootIDThreeWayA9Right();
		List<Resource> origin = input.getRootIDThreeWayA9Origin();

		RootIDMatchingStrategy strategy = new RootIDMatchingStrategy();
		List<MatchResource> matchResources = strategy.matchResources(left, right, origin);

		assertEquals(0, matchResources.size());
	}

	private void assertContainsMatching(Collection<MatchResource> matchResources, String left, String right,
			String origin) {
		boolean foundMatching = false;
		for (MatchResource matchResource : matchResources) {
			if (nameEquals(matchResource.getLeftURI(), left)
					&& nameEquals(matchResource.getRightURI(), right)
					&& nameEquals(matchResource.getOriginURI(), origin)) {
				foundMatching = true;
				break;
			}
		}
		assertTrue(foundMatching);
	}

	private boolean nameEquals(String uri, String name) {
		if (uri == null && name == null) {
			return true;
		}
		if (uri == null || name == null) {
			return false;
		}
		return uri.endsWith(name);
	}
}

Back to the top