Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 39fa625f9b8b8a38454f246d8bb03fe12c51c175 (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
/*******************************************************************************
 * Copyright (c) 2005, 2008 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.compare.tests;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.eclipse.compare.internal.Utilities;
import org.eclipse.compare.internal.patch.FileDiff;
import org.eclipse.compare.internal.patch.FileDiffResult;
import org.eclipse.compare.internal.patch.Hunk;
import org.eclipse.compare.internal.patch.Patcher;
import org.eclipse.compare.patch.ApplyPatchOperation;
import org.eclipse.compare.patch.IFilePatch;
import org.eclipse.compare.patch.IFilePatchResult;
import org.eclipse.compare.patch.PatchConfiguration;
import org.eclipse.compare.patch.WorkspacePatcherUI;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;

public class FileDiffResultTest extends WorkspaceTest {

	public FileDiffResultTest() {
		super();
	}

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

	private static final String PATCH_FILE = "patchfile";

	private static final String NEW_FILENAME = "newfile";

	private static final String NEW_FILE_CONTENT = "Hi There";

	private IProgressMonitor nullProgressMonitor = new NullProgressMonitor();

	private PatchConfiguration patchConfiguration = new PatchConfiguration();

	/**
	 * Tests applying a patch which creates a new file in a project. The file
	 * doesn't exist in the project.
	 * 
	 * @throws CoreException
	 */
	public void testPatchAddsNewFile() throws CoreException {
		IProject project = createProject("FileDiffResultTest",
				new String[] { "oldfile" });

		try {
			// create the patch file
			IFile file = project.getFile(PATCH_FILE);
			file.create(new ByteArrayInputStream(createPatchAddingFile(project,
					NEW_FILENAME, false /* the file doesn't exist */)
					.getBytes()), true, null);

			assertFalse(project.getFile(NEW_FILENAME).exists());

			IFilePatch[] filePatch = ApplyPatchOperation.parsePatch(file);
			assertNotNull(filePatch);
			assertEquals(1, filePatch.length);

			IFilePatchResult filePatchResult = filePatch[0].apply(null,
					patchConfiguration, nullProgressMonitor);
			assertTrue(filePatchResult.hasMatches());
			assertEquals(0, filePatchResult.getRejects().length);
			assertEquals("", getStringFromStream(filePatchResult
					.getOriginalContents()));
			assertEquals(NEW_FILE_CONTENT, getStringFromStream(filePatchResult
					.getPatchedContents()));

		} catch (IOException e) {
			fail();
		}
	}

	/**
	 * Tests applying a patch which creates a new file in a project. The file
	 * already exists in the project.
	 * 
	 * @throws CoreException
	 */
	public void testPatchAddsExistingFileWithSameContents()
			throws CoreException {
		IProject project = createProject("FileDiffResultTest",
				new String[] { NEW_FILENAME });

		try {
			// create the patch file
			IFile file = project.getFile(PATCH_FILE);
			file.create(new ByteArrayInputStream(createPatchAddingFile(project,
					NEW_FILENAME, true).getBytes()), true, null);

			assertTrue(project.getFile(NEW_FILENAME).exists());

			IFilePatch[] filePatch = ApplyPatchOperation.parsePatch(file);
			assertNotNull(filePatch);
			assertEquals(1, filePatch.length);

			IFilePatchResult filePatchResult = filePatch[0].apply(project
					.getFile(NEW_FILENAME), patchConfiguration,
					nullProgressMonitor);

			assertFalse(filePatchResult.hasMatches());
			assertEquals(1, filePatchResult.getRejects().length);

			assertNotNull(filePatchResult.getOriginalContents());
			assertNotNull(filePatchResult.getPatchedContents());

			assertEquals(new FileInputStream(project.getFile(NEW_FILENAME)
					.getLocation().toFile()), filePatchResult
					.getOriginalContents());
			assertEquals(filePatchResult.getOriginalContents(), filePatchResult
					.getPatchedContents());

		} catch (IOException e) {
			fail();
		}

	}

	/**
	 * Tests applying a patch which creates a new file in a project. The file
	 * already exists in the project, but has different contents.
	 * 
	 * @throws CoreException
	 */
	public void testPatchAddsExistingFileWithDifferentContents()
			throws CoreException {
		IProject project = createProject("FileDiffResultTest",
				new String[] { NEW_FILENAME });

		project.getFile(NEW_FILENAME).setContents(
				new ByteArrayInputStream("I'm a different content".getBytes()),
				IResource.NONE, null);

		try {
			// create the patch file
			IFile file = project.getFile(PATCH_FILE);
			file.create(new ByteArrayInputStream(createPatchAddingFile(project,
					NEW_FILENAME, false).getBytes()), true, null);

			assertTrue(project.getFile(NEW_FILENAME).exists());

			IFilePatch[] filePatch = ApplyPatchOperation.parsePatch(file);
			assertNotNull(filePatch);
			assertEquals(1, filePatch.length);

			IFilePatchResult filePatchResult = filePatch[0].apply(project
					.getFile(NEW_FILENAME), patchConfiguration,
					nullProgressMonitor);
			assertFalse(filePatchResult.hasMatches());
			assertEquals(1, filePatchResult.getRejects().length);

			assertNotNull(filePatchResult.getOriginalContents());
			assertNotNull(filePatchResult.getPatchedContents());

			assertEquals(new FileInputStream(project.getFile(NEW_FILENAME)
					.getLocation().toFile()), filePatchResult
					.getOriginalContents());
			assertEquals("I'm a different content",
					getStringFromStream(filePatchResult.getOriginalContents()));
			assertEquals(filePatchResult.getOriginalContents(), filePatchResult
					.getPatchedContents());

		} catch (IOException e) {
			fail();
		}

	}

	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=185379
	public void testFileDiffResultWithNullPath() {
		MyFileDiff myFileDiff = new MyFileDiff();
		FileDiffResult fileDiffResult = new FileDiffResult(myFileDiff,
				patchConfiguration);
		try {
			fileDiffResult.calculateFuzz(new ArrayList(), nullProgressMonitor);
		} catch (NullPointerException e) {
			fail();
		}
	}
	
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=187365
	public void testExcludePartOfNonWorkspacePatch() {
		Patcher patcher = new Patcher();
		MyFileDiff myFileDiff = new MyFileDiff();
		try {
			patcher.setEnabled(myFileDiff, false);
		} catch (NullPointerException e) {
			fail();
		}
	}

	// utility methods

	/**
	 * A mock FileDiff class.
	 */
	private class MyFileDiff extends FileDiff {
		protected MyFileDiff() {
			super(null, 0, null, 0);
			add(new Hunk(this, new int[] { 0, 0 }, new int[] { 0, 0 },
					new ArrayList(), false, false, false));
		}
	}

	/**
	 * @param project
	 *            The project for which the patch is prepared.
	 * @param filename
	 *            Filename of the file to be added by the patch.
	 * @param sameContents
	 *            Should the file added by the patch has the same content as the
	 *            existing one. Enter <code>false</code>, if the file doesn't
	 *            exist.
	 * @return Content of the patch.
	 * @throws IOException
	 * @throws CoreException
	 */
	private String createPatchAddingFile(IProject project, String filename,
			boolean sameContents) throws IOException, CoreException {
		StringBuffer sb = new StringBuffer();
		sb.append(WorkspacePatcherUI.getWorkspacePatchHeader() + "\n");
		sb.append(WorkspacePatcherUI.getWorkspacePatchProjectHeader(project)
				+ "\n");
		sb.append("Index: " + filename + "\n");
		sb
				.append("===================================================================\n");
		sb.append("RCS file: " + filename + "\n");
		sb.append("diff -N " + filename + "\n");
		sb.append("--- /dev/null	1 Jan 1970 00:00:00 -0000\n");
		sb.append("+++ " + filename + "	1 Jan 1970 00:00:00 -0000\n");
		sb.append("@@ -0,0 +1,1 @@\n");
		if (sameContents) {
			sb.append("+" + getStringFromIFile(project.getFile(filename)));
		} else {
			sb.append("+" + NEW_FILE_CONTENT);
		}

		return sb.toString();
	}

	/**
	 * Return string read from an input stream.
	 * 
	 * @param in
	 *            Input stream.
	 * @return String read from the stream.
	 * @throws IOException
	 */
	private static String getStringFromStream(InputStream in)
			throws IOException {
		return Utilities.readString(in, ResourcesPlugin.getEncoding());
	}

	/**
	 * Returns content of a file.
	 * 
	 * @param file
	 *            A file.
	 * @return Content of the file.
	 * @throws IOException
	 * @throws CoreException
	 */
	private static String getStringFromIFile(IFile file) throws IOException,
			CoreException {
		return getStringFromStream(new BufferedInputStream(file.getContents()));
	}

	/**
	 * Check if two input streams are equal. They can't be null, all bytes need
	 * to be the same, and they need to have same length.
	 * 
	 * @param inputStream1
	 *            First stream to check.
	 * @param inputStream2
	 *            Second stream to check.
	 * @throws IOException
	 */
	private static void assertEquals(InputStream inputStream1,
			InputStream inputStream2) throws IOException {

		assertNotNull(inputStream1);
		assertNotNull(inputStream2);

		int byte1, byte2;
		do {
			byte1 = inputStream1.read();
			byte2 = inputStream2.read();
			// compare every byte of the streams
			assertEquals(byte1, byte2);
		} while (byte1 != -1 || byte2 != -1);

		// the end of the streams should be reached at the same time
		assertEquals(-1, byte1);
		assertEquals(-1, byte2);

		inputStream1.close();
		inputStream2.close();
	}
}

Back to the top