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

import java.util.*;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.resources.RemoteFolder;
import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
import org.eclipse.team.internal.ccvs.ui.operations.RemoteCompareOperation;
import org.eclipse.team.tests.ccvs.core.CVSTestSetup;
import org.eclipse.ui.IWorkbenchPart;

public class CompareOperationTests extends CVSOperationTest {

	public class TestRemoteCompareOperation extends RemoteCompareOperation {
		private ICVSRemoteFolder leftTree, rightTree;

		public TestRemoteCompareOperation(IWorkbenchPart part, ICVSRemoteResource resource, CVSTag tag) {
			super(part, resource, tag);
		}
		
		/*
		 * Override to prevent compare editor from opening and to capture the results
		 */
		protected void openCompareEditor(CompareTreeBuilder builder) {
			this.leftTree = builder.getLeftTree();
			this.rightTree = builder.getRightTree();
		}

		public ICVSRemoteFolder getLeftTree() {
			return leftTree;
		}

		public ICVSRemoteFolder getRightTree() {
			return rightTree;
		}

	}
	
	public CompareOperationTests() {
		super();
	}

	public CompareOperationTests(String name) {
		super(name);
	}
	
	public static Test suite() {
		String testName = System.getProperty("eclipse.cvs.testName");
		if (testName == null) {
			TestSuite suite = new TestSuite(CompareOperationTests.class);
			return new CVSTestSetup(suite);
		} else {
			return new CVSTestSetup(new CompareOperationTests(testName));
		}
	}
	

	/**
	 * Assert that the revisions of any files in the remote tree match the revisions in the local tree
	 */

	private void assertRevisionsMatch(ICVSRemoteFolder folder, IProject project, String[] filePathsWithRevisions, String[] filePathsWithoutRevisions) throws CoreException {
		if (filePathsWithRevisions == null) filePathsWithRevisions = new String[0];
		if (filePathsWithoutRevisions == null) filePathsWithoutRevisions = new String[0];
		IResource[] filesWithRevisions = getResources(project, filePathsWithRevisions);
		IResource[] filesWithoutRevisions = getResources(project, filePathsWithoutRevisions);
		ICVSRemoteFile[] files= getAllFiles(folder);
		assertTrue("The number of remote files with differences does not match the expected number", files.length == (filePathsWithoutRevisions.length + filePathsWithRevisions.length));
		for (int i = 0; i < files.length; i++) {
			ICVSRemoteFile remoteFile = files[i];
			for (int j = 0; j < filesWithRevisions.length; j++) {
				IResource local = filesWithRevisions[j];
				ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor((IFile)local);
				if (cvsFile.getRepositoryRelativePath().equals(remoteFile.getRepositoryRelativePath())) {
					ResourceSyncInfo info = cvsFile.getSyncInfo();
					assertNotNull(info);
					String localRevision = info.getRevision();
					assertNotNull(localRevision);
					String remoteRevision = files[i].getRevision();
					assertNotNull(remoteRevision);
					assertEquals("Revisions do not match for " + local.getProjectRelativePath(), localRevision, remoteRevision);
				}
			}
			for (int j = 0; j < filesWithoutRevisions.length; j++) {
				IResource local = filesWithoutRevisions[j];
				ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor((IFile)local);
				if (cvsFile.getRepositoryRelativePath().equals(remoteFile.getRepositoryRelativePath())) {
					ResourceSyncInfo info = cvsFile.getSyncInfo();
					assertNotNull(info);
					String localRevision = info.getRevision();
					assertNotNull(localRevision);
					// Cannot assert anything about the remote revision
				}
			}
		}
	}
	
	private ICVSRemoteFile[] getAllFiles(ICVSRemoteFolder folder) {
		List result = new ArrayList();
		ICVSRemoteResource[] children = ((RemoteFolder)folder).getChildren();
		if (children != null) {
			for (int i = 0; i < children.length; i++) {
				ICVSRemoteResource resource = children[i];
				if (resource.isContainer()) {
					result.addAll(Arrays.asList(getAllFiles((ICVSRemoteFolder)resource)));
				} else {
					result.add(resource);
				}
			}
		}
		return (ICVSRemoteFile[]) result.toArray(new ICVSRemoteFile[result.size()]);
	}

	public void testCompareWithLatest() throws TeamException, CoreException {
		// Create a test project
		IProject project = createProject(new String[] { "file1.txt", "folder1/", "folder1/a.txt", "folder1/b.txt"});
		CVSTag v1 = new CVSTag("v1", CVSTag.VERSION);
		tagProject(project, v1, false);
		
		// Checkout and modify a copy (and commit the changes)
		IProject copy = checkoutCopy(project, "-copy");
		setContentsAndEnsureModified(copy.getFile("folder1/a.txt"));
		addResources(copy, new String[] { "folder1/newFile", "folder2/folder3/add.txt" }, false);
		deleteResources(copy, new String[] {"folder1/b.txt"}, false);
		commitResources(new IResource[] {copy}, IResource.DEPTH_INFINITE);

		// Run the compare operation of the project folder
		ICVSRemoteResource remoteResource = CVSWorkspaceRoot.getRemoteResourceFor(project);
		TestRemoteCompareOperation op = new TestRemoteCompareOperation(null, remoteResource, v1);
		run(op);
		assertRevisionsMatch(op.getRightTree(), project, new String[] {"folder1/a.txt", "folder1/b.txt"}, null);
		assertRevisionsMatch(op.getLeftTree(), copy, new String[] {"folder1/a.txt" }, new String[] {"folder1/newFile", "folder2/folder3/add.txt" } /* files with no revision */);
		
		
		// Run the compare operation of the project folder the other way
		remoteResource = CVSWorkspaceRoot.getRemoteResourceFor(project);
		remoteResource = ((ICVSRemoteFolder)remoteResource).forTag(v1);
		op = new TestRemoteCompareOperation(null, remoteResource, CVSTag.DEFAULT);
		run(op);
		assertRevisionsMatch(op.getLeftTree(), project, new String[] {"folder1/a.txt"}, new String[] {"folder1/b.txt"});
		assertRevisionsMatch(op.getRightTree(), copy, new String[] {"folder1/a.txt", "folder1/newFile", "folder2/folder3/add.txt" }, null /* files with no revision */);
		
		// Run the compare operation of a subfolder
		remoteResource = CVSWorkspaceRoot.getRemoteResourceFor(project.getFolder("folder1"));
		op = new TestRemoteCompareOperation(null, remoteResource, v1);
		run(op);
		assertRevisionsMatch(op.getRightTree(), project, new String[] {"folder1/a.txt", "folder1/b.txt"}, null);
		assertRevisionsMatch(op.getLeftTree(), copy, new String[] {"folder1/a.txt"}, new String[] {"folder1/newFile" } /* files with no revision */);
		
		// Run the operation on a single file
		remoteResource = CVSWorkspaceRoot.getRemoteResourceFor(copy.getFile("folder1/a.txt"));
		op = new TestRemoteCompareOperation(null, remoteResource, v1);
		run(op);
		assertRevisionsMatch(op.getRightTree(), project, new String[] {"folder1/a.txt"}, null);
		assertRevisionsMatch(op.getLeftTree(), copy, new String[] {"folder1/a.txt" }, null /* files with no revision */);
		
		// Run the operation on a single file using RemoteCompareOperation.getTag
		// to determine the tag
		remoteResource = CVSWorkspaceRoot.getRemoteResourceFor(copy.getFile("folder1/a.txt"));
		op = new TestRemoteCompareOperation(null, remoteResource, RemoteCompareOperation.getTag(CVSWorkspaceRoot.getRemoteResourceFor(project.getFile("folder1/a.txt"))));
		run(op);
		assertRevisionsMatch(op.getRightTree(), project, new String[] {"folder1/a.txt"}, null);
		assertRevisionsMatch(op.getLeftTree(), copy, new String[] {"folder1/a.txt" }, null /* files with no revision */);
		
	}

}

Back to the top