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
;
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
{
57 public void setUpEnabledModelProvider() {
58 setEnabledModelProvider(ModelProvider
.RESOURCE_MODEL_PROVIDER_ID
);
62 public void shouldReturnNoChanges() throws Exception
{
64 changeFilesInProject();
67 launchSynchronization(HEAD
, R_HEADS
+ MASTER
, false);
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
));
81 public void shouldReturnListOfChanges() throws Exception
{
83 changeFilesInProject();
86 launchSynchronization(HEAD
, HEAD
, true);
89 SWTBotTree syncViewTree
= bot
.viewById(ISynchronizeView
.VIEW_ID
).bot().tree();
90 SWTBotTreeItem
[] syncItems
= syncViewTree
.getAllItems();
91 assertTrue(syncItems
[0].getText().contains(PROJ1
));
95 public void shouldCompareBranchAgainstTag() throws Exception
{
97 makeChangesAndCommit(PROJ1
);
100 launchSynchronization(INITIAL_TAG
, HEAD
, false);
103 SWTBotTree syncViewTree
= bot
.viewById(ISynchronizeView
.VIEW_ID
).bot().tree();
104 assertEquals(1, syncViewTree
.getAllItems().length
);
108 public void shouldCompareTagAgainstTag() throws Exception
{
110 makeChangesAndCommit(PROJ1
);
114 launchSynchronization(INITIAL_TAG
, R_TAGS
+ "v0.1", false);
117 SWTBotTree syncViewTree
= bot
.viewById(ISynchronizeView
.VIEW_ID
).bot().tree();
118 assertEquals(1, syncViewTree
.getAllItems().length
);
122 public void shouldOpenCompareEditor()
125 makeChangesAndCommit(PROJ1
);
126 changeFilesInProject();
129 launchSynchronization(HEAD
, INITIAL_TAG
, true);
132 CompareEditorTester compare
= getCompareEditorForFileInWorkspaceModel(FILE1
);
133 assertNotNull(compare
);
137 public void shouldListFileDeletedChange() throws Exception
{
139 deleteFileAndCommit(PROJ1
);
142 launchSynchronization(HEAD
, HEAD
+ "~1", true);
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());
159 public void shouldSynchronizeInEmptyRepository() throws Exception
{
161 createEmptyRepository();
164 launchSynchronization(EMPTY_PROJECT
, "", "", true);
167 SWTBotTree syncViewTree
= bot
.viewById(ISynchronizeView
.VIEW_ID
).bot().tree();
168 SWTBotTreeItem projectTree
= waitForNodeWithText(syncViewTree
,
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());
182 public void shouldExchangeCompareEditorSidesBetweenIncomingAndOutgoingChanges()
185 makeChangesAndCommit(PROJ1
);
187 // compare HEAD against tag
188 launchSynchronization(HEAD
, INITIAL_TAG
, false);
189 CompareEditorTester outgoingCompare
= getCompareEditorForFileInWorkspaceModel(
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();
198 // compare tag against HEAD
199 launchSynchronization(INITIAL_TAG
, HEAD
, false);
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
));
212 public void shouldNotShowIgnoredFiles()
215 String ignoredName
= "to-be-ignored.txt";
217 IProject proj
= ResourcesPlugin
.getWorkspace().getRoot()
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");
226 new ByteArrayInputStream(ignoredName
.getBytes(proj
227 .getDefaultCharset())), false, null);
228 proj
.refreshLocal(IResource
.DEPTH_INFINITE
, null);
231 launchSynchronization(INITIAL_TAG
, HEAD
, true);
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
);
241 public void shouldRefreshSyncResultAfterWorkspaceChange() throws Exception
{
243 String newFileName
= "new.txt";
244 launchSynchronization(INITIAL_TAG
, HEAD
, true);
245 IProject proj
= ResourcesPlugin
.getWorkspace().getRoot()
249 IFile newFile
= proj
.getFile(newFileName
);
251 new ByteArrayInputStream("content of new file".getBytes(proj
252 .getDefaultCharset())), false, null);
253 Job
.getJobManager().join(ResourcesPlugin
.FAMILY_AUTO_BUILD
, null);
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
));
265 public void shouldRefreshSyncResultAfterRepositoryChange() throws Exception
{
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
);
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
));
292 @Test @Ignore// workspace model dosn't show non-workspace files ... yet ;)
293 public void shouldShowNonWorkspaceFileInSynchronization()
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");
305 launchSynchronization(INITIAL_TAG
, HEAD
, true);
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()
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
);
328 launchSynchronization(INITIAL_TAG
, HEAD
, true);
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
);
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(
348 SWTBotTree syncViewTree
= bot
.viewById(ISynchronizeView
.VIEW_ID
).bot().tree();
350 SWTBotTreeItem projNode
= waitForNodeWithText(syncViewTree
, PROJ1
);
351 return getCompareEditor(projNode
, fileName
);