Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 833849309aec22bcca8c9364ef635e8d60da7403 (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
/*******************************************************************************
 * 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.ui.synchronize;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Test;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.swt.widgets.Item;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.synchronize.*;
import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.tests.core.TeamTest;
import org.eclipse.team.tests.ui.views.ContentProviderTestView;
import org.eclipse.team.tests.ui.views.TestTreeViewer;
import org.eclipse.team.ui.synchronize.viewers.*;


public class TestDiffNodePresentationModel extends TeamTest {
	
	private ContentProviderTestView view;
	private SyncInfoTree set;
	private TreeViewerAdvisor configuration;
	
	public TestDiffNodePresentationModel() {
		super();
	}

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

	public static Test suite() {
		return suite(TestDiffNodePresentationModel.class);
	}
	
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
		this.set = new SyncInfoTree();
		this.configuration = new TreeViewerAdvisor(set) {
			protected SynchronizeModelProvider getDiffNodeController() {
				return TestDiffNodePresentationModel.this.getDiffNodeController(set);
			}
		};
		view = ContentProviderTestView.findViewInActivePage(null);
		configuration.initializeViewer(view.getViewer());
	}
	
	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		set = null;
		configuration.dispose();
		super.tearDown();
	}
	
	protected SynchronizeModelProvider getDiffNodeController(SyncInfoTree set) {
		return new HierarchicalModelProvider(set);
	}
	
	/*
	 * This method creates a project with the given resources, imports
	 * it to CVS and checks it out
	 */
	protected IProject createProject(String prefix, String[] resources) throws CoreException {
		IProject project = getUniqueTestProject(prefix);
		buildResources(project, resources, true);
		return project;
	}
	
	/*
	 * Create a test project using the currently running test case as the project name prefix
	 */
	protected IProject createProject(String[] resources) throws CoreException {
		return createProject(getName(), resources);
	}
		
	private void adjustSet(SyncInfoSet set, IProject project, String[] resourceStrings, int[] syncKind) throws TeamException {
		IResource[] resources = buildResources(project, resourceStrings);
		try {
			set.beginInput();
			for (int i = 0; i < resources.length; i++) {
				IResource resource = resources[i];
				int kind = syncKind[i];
				if (kind == SyncInfo.IN_SYNC) {
					set.remove(resource);
				} else {
					SyncInfo newInfo = new TestSyncInfo(resource, kind);
					set.add(newInfo);
				}
			}
		} finally {
			set.endInput(null);
		}
	}

	/**
	 * Ensure that the resource
	 * @param resources
	 */
	protected void assertProperVisibleItems() {
		IResource[] resources = set.getResources();
		List resourceList = new ArrayList();
		for (int i = 0; i < resources.length; i++) {
			IResource resource = resources[i];
			resourceList.add(resource);
		}
		TestTreeViewer viewer = view.getViewer();
		Item[] items = viewer.getRootItems();
		if (resources.length ==  0) {
			assertTrue("There are items visible when there should not be.", items.length == 0);
			return;
		}
		// Test that all items in the tree are expected
		for (int i = 0; i < items.length; i++) {
			Item item = items[i];
			assertThatAllOutOfSyncResourcesAreShown(item, resourceList);
		}
		// Test that all expected resources and their parents are present
		assertTrue("The tree did not contain all expected resources: " + resourceList.toString(), resourceList.isEmpty());
	}
	
	/**
	 * Traverse every element shown in the view and ensure that every out-of-sync 
	 * resource in the set is at least shown. This doesn't test the actual logical
	 * organization, but does ensure that all out-of-sync resources are shown only
	 * once.
	 */
	protected void assertThatAllOutOfSyncResourcesAreShown(Item item, List outOfSyncResources) {
		Object node = item.getData();
		SyncInfo info = (SyncInfo)Utils.getAdapter(node, SyncInfo.class);
		if(info != null) {
			assertTrue("The tree contained an out-of-sync resource that wasn't in the set", outOfSyncResources.remove(info.getLocal()));
		}
		Item[] children = view.getViewer().getChildren(item);
		for (int i = 0; i < children.length; i++) {
			Item child = children[i];
			assertThatAllOutOfSyncResourcesAreShown(child, outOfSyncResources);
		}
	}
	
	public void testNestedFolder() throws CoreException {
		IProject project = createProject(new String[]{"file.txt", "folder1/file2.txt", "folder1/folder2/file3.txt"});
		adjustSet(set, project, 
				new String[]{"file.txt"}, 
				new int[]{SyncInfo.OUTGOING | SyncInfo.CHANGE});
		assertProperVisibleItems();
		adjustSet(set, project, 
				new String[]{"folder1/file2.txt", "folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.OUTGOING | SyncInfo.CHANGE, SyncInfo.OUTGOING | SyncInfo.CHANGE});
		assertProperVisibleItems();
		adjustSet(set, project, 
				new String[]{"folder1/file2.txt"}, 
				new int[]{SyncInfo.IN_SYNC,});
		assertProperVisibleItems();
	}

	public void testParentRemovalWithChildRemaining() throws CoreException {
		IProject project = createProject(new String[]{"file.txt", "folder1/file2.txt", "folder1/folder2/file3.txt"});
		adjustSet(set, project, 
				new String[]{"folder1/folder2/", "folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.CONFLICTING | SyncInfo.CHANGE, SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		assertProperVisibleItems();
		
		adjustSet(set, project, 
				new String[]{"folder1/folder2/", "folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.IN_SYNC, SyncInfo.OUTGOING | SyncInfo.CHANGE});
		assertProperVisibleItems();
	}
	
	public void testEmptyFolderChange() throws CoreException {
		IProject project = createProject(new String[]{"file.txt", "folder1/file2.txt", "folder1/folder2/file3.txt", "folder3/"});
		adjustSet(set, project, 
				new String[]{"folder1/folder2/", "folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.CONFLICTING | SyncInfo.CHANGE, SyncInfo.CONFLICTING | SyncInfo.CHANGE});
		assertProperVisibleItems();
		
		adjustSet(set, project, 
				new String[]{"folder1/folder2/", "folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.IN_SYNC, SyncInfo.OUTGOING | SyncInfo.CHANGE});
		assertProperVisibleItems();
		
		adjustSet(set, project, 
				new String[]{"folder1/folder2/file3.txt"}, 
				new int[]{SyncInfo.IN_SYNC});
		assertProperVisibleItems();
		
		adjustSet(set, project, 
				new String[]{"folder3/"}, 
				new int[]{SyncInfo.INCOMING | SyncInfo.ADDITION});
		assertProperVisibleItems();
	}
}

Back to the top