Skip to main content
summaryrefslogtreecommitdiffstats
blob: 063433443d2f4bbbf5153fab36ffbc1e7eddc17c (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
/*******************************************************************************
 * 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.tests.monitor;

import static org.junit.Assert.assertEquals;

import com.google.common.collect.Iterators;

import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.common.util.BasicMonitor;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.Monitor;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.ComparisonCanceledException;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.diff.DefaultDiffEngine;
import org.eclipse.emf.compare.diff.IDiffEngine;
import org.eclipse.emf.compare.equi.DefaultEquiEngine;
import org.eclipse.emf.compare.equi.IEquiEngine;
import org.eclipse.emf.compare.match.DefaultMatchEngine;
import org.eclipse.emf.compare.match.IMatchEngine;
import org.eclipse.emf.compare.match.IMatchEngine.Factory;
import org.eclipse.emf.compare.req.DefaultReqEngine;
import org.eclipse.emf.compare.req.IReqEngine;
import org.eclipse.emf.compare.scope.AbstractComparisonScope;
import org.eclipse.emf.compare.scope.DefaultComparisonScope;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.compare.tests.monitor.data.MonitorCancelInputData;
import org.eclipse.emf.compare.utils.UseIdentifiers;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.junit.Before;
import org.junit.Test;

public class MonitorCancelTest {

	private Comparison fComparison;

	private Comparison fComparisonWithDiff;

	/**
	 * Checks that the sending of a ComparisonCanceledException by the match engine causes a cancelled
	 * comparison to be returned.
	 */
	@Test
	public void testCancelDuringMatch() {
		EMFCompare compare = EMFCompare.builder().setMatchEngineFactoryRegistry(TestRegistry.INSTANCE)
				.build();
		Comparison comparison = compare.compare(getEmptyScope(), new BasicMonitor());
		assertEquals(Diagnostic.CANCEL, comparison.getDiagnostic().getSeverity());
	}

	/**
	 * Checks that the sending of a ComparisonCanceledException by the diff engine causes a cancelled
	 * comparison to be returned.
	 */
	@Test
	public void testCancelDuringDiff() {
		EMFCompare compare = EMFCompare.builder().setDiffEngine(new IDiffEngine() {
			public void diff(Comparison comparison, Monitor monitor) {
				throw new ComparisonCanceledException();
			}
		}).build();
		Comparison comparison = compare.compare(getEmptyScope(), new BasicMonitor());
		assertEquals(Diagnostic.CANCEL, comparison.getDiagnostic().getSeverity());
	}

	/**
	 * Checks that the sending of a ComparisonCanceledException by the req engine causes a cancelled
	 * comparison to be returned.
	 */
	@Test
	public void testCancelDuringReq() {
		EMFCompare compare = EMFCompare.builder().setRequirementEngine(new IReqEngine() {
			public void computeRequirements(Comparison comparison, Monitor monitor) {
				throw new ComparisonCanceledException();
			}
		}).build();
		Comparison comparison = compare.compare(getEmptyScope(), new BasicMonitor());
		assertEquals(Diagnostic.CANCEL, comparison.getDiagnostic().getSeverity());
	}

	/**
	 * Checks that the sending of a ComparisonCanceledException by the equivalence engine causes a cancelled
	 * comparison to be returned.
	 */
	@Test
	public void testCancelDuringEqui() {
		EMFCompare compare = EMFCompare.builder().setEquivalenceEngine(new IEquiEngine() {
			public void computeEquivalences(Comparison comparison, Monitor monitor) {
				throw new ComparisonCanceledException();
			}
		}).build();
		Comparison comparison = compare.compare(getEmptyScope(), new BasicMonitor());
		assertEquals(Diagnostic.CANCEL, comparison.getDiagnostic().getSeverity());
	}

	@Test(expected = ComparisonCanceledException.class)
	public void testMonitorCancelInMatch() throws IOException {
		IMatchEngine engine = DefaultMatchEngine.create(UseIdentifiers.WHEN_AVAILABLE);
		engine.match(getNonEmptyTestScope(), getCanceledMonitor());
	}

	@Test(expected = ComparisonCanceledException.class)
	public void testMonitorCancelInDiff() throws IOException {
		IDiffEngine diffEngine = new DefaultDiffEngine();
		diffEngine.diff(fComparison, getCanceledMonitor());
	}

	@Test(expected = ComparisonCanceledException.class)
	public void testMonitorCancelInReq() throws IOException {
		IReqEngine reqEngine = new DefaultReqEngine();
		reqEngine.computeRequirements(fComparisonWithDiff, getCanceledMonitor());
	}

	@Test(expected = ComparisonCanceledException.class)
	public void testMonitorCancelInEqui() throws IOException {
		IEquiEngine equiEngine = new DefaultEquiEngine();
		equiEngine.computeEquivalences(fComparisonWithDiff, getCanceledMonitor());
	}

	@Before
	public void setUp() throws IOException {
		IMatchEngine matchEngine = DefaultMatchEngine.create(UseIdentifiers.WHEN_AVAILABLE);
		fComparison = matchEngine.match(getNonEmptyTestScope(), new BasicMonitor());
		fComparisonWithDiff = matchEngine.match(getNonEmptyTestScope(), new BasicMonitor());
		IDiffEngine diffEngine = new DefaultDiffEngine();
		diffEngine.diff(fComparisonWithDiff, new BasicMonitor());
	}

	private BasicMonitor getCanceledMonitor() {
		return new BasicMonitor() {
			@Override
			public boolean isCanceled() {
				return true;
			}
		};
	}

	private IComparisonScope getNonEmptyTestScope() throws IOException {
		final MonitorCancelInputData testData = new MonitorCancelInputData();
		return new DefaultComparisonScope(testData.getLeft(), testData.getRight(), testData.getOrigin());
	}

	private IComparisonScope getEmptyScope() {
		return new AbstractComparisonScope(null, null, null) {

			public Iterator<? extends Resource> getCoveredResources(ResourceSet resourceSet) {
				return Iterators.emptyIterator();
			}

			public Iterator<? extends EObject> getCoveredEObjects(Resource resource) {
				return Iterators.emptyIterator();
			}

			public Iterator<? extends EObject> getChildren(EObject eObject) {
				return Iterators.emptyIterator();
			}
		};
	}

	/**
	 * A match engine to use for tests, which systematically throws a ComparisonCanceledException.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	private static final class CanceledMatchEngine implements IMatchEngine {

		private static final CanceledMatchEngine INSTANCE = new CanceledMatchEngine();

		public Comparison match(IComparisonScope scope, Monitor monitor) {
			throw new ComparisonCanceledException();
		}

	}

	/**
	 * A factory for tests that provides a CanceledMatchEngine.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a
	 */
	private static final class TestFactory implements IMatchEngine.Factory {

		private static final TestFactory INSTANCE = new TestFactory();

		public IMatchEngine getMatchEngine() {
			return CanceledMatchEngine.INSTANCE;
		}

		public int getRanking() {
			return 0;
		}

		public void setRanking(int parseInt) {
			// Intentionally blank
		}

		public boolean isMatchEngineFactoryFor(IComparisonScope scope) {
			return true;
		}

	}

	/**
	 * A registry for tests that provides a TestFactory.
	 * 
	 * @author <a href="mailto:laurent.delaigue@obeo.fr">Laurent Delaigue</a>
	 */
	private static final class TestRegistry implements IMatchEngine.Factory.Registry {

		private static final TestRegistry INSTANCE = new TestRegistry();

		public Factory getHighestRankingMatchEngineFactory(IComparisonScope scope) {
			return TestFactory.INSTANCE;
		}

		public List<Factory> getMatchEngineFactories(IComparisonScope scope) {
			return Arrays.<Factory> asList(TestFactory.INSTANCE);
		}

		public Factory add(Factory matchEngineFactory) {
			throw new RuntimeException();
		}

		public Factory remove(String className) {
			return null;
		}

		public void clear() {
			// Intentionally blank
		}

	}
}

Back to the top