Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e4bf058b4c8a8a1b267cc48d56db2e105586b989 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*******************************************************************************
 * Copyright (c) 2000, 2009 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.core;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import junit.textui.TestRunner;
import junit.framework.TestCase;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;

/**
 * Base-class to the low level-testcases for the Session.
 * Supplies convinience-methods and default attributes for the testcases.
 * Especally data for a default-connection to the server is stored.
 */
public abstract class JUnitTestCase extends TestCase {
	protected static final int RANDOM_CONTENT_SIZE = 10000;
	protected static final boolean NEWLINE_TEST = false;
	protected static final String PLATFORM_NEWLINE = System.getProperty("line.separator");
	protected static final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
	
	public static final String[] EMPTY_ARGS = new String[0];

	/**
	 * Init the options and arguments to standard-values
	 */
	public JUnitTestCase(String name) {
		super(name);
	}

	/**
	 * Delete a project/resource form the standard cvs-server
	 */
	protected void magicDeleteRemote(String remoteName) throws CVSException {
		magicDeleteRemote(CVSTestSetup.repository, remoteName);
	}

	/**
	 * Delete a project/resource form the specified cvs-server
	 */
	protected static void magicDeleteRemote(ICVSRepositoryLocation location, String remoteName)
		throws CVSException {
		CVSTestSetup.executeRemoteCommand(location, "rm -rf " + 
			new Path(location.getRootDirectory()).append(remoteName).toString());
	}

	/**
	 * Sends the project to the standard cvs-server so that it contains the resources
	 * described in createResources.  The files have random content.
	 *
	 * @param projectName the name of the project to import
	 * @param createResources e.g. new String[]{"a.txt","f1/b.txt","f1/c.txt","f2/d.txt"}
	 */
	protected void magicSetUpRepo(String projectName, String[] createResources)
		throws  IOException, CoreException, CVSException {
		magicSetUpRepo(CVSTestSetup.repository, projectName, createResources);
	}
	
	/**
	 * Sends the project to the specified cvs-server so that it contains the resources
	 * described in createResources.  The files have random content.
	 * 
	 * @param location the CVS repository location
	 * @param projectName the name of the project to import
	 * @param createResources e.g. new String[]{"a.txt","f1/b.txt","f1/c.txt","f2/d.txt"}
	 */
	protected static void magicSetUpRepo(ICVSRepositoryLocation location, String projectName,
		String[] createResources) throws IOException, CoreException, CVSException {
		IProject projectRoot = workspaceRoot.getProject(projectName + "-setup-tmp");
		mkdirs(projectRoot);
		createRandomFile(projectRoot, createResources);
		magicDeleteRemote(location, projectName);
		
		String[] lOptions = new String[]{"-m","msg"};
		String[] args = new String[]{projectName,"a","b"};
	
		EclipseCVSClient.execute(location, CVSWorkspaceRoot.getCVSFolderFor(projectRoot),
			"import", EMPTY_ARGS, lOptions, args);
		projectRoot.delete(false /*force*/, null);
	}
	
	/**
	 *  Compare Arrays and find the first different element
	 */
	protected static void assertEqualsArrays(Object[] obArr1, Object[] obArr2) {
		
		assertEquals("Called assertEqualsArrays with null on one side", obArr1 == null,obArr2 == null);
		if (obArr1 == null) {
			return;
		}

		for (int i=0; i<Math.min(obArr1.length,obArr2.length); i++) {
			assertEquals("At Element " + i + " of the array",obArr1[i],obArr2[i]);
		}
		
		// If the Arrays are different in length, look for the first
		// not existing element and compare it to the existing in the
		// other array
		if (obArr1.length > obArr2.length) {
			assertEquals("Arrays of different length",obArr1[obArr2.length],null);
			return;
		}
	
		if (obArr1.length < obArr2.length) {
			assertEquals("Arrays of different length",obArr2[obArr1.length],null);
			return;
		}
			
	}
	
	/**
	 * Write text lines to file from an array of strings.
	 */
	protected static void writeToFile(IFile file, String[] contents)
		throws IOException, CoreException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		PrintStream os = new PrintStream(bos);
		try {
			for (int i = 0; i < contents.length; i++) {
				os.println(contents[i]);
			}
			ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
			if (file.exists()) {
				file.setContents(bis, false /*force*/, true /*keepHistory*/, null);
			} else {
				mkdirs(file.getParent());
				file.create(bis, false /*force*/, null);
			}
		} finally {
			os.close();
		}
	}
	
	/**
	 * Read text lines from file into an array of strings.
	 */
	protected static String[] readFromFile(IFile file)
		throws IOException, CoreException {
		if (! file.exists()) return null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
		List fileContentStore = new ArrayList();
		try {
			String line;
			while ((line = reader.readLine()) != null) {
				fileContentStore.add(line);
			}			
		} finally {
			reader.close();
		}
		return (String[]) fileContentStore.toArray(new String[fileContentStore.size()]);
	}

	/**
	 * Append text files to file from an array of strings, create new file if it
	 * does not exist yet.
	 */
	protected static void appendToFile(IFile file, String[] contents)
		throws IOException, CoreException {
		String[] oldContents = readFromFile(file);
		String[] newContents;
		if (oldContents == null) {
			newContents = contents;
		} else {
			newContents = new String[oldContents.length + contents.length];
			System.arraycopy(oldContents, 0, newContents, 0, oldContents.length);
			System.arraycopy(contents, 0, newContents, oldContents.length, contents.length);
		}
		writeToFile(file, newContents);
	}
	
	/**
	 * Pre-Append text files to file from an array of strings, create new file if it
	 * does not exist yet.
	 */
	protected static void prefixToFile(IFile file, String[] contents)
		throws IOException, CoreException {
		String[] oldContents = readFromFile(file);
		String[] newContents;
		if (oldContents == null) {
			newContents = contents;
		} else {
			newContents = new String[oldContents.length + contents.length];
			System.arraycopy(contents, 0, newContents, 0, contents.length);
			System.arraycopy(oldContents, 0, newContents, contents.length, oldContents.length);
		}
		writeToFile(file, newContents);
	}
	
	/**
	 * genertates Random content meand to be written in a File
	 */
	protected static String createRandomContent() {
		
		StringBuffer content = new StringBuffer();
		int contentSize;
		
		content.append("Random file generated for test" + PLATFORM_NEWLINE);
		
		contentSize = (int) Math.round(RANDOM_CONTENT_SIZE * 2 * Math.random());
		for (int i=0; i<contentSize; i++) {
			
			if (Math.random()>0.99) {
				content.append(PLATFORM_NEWLINE);
			}

			if (Math.random()>0.99) {
				content.append("\n");
			}
			
			if (NEWLINE_TEST) {
				if (Math.random()>0.99) {
					content.append("\n\r");
				}
				if (Math.random()>0.99) {
					content.append('\r');
				}
				if (Math.random()>0.99) {
					content.append("\r\n");
				}
				if (Math.random()>0.99) {
					content.append("\n");
				}
				if (Math.random()>0.99) {
					content.append("\n\n");
				}
				if (Math.random()>0.99) {
					content.append("\r\r");
				}
			}
			
			content.append((char)('\u0021' + Math.round(60 * Math.random())));
		}
		
		return content.toString();
	}
	
	/**
	 * Creates a folder (and its parents if needed).
	 */
	protected static void mkdirs(IContainer container) throws CoreException {
		if (container.getType() == IResource.PROJECT) {
			IProject project = (IProject) container;
			if (! project.exists()) {
				project.create(null);
			}
			project.open(null);
		} else if (container.getType() == IResource.FOLDER) {
			IFolder folder = (IFolder) container;
			if (! folder.exists()) {
				mkdirs(folder.getParent());
				folder.create(false /*force*/, true /*local*/, null);
			}
		}
	}

	/**
	 * Creates the file with random content, and all the folders on the
	 * way to there.
	 */
	private static void createRandomFile(IFile file)
		throws IOException, CoreException {
		mkdirs(file.getParent());
		writeToFile(file, new String[] { createRandomContent() });
	}
		
	/**
	 * Build the given fileStructure, all files are going to have
	 * sample content, all folders on the way are created.
	 */
	protected static void createRandomFile(IContainer parent, String[] fileNameArray) 
		throws IOException, CoreException {
		for (int i = 0; i < fileNameArray.length; i++) {
			IFile file = parent.getFile(new Path(fileNameArray[i]));
			createRandomFile(file);
		}
	}

	/**
	 * wait milliseconds to continou the execution
	 */
	public static void waitMsec(int msec) {	
		try {
			int wait = CVSTestSetup.WAIT_FACTOR * msec;
			long start = System.currentTimeMillis();
			Thread.sleep(wait);
			long end = System.currentTimeMillis();
			// Allow a 100 ms error in waiting
			assertTrue("Error in thread class. Did not wait long enough", (end - start) > (wait - 100));
		} catch(InterruptedException e) {
			fail("wait-problem");
		}
	}
	
	/**
	 * Call this method from the main-method of your test-case.
	 * It initialises some required parameter and runs the testcase.
	 */
	protected static void run(Class test) {
		// XXX is this property used anywhere?
		System.setProperty("eclipse.cvs.standalone", "true");
		TestRunner.run(test);
	}
}

Back to the top