Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b0c80a574fa4418d72c9bb3b851bafd50df3847 (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
/*******************************************************************************
 * Copyright (C) 2016 Obeo.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.view.synchronize;

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

import java.io.ByteArrayInputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.resources.mapping.ModelProvider;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.op.CommitOperation;
import org.eclipse.egit.core.op.ConnectProviderOperation;
import org.eclipse.egit.core.op.CreateLocalBranchOperation;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.UIPreferences;
import org.eclipse.egit.ui.test.ContextMenuHelper;
import org.eclipse.egit.ui.test.TestUtil;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.eclipse.team.ui.synchronize.ISynchronizeView;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SynchronizeViewRemoteAwareChangeSetModelTest
		extends AbstractSynchronizeViewTest {

	private IFile mockLogicalFile;

	private static final String MOCK_LOGICAL_PROJECT = "MockLogical";

	@Before
	public void setUpPreferences() {
		Activator.getDefault().getPreferenceStore()
				.setValue(UIPreferences.USE_LOGICAL_MODEL, true);
	}

	@Before
	public void setUpEnabledModelProvider() {
		setEnabledModelProvider(ModelProvider.RESOURCE_MODEL_PROVIDER_ID);
	}

	@After
	public void tearDownPreferences() {
		Activator.getDefault().getPreferenceStore()
				.setToDefault(UIPreferences.USE_LOGICAL_MODEL);
	}

	/**
	 * Make sure that files that are not part of the logical model because of
	 * remote file content will be taken into account in the comparison.
	 *
	 * @throws Exception
	 */
	@Test
	public void shouldShowRemoteFilesInSynchronization() throws Exception {
		// given
		createMockLogicalRepository();

		// when comparing file 'index.mocklogical' with branch 'stable'
		String compareWithBranchActionLabel = util
				.getPluginLocalizedValue("ReplaceWithRefAction_label");
		clickCompareWith(compareWithBranchActionLabel);
		SWTBotShell compareShell = bot.shell("Compare");
		SWTBotTree tree = compareShell.bot().tree();
		for (SWTBotTreeItem item : tree.getTreeItem("Local").getItems()) {
			if (item.getText().contains("stable")) {
				tree.select(item);
				break;
			}
		}
		bot.button("Compare").click();

		// then
		SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot()
				.tree();
		SWTBotTreeItem mockLogicalProjectItem = waitForNodeWithText(
				syncViewTree, MOCK_LOGICAL_PROJECT);
		SWTBotTreeItem[] items = mockLogicalProjectItem.getItems();
		assertEquals(4, items.length);

		SWTBotTreeItem fileTree = items[0];
		assertEquals("file1.txt", fileTree.getText());
		fileTree = items[1];
		assertEquals("file2.txt", fileTree.getText());
		fileTree = items[2];
		assertEquals("file3.txt", fileTree.getText());
		fileTree = items[3];
		assertEquals("index.mocklogical", fileTree.getText());
	}

	private void clickCompareWith(String menuLabel) {
		SWTBotTree projectExplorerTree = selectMockLogicalItem();
		ContextMenuHelper.clickContextMenu(projectExplorerTree, "Compare With",
				menuLabel);
	}

	private SWTBotTree selectMockLogicalItem() {
		SWTBotTree projectExplorerTree = TestUtil.getExplorerTree();
		SWTBotTreeItem projectItem = getProjectItem(projectExplorerTree,
				MOCK_LOGICAL_PROJECT);
		projectItem.expand();
		for (SWTBotTreeItem item : projectItem.getItems()) {
			if (item.getText().contains("index.mocklogical")) {
				item.select();
			}
		}
		return projectExplorerTree;
	}

	protected void createMockLogicalRepository() throws Exception {
		File gitDir = new File(
				new File(getTestDirectory(), MOCK_LOGICAL_PROJECT),
				Constants.DOT_GIT);
		Repository repo = FileRepositoryBuilder.create(gitDir);
		repo.create();

		// we need to commit into master first
		IProject project = ResourcesPlugin.getWorkspace().getRoot()
				.getProject(MOCK_LOGICAL_PROJECT);

		if (project.exists()) {
			project.delete(true, null);
			TestUtil.waitForJobs(100, 5000);
		}
		IProjectDescription desc = ResourcesPlugin.getWorkspace()
				.newProjectDescription(MOCK_LOGICAL_PROJECT);
		desc.setLocation(new Path(
				new File(repo.getWorkTree(), MOCK_LOGICAL_PROJECT).getPath()));
		project.create(desc, null);
		project.open(null);
		assertTrue("Project is not accessible: " + project,
				project.isAccessible());

		TestUtil.waitForJobs(50, 5000);
		try {
			new ConnectProviderOperation(project, gitDir).execute(null);
		} catch (Exception e) {
			Activator.logError("Failed to connect project to repository", e);
		}
		assertConnected(project);

		mockLogicalFile = project.getFile("index.mocklogical");
		mockLogicalFile.create(new ByteArrayInputStream(
				"file1.txt\nfile2.txt".getBytes(project.getDefaultCharset())),
				false, null);
		IFile file1 = project.getFile("file1.txt");
		file1.create(
				new ByteArrayInputStream(
						"Content 1".getBytes(project.getDefaultCharset())),
				false, null);
		IFile file2 = project.getFile("file2.txt");
		file2.create(
				new ByteArrayInputStream(
						"Content 2".getBytes(project.getDefaultCharset())),
				false, null);

		IFile[] commitables = new IFile[] { mockLogicalFile, file1, file2 };
		List<IFile> untracked = new ArrayList<>();
		untracked.addAll(Arrays.asList(commitables));
		CommitOperation op = new CommitOperation(commitables, untracked,
				TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "Initial commit");
		op.execute(null);
		RevCommit firstCommit = op.getCommit();

		CreateLocalBranchOperation createBranchOp = new CreateLocalBranchOperation(
				repo, "refs/heads/stable", firstCommit);
		createBranchOp.execute(null);

		// Delete file2.txt from logical model and add file3
		mockLogicalFile = touch(MOCK_LOGICAL_PROJECT, "index.mocklogical",
				"file1.txt\nfile3.txt");
		file2.delete(true, null);
		touch(MOCK_LOGICAL_PROJECT, "file1.txt", "Content 1 modified");
		IFile file3 = project.getFile("file3.txt");
		file3.create(
				new ByteArrayInputStream(
						"Content 3".getBytes(project.getDefaultCharset())),
				false, null);
		commitables = new IFile[] { mockLogicalFile, file1, file2, file3 };
		untracked = new ArrayList<>();
		untracked.add(file3);
		op = new CommitOperation(commitables, untracked, TestUtil.TESTAUTHOR,
				TestUtil.TESTCOMMITTER, "Second commit");
		op.execute(null);
	}

}

Back to the top