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

import java.io.BufferedReader;
import java.io.IOException;
import java.lang.reflect.Field;

import junit.framework.TestCase;

import org.eclipse.compare.internal.core.patch.FilePatch2;
import org.eclipse.compare.internal.patch.WorkspacePatcher;
import org.eclipse.compare.patch.IHunk;

public class PatchLinesTest extends TestCase {

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

	// unified diff format

	public void test_196847() throws Exception {
		int[] lines = parsePatch("196847/stuff_patch.txt");
		assertEquals(6, lines[0]);
		assertEquals(5, lines[1]);
	}

	public void test_deletion_autofuzz() throws Exception {
		int[] lines = parsePatch("deletion_autofuzz/patch.txt");
		assertEquals(0, lines[0]);
		assertEquals(1, lines[1]);
	}

	public void test_patch_addition() throws Exception {
		int[] lines = parsePatch("patch_addition.txt");
		assertEquals(9, lines[0]);
		assertEquals(0, lines[1]);
	}

	public void test_patch_context1() throws Exception {
		int[] lines = parsePatch("patch_context1.txt");
		assertEquals(4, lines[0]);
		assertEquals(2, lines[1]);
	}

	public void test_patch_context3() throws Exception {
		int[] lines = parsePatch("patch_context3.txt");
		assertEquals(4, lines[0]);
		assertEquals(2, lines[1]);
	}

	public void test_patch_workspacePatchAddition() throws Exception {
		int[] lines = parsePatch("patch_workspacePatchAddition.txt");
		assertEquals(31, lines[0]);
		assertEquals(0, lines[1]);
	}

	public void test_patch_workspacePatchDelete() throws Exception {
		int[] lines = parsePatch("patch_workspacePatchDelete.txt");
		assertEquals(29, lines[0]);
		assertEquals(46, lines[1]);
	}

	public void test_patch_workspacePatchMod() throws Exception {
		int[] lines = parsePatch("patch_workspacePatchMod.txt");
		assertEquals(33, lines[0]);
		assertEquals(8, lines[1]);
	}

	// context diff format

	public void test_patch_addition_context() throws Exception {
		int[] lines = parsePatch("patch_addition_context.txt");
		assertEquals(9, lines[0]);
		assertEquals(0, lines[1]);
	}

	public void test_patch_oneline_context() throws Exception {
		int[] lines = parsePatch("patch_oneline_context.txt");
		assertEquals(1, lines[0]);
		assertEquals(0, lines[1]);
	}

	public void test_patch_context3_context() throws Exception {
		int[] lines = parsePatch("patch_context3_context.txt");
		assertEquals(4, lines[0]);
		assertEquals(2, lines[1]);
	}

	private int[] parsePatch(String patch) {
		WorkspacePatcher patcher = new WorkspacePatcher();
		try {
			patcher.parse(getReader(patch));
			patcher.countLines();
		} catch (IOException e) {
			e.printStackTrace();
		}

		int[] result = new int[] { 0, 0 };
		int hunksLengthSum = 0;
		int fileDiffSum = 0;

		FilePatch2[] diffs = patcher.getDiffs();
		for (int i = 0; i < diffs.length; i++) {
			FilePatch2 fileDiff = diffs[i];
			IHunk[] hunks = fileDiff.getHunks();
			for (int j = 0; j < hunks.length; j++) {
				IHunk hunk = hunks[j];
				hunksLengthSum += getNewLength(hunk);
				hunksLengthSum -= getOldLength(hunk);
			}
			result[0] += fileDiff.getAddedLines();
			result[1] += fileDiff.getRemovedLines();
			fileDiffSum += fileDiff.getAddedLines();
			fileDiffSum -= fileDiff.getRemovedLines();
		}

		assertEquals(hunksLengthSum, fileDiffSum);
		return result;
	}

	private BufferedReader getReader(String name) {
		return PatchUtils.getReader(name);
	}

	private int getNewLength(IHunk hunk) {
		Class<?> cls = hunk.getClass();
		try {
			Field fld = cls.getDeclaredField("fNewLength");
			fld.setAccessible(true);
			return fld.getInt(hunk);
		} catch (SecurityException e) {
			fail(e.getMessage());
		} catch (NoSuchFieldException e) {
			fail(e.getMessage());
		} catch (IllegalArgumentException e) {
			fail(e.getMessage());
		} catch (IllegalAccessException e) {
			fail(e.getMessage());
		}
		fail();
		return -1;
	}

	private int getOldLength(IHunk hunk) {
		Class<?> cls = hunk.getClass();
		try {
			Field fld = cls.getDeclaredField("fOldLength");
			fld.setAccessible(true);
			return fld.getInt(hunk);
		} catch (SecurityException e) {
			fail(e.getMessage());
		} catch (NoSuchFieldException e) {
			fail(e.getMessage());
		} catch (IllegalArgumentException e) {
			fail(e.getMessage());
		} catch (IllegalAccessException e) {
			fail(e.getMessage());
		}
		fail();
		return -1;
	}
}

Back to the top