Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64bb7dbf1b25f4fd159f8ee1d518f701b92be879 (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
/*******************************************************************************
 * Copyright (c) 2000, 2011 IBM Corporation and others.
 *
 * 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
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.tests.ccvs.ui;

import junit.framework.AssertionFailedError;

import org.eclipse.compare.structuremergeviewer.IDiffContainer;
import org.eclipse.compare.structuremergeviewer.IDiffElement;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.diff.IDiff;
import org.eclipse.team.core.subscribers.Subscriber;
import org.eclipse.team.core.synchronize.SyncInfo;
import org.eclipse.team.core.synchronize.SyncInfoTree;
import org.eclipse.team.internal.ccvs.core.CVSCompareSubscriber;
import org.eclipse.team.internal.ccvs.core.CVSMergeSubscriber;
import org.eclipse.team.internal.ccvs.core.CVSTag;
import org.eclipse.team.internal.ccvs.ui.subscriber.CompareParticipant;
import org.eclipse.team.internal.ccvs.ui.subscriber.MergeSynchronizeParticipant;
import org.eclipse.team.internal.ccvs.ui.subscriber.WorkspaceSynchronizeParticipant;
import org.eclipse.team.internal.core.subscribers.SubscriberSyncInfoCollector;
import org.eclipse.team.internal.ui.TeamUIPlugin;
import org.eclipse.team.internal.ui.synchronize.AbstractSynchronizeModelProvider;
import org.eclipse.team.internal.ui.synchronize.SubscriberParticipantPage;
import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
import org.eclipse.team.internal.ui.synchronize.SynchronizeModelManager;
import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
import org.eclipse.team.internal.ui.synchronize.SynchronizeView;
import org.eclipse.team.internal.ui.synchronize.TreeViewerAdvisor;
import org.eclipse.team.tests.ccvs.core.EclipseTest;
import org.eclipse.team.ui.TeamUI;
import org.eclipse.team.ui.synchronize.ISynchronizeManager;
import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
import org.eclipse.team.ui.synchronize.ISynchronizeParticipant;
import org.eclipse.team.ui.synchronize.ISynchronizeParticipantReference;
import org.eclipse.team.ui.synchronize.ISynchronizeView;
import org.eclipse.team.ui.synchronize.SubscriberParticipant;
import org.eclipse.team.ui.synchronize.WorkspaceScope;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.IPage;

/**
 * SyncInfoSource that obtains SyncInfo from the SynchronizeView's SyncSet.
 */
public class SubscriberParticipantSyncInfoSource extends ParticipantSyncInfoSource {

	public SubscriberParticipantSyncInfoSource() {
		super();
	}
	
	public SyncInfo getSyncInfo(Subscriber subscriber, IResource resource) throws TeamException {
		// Wait for the collector
		getCollector(subscriber);
		// Obtain the sync info from the viewer to ensure that the 
		// entire chain has the proper state
		SyncInfo info = internalGetSyncInfo(subscriber, resource);
		// Do a sanity check on the collected sync info
		if (info == null) {
			info = subscriber.getSyncInfo(resource);
			if ((info != null && info.getKind() != SyncInfo.IN_SYNC)) {
				throw new AssertionFailedError(
						"Sync state for " 
						+ resource.getFullPath() 
						+ " was "
						+ SyncInfo.kindToString(info.getKind())
						+ " but resource was not collected");
			}
		} else {
			SyncInfo realInfo = subscriber.getSyncInfo(resource);
			if (info.getKind() != realInfo.getKind()) {
				throw new AssertionFailedError(
						"Collected sync state for " 
						+ resource.getFullPath() 
						+ " was "
						+ SyncInfo.kindToString(info.getKind())
						+ " but the real state was "
						+ SyncInfo.kindToString(realInfo.getKind()));
			}
		}
		return info;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.tests.ccvs.core.subscriber.SyncInfoSource#getDiff(org.eclipse.team.core.subscribers.Subscriber, org.eclipse.core.resources.IResource)
	 */
	public IDiff getDiff(Subscriber subscriber, IResource resource) throws CoreException {
		SyncInfo info = getSyncInfo(subscriber, resource);
		if (info == null || info.getKind() == SyncInfo.IN_SYNC) {
			return null;
		}
		return getConverter(subscriber).getDeltaFor(info);
	}

	public static SubscriberParticipant getParticipant(Subscriber subscriber) {
		// show the sync view
		ISynchronizeParticipantReference[] participants = TeamUI.getSynchronizeManager().getSynchronizeParticipants();
		for (int i = 0; i < participants.length; i++) {
			ISynchronizeParticipant participant;
			try {
				participant = participants[i].getParticipant();
			} catch (TeamException e) {
				return null;
			}
			if(participant instanceof SubscriberParticipant) {
				if(((SubscriberParticipant)participant).getSubscriber() == subscriber) {
					return (SubscriberParticipant)participant;
				}
			}
		}
		return null;
	}
	
	public static SubscriberSyncInfoCollector getCollector(Subscriber subscriber) {
		SubscriberParticipant participant = getParticipant(subscriber);
		if (participant == null) return null;
		SubscriberSyncInfoCollector syncInfoCollector = participant.getSubscriberSyncInfoCollector();
		EclipseTest.waitForSubscriberInputHandling(syncInfoCollector);
        SubscriberParticipantPage page = getPage(subscriber);
        SynchronizeModelManager manager = (SynchronizeModelManager)page.getConfiguration().getProperty(SynchronizePageConfiguration.P_MODEL_MANAGER);
        AbstractSynchronizeModelProvider provider = (AbstractSynchronizeModelProvider)manager.getActiveModelProvider();
        provider.waitUntilDone(new IProgressMonitor() {
			public void beginTask(String name, int totalWork) {
			}
			public void done() {
			}
			public void internalWorked(double work) {
			}
			public boolean isCanceled() {
				return false;
			}
			public void setCanceled(boolean value) {
			}
			public void setTaskName(String name) {
			}
			public void subTask(String name) {
			}
			public void worked(int work) {
				while (Display.getCurrent().readAndDispatch()) {}
			}
		});
		return syncInfoCollector;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.tests.ccvs.core.subscriber.SyncInfoSource#assertProjectRemoved(org.eclipse.team.core.subscribers.TeamSubscriber, org.eclipse.core.resources.IProject)
	 */
	protected void assertProjectRemoved(Subscriber subscriber, IProject project) {		
		super.assertProjectRemoved(subscriber, project);
		SyncInfoTree set = getCollector(subscriber).getSyncInfoSet();
		if (set.hasMembers(project)) {
			throw new AssertionFailedError("The sync set still contains resources from the deleted project " + project.getName());	
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.tests.ccvs.core.subscriber.SyncInfoSource#createMergeSubscriber(org.eclipse.core.resources.IProject, org.eclipse.team.internal.ccvs.core.CVSTag, org.eclipse.team.internal.ccvs.core.CVSTag)
	 */
	public CVSMergeSubscriber createMergeSubscriber(IProject project, CVSTag root, CVSTag branch, boolean isModelSync) {
		CVSMergeSubscriber mergeSubscriber = super.createMergeSubscriber(project, root, branch, isModelSync);
		SubscriberParticipant participant = new MergeSynchronizeParticipant(mergeSubscriber);
		showParticipant(participant);
		return mergeSubscriber;
	}
	
	public Subscriber createWorkspaceSubscriber() throws TeamException {
		ISynchronizeManager synchronizeManager = TeamUI.getSynchronizeManager();
		ISynchronizeParticipantReference[] participants = synchronizeManager.get(WorkspaceSynchronizeParticipant.ID);
		if (participants.length > 0) {
			return ((SubscriberParticipant)participants[0].getParticipant()).getSubscriber();
		}
		SubscriberParticipant participant = new WorkspaceSynchronizeParticipant(new WorkspaceScope());
		showParticipant(participant);
		return participant.getSubscriber();
	}
	
	
	/* (non-Javadoc)
	 * @see org.eclipse.team.tests.ccvs.core.subscriber.SyncInfoSource#createCompareSubscriber(org.eclipse.core.resources.IProject, org.eclipse.team.internal.ccvs.core.CVSTag)
	 */
	public CVSCompareSubscriber createCompareSubscriber(IResource resource, CVSTag tag) {
		CVSCompareSubscriber s = super.createCompareSubscriber(resource, tag);
		SubscriberParticipant participant = new CompareParticipant(s);
		showParticipant(participant);
		return s;
	}
	
	private SyncInfo internalGetSyncInfo(Subscriber subscriber, IResource resource) {
		ISynchronizeModelElement root = getModelRoot(subscriber);
		return findSyncInfo(root, resource);
	}
	
    private SyncInfo findSyncInfo(ISynchronizeModelElement node, IResource resource) {
        if (node instanceof SyncInfoModelElement) {
            SyncInfoModelElement element = (SyncInfoModelElement)node;
            if (element.getResource().equals(resource)) {
                return element.getSyncInfo();
            }
        }
        IDiffElement[] children = node.getChildren();
        for (int i = 0; i < children.length; i++) {
            ISynchronizeModelElement child = (ISynchronizeModelElement)children[i];
            SyncInfo info = findSyncInfo(child, resource);
            if (info != null)
                return info;
        }
        return null;
    }
    
    public void assertViewMatchesModel(Subscriber subscriber) {
    	// Getting the collector waits for the subscriber input handlers
    	getCollector(subscriber);
		ISynchronizeModelElement root = getModelRoot(subscriber);
		TreeItem[] rootItems = getTreeItems(subscriber);
		assertMatchingTrees(root, rootItems, root.getChildren());
    }

    private ISynchronizeModelElement getModelRoot(Subscriber subscriber) {
        SubscriberParticipantPage page = getPage(subscriber);
        return ((TreeViewerAdvisor)page.getViewerAdvisor()).getModelManager().getModelRoot();
    }
    
    private TreeItem[] getTreeItems(Subscriber subscriber) {
        SubscriberParticipantPage page = getPage(subscriber);
        Viewer v = page.getViewer();
        if (v instanceof TreeViewer) {
            TreeViewer treeViewer = (TreeViewer)v;
            treeViewer.expandAll();
            Tree t = (treeViewer).getTree();
            return t.getItems();
        }
        throw new AssertionFailedError("The tree for " + subscriber.getName() + " could not be retrieved");
    }

    private static SubscriberParticipantPage getPage(Subscriber subscriber) {
        try {
            SubscriberParticipant participant = getParticipant(subscriber);
            if (participant == null)
            	throw new AssertionFailedError("The participant for " + subscriber.getName() + " could not be retrieved");
            IWorkbenchPage activePage = TeamUIPlugin.getActivePage();
            ISynchronizeView view = (ISynchronizeView)activePage.showView(ISynchronizeView.VIEW_ID);
            IPage page = ((SynchronizeView)view).getPage(participant);
            if (page instanceof SubscriberParticipantPage) {
            	SubscriberParticipantPage subscriberPage = (SubscriberParticipantPage)page;
            	return subscriberPage;
            }
        } catch (PartInitException e) {
            throw new AssertionFailedError("Cannot show sync view in active page");
        }
        throw new AssertionFailedError("The page for " + subscriber.getName() + " could not be retrieved");
    }
    
    private void assertMatchingTrees(IDiffElement parent, TreeItem[] items, IDiffElement[] children) {
        if ((items == null || items.length == 0) && (children == null || children.length == 0)) {
            // No children in either case so just return
            return;
        }
        if (items == null || children == null || items.length != children.length) {
            throw new AssertionFailedError("The number of children of " + parent.getName() + " is " + 
                    (children == null ? 0: children.length) + " but the view has " + 
                    (items == null ? 0 : items.length));
        }
        for (int i = 0; i < children.length; i++) {
            IDiffElement element = children[i];
            TreeItem foundItem = null;
            for (int j = 0; j < items.length; j++) {
                TreeItem item = items[j];
                if (item.getData() == element) {
                    foundItem = item;
                    break;
                }
            }
            if (foundItem == null) {
                throw new AssertionFailedError("Element" + element.getName() + " is in the model but not in the view");
            } else {
                assertMatchingTrees(element, foundItem.getItems(), ((IDiffContainer)element).getChildren());
            }
        }
        
    }
}

Back to the top