Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
package org.eclipse.compare.tests; |
12 |
|
13 |
import java.io.IOException; |
14 |
import java.io.InputStream; |
15 |
import java.util.List; |
16 |
|
17 |
import junit.framework.TestCase; |
18 |
|
19 |
import org.eclipse.compare.internal.core.patch.FileDiff; |
20 |
import org.eclipse.compare.internal.core.patch.Hunk; |
21 |
import org.eclipse.compare.internal.core.patch.LineReader; |
22 |
import org.eclipse.compare.internal.patch.FileDiffWrapper; |
23 |
import org.eclipse.compare.patch.ApplyPatchOperation; |
24 |
import org.eclipse.compare.patch.IFilePatch; |
25 |
import org.eclipse.compare.patch.IFilePatch2; |
26 |
import org.eclipse.compare.patch.IFilePatchResult; |
27 |
import org.eclipse.compare.patch.IHunk; |
28 |
import org.eclipse.compare.patch.PatchBuilder; |
29 |
import org.eclipse.compare.patch.PatchConfiguration; |
30 |
import org.eclipse.compare.tests.PatchUtils.StringStorage; |
31 |
import org.eclipse.core.resources.IStorage; |
32 |
import org.eclipse.core.runtime.CoreException; |
33 |
import org.eclipse.core.runtime.NullProgressMonitor; |
34 |
import org.eclipse.core.runtime.Path; |
35 |
|
36 |
public class PatchBuilderTest extends TestCase { |
37 |
|
38 |
public PatchBuilderTest() { |
39 |
|
40 |
} |
41 |
|
42 |
protected void setUp() throws Exception { |
43 |
// Nothing to do |
44 |
} |
45 |
|
46 |
protected void tearDown() throws Exception { |
47 |
super.tearDown(); |
48 |
} |
49 |
|
50 |
public void testModifyHunks() throws CoreException, IOException { |
51 |
IStorage patchStorage = new StringStorage("patch_modifyHunks.txt"); |
52 |
IStorage contextStorage = new StringStorage("context.txt"); |
53 |
IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); |
54 |
assertEquals(1, patches.length); |
55 |
IHunk[] hunksBefore = patches[0].getHunks(); |
56 |
assertEquals(5, hunksBefore.length); |
57 |
|
58 |
String[] lines = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", |
59 |
"+[d4]", " [e]" }; |
60 |
addLineDelimiters(lines); |
61 |
IHunk[] toAdd = new IHunk[] { PatchBuilder.createHunk(3, lines) }; |
62 |
IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); |
63 |
|
64 |
IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[2] }; |
65 |
filePatch = PatchBuilder.removeHunks(filePatch, toRemove); |
66 |
|
67 |
IHunk[] hunksAfter = filePatch.getHunks(); |
68 |
assertEquals(4, hunksAfter.length); |
69 |
assertEquals(3, ((Hunk) hunksAfter[0]).getStart(false)); |
70 |
assertEquals(3, ((Hunk) hunksAfter[0]).getStart(true)); |
71 |
assertEquals(7, ((Hunk) hunksAfter[1]).getStart(false)); |
72 |
assertEquals(11, ((Hunk) hunksAfter[1]).getStart(true)); |
73 |
assertEquals(18, ((Hunk) hunksAfter[2]).getStart(false)); |
74 |
assertEquals(22, ((Hunk) hunksAfter[2]).getStart(true)); |
75 |
assertEquals(28, ((Hunk) hunksAfter[3]).getStart(false)); |
76 |
assertEquals(33, ((Hunk) hunksAfter[3]).getStart(true)); |
77 |
|
78 |
FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); |
79 |
IFilePatchResult result = wrapper.apply(contextStorage, |
80 |
new PatchConfiguration(), new NullProgressMonitor()); |
81 |
|
82 |
IHunk[] rejects = result.getRejects(); |
83 |
assertEquals(1, rejects.length); |
84 |
|
85 |
InputStream actual = result.getPatchedContents(); |
86 |
|
87 |
LineReader lr = new LineReader(PatchUtils |
88 |
.getReader("exp_modifyHunks.txt")); |
89 |
List inLines = lr.readLines(); |
90 |
String expected = LineReader.createString(false, inLines); |
91 |
|
92 |
assertEquals(expected, PatchUtils.asString(actual)); |
93 |
} |
94 |
|
95 |
public void testAddHunks() throws CoreException, IOException { |
96 |
IStorage patchStorage = new StringStorage("patch_addHunks.txt"); |
97 |
IStorage contextStorage = new StringStorage("context_full.txt"); |
98 |
IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); |
99 |
assertEquals(1, patches.length); |
100 |
IHunk[] hunksBefore = patches[0].getHunks(); |
101 |
assertEquals(3, hunksBefore.length); |
102 |
|
103 |
String[] lines0 = new String[] { " [d]", "+[d1]", "+[d2]", "+[d3]", |
104 |
"+[d4]", " [e]" }; |
105 |
addLineDelimiters(lines0); |
106 |
IHunk hunk0 = PatchBuilder.createHunk(3, lines0); |
107 |
|
108 |
String[] lines1 = new String[] { " [K]", " [L]", "-[M]", " [N]", |
109 |
"+[N1]", "+[N2]", " [O]", " [P]" }; |
110 |
addLineDelimiters(lines1); |
111 |
IHunk hunk1 = PatchBuilder.createHunk(36, lines1); |
112 |
|
113 |
IHunk[] toAdd = new IHunk[] { hunk0, hunk1 }; |
114 |
IFilePatch2 filePatch = PatchBuilder.addHunks(patches[0], toAdd); |
115 |
|
116 |
IHunk[] hunksAfter = filePatch.getHunks(); |
117 |
assertEquals(5, hunksAfter.length); |
118 |
assertEquals(0, ((Hunk) hunksAfter[0]).getStart(false)); |
119 |
assertEquals(0, ((Hunk) hunksAfter[0]).getStart(true)); |
120 |
assertEquals(3, ((Hunk) hunksAfter[1]).getStart(false)); |
121 |
assertEquals(5, ((Hunk) hunksAfter[1]).getStart(true)); |
122 |
assertEquals(19, ((Hunk) hunksAfter[2]).getStart(false)); |
123 |
assertEquals(25, ((Hunk) hunksAfter[2]).getStart(true)); |
124 |
assertEquals(36, ((Hunk) hunksAfter[3]).getStart(false)); |
125 |
assertEquals(40, ((Hunk) hunksAfter[3]).getStart(true)); |
126 |
assertEquals(46, ((Hunk) hunksAfter[4]).getStart(false)); |
127 |
assertEquals(51, ((Hunk) hunksAfter[4]).getStart(true)); |
128 |
|
129 |
FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); |
130 |
IFilePatchResult result = wrapper.apply(contextStorage, |
131 |
new PatchConfiguration(), new NullProgressMonitor()); |
132 |
|
133 |
IHunk[] rejects = result.getRejects(); |
134 |
assertEquals(0, rejects.length); |
135 |
|
136 |
InputStream actual = result.getPatchedContents(); |
137 |
|
138 |
LineReader lr = new LineReader(PatchUtils.getReader("exp_addHunks.txt")); |
139 |
List inLines = lr.readLines(); |
140 |
String expected = LineReader.createString(false, inLines); |
141 |
|
142 |
assertEquals(expected, PatchUtils.asString(actual)); |
143 |
} |
144 |
|
145 |
public void testRemoveHunks() throws CoreException, IOException { |
146 |
IStorage patchStorage = new StringStorage("patch_removeHunks.txt"); |
147 |
IStorage contextStorage = new StringStorage("context_full.txt"); |
148 |
IFilePatch[] patches = ApplyPatchOperation.parsePatch(patchStorage); |
149 |
assertEquals(1, patches.length); |
150 |
IHunk[] hunksBefore = patches[0].getHunks(); |
151 |
assertEquals(5, hunksBefore.length); |
152 |
|
153 |
IHunk[] toRemove = new IHunk[] { hunksBefore[0], hunksBefore[1] }; |
154 |
IFilePatch2 filePatch = PatchBuilder.removeHunks(patches[0], toRemove); |
155 |
|
156 |
IHunk[] hunksAfter = filePatch.getHunks(); |
157 |
assertEquals(3, hunksAfter.length); |
158 |
assertEquals(19, ((Hunk) hunksAfter[0]).getStart(false)); |
159 |
assertEquals(19, ((Hunk) hunksAfter[0]).getStart(true)); |
160 |
assertEquals(29, ((Hunk) hunksAfter[1]).getStart(false)); |
161 |
assertEquals(27, ((Hunk) hunksAfter[1]).getStart(true)); |
162 |
assertEquals(46, ((Hunk) hunksAfter[2]).getStart(false)); |
163 |
assertEquals(43, ((Hunk) hunksAfter[2]).getStart(true)); |
164 |
|
165 |
FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); |
166 |
IFilePatchResult result = wrapper.apply(contextStorage, |
167 |
new PatchConfiguration(), new NullProgressMonitor()); |
168 |
|
169 |
IHunk[] rejects = result.getRejects(); |
170 |
assertEquals(0, rejects.length); |
171 |
|
172 |
InputStream actual = result.getPatchedContents(); |
173 |
|
174 |
LineReader lr = new LineReader(PatchUtils |
175 |
.getReader("exp_removeHunks.txt")); |
176 |
List inLines = lr.readLines(); |
177 |
String expected = LineReader.createString(false, inLines); |
178 |
|
179 |
assertEquals(expected, PatchUtils.asString(actual)); |
180 |
} |
181 |
|
182 |
public void testCreateFilePatch() throws CoreException, IOException { |
183 |
IStorage contextStorage = new StringStorage("context.txt"); |
184 |
|
185 |
String[] lines0 = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]" }; |
186 |
addLineDelimiters(lines0); |
187 |
Hunk hunk0 = (Hunk) PatchBuilder.createHunk(0, lines0); |
188 |
|
189 |
String[] lines1 = new String[] { " [b]", " [c]", "-[d]", "-[e]", |
190 |
" [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", |
191 |
"+[j2]", " [k]", " [l]" }; |
192 |
addLineDelimiters(lines1); |
193 |
Hunk hunk1 = (Hunk) PatchBuilder.createHunk(1, lines1); |
194 |
|
195 |
IHunk[] hunks = new IHunk[] { hunk1, hunk0 }; |
196 |
|
197 |
IFilePatch2 filePatch = PatchBuilder.createFilePatch(new Path(""), |
198 |
IFilePatch2.DATE_UNKNOWN, new Path(""), |
199 |
IFilePatch2.DATE_UNKNOWN, hunks); |
200 |
|
201 |
assertEquals(2, filePatch.getHunks().length); |
202 |
assertEquals(hunk0, filePatch.getHunks()[0]); |
203 |
assertEquals(hunk1, filePatch.getHunks()[1]); |
204 |
|
205 |
FileDiffWrapper wrapper = new FileDiffWrapper((FileDiff) filePatch); |
206 |
IFilePatchResult result = wrapper.apply(contextStorage, |
207 |
new PatchConfiguration(), new NullProgressMonitor()); |
208 |
|
209 |
InputStream actual = result.getPatchedContents(); |
210 |
|
211 |
LineReader lr = new LineReader(PatchUtils |
212 |
.getReader("exp_createFilePatch.txt")); |
213 |
List inLines = lr.readLines(); |
214 |
String expected = LineReader.createString(false, inLines); |
215 |
|
216 |
assertEquals(expected, PatchUtils.asString(actual)); |
217 |
} |
218 |
|
219 |
public void testCreateHunk0() throws CoreException { |
220 |
IStorage patch = new StringStorage("patch_createHunk0.txt"); |
221 |
IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); |
222 |
assertEquals(1, filePatches.length); |
223 |
assertEquals(1, filePatches[0].getHunks().length); |
224 |
|
225 |
String[] lines = new String[] { "+[a1]", "+[a2]", "+[a3]", " [a]", |
226 |
" [b]", "-[c]", " [d]", " [e]", " [f]" }; |
227 |
addLineDelimiters(lines); |
228 |
Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); |
229 |
|
230 |
assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); |
231 |
} |
232 |
|
233 |
public void testCreateHunk1() throws CoreException { |
234 |
IStorage patch = new StringStorage("patch_createHunk1.txt"); |
235 |
IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); |
236 |
assertEquals(1, filePatches.length); |
237 |
assertEquals(1, filePatches[0].getHunks().length); |
238 |
|
239 |
String[] lines = new String[] { " [a]", " [b]", "-[c]", " [d]", "-[e]", |
240 |
" [f]", " [g]", " [h]", "+[h1]", " [i]", " [j]", "+[j1]", |
241 |
"+[j2]", " [k]", " [l]", " [m]" }; |
242 |
addLineDelimiters(lines); |
243 |
Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); |
244 |
|
245 |
assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); |
246 |
} |
247 |
|
248 |
public void testCreateHunk2() throws CoreException { |
249 |
IStorage patch = new StringStorage("patch_createHunk2.txt"); |
250 |
IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); |
251 |
assertEquals(1, filePatches.length); |
252 |
assertEquals(1, filePatches[0].getHunks().length); |
253 |
|
254 |
String[] lines = new String[] { "+[aa]", "+[bb]", "+[cc]" }; |
255 |
addLineDelimiters(lines); |
256 |
Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); |
257 |
|
258 |
assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); |
259 |
} |
260 |
|
261 |
public void testCreateHunk3() throws CoreException { |
262 |
IStorage patch = new StringStorage("patch_createHunk3.txt"); |
263 |
IFilePatch[] filePatches = ApplyPatchOperation.parsePatch(patch); |
264 |
assertEquals(1, filePatches.length); |
265 |
assertEquals(1, filePatches[0].getHunks().length); |
266 |
|
267 |
String[] lines = new String[] { "-[aa]", "-[bb]", "-[cc]", "-[dd]" }; |
268 |
addLineDelimiters(lines); |
269 |
Hunk hunk = (Hunk) PatchBuilder.createHunk(0, lines); |
270 |
|
271 |
assertHunkEquals(hunk, (Hunk) filePatches[0].getHunks()[0]); |
272 |
} |
273 |
|
274 |
private void assertHunkEquals(Hunk h1, Hunk h2) { |
275 |
String[] l1 = h1.getLines(); |
276 |
String[] l2 = h2.getLines(); |
277 |
assertEquals(l1.length, l2.length); |
278 |
for (int i = 0; i < l1.length; i++) { |
279 |
assertFalse(l1[i] == null && l2[i] != null); |
280 |
assertEquals(l1[i], (l2[i])); |
281 |
} |
282 |
assertEquals(h1.getStart(false), h2.getStart(false)); |
283 |
assertEquals(h1.getStart(true), h2.getStart(true)); |
284 |
assertEquals(h1.getLength(false), h2.getLength(false)); |
285 |
assertEquals(h1.getLength(true), h2.getLength(true)); |
286 |
assertEquals(h1.getHunkType(false), h2.getHunkType(false)); |
287 |
assertEquals(h1.getHunkType(true), h2.getHunkType(true)); |
288 |
} |
289 |
|
290 |
private void addLineDelimiters(String[] lines) { |
291 |
for (int i = 0; i < lines.length; i++) { |
292 |
lines[i] = lines[i] + "\r\n"; |
293 |
} |
294 |
} |
295 |
|
296 |
} |