refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.ui.test / src / org / eclipse / egit / ui / view / synchronize / SynchronizeViewRemoteAwareChangeSetModelTest.java
blob8b0c80a574fa4418d72c9bb3b851bafd50df3847
1 /*******************************************************************************
2 * Copyright (C) 2016 Obeo.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.view.synchronize;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertTrue;
16 import java.io.ByteArrayInputStream;
17 import java.io.File;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IProjectDescription;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.resources.mapping.ModelProvider;
27 import org.eclipse.core.runtime.Path;
28 import org.eclipse.egit.core.op.CommitOperation;
29 import org.eclipse.egit.core.op.ConnectProviderOperation;
30 import org.eclipse.egit.core.op.CreateLocalBranchOperation;
31 import org.eclipse.egit.ui.Activator;
32 import org.eclipse.egit.ui.UIPreferences;
33 import org.eclipse.egit.ui.test.ContextMenuHelper;
34 import org.eclipse.egit.ui.test.TestUtil;
35 import org.eclipse.jgit.lib.Constants;
36 import org.eclipse.jgit.lib.Repository;
37 import org.eclipse.jgit.revwalk.RevCommit;
38 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
39 import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
40 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
41 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
42 import org.eclipse.team.ui.synchronize.ISynchronizeView;
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
47 public class SynchronizeViewRemoteAwareChangeSetModelTest
48 extends AbstractSynchronizeViewTest {
50 private IFile mockLogicalFile;
52 private static final String MOCK_LOGICAL_PROJECT = "MockLogical";
54 @Before
55 public void setUpPreferences() {
56 Activator.getDefault().getPreferenceStore()
57 .setValue(UIPreferences.USE_LOGICAL_MODEL, true);
60 @Before
61 public void setUpEnabledModelProvider() {
62 setEnabledModelProvider(ModelProvider.RESOURCE_MODEL_PROVIDER_ID);
65 @After
66 public void tearDownPreferences() {
67 Activator.getDefault().getPreferenceStore()
68 .setToDefault(UIPreferences.USE_LOGICAL_MODEL);
71 /**
72 * Make sure that files that are not part of the logical model because of
73 * remote file content will be taken into account in the comparison.
75 * @throws Exception
77 @Test
78 public void shouldShowRemoteFilesInSynchronization() throws Exception {
79 // given
80 createMockLogicalRepository();
82 // when comparing file 'index.mocklogical' with branch 'stable'
83 String compareWithBranchActionLabel = util
84 .getPluginLocalizedValue("ReplaceWithRefAction_label");
85 clickCompareWith(compareWithBranchActionLabel);
86 SWTBotShell compareShell = bot.shell("Compare");
87 SWTBotTree tree = compareShell.bot().tree();
88 for (SWTBotTreeItem item : tree.getTreeItem("Local").getItems()) {
89 if (item.getText().contains("stable")) {
90 tree.select(item);
91 break;
94 bot.button("Compare").click();
96 // then
97 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot()
98 .tree();
99 SWTBotTreeItem mockLogicalProjectItem = waitForNodeWithText(
100 syncViewTree, MOCK_LOGICAL_PROJECT);
101 SWTBotTreeItem[] items = mockLogicalProjectItem.getItems();
102 assertEquals(4, items.length);
104 SWTBotTreeItem fileTree = items[0];
105 assertEquals("file1.txt", fileTree.getText());
106 fileTree = items[1];
107 assertEquals("file2.txt", fileTree.getText());
108 fileTree = items[2];
109 assertEquals("file3.txt", fileTree.getText());
110 fileTree = items[3];
111 assertEquals("index.mocklogical", fileTree.getText());
114 private void clickCompareWith(String menuLabel) {
115 SWTBotTree projectExplorerTree = selectMockLogicalItem();
116 ContextMenuHelper.clickContextMenu(projectExplorerTree, "Compare With",
117 menuLabel);
120 private SWTBotTree selectMockLogicalItem() {
121 SWTBotTree projectExplorerTree = TestUtil.getExplorerTree();
122 SWTBotTreeItem projectItem = getProjectItem(projectExplorerTree,
123 MOCK_LOGICAL_PROJECT);
124 projectItem.expand();
125 for (SWTBotTreeItem item : projectItem.getItems()) {
126 if (item.getText().contains("index.mocklogical")) {
127 item.select();
130 return projectExplorerTree;
133 protected void createMockLogicalRepository() throws Exception {
134 File gitDir = new File(
135 new File(getTestDirectory(), MOCK_LOGICAL_PROJECT),
136 Constants.DOT_GIT);
137 Repository repo = FileRepositoryBuilder.create(gitDir);
138 repo.create();
140 // we need to commit into master first
141 IProject project = ResourcesPlugin.getWorkspace().getRoot()
142 .getProject(MOCK_LOGICAL_PROJECT);
144 if (project.exists()) {
145 project.delete(true, null);
146 TestUtil.waitForJobs(100, 5000);
148 IProjectDescription desc = ResourcesPlugin.getWorkspace()
149 .newProjectDescription(MOCK_LOGICAL_PROJECT);
150 desc.setLocation(new Path(
151 new File(repo.getWorkTree(), MOCK_LOGICAL_PROJECT).getPath()));
152 project.create(desc, null);
153 project.open(null);
154 assertTrue("Project is not accessible: " + project,
155 project.isAccessible());
157 TestUtil.waitForJobs(50, 5000);
158 try {
159 new ConnectProviderOperation(project, gitDir).execute(null);
160 } catch (Exception e) {
161 Activator.logError("Failed to connect project to repository", e);
163 assertConnected(project);
165 mockLogicalFile = project.getFile("index.mocklogical");
166 mockLogicalFile.create(new ByteArrayInputStream(
167 "file1.txt\nfile2.txt".getBytes(project.getDefaultCharset())),
168 false, null);
169 IFile file1 = project.getFile("file1.txt");
170 file1.create(
171 new ByteArrayInputStream(
172 "Content 1".getBytes(project.getDefaultCharset())),
173 false, null);
174 IFile file2 = project.getFile("file2.txt");
175 file2.create(
176 new ByteArrayInputStream(
177 "Content 2".getBytes(project.getDefaultCharset())),
178 false, null);
180 IFile[] commitables = new IFile[] { mockLogicalFile, file1, file2 };
181 List<IFile> untracked = new ArrayList<>();
182 untracked.addAll(Arrays.asList(commitables));
183 CommitOperation op = new CommitOperation(commitables, untracked,
184 TestUtil.TESTAUTHOR, TestUtil.TESTCOMMITTER, "Initial commit");
185 op.execute(null);
186 RevCommit firstCommit = op.getCommit();
188 CreateLocalBranchOperation createBranchOp = new CreateLocalBranchOperation(
189 repo, "refs/heads/stable", firstCommit);
190 createBranchOp.execute(null);
192 // Delete file2.txt from logical model and add file3
193 mockLogicalFile = touch(MOCK_LOGICAL_PROJECT, "index.mocklogical",
194 "file1.txt\nfile3.txt");
195 file2.delete(true, null);
196 touch(MOCK_LOGICAL_PROJECT, "file1.txt", "Content 1 modified");
197 IFile file3 = project.getFile("file3.txt");
198 file3.create(
199 new ByteArrayInputStream(
200 "Content 3".getBytes(project.getDefaultCharset())),
201 false, null);
202 commitables = new IFile[] { mockLogicalFile, file1, file2, file3 };
203 untracked = new ArrayList<>();
204 untracked.add(file3);
205 op = new CommitOperation(commitables, untracked, TestUtil.TESTAUTHOR,
206 TestUtil.TESTCOMMITTER, "Second commit");
207 op.execute(null);