Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6f2b849052f6d4cb0dfabe677caa62c76f940b07 (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
/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.tests.ccvs.core.subscriber;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import junit.framework.AssertionFailedError;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.subscribers.ITeamResourceChangeListener;
import org.eclipse.team.core.subscribers.SyncInfo;
import org.eclipse.team.core.subscribers.TeamDelta;
import org.eclipse.team.core.subscribers.TeamSubscriber;
import org.eclipse.team.core.sync.RemoteSyncElement;
import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.tests.ccvs.core.EclipseTest;

/**
 * Provides test methods common to CVS sync subscribers
 */
public abstract class CVSSyncSubscriberTest extends EclipseTest {

	private ITeamResourceChangeListener listener;
	private List accumulatedTeamDeltas = new ArrayList();
	private static SyncInfoSource source = new SyncInfoSource();

	public CVSSyncSubscriberTest() {
		super();
	}
	
	public CVSSyncSubscriberTest(String name) {
		super(name);
	}

	public static void setSyncSource(SyncInfoSource newSource) {
		source = newSource;
	}
	
	protected TeamSubscriber getWorkspaceSubscriber() throws TeamException {
		TeamSubscriber subscriber = CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber();
		if (subscriber == null) fail("The CVS sync subsciber is not registered");
		return subscriber;
	}
	
	public SyncInfoSource getSyncInfoSource() {
		assertNotNull(source);
		return source;
	}
	
	protected void refresh(TeamSubscriber subscriber, IResource resource) throws TeamException {
		getSyncInfoSource().refresh(subscriber, resource);
	}
	
	/*
	 * Assert that the specified resources in the subscriber have the specified sync kind
	 * Ignore conflict types if they are not specified in the assert statement
	 */
	protected void assertSyncEquals(String message, TeamSubscriber subscriber, IContainer root, String[] resourcePaths, boolean refresh, int[] syncKinds) throws CoreException, TeamException {
		assertTrue(resourcePaths.length == syncKinds.length);
		if (refresh) refresh(subscriber, root);
		IResource[] resources = getResources(root, resourcePaths);
		for (int i=0;i<resources.length;i++) {
			assertSyncEquals(message, subscriber, resources[i], syncKinds[i]);
		}
		
	}
	
	protected void assertSyncEquals(String message, TeamSubscriber subscriber, IResource resource, int syncKind) throws TeamException {
		int conflictTypeMask = 0x0F; // ignore manual and auto merge sync types for now.
		SyncInfo info = getSyncInfo(subscriber, resource);
		int kind;
		int kindOther = syncKind & conflictTypeMask;
		if (info == null) {
			kind = SyncInfo.IN_SYNC;
		} else {
			kind = info.getKind() & conflictTypeMask;
		}
		assertTrue(message + ": improper sync state for " + resource + " expected " + 
				   RemoteSyncElement.kindToString(kindOther) + " but was " +
				   RemoteSyncElement.kindToString(kind), kind == kindOther);
	}
	
	protected SyncInfo getSyncInfo(TeamSubscriber subscriber, IResource resource) throws TeamException {
		return getSyncInfoSource().getSyncInfo(subscriber, resource);
	}

	/**
	 * @param changes
	 * @param resources
	 */
	protected void assertSyncChangesMatch(TeamDelta[] changes, IResource[] resources) {
		// First, ensure that all the resources appear in the delta
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			boolean found = false;
			for (int j = 0; j < changes.length; j++) {
				TeamDelta delta = changes[j];
				if (delta.getResource().equals(resource)) {
					found = true;
					break;
				}
			}
			assertTrue("No change reported for " + resource, found);
		}
		// TODO: We'll worry about extra deltas later
//		// Next, ensure there are no extra deltas
//		List changedResources = new ArrayList(resources.length);
//		changedResources.addAll(Arrays.asList(resources));
//		for (int i = 0; i < changes.length; i++) {
//			TeamDelta change = changes[i];
//			IResource resource = change.getResource();
//			assertTrue("Unanticipated change reported for " + resource, changedResources.contains(resource));
//		}
	}
	
	/* 
	 * Assert that the named resources have no local resource or sync info
	 */
	protected void assertDeleted(String message, IContainer root, String[] resourcePaths) throws CoreException, TeamException {
		IResource[] resources = getResources(root, resourcePaths);
		for (int i=0;i<resources.length;i++) {
			try {
				if (! resources[i].exists())
					break;
			} catch (AssertionFailedError e) {
				break;
			}
			assertTrue(message + ": resource " + resources[i] + " still exists in some form", false);
		}
	}
	
	public static class ResourceCondition {
		public boolean matches(IResource resource) throws CoreException, TeamException {
			return true;
		}
	}
	
	protected IResource[] collect(IResource[] resources, final ResourceCondition condition, int depth) throws CoreException, TeamException {
		final Set affected = new HashSet();
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			if (resource.exists() || resource.isPhantom()) {
				resource.accept(new IResourceVisitor() {
					public boolean visit(IResource r) throws CoreException {
						try {
							if (condition.matches(r)) {
								affected.add(r);
							}
						} catch (TeamException e) {
							throw new CoreException(e.getStatus());
						}
						return true;
					}
				}, depth, true /* include phantoms */);
			} else {
				if (condition.matches(resource)) {
					affected.add(resource);
				}
			}
		}
		return (IResource[]) affected.toArray(new IResource[affected.size()]);
	}
	
	/**
	 * @param resources
	 * @param condition
	 * @return
	 */
	protected IResource[] collectAncestors(IResource[] resources, ResourceCondition condition) throws CoreException, TeamException {
		Set affected = new HashSet();
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			while (resource.getType() != IResource.ROOT) {
				if (condition.matches(resource)) {
					affected.add(resource);
				} else {
					break;
				}
				resource = resource.getParent();
			}
		}
		return (IResource[]) affected.toArray(new IResource[affected.size()]);
	}
	
	protected TeamDelta[] deregisterSubscriberListener(TeamSubscriber subscriber) throws TeamException {
		subscriber.removeListener(listener);
		return (TeamDelta[]) accumulatedTeamDeltas.toArray(new TeamDelta[accumulatedTeamDeltas.size()]);
	}

	protected ITeamResourceChangeListener registerSubscriberListener(TeamSubscriber subscriber) throws TeamException {
		listener = new ITeamResourceChangeListener() {
			public void teamResourceChanged(TeamDelta[] deltas) {
				accumulatedTeamDeltas.addAll(Arrays.asList(deltas));
			}
		};
		accumulatedTeamDeltas.clear();
		subscriber.addListener(listener);
		return listener;
	}
	
	protected SyncInfo[] createSyncInfos(TeamSubscriber subscriber, IResource[] resources) throws TeamException {
		SyncInfo[] result = new SyncInfo[resources.length];
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			result[i] = getSyncInfo(subscriber, resource);
		}
		return result;
	}

	protected void assertProjectRemoved(TeamSubscriber subscriber, IProject project) throws TeamException {
		getSyncInfoSource().assertProjectRemoved(subscriber, project);
	}
}

Back to the top