Skip to main content
summaryrefslogtreecommitdiffstats
blob: fd898ff03a22b1c2561339497528ae1be81f7533 (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) 2015 EclipseSource Munich 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:
 *     Florian Zoubek - initial API and implementation
 *     Philip Langer - refactoring and minor improvements
 *******************************************************************************/
package org.eclipse.emf.compare.ide.ui.tests.unit;

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

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.compare.ide.ui.dependency.DependencyProviderDescriptor;
import org.eclipse.emf.compare.ide.ui.dependency.IDependencyProvider;
import org.eclipse.emf.compare.ide.ui.dependency.ModelDependencyProviderRegistry;
import org.eclipse.emf.compare.ide.ui.internal.EMFCompareIDEUIPlugin;
import org.eclipse.emf.compare.ide.ui.tests.egit.CompareGitTestCase;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.lib.Constants;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

@SuppressWarnings({"nls", "restriction" })
public class MovedImplicitResourceAmongChangedResourcesTest extends CompareGitTestCase {

	private static final HashMap<Object, Object> EMPTY_MAP = new HashMap<Object, Object>();

	private static final String MASTER = Constants.R_HEADS + Constants.MASTER;

	private static final String MOVED_MODEL_BRANCH = Constants.R_HEADS + "movedModelBranch";

	private static final String CHANGED_MODEL_BRANCH = Constants.R_HEADS + "changedModelBranch";

	private static final String ROOT_FILE_PATH = "directory1/file1.ecore";

	private static final String IMPLICIT_FILE_PATH = "directory1/file2.ecore";

	private static final String IMPLICIT_FILE_MOVED_PATH = "directory2/file2.ecore";

	private static final String SUBMODEL_FILE_PATH = "directory1/file3.ecore";

	private static final String SUBMODEL_FILE_MOVED_PATH = "directory2/file3.ecore";

	private File rootFile;

	private File identicalFile;

	private File submodelFile;

	private IFile rootIFile;

	private IFile identicalIFile;

	private IFile submodelIFile;

	private ResourceSet resourceSet;

	private IProject iProject;

	private Resource rootResource;

	private Resource identicalResource;

	private Resource submodelResource;

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

		rootFile = project.getOrCreateFile(iProject, ROOT_FILE_PATH);
		identicalFile = project.getOrCreateFile(iProject, IMPLICIT_FILE_PATH);
		submodelFile = project.getOrCreateFile(iProject, SUBMODEL_FILE_PATH);
		rootIFile = project.getIFile(iProject, rootFile);
		identicalIFile = project.getIFile(iProject, identicalFile);
		submodelIFile = project.getIFile(iProject, submodelFile);

		rootResource = connectResource(rootIFile, resourceSet);
		identicalResource = connectResource(identicalIFile, resourceSet);
		submodelResource = connectResource(submodelIFile, resourceSet);
	}

	@Ignore("Due to Bug 464379, this test no longer works. It needs management of resource renaming via ResourceAttachmentChange.MOVE")
	@Test
	public void testRebaseNoConflictMovedSubmodel() throws Exception {
		setUpMovedIdenticalResourceAndChangedResourceBranches();

		installMockModelDependencyProvider(
				ImmutableMap.of(submodelFile.getName(), ImmutableSet.of(identicalFile.getName())));

		repository.checkoutBranch(MOVED_MODEL_BRANCH);
		iProject.refreshLocal(IResource.DEPTH_INFINITE, null);

		repository.rebaseLogical(CHANGED_MODEL_BRANCH);
		iProject.refreshLocal(IResource.DEPTH_INFINITE, null);

		assertTrue(repository.status().getConflicting().isEmpty());

		assertFalse(iProject.getFile(IMPLICIT_FILE_PATH).exists());
		assertFalse(iProject.getFile(SUBMODEL_FILE_PATH).exists());

		final IFile movedSubmodelFile = iProject.getFile(SUBMODEL_FILE_MOVED_PATH);
		final IFile movedIdenticalFile = iProject.getFile(IMPLICIT_FILE_MOVED_PATH);
		assertTrue(iProject.getFile(ROOT_FILE_PATH).exists());
		assertTrue(movedSubmodelFile.exists());
		assertTrue(movedIdenticalFile.exists());

		// Check the contents
		unload(rootResource, identicalResource, submodelResource);
		resourceSet.getResources().clear();
		rootResource = connectResource(iProject.getFile(ROOT_FILE_PATH), resourceSet);
		Resource movedIdenticalResource = connectResource(movedIdenticalFile, resourceSet);
		Resource movedSubmodelResource = connectResource(movedSubmodelFile, resourceSet);
		rootResource.load(EMPTY_MAP);
		movedIdenticalResource.load(EMPTY_MAP);
		movedSubmodelResource.load(EMPTY_MAP);

		EPackage testRootPack = (EPackage)rootResource.getContents().get(0);
		assertEquals("parent1", testRootPack.getName());

		EPackage testImplicitRootPack = (EPackage)movedIdenticalResource.getContents().get(0);
		assertEquals("parent2", testImplicitRootPack.getName());

		EPackage testSubmodelPack = testRootPack.getESubpackages().get(0);
		assertEquals("child", testSubmodelPack.getName());
		assertSame(movedSubmodelResource, ((InternalEObject)testSubmodelPack).eDirectResource());

		EClass testC1 = (EClass)testSubmodelPack.getEClassifiers().get(0);
		assertEquals("C1", testC1.getName());

		EClass nonConflictingClass = (EClass)testSubmodelPack.getEClassifiers().get(1);
		assertEquals("NonConflicting", nonConflictingClass.getName());
	}

	/**
	 * Sets up a git repository with a model and a submodel with an implicit dependency in the master branch,
	 * and two branches that base on the master branch: A branch {@link #MOVED_MODEL_BRANCH} with the submodel
	 * and implicit dependency moved into another directory, as well as a branch {@link #CHANGED_MODEL_BRANCH}
	 * with the submodel changed.
	 */
	public void setUpMovedIdenticalResourceAndChangedResourceBranches() throws Exception {
		EPackage root = createPackage(null, "parent1");
		EPackage implicitRoot = createPackage(null, "parent2");
		EPackage submodelChild = createPackage(root, "child");
		createClass(submodelChild, "C1");
		saveTestResource(submodelResource, submodelChild);
		saveTestResource(identicalResource, implicitRoot);
		saveTestResource(rootResource, root);
		repository.addAllAndCommit("initial-commit");

		// create and checkout branch for the moved submodel
		repository.createBranch(MASTER, MOVED_MODEL_BRANCH);
		repository.checkoutBranch(MOVED_MODEL_BRANCH);
		iProject.refreshLocal(IResource.DEPTH_INFINITE, null);

		reload(rootResource, submodelResource, identicalResource);

		// create new files with "moved" path
		IFile movedSubmodelFile = project.getIFile(iProject,
				project.getOrCreateFile(iProject, SUBMODEL_FILE_MOVED_PATH));
		IFile movedIdenticalFile = project.getIFile(iProject,
				project.getOrCreateFile(iProject, IMPLICIT_FILE_MOVED_PATH));
		Resource movedSubRes = connectResource(movedSubmodelFile, resourceSet);
		Resource movedIdenticalRes = connectResource(movedIdenticalFile, resourceSet);

		// save the existing objects from the old files into the new "moved" files
		saveTestResource(movedSubRes, (EPackage)findObject(submodelResource, "child"));
		saveTestResource(movedIdenticalRes, (EPackage)findObject(identicalResource, "parent2"));

		// save other resources as well
		save(rootResource, submodelResource, identicalResource);

		// delete old files
		iProject.getFile(IMPLICIT_FILE_PATH).delete(true, new NullProgressMonitor());
		iProject.getFile(SUBMODEL_FILE_PATH).delete(true, new NullProgressMonitor());

		// remove former files from the index
		repository.removeFromIndex(submodelIFile);
		repository.removeFromIndex(identicalIFile);
		repository.addAllAndCommit("Moved submodel file and implicit dependency to other directory");

		// create and checkout branch for the model changes
		repository.createBranch(MASTER, CHANGED_MODEL_BRANCH);
		repository.checkoutBranch(CHANGED_MODEL_BRANCH);
		repository.reset(CHANGED_MODEL_BRANCH, ResetType.HARD);
		iProject.refreshLocal(IResource.DEPTH_INFINITE, null);

		// reload resources
		unload(rootResource, identicalResource, submodelResource, movedIdenticalRes, movedSubRes);
		resourceSet.getResources().clear();
		reload(rootResource, identicalResource, submodelResource);

		// add a class and save all resources
		createClass((EPackage)submodelResource.getContents().get(0), "NonConflicting");
		save(submodelResource);
		save(identicalResource);
		save(rootResource);
		repository.addAllAndCommit("Added class to child.");

		// reset current branch to master
		repository.checkoutBranch(MASTER);
		repository.reset(MASTER, ResetType.HARD);
		iProject.refreshLocal(IResource.DEPTH_INFINITE, null);

		// unload resources
		unload(rootResource, identicalResource, submodelResource, movedIdenticalRes, movedSubRes);
		resourceSet.getResources().clear();
	}

	protected void saveTestResource(Resource resource, EPackage pkg) throws IOException, CoreException {
		resource.getContents().clear();
		resource.getContents().add(pkg);
		save(resource);
	}

	@Override
	@After
	public void tearDown() throws Exception {
		getModelDependencyProviderRegistry().clear();
		unload(rootResource, identicalResource, submodelResource);
		super.tearDown();
	}

	private ModelDependencyProviderRegistry getModelDependencyProviderRegistry() {
		return EMFCompareIDEUIPlugin.getDefault().getModelDependencyProviderRegistry();
	}

	private void installMockModelDependencyProvider(
			final ImmutableMap<String, ImmutableSet<String>> dependencies) {
		getModelDependencyProviderRegistry().addProvider("mock",
				new DependencyProviderDescriptor(null, null) {
					private final IDependencyProvider mock = new MockDependencyProvider(dependencies);

					@Override
					public IDependencyProvider getDependencyProvider() {
						return mock;
					}
				});
	}

	private class MockDependencyProvider implements IDependencyProvider {

		private ImmutableMap<String, ImmutableSet<String>> dependencies;

		public MockDependencyProvider(ImmutableMap<String, ImmutableSet<String>> dependencies) {
			this.dependencies = dependencies;
		}

		public boolean apply(URI uri) {
			return dependencies.containsKey(uri.lastSegment());
		}

		public Set<URI> getDependencies(URI uri, URIConverter uriConverter) {
			if (dependencies.containsKey(uri.lastSegment())) {
				final Set<URI> uris = new HashSet<URI>();
				for (String fileName : dependencies.get(uri.lastSegment())) {
					final URI dependentUri = uri.trimSegments(1).appendSegment(fileName);
					if (uriConverter.exists(dependentUri, Collections.emptyMap())) {
						uris.add(dependentUri);
					}
				}
				return uris;
			} else {
				return Collections.emptySet();
			}
		}

	}
}

Back to the top