Skip to main content
summaryrefslogtreecommitdiffstats
blob: fdb1c681e8788890cf6c1b0af9c88ff655296fa7 (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************************
 * Copyright (c) 2015 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.rcp.ui.tests.match;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

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

import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.match.IMatchEngine;
import org.eclipse.emf.compare.match.IMatchEngine.Factory;
import org.eclipse.emf.compare.match.impl.MatchEngineFactoryImpl;
import org.eclipse.emf.compare.rcp.EMFCompareRCPPlugin;
import org.eclipse.emf.compare.rcp.internal.extension.IItemDescriptor;
import org.eclipse.emf.compare.rcp.internal.extension.impl.EMFCompareBuilderConfigurator;
import org.eclipse.emf.compare.rcp.internal.extension.impl.ItemUtil;
import org.eclipse.emf.compare.rcp.internal.match.MatchEngineFactoryRegistryWrapper;
import org.eclipse.emf.compare.rcp.internal.preferences.EMFComparePreferences;
import org.eclipse.emf.compare.rcp.ui.tests.match.data.EcoreInputData;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.osgi.service.prefs.BackingStoreException;

/**
 * Test class for {@link MatchEngineFactoryRegistryWrapper}.
 * 
 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
 */
@SuppressWarnings({"restriction", "nls" })
public class RCPMatchEngineFactoryRegistryTest {

	private IMatchEngine.Factory.Registry registryWrapper;

	private IEclipsePreferences preferences;

	/**
	 * Creates a comparison scope from Ecore model.
	 * 
	 * @return {@link IComparisonScope}
	 * @throws IOException
	 */
	private IComparisonScope createComparisonScope() throws IOException {
		EcoreInputData ecoreData = new EcoreInputData();
		return new DefaultComparisonScope(ecoreData.getLeft(), ecoreData.getRight(), ecoreData.getOrigin());
	}

	@Before
	public void setUp() throws BackingStoreException {
		preferences = EMFCompareRCPPlugin.getDefault().getEMFComparePreferences();
		registryWrapper = EMFCompareRCPPlugin.getDefault().getMatchEngineFactoryRegistry();
	}

	@After
	public void tearDown() throws BackingStoreException {
		preferences.clear();
	}

	/**
	 * Nominal use case: Adds {@link IMatchEngine.Factory} in the registry.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testAdd() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(50);
		registryWrapper.add(factory);
		IComparisonScope createComparisonScope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(createComparisonScope), factory);
		assertEquals(registryWrapper.getMatchEngineFactories(createComparisonScope).size(), 4);
	}

	/**
	 * Adds a factory with no ranking set into the registry.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testAddFactoryWithNoRanking() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		registryWrapper.add(factory);
		IMatchEngine.Factory factory2 = new MockMatchEngineFactory2();
		factory2.setRanking(50);
		registryWrapper.add(factory2);
		IComparisonScope createComparisonScope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(createComparisonScope), factory2);
	}

	/**
	 * Adds null to the registry.
	 */
	@Test(expected = NullPointerException.class)
	public void testAddNull() {
		registryWrapper.add(null);
	}

	/**
	 * Adds two factories with identical id in the registry.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testAddSameID() throws IOException {
		MockMatchEngineFactory1 factory1 = new MockMatchEngineFactory1();
		factory1.setRanking(50);
		registryWrapper.add(factory1);
		MockMatchEngineFactory1 factory2 = new MockMatchEngineFactory1();
		factory2.setRanking(50);
		Factory oldValue = registryWrapper.add(factory2);
		assertSame(oldValue, factory1);
		IComparisonScope createComparisonScope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(createComparisonScope), factory2);
	}

	/**
	 * Nominal use case: Gets the highest ranking match engine factory.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testHighestRankingMatchEngineFactory() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(10);
		registryWrapper.add(factory);
		IMatchEngine.Factory factory2 = new MockMatchEngineFactory2();
		factory2.setRanking(20);
		registryWrapper.add(factory2);
		IMatchEngine.Factory factory3 = new MockMatchEngineFactory3();
		factory3.setRanking(30);
		registryWrapper.add(factory3);

		IComparisonScope scope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(scope), factory3);
		assertTrue(registryWrapper.getMatchEngineFactories(scope).containsAll(
				Lists.newArrayList(factory, factory2, factory3)));
	}

	/**
	 * Nominal use case: Gets the highest ranking match engine factory with more complex registry.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testHighestRankingMatchEngineFactoryWithDisabledFactories() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(10);
		registryWrapper.add(factory);

		IMatchEngine.Factory factory2 = new MockMatchEngineFactory2();
		factory2.setRanking(20);
		registryWrapper.add(factory2);

		IMatchEngine.Factory factory3 = new MockMatchEngineFactory3();
		factory3.setRanking(30);
		registryWrapper.add(factory3);

		IMatchEngine.Factory disabledFactory1 = new MockDisabledMatchEngineFactory1();
		disabledFactory1.setRanking(40);
		registryWrapper.add(disabledFactory1);

		IMatchEngine.Factory disabledFactory2 = new MockDisabledMatchEngineFactory2();
		disabledFactory2.setRanking(50);
		registryWrapper.add(disabledFactory2);

		IMatchEngine.Factory disabledFactory3 = new MockDisabledMatchEngineFactory3();
		disabledFactory2.setRanking(60);
		registryWrapper.add(disabledFactory3);

		IComparisonScope scope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(scope), factory3);
		assertTrue(registryWrapper.getMatchEngineFactories(scope).containsAll(
				Lists.newArrayList(factory, factory2, factory3)));

	}

	/**
	 * Nominal use case: Removes a factory from the registry.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testRemove() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(50);
		registryWrapper.add(factory);

		Factory oldValue = registryWrapper.remove(MockMatchEngineFactory1.class.getName());
		assertSame(oldValue, factory);
		assertFalse(registryWrapper.getMatchEngineFactories(createComparisonScope()).contains(factory));
	}

	/**
	 * Tries to remove null from the registry.
	 */
	@Test(expected = NullPointerException.class)
	public void testRemoveNull() {
		registryWrapper.remove(null);
	}

	/**
	 * Tries to remove a factory from the registry with an incoherent id.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testRemoveWrongID() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(10);
		registryWrapper.add(factory);

		Factory oldValue = registryWrapper.remove("IncohereId");
		assertNull(oldValue);
		assertTrue(!registryWrapper.getMatchEngineFactories(createComparisonScope()).isEmpty());
	}

	/**
	 * Nominal use case: Disables Match Engine factory from preferences. Checks that the registry to do use
	 * them anymore.
	 * 
	 * @throws IOException
	 */
	@Test
	public void testDisablingMatchEngine() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(10);
		registryWrapper.add(factory);

		IMatchEngine.Factory factory2 = new MockMatchEngineFactory2();
		factory2.setRanking(20);
		registryWrapper.add(factory2);

		IMatchEngine.Factory factory3 = new MockMatchEngineFactory3();
		factory3.setRanking(30);
		registryWrapper.add(factory3);

		IComparisonScope scope = createComparisonScope();
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(scope), factory3);

		disableEngine(preferences, EMFComparePreferences.MATCH_ENGINE_DISABLE_ENGINES, Collections
				.singleton(factory3.getClass().getName()));
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(scope), factory2);
		assertTrue(!registryWrapper.getMatchEngineFactories(scope).contains(factory3));

		disableEngine(preferences, EMFComparePreferences.MATCH_ENGINE_DISABLE_ENGINES, Lists.newArrayList(
				factory3.getClass().getName(), factory2.getClass().getName()));
		assertSame(registryWrapper.getHighestRankingMatchEngineFactory(scope), factory);
		assertTrue(!registryWrapper.getMatchEngineFactories(scope).contains(factory2));

	}

	@Test
	public void testEMFCompareBuilder() throws IOException {
		IMatchEngine.Factory factory = new MockMatchEngineFactory1();
		factory.setRanking(10);
		registryWrapper.add(factory);

		IMatchEngine.Factory factory2 = new MockMatchEngineFactory2();
		factory2.setRanking(20);
		registryWrapper.add(factory2);

		IMatchEngine.Factory factory3 = new MockMatchEngineFactory3();
		factory3.setRanking(30);
		registryWrapper.add(factory3);

		MockBuilder mockBuilder = new MockBuilder();
		mockBuilder.build();

		IMatchEngine.Factory.Registry builderMatchEngineFactoryRegistry = mockBuilder
				.getMatchEngineFactoryRegistry();

		assertNotSame(registryWrapper, builderMatchEngineFactoryRegistry);

		EMFCompareBuilderConfigurator.createDefault().configure(mockBuilder);
		mockBuilder.build();
		builderMatchEngineFactoryRegistry = mockBuilder.getMatchEngineFactoryRegistry();

		assertSame(registryWrapper, builderMatchEngineFactoryRegistry);

	}

	/**
	 * Disables engine in preferences.
	 * 
	 * @param preference
	 *            {@link IEclipsePreferences}
	 * @param key
	 * @param toDisable
	 *            {@link Collection} of {@link IItemDescriptor} to disable.
	 */
	private void disableEngine(IEclipsePreferences preference, String key, Collection<String> toDisable) {
		if (toDisable != null && !toDisable.isEmpty()) {
			String newPreferenceValue = Joiner.on(ItemUtil.PREFERENCE_DELIMITER).join(toDisable);
			preference.put(key, newPreferenceValue);
		} else {
			preference.remove(key);
		}
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that does not handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockDisabledMatchEngineFactory1 extends MatchEngineFactoryImpl {
		@Override
		public boolean isMatchEngineFactoryFor(IComparisonScope scope) {
			return false;
		}
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that does not handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockDisabledMatchEngineFactory2 extends MockDisabledMatchEngineFactory1 {
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that does not handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockDisabledMatchEngineFactory3 extends MockDisabledMatchEngineFactory1 {
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that can handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockMatchEngineFactory1 extends MatchEngineFactoryImpl {
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that can handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockMatchEngineFactory2 extends MatchEngineFactoryImpl {
	}

	/**
	 * Mock {@link IMatchEngine.Factory} that can handle any {@link IComparisonScope}.
	 * 
	 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
	 */
	private static class MockMatchEngineFactory3 extends MatchEngineFactoryImpl {
	}

	/**
	 * Mock {@link EMFCompare.Builder} in order to access matchEngineFactoryRegistry field.
	 * 
	 * @author <a href="mailto:axel.richard@obeo.fr">Axel Richard</a>
	 */
	private static class MockBuilder extends EMFCompare.Builder {
		public IMatchEngine.Factory.Registry getMatchEngineFactoryRegistry() {
			return this.matchEngineFactoryRegistry;
		}
	}
}

Back to the top