Skip to main content
summaryrefslogtreecommitdiffstats
blob: 790c17bf5822d7b821a6aeac198f1e5e50966be7 (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
/*******************************************************************************
 * 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.postprocess;

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

import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;

import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.postprocessor.IPostProcessor;
import org.eclipse.emf.compare.postprocessor.PostProcessorDescriptorRegistryImpl;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.compare.tests.nodes.NodesPackage;
import org.eclipse.emf.compare.tests.postprocess.data.PostProcessInputData;
import org.eclipse.emf.compare.tests.postprocess.data.TestPostProcessor;
import org.eclipse.emf.compare.tests.postprocess.data.TestPostProcessor.TestPostProcessorDescriptor;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.junit.BeforeClass;
import org.junit.Test;

@SuppressWarnings("nls")
public class PostProcessorTest {

	private PostProcessInputData input = new PostProcessInputData();

	@BeforeClass
	public static void beforeClass() {
		EPackage.Registry.INSTANCE.put(NodesPackage.eNS_URI, NodesPackage.eINSTANCE);
	}

	@Test
	public void testNominalUseCase() throws IOException {
		// No post processes.
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		final IComparisonScope scope = new DefaultComparisonScope(left, right, null);
		final Comparison comparison = EMFCompare.builder().build().compare(scope);

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));
	}

	@Test
	public void testA1UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// Post processing (add a match element) if EMF Compare scans a model coming from the namespace URI
		// "http://www.eclipse.org/emf/compare/tests/nodes" at least.
		final Comparison comparison = compareWithPostProcessing(left, right, Pattern
				.compile("http://www.eclipse.org/emf/compare/tests/nodes"), null);

		assertSame(Integer.valueOf(2), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testA2UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// Post processing (add a match element) if EMF Compare scans a model from the names
		// matches the regex ".*/nodes" at least.
		final Comparison comparison = compareWithPostProcessing(left, right, Pattern.compile(".*/nodes"),
				null);

		assertSame(Integer.valueOf(2), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testA3UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned namespace URIs.
		final Comparison comparison = compareWithPostProcessing(left, right, Pattern.compile(".*/nides"),
				null);

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testA4UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned namespace URIs (null value case)
		final Comparison comparison = compareWithPostProcessing(left, right, null, null);

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testA5UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned namespace URIs (empty value cas)
		final Comparison comparison = compareWithPostProcessing(left, right, Pattern.compile(""), null);

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testA6UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned namespace URIs (blank value cas)
		final Comparison comparison = compareWithPostProcessing(left, right, Pattern.compile(" "), null);

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testB1UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// Post processing (add a match element) if EMF Compare scans a resource where its U
		// the specified one at least.
		final Comparison comparison = compareWithPostProcessing(left, right, null, Pattern.compile(left
				.getURI().toString()));

		assertSame(Integer.valueOf(2), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testB2UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// Post processing (add a match element) if EMF Compare scans a resource where its U
		// specified regex at least.
		final Comparison comparison = compareWithPostProcessing(left, right, null, Pattern
				.compile(".*.nodes"));

		assertSame(Integer.valueOf(2), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testB3UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned resource URIs
		final Comparison comparison = compareWithPostProcessing(left, right, null, Pattern
				.compile(".*.nides"));

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testB4UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned resource URIs (empty value)
		final Comparison comparison = compareWithPostProcessing(left, right, null, Pattern.compile(""));

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testB5UseCase() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();

		// No post processes if the regex matches no scanned resource URIs (blank value)
		final Comparison comparison = compareWithPostProcessing(left, right, null, Pattern.compile(" "));

		assertSame(Integer.valueOf(1), Integer.valueOf(comparison.getMatches().size()));

	}

	@Test
	public void testOrderingPostProcessors() throws IOException {
		final Resource left = input.getLeft();
		final Resource right = input.getRight();
		final IComparisonScope scope = new DefaultComparisonScope(left, right, null);

		PostProcessorDescriptorRegistryImpl registry = new PostProcessorDescriptorRegistryImpl();
		registry.put(TestPostProcessor1.class.getName(), new TestPostProcessorDescriptor(Pattern.compile(""),
				null, new TestPostProcessor1(), 1));
		registry.put(TestPostProcessor2.class.getName(), new TestPostProcessorDescriptor(Pattern
				.compile("http://www.eclipse.org/emf/compare/tests/nodes"), null, new TestPostProcessor2(),
				10));
		registry.put(TestPostProcessor3.class.getName(),
				new TestPostProcessorDescriptor(Pattern
						.compile("http://www.eclipse.org/emf/compare/tests/nodes"), null,
						new TestPostProcessor3(), 9));
		registry.put(TestPostProcessor4.class.getName(), new TestPostProcessorDescriptor(Pattern
				.compile("http://www.eclipse.org/emf/compare/tests/nodes"), null, new TestPostProcessor4(),
				11));

		// Populate nsURIs of the scope.
		EMFCompare.builder().setPostProcessorRegistry(registry).build().compare(scope);

		List<IPostProcessor> postProcessors = registry.getPostProcessors(scope);

		assertEquals(3, postProcessors.size());
		assertTrue(postProcessors.get(0) instanceof TestPostProcessor3);
		assertTrue(postProcessors.get(1) instanceof TestPostProcessor2);
		assertTrue(postProcessors.get(2) instanceof TestPostProcessor4);
	}

	private Comparison compareWithPostProcessing(Resource left, Resource right, Pattern nsURI,
			Pattern resourceURI) {
		PostProcessorDescriptorRegistryImpl registry = new PostProcessorDescriptorRegistryImpl();
		registry.put(TestPostProcessor.class.getName(), new TestPostProcessorDescriptor(nsURI, resourceURI,
				new TestPostProcessor(), 10));

		final IComparisonScope scope = new DefaultComparisonScope(left, right, null);
		return EMFCompare.builder().setPostProcessorRegistry(registry).build().compare(scope);
	}

	/**
	 * Empty Implementation, used to test ordering
	 */
	private class TestPostProcessor1 extends TestPostProcessor {

	}

	/**
	 * Empty Implementation, used to test ordering
	 */
	private class TestPostProcessor2 extends TestPostProcessor {

	}

	/**
	 * Empty Implementation, used to test ordering
	 */
	private class TestPostProcessor3 extends TestPostProcessor {

	}

	/**
	 * Empty Implementation, used to test ordering
	 */
	private class TestPostProcessor4 extends TestPostProcessor {

	}
}

Back to the top