Skip to main content
summaryrefslogtreecommitdiffstats
blob: 748a2136aa7c3555227a2732721d8ef3be5ccaab (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
/*******************************************************************************
 * Copyright (c) 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
 *******************************************************************************/
package org.eclipse.emf.compare.ide.ui.tests.unit;

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

import java.io.File;
import java.util.List;

import org.eclipse.compare.ITypedElement;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.BasicMonitor;
import org.eclipse.emf.compare.Comparison;
import org.eclipse.emf.compare.Diff;
import org.eclipse.emf.compare.EMFCompare;
import org.eclipse.emf.compare.ide.ui.internal.logical.ComparisonScopeBuilder;
import org.eclipse.emf.compare.ide.ui.internal.logical.IdenticalResourceMinimizer;
import org.eclipse.emf.compare.ide.ui.internal.logical.LogicalModelResolver;
import org.eclipse.emf.compare.ide.ui.internal.logical.SubscriberStorageAccessor;
import org.eclipse.emf.compare.ide.ui.logical.IStorageProvider;
import org.eclipse.emf.compare.ide.ui.logical.IStorageProviderAccessor;
import org.eclipse.emf.compare.ide.ui.tests.StorageTypedElement;
import org.eclipse.emf.compare.ide.ui.tests.egit.CompareGitTestCase;
import org.eclipse.emf.compare.scope.IComparisonScope;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.team.core.subscribers.Subscriber;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings("nls")
public class GitLogicalModelTest extends CompareGitTestCase {
	private static final String MASTER = Constants.R_HEADS + Constants.MASTER;

	private static final String BRANCH = Constants.R_HEADS + "branch";

	private static final String FILE1_SUFFIX = "_file1";

	private static final String FILE2_SUFFIX = "_file2";

	private IFile iFile1;

	private IFile iFile2;

	private Resource resource1;

	private Resource resource2;

	private String[] masterTipSynonyms;

	private String[] branchTipSynonyms;

	/**
	 * We'll have seven commits during this test, roughly looking like this:
	 * 
	 * <pre>
	 * o--o--o--o--o
	 *        \--o--o
	 * </pre>
	 * 
	 * In order within this array:
	 * 
	 * <pre>
	 * 0--1--2--3--4
	 *        \--5--6
	 * </pre>
	 * 
	 * There will be two ecore files within this repository. file1 (F1) references file2 (F2) and conversely
	 * so that the logical model, whatever the starting point, comprises both files. The following shows which
	 * files were modified with which commits:
	 * 
	 * <pre>
	 * (F1+F2)--(F2)--(F2)--(F2)--(F1+F2)
	 *                    \--(F1+F2)--(F2)
	 * </pre>
	 */
	private RevCommit[] commits = new RevCommit[7];

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		final IProject iProject = project.getProject();
		final ResourceSet resourceSet = new ResourceSetImpl();

		final File file1 = project.getOrCreateFile(iProject, "file1.ecore");
		final File file2 = project.getOrCreateFile(iProject, "file2.ecore");
		iFile1 = project.getIFile(iProject, file1);
		iFile2 = project.getIFile(iProject, file2);

		resource1 = connectResource(iFile1, resourceSet);
		resource2 = connectResource(iFile2, resourceSet);

		resource1.getContents().add(createBasicModel(FILE1_SUFFIX));
		resource2.getContents().add(createBasicModel(FILE2_SUFFIX));

		makeCrossReference(resource1, resource2, CLASS1_NAME_PREFIX, CLASS1_NAME_PREFIX);
		makeCrossReference(resource2, resource1, CLASS2_NAME_PREFIX, CLASS2_NAME_PREFIX);

		save(resource1, resource2);
		commits[0] = repository.addAndCommit(project, "master-commit-1", file1, file2);

		// Second commit: add class in second resource
		EPackage packFile2 = (EPackage)findObject(resource2, PACKAGE_NAME_PREFIX);
		final EClass newClassFile2 = createClass(packFile2, CLASS3_NAME_PREFIX + FILE2_SUFFIX);
		save(resource2);
		commits[1] = repository.addAndCommit(project, "master-commit-2", file2);

		// Third: rename that new class
		newClassFile2.setName(CLASS4_NAME_PREFIX + FILE2_SUFFIX);
		save(resource2);
		commits[2] = repository.addAndCommit(project, "master-commit-3", file2);

		// Branching point, though stay on master for now
		repository.createBranch(MASTER, BRANCH);

		// fourth commit: remove the new class
		packFile2.getEClassifiers().remove(newClassFile2);
		save(resource2);
		commits[3] = repository.addAndCommit(project, "master-commit-4", file2);

		// fifth and last commit on master: rename second class in both files
		final EClass classBFile1 = (EClass)findObject(resource1, CLASS2_NAME_PREFIX);
		final EClass classBFile2 = (EClass)findObject(resource2, CLASS2_NAME_PREFIX);
		classBFile1.setName(CLASS3_NAME_PREFIX + FILE1_SUFFIX);
		classBFile2.setName(CLASS3_NAME_PREFIX + FILE2_SUFFIX);
		save(resource1, resource2);
		commits[4] = repository.addAndCommit(project, "master-commit-5", file1, file2);

		// checkout the branch now
		repository.checkoutBranch(BRANCH);
		// Note that we have to reload our two resources now
		reload(resource1, resource2);

		// first commit of the branch : rename first class in both files
		final EClass classAFile1 = (EClass)findObject(resource1, CLASS1_NAME_PREFIX);
		final EClass classAFile2 = (EClass)findObject(resource2, CLASS1_NAME_PREFIX);
		classAFile1.setName(CLASS3_NAME_PREFIX + FILE1_SUFFIX);
		classAFile2.setName(CLASS3_NAME_PREFIX + FILE2_SUFFIX);
		save(resource1, resource2);
		commits[5] = repository.addAndCommit(project, "branch-commit-1", file1, file2);

		// second commit of the branch : delete the new (third) class
		// We've reloaded the resource, our reference to the class must be reinitialized
		EcoreUtil.remove(findObject(resource2, CLASS4_NAME_PREFIX));
		save(resource2);
		commits[6] = repository.addAndCommit(project, "branch-commit-2", file2);

		masterTipSynonyms = new String[] {MASTER, commits[4].getName(), };
		branchTipSynonyms = new String[] {BRANCH, commits[6].getName(), Constants.HEAD, };
	}

	@Test
	public void testCompareTipsFile1() throws Exception {
		// 1 conflict (deleted third class)
		// 6 diffs total (renamed both classes in both versions)

		for (String masterTip : masterTipSynonyms) {
			for (String branchTip : branchTipSynonyms) {
				compareBothDirectionsAndCheck(iFile1, masterTip, branchTip, 1, 3, 3);
			}
		}
	}

	@Test
	public void testCompareMasterTipWithBranchingPoint() throws Exception {
		final String branchingPoint = commits[2].getName();
		for (String masterTip : masterTipSynonyms) {
			// No conflict : comparing a version with its ancestor
			// 3 diffs : renamed class in both files, deleted the third

			compareBothDirectionsAndCheck(iFile1, masterTip, branchingPoint, 0, 3, 0);
		}
	}

	@Test
	public void testCompareBranchTipWithBranchingPoint() throws Exception {
		final String branchingPoint = commits[2].getName();
		for (String branchTip : branchTipSynonyms) {
			// No conflict : comparing a version with its ancestor
			// 3 diffs : renamed class in both files, deleted the third

			compareBothDirectionsAndCheck(iFile1, branchTip, branchingPoint, 0, 3, 0);
		}
	}

	@Test
	public void testCompareMasterTipWithInitial() throws Exception {
		final String initialCommit = commits[0].getName();
		for (String masterTip : masterTipSynonyms) {
			// No conflict : comparing a version with its ancestor
			// 2 diffs : renamed class in both files

			compareBothDirectionsAndCheck(iFile1, masterTip, initialCommit, 0, 2, 0);
		}
	}

	@Test
	public void testCompareBranchTipWithInitial() throws Exception {
		final String initialCommit = commits[0].getName();
		for (String branchTip : branchTipSynonyms) {
			// No conflict : comparing a version with its ancestor
			// 2 diffs : renamed class in both files

			compareBothDirectionsAndCheck(iFile1, branchTip, initialCommit, 0, 2, 0);
		}
	}

	@Test
	public void testCompareSecondToLastCommitBothBranch() throws Exception {
		// no conflict
		// 3 diffs : renamed two class on branch, removed the third on master
		final String masterCommit = commits[3].getName();
		final String branchCommit = commits[5].getName();

		compareBothDirectionsAndCheck(iFile1, masterCommit, branchCommit, 0, 1, 2);
	}

	private void compareBothDirectionsAndCheck(IFile file, String source, String destination,
			int expectedConflicts, int diffsInSource, int diffsInDestination) throws Exception {
		Comparison compareResult = compare(source, destination, file);

		assertEquals(expectedConflicts, compareResult.getConflicts().size());
		assertDiffCount(compareResult.getDifferences(), diffsInSource, diffsInDestination);

		compareResult = compare(destination, source, file);

		assertEquals(expectedConflicts, compareResult.getConflicts().size());
		assertDiffCount(compareResult.getDifferences(), diffsInDestination, diffsInSource);
	}

	private void assertDiffCount(List<Diff> differences, int expectedOutgoing, int expectedIncoming) {
		assertEquals(expectedOutgoing + expectedIncoming, differences.size());

		int outgoingCount = 0;
		int incomingCount = 0;
		for (Diff diff : differences) {
			switch (diff.getSource()) {
				case LEFT:
					outgoingCount++;
					break;
				case RIGHT:
					incomingCount++;
					break;
				default:
					break;
			}
		}

		assertEquals(expectedOutgoing, outgoingCount);
		assertEquals(expectedIncoming, incomingCount);
	}

	private Comparison compare(String sourceRev, String targetRev, IFile file) throws Exception {
		final String fullPath = file.getFullPath().toString();
		final Subscriber subscriber = repository.createSubscriberForComparison(sourceRev, targetRev, file);
		final IStorageProviderAccessor accessor = new SubscriberStorageAccessor(subscriber);
		final IStorageProvider sourceProvider = accessor.getStorageProvider(iFile1,
				IStorageProviderAccessor.DiffSide.SOURCE);
		final IStorageProvider remoteProvider = accessor.getStorageProvider(iFile1,
				IStorageProviderAccessor.DiffSide.REMOTE);
		final IStorageProvider ancestorProvider = accessor.getStorageProvider(iFile1,
				IStorageProviderAccessor.DiffSide.ORIGIN);
		assertNotNull(sourceProvider);
		assertNotNull(remoteProvider);
		assertNotNull(ancestorProvider);

		final IProgressMonitor monitor = new NullProgressMonitor();
		final IStorageProviderAccessor storageAccessor = new SubscriberStorageAccessor(subscriber);
		final ITypedElement left = new StorageTypedElement(sourceProvider.getStorage(monitor), fullPath);
		final ITypedElement right = new StorageTypedElement(remoteProvider.getStorage(monitor), fullPath);
		final ITypedElement origin = new StorageTypedElement(ancestorProvider.getStorage(monitor), fullPath);
		final ComparisonScopeBuilder scopeBuilder = new ComparisonScopeBuilder(new LogicalModelResolver(),
				new IdenticalResourceMinimizer(), storageAccessor);
		final IComparisonScope scope = scopeBuilder.build(left, right, origin, monitor);

		final ResourceSet leftResourceSet = (ResourceSet)scope.getLeft();
		final ResourceSet rightResourceSet = (ResourceSet)scope.getRight();
		final ResourceSet originResourceSet = (ResourceSet)scope.getOrigin();

		assertEquals(2, leftResourceSet.getResources().size());
		assertEquals(2, rightResourceSet.getResources().size());
		assertEquals(2, originResourceSet.getResources().size());

		return EMFCompare.builder().build().compare(scope, new BasicMonitor());
	}
}

Back to the top