refactor: simplify collection.toArray()
[egit/eclipse.git] / org.eclipse.egit.ui.test / src / org / eclipse / egit / ui / view / synchronize / SynchronizeViewWorkspaceModelTest.java
blob2449d28fac5f70cbc0064bce64758fdabc142272
1 /*******************************************************************************
2 * Copyright (C) 2010, 2013 Dariusz Luksza <dariusz@luksza.org> and others.
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.eclipse.jgit.lib.Constants.HEAD;
14 import static org.eclipse.jgit.lib.Constants.MASTER;
15 import static org.eclipse.jgit.lib.Constants.R_HEADS;
16 import static org.eclipse.jgit.lib.Constants.R_TAGS;
17 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf;
18 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType;
19 import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withRegex;
20 import static org.hamcrest.CoreMatchers.equalTo;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNotSame;
24 import static org.junit.Assert.assertThat;
25 import static org.junit.Assert.assertTrue;
27 import java.io.BufferedWriter;
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.FileOutputStream;
31 import java.io.OutputStreamWriter;
33 import org.eclipse.core.resources.IFile;
34 import org.eclipse.core.resources.IProject;
35 import org.eclipse.core.resources.IResource;
36 import org.eclipse.core.resources.ResourcesPlugin;
37 import org.eclipse.core.resources.mapping.ModelProvider;
38 import org.eclipse.core.runtime.jobs.Job;
39 import org.eclipse.egit.ui.common.CompareEditorTester;
40 import org.eclipse.egit.ui.test.TestUtil;
41 import org.eclipse.swt.widgets.Label;
42 import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
43 import org.eclipse.swtbot.swt.finder.SWTBot;
44 import org.eclipse.swtbot.swt.finder.widgets.SWTBotLabel;
45 import org.eclipse.swtbot.swt.finder.widgets.SWTBotStyledText;
46 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
47 import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
48 import org.eclipse.team.ui.synchronize.ISynchronizeView;
49 import org.hamcrest.Matcher;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Test;
54 public class SynchronizeViewWorkspaceModelTest extends AbstractSynchronizeViewTest {
56 @Before
57 public void setUpEnabledModelProvider() {
58 setEnabledModelProvider(ModelProvider.RESOURCE_MODEL_PROVIDER_ID);
61 @Test
62 public void shouldReturnNoChanges() throws Exception {
63 // given
64 changeFilesInProject();
66 // when
67 launchSynchronization(HEAD, R_HEADS + MASTER, false);
69 // then
70 SWTBot viewBot = bot.viewById(ISynchronizeView.VIEW_ID).bot();
71 @SuppressWarnings("unchecked")
72 Matcher matcher = allOf(widgetOfType(Label.class),
73 withRegex("No changes in .*"));
75 @SuppressWarnings("unchecked")
76 SWTBotLabel l = new SWTBotLabel((Label) viewBot.widget(matcher));
77 assertNotNull(l);
80 @Test
81 public void shouldReturnListOfChanges() throws Exception {
82 // given
83 changeFilesInProject();
85 // when
86 launchSynchronization(HEAD, HEAD, true);
88 // then
89 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
90 SWTBotTreeItem[] syncItems = syncViewTree.getAllItems();
91 assertTrue(syncItems[0].getText().contains(PROJ1));
94 @Test
95 public void shouldCompareBranchAgainstTag() throws Exception {
96 // given
97 makeChangesAndCommit(PROJ1);
99 // when
100 launchSynchronization(INITIAL_TAG, HEAD, false);
102 // then
103 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
104 assertEquals(1, syncViewTree.getAllItems().length);
107 @Test
108 public void shouldCompareTagAgainstTag() throws Exception {
109 // given
110 makeChangesAndCommit(PROJ1);
111 createTag("v0.1");
113 // when
114 launchSynchronization(INITIAL_TAG, R_TAGS + "v0.1", false);
116 // then
117 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
118 assertEquals(1, syncViewTree.getAllItems().length);
121 @Test
122 public void shouldOpenCompareEditor()
123 throws Exception {
124 // given
125 makeChangesAndCommit(PROJ1);
126 changeFilesInProject();
128 // when
129 launchSynchronization(HEAD, INITIAL_TAG, true);
131 // then
132 CompareEditorTester compare = getCompareEditorForFileInWorkspaceModel(FILE1);
133 assertNotNull(compare);
136 @Test
137 public void shouldListFileDeletedChange() throws Exception {
138 // given
139 deleteFileAndCommit(PROJ1);
141 // when
142 launchSynchronization(HEAD, HEAD + "~1", true);
144 // then
145 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
146 assertEquals(1, syncViewTree.getAllItems().length);
148 SWTBotTreeItem projectTree = waitForNodeWithText(syncViewTree, PROJ1);
149 assertEquals(1, projectTree.getItems().length);
151 SWTBotTreeItem folderTree = waitForNodeWithText(projectTree, FOLDER);
152 assertEquals(1, folderTree.getItems().length);
154 SWTBotTreeItem fileTree = folderTree.getItems()[0];
155 assertEquals("test.txt", fileTree.getText());
158 @Test
159 public void shouldSynchronizeInEmptyRepository() throws Exception {
160 // given
161 createEmptyRepository();
163 // when
164 launchSynchronization(EMPTY_PROJECT, "", "", true);
166 // then
167 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
168 SWTBotTreeItem projectTree = waitForNodeWithText(syncViewTree,
169 EMPTY_PROJECT);
170 assertEquals(1, syncViewTree.getAllItems().length);
172 SWTBotTreeItem folderTree = waitForNodeWithText(projectTree, FOLDER);
173 assertEquals(2, folderTree.getItems().length);
175 SWTBotTreeItem fileTree = folderTree.getItems()[0];
176 assertEquals(FILE1, fileTree.getText());
177 fileTree = folderTree.getItems()[1];
178 assertEquals(FILE2, fileTree.getText());
181 @Test
182 public void shouldExchangeCompareEditorSidesBetweenIncomingAndOutgoingChanges()
183 throws Exception {
184 // given
185 makeChangesAndCommit(PROJ1);
187 // compare HEAD against tag
188 launchSynchronization(HEAD, INITIAL_TAG, false);
189 CompareEditorTester outgoingCompare = getCompareEditorForFileInWorkspaceModel(
190 FILE1);
191 // save left value from compare editor
192 String outgoingLeft = outgoingCompare.getLeftEditor().getText();
193 // save right value from compare editor
194 String outgoingRight = outgoingCompare.getRightEditor().getText();
195 outgoingCompare.close();
197 // when
198 // compare tag against HEAD
199 launchSynchronization(INITIAL_TAG, HEAD, false);
201 // then
202 CompareEditorTester incomingComp = getCompareEditorForFileInWorkspaceModel(FILE1);
203 String incomingLeft = incomingComp.getLeftEditor().getText();
204 String incomingRight = incomingComp.getRightEditor().getText();
205 // right side from compare editor should be equal with left
206 assertThat(outgoingLeft, equalTo(incomingRight));
207 // left side from compare editor should be equal with right
208 assertThat(outgoingRight, equalTo(incomingLeft));
211 @Test
212 public void shouldNotShowIgnoredFiles()
213 throws Exception {
214 // given
215 String ignoredName = "to-be-ignored.txt";
217 IProject proj = ResourcesPlugin.getWorkspace().getRoot()
218 .getProject(PROJ1);
220 IFile ignoredFile = proj.getFile(ignoredName);
221 ignoredFile.create(new ByteArrayInputStream("content of ignored file"
222 .getBytes(proj.getDefaultCharset())), false, null);
224 IFile gitignore = proj.getFile(".gitignore");
225 gitignore.create(
226 new ByteArrayInputStream(ignoredName.getBytes(proj
227 .getDefaultCharset())), false, null);
228 proj.refreshLocal(IResource.DEPTH_INFINITE, null);
230 // when
231 launchSynchronization(INITIAL_TAG, HEAD, true);
233 // then
234 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
235 SWTBotTreeItem projectTree = waitForNodeWithText(syncViewTree, PROJ1);
236 TestUtil.expandAndWait(projectTree);
237 assertEquals(1, projectTree.getItems().length);
240 @Test
241 public void shouldRefreshSyncResultAfterWorkspaceChange() throws Exception {
242 // given
243 String newFileName = "new.txt";
244 launchSynchronization(INITIAL_TAG, HEAD, true);
245 IProject proj = ResourcesPlugin.getWorkspace().getRoot()
246 .getProject(PROJ1);
248 // when
249 IFile newFile = proj.getFile(newFileName);
250 newFile.create(
251 new ByteArrayInputStream("content of new file".getBytes(proj
252 .getDefaultCharset())), false, null);
253 Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null);
255 // then
256 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
257 SWTBotTreeItem[] syncItems = syncViewTree.getAllItems();
258 assertTrue(syncItems[0].getText().contains(PROJ1));
259 TestUtil.expandAndWait(syncItems[0]);
260 // WidgetNotFoundException will be thrown when node named 'new.txt' not exists
261 assertNotNull(syncItems[0].getNode(newFileName));
264 @Test
265 public void shouldRefreshSyncResultAfterRepositoryChange() throws Exception {
266 // given
267 changeFilesInProject();
268 launchSynchronization(HEAD, HEAD, true);
270 // preconditions - sync result should contain two uncommitted changes
271 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
272 SWTBotTreeItem[] syncItems = syncViewTree.getAllItems();
273 assertTrue(syncItems[0].getText().contains(PROJ1));
274 TestUtil.expandAndWait(syncItems[0]);
275 TestUtil.expandAndWait(syncItems[0].getItems()[0]);
276 assertEquals(2, syncItems[0].getItems()[0].getItems().length);
278 // when
279 commit(PROJ1);
281 // then - synchronize view should be empty
282 SWTBot viewBot = bot.viewById(ISynchronizeView.VIEW_ID).bot();
283 @SuppressWarnings("unchecked")
284 Matcher matcher = allOf(widgetOfType(Label.class),
285 withRegex("No changes in .*"));
287 @SuppressWarnings("unchecked")
288 SWTBotLabel l = new SWTBotLabel((Label) viewBot.widget(matcher));
289 assertNotNull(l);
292 @Test @Ignore// workspace model dosn't show non-workspace files ... yet ;)
293 public void shouldShowNonWorkspaceFileInSynchronization()
294 throws Exception {
295 // given
296 String name = "non-workspace.txt";
297 File root = new File(getTestDirectory(), REPO1);
298 File nonWorkspace = new File(root, name);
299 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
300 new FileOutputStream(nonWorkspace), "UTF-8"));
301 writer.append("file content");
302 writer.close();
304 // when
305 launchSynchronization(INITIAL_TAG, HEAD, true);
307 // then
308 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
309 SWTBotTreeItem workingTree = syncViewTree.expandNode(PROJ1);
310 assertEquals(1, syncViewTree.getAllItems().length);
311 assertEquals(1, workingTree.getNodes(name).size());
314 @Test @Ignore// workspace model dosn't show non-workspace files ... yet ;)
315 public void shouldShowCompareEditorForNonWorkspaceFileFromSynchronization()
316 throws Exception {
317 // given
318 String content = "file content";
319 String name = "non-workspace.txt";
320 File root = new File(getTestDirectory(), REPO1);
321 File nonWorkspace = new File(root, name);
322 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
323 new FileOutputStream(nonWorkspace), "UTF-8"));
324 writer.append(content);
325 writer.close();
327 // when
328 launchSynchronization(INITIAL_TAG, HEAD, true);
330 // then
331 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
332 SWTBotTreeItem workingTree = syncViewTree.expandNode(PROJ1);
333 assertEquals(1, syncViewTree.getAllItems().length);
334 TestUtil.expandAndWait(workingTree).getNode(name).doubleClick();
336 SWTBotEditor editor = bot.editorByTitle(name);
337 editor.setFocus();
339 // the WidgetNotFoundException will be thrown when widget with given content cannot be not found
340 SWTBotStyledText left = editor.bot().styledText(content);
341 SWTBotStyledText right = editor.bot().styledText("");
342 // to be complete sure assert that both sides are not the same
343 assertNotSame(left, right);
346 protected CompareEditorTester getCompareEditorForFileInWorkspaceModel(
347 String fileName) {
348 SWTBotTree syncViewTree = bot.viewById(ISynchronizeView.VIEW_ID).bot().tree();
350 SWTBotTreeItem projNode = waitForNodeWithText(syncViewTree, PROJ1);
351 return getCompareEditor(projNode, fileName);