1
2
3
4
5
6
7
8
9
10
11 package org.eclipse.jgit.patch;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertFalse;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 import static org.junit.Assert.assertSame;
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assert.fail;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import org.eclipse.jgit.junit.JGitTestUtil;
25 import org.eclipse.jgit.lib.FileMode;
26 import org.eclipse.jgit.lib.ObjectId;
27 import org.junit.Test;
28
29 public class PatchTest {
30 @Test
31 public void testEmpty() {
32 final Patch p = new Patch();
33 assertTrue(p.getFiles().isEmpty());
34 assertTrue(p.getErrors().isEmpty());
35 }
36
37 @Test
38 public void testParse_ConfigCaseInsensitive() throws IOException {
39 final Patch p = parseTestPatchFile();
40 assertEquals(2, p.getFiles().size());
41 assertTrue(p.getErrors().isEmpty());
42
43 final FileHeader fRepositoryConfigTest = p.getFiles().get(0);
44 final FileHeader fRepositoryConfig = p.getFiles().get(1);
45
46 assertEquals(
47 "org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
48 fRepositoryConfigTest.getNewPath());
49
50 assertEquals(
51 "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
52 fRepositoryConfig.getNewPath());
53
54 assertEquals(572, fRepositoryConfigTest.startOffset);
55 assertEquals(1490, fRepositoryConfig.startOffset);
56
57 assertEquals("da7e704", fRepositoryConfigTest.getOldId().name());
58 assertEquals("34ce04a", fRepositoryConfigTest.getNewId().name());
59 assertSame(FileHeader.PatchType.UNIFIED, fRepositoryConfigTest
60 .getPatchType());
61 assertSame(FileMode.REGULAR_FILE, fRepositoryConfigTest.getOldMode());
62 assertSame(FileMode.REGULAR_FILE, fRepositoryConfigTest.getNewMode());
63 assertEquals(1, fRepositoryConfigTest.getHunks().size());
64 {
65 final HunkHeader h = fRepositoryConfigTest.getHunks().get(0);
66 assertSame(fRepositoryConfigTest, h.getFileHeader());
67 assertEquals(921, h.startOffset);
68 assertEquals(109, h.getOldImage().getStartLine());
69 assertEquals(4, h.getOldImage().getLineCount());
70 assertEquals(109, h.getNewStartLine());
71 assertEquals(11, h.getNewLineCount());
72
73 assertEquals(4, h.getLinesContext());
74 assertEquals(7, h.getOldImage().getLinesAdded());
75 assertEquals(0, h.getOldImage().getLinesDeleted());
76 assertSame(fRepositoryConfigTest.getOldId(), h.getOldImage()
77 .getId());
78
79 assertEquals(1490, h.endOffset);
80 }
81
82 assertEquals("45c2f8a", fRepositoryConfig.getOldId().name());
83 assertEquals("3291bba", fRepositoryConfig.getNewId().name());
84 assertSame(FileHeader.PatchType.UNIFIED, fRepositoryConfig
85 .getPatchType());
86 assertSame(FileMode.REGULAR_FILE, fRepositoryConfig.getOldMode());
87 assertSame(FileMode.REGULAR_FILE, fRepositoryConfig.getNewMode());
88 assertEquals(3, fRepositoryConfig.getHunks().size());
89 {
90 final HunkHeader h = fRepositoryConfig.getHunks().get(0);
91 assertSame(fRepositoryConfig, h.getFileHeader());
92 assertEquals(1803, h.startOffset);
93 assertEquals(236, h.getOldImage().getStartLine());
94 assertEquals(9, h.getOldImage().getLineCount());
95 assertEquals(236, h.getNewStartLine());
96 assertEquals(9, h.getNewLineCount());
97
98 assertEquals(7, h.getLinesContext());
99 assertEquals(2, h.getOldImage().getLinesAdded());
100 assertEquals(2, h.getOldImage().getLinesDeleted());
101 assertSame(fRepositoryConfig.getOldId(), h.getOldImage().getId());
102
103 assertEquals(2434, h.endOffset);
104 }
105 {
106 final HunkHeader h = fRepositoryConfig.getHunks().get(1);
107 assertEquals(2434, h.startOffset);
108 assertEquals(300, h.getOldImage().getStartLine());
109 assertEquals(7, h.getOldImage().getLineCount());
110 assertEquals(300, h.getNewStartLine());
111 assertEquals(7, h.getNewLineCount());
112
113 assertEquals(6, h.getLinesContext());
114 assertEquals(1, h.getOldImage().getLinesAdded());
115 assertEquals(1, h.getOldImage().getLinesDeleted());
116
117 assertEquals(2816, h.endOffset);
118 }
119 {
120 final HunkHeader h = fRepositoryConfig.getHunks().get(2);
121 assertEquals(2816, h.startOffset);
122 assertEquals(954, h.getOldImage().getStartLine());
123 assertEquals(7, h.getOldImage().getLineCount());
124 assertEquals(954, h.getNewStartLine());
125 assertEquals(7, h.getNewLineCount());
126
127 assertEquals(6, h.getLinesContext());
128 assertEquals(1, h.getOldImage().getLinesAdded());
129 assertEquals(1, h.getOldImage().getLinesDeleted());
130
131 assertEquals(3035, h.endOffset);
132 }
133 }
134
135 @Test
136 public void testParse_NoBinary() throws IOException {
137 final Patch p = parseTestPatchFile();
138 assertEquals(5, p.getFiles().size());
139 assertTrue(p.getErrors().isEmpty());
140
141 for (int i = 0; i < 4; i++) {
142 final FileHeader fh = p.getFiles().get(i);
143 assertSame(FileHeader.ChangeType.ADD, fh.getChangeType());
144 assertNotNull(fh.getOldId());
145 assertNotNull(fh.getNewId());
146 assertEquals("0000000", fh.getOldId().name());
147 assertSame(FileMode.MISSING, fh.getOldMode());
148 assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
149 assertTrue(fh.getNewPath().startsWith(
150 "org.spearce.egit.ui/icons/toolbar/"));
151 assertSame(FileHeader.PatchType.BINARY, fh.getPatchType());
152 assertTrue(fh.getHunks().isEmpty());
153 assertTrue(fh.hasMetaDataChanges());
154
155 assertNull(fh.getForwardBinaryHunk());
156 assertNull(fh.getReverseBinaryHunk());
157 }
158
159 final FileHeader fh = p.getFiles().get(4);
160 assertEquals("org.spearce.egit.ui/plugin.xml", fh.getNewPath());
161 assertSame(FileHeader.ChangeType.MODIFY, fh.getChangeType());
162 assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
163 assertFalse(fh.hasMetaDataChanges());
164 assertEquals("ee8a5a0", fh.getNewId().name());
165 assertNull(fh.getForwardBinaryHunk());
166 assertNull(fh.getReverseBinaryHunk());
167 assertEquals(1, fh.getHunks().size());
168 assertEquals(272, fh.getHunks().get(0).getOldImage().getStartLine());
169 }
170
171 @Test
172 public void testParse_GitBinaryLiteral() throws IOException {
173 final Patch p = parseTestPatchFile();
174 final int[] binsizes = { 359, 393, 372, 404 };
175 assertEquals(5, p.getFiles().size());
176 assertTrue(p.getErrors().isEmpty());
177
178 for (int i = 0; i < 4; i++) {
179 final FileHeader fh = p.getFiles().get(i);
180 assertSame(FileHeader.ChangeType.ADD, fh.getChangeType());
181 assertNotNull(fh.getOldId());
182 assertNotNull(fh.getNewId());
183 assertEquals(ObjectId.zeroId().name(), fh.getOldId().name());
184 assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
185 assertTrue(fh.getNewPath().startsWith(
186 "org.spearce.egit.ui/icons/toolbar/"));
187 assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
188 assertTrue(fh.getHunks().isEmpty());
189 assertTrue(fh.hasMetaDataChanges());
190
191 final BinaryHunk fwd = fh.getForwardBinaryHunk();
192 final BinaryHunk rev = fh.getReverseBinaryHunk();
193 assertNotNull(fwd);
194 assertNotNull(rev);
195 assertEquals(binsizes[i], fwd.getSize());
196 assertEquals(0, rev.getSize());
197
198 assertSame(fh, fwd.getFileHeader());
199 assertSame(fh, rev.getFileHeader());
200
201 assertSame(BinaryHunk.Type.LITERAL_DEFLATED, fwd.getType());
202 assertSame(BinaryHunk.Type.LITERAL_DEFLATED, rev.getType());
203 }
204
205 final FileHeader fh = p.getFiles().get(4);
206 assertEquals("org.spearce.egit.ui/plugin.xml", fh.getNewPath());
207 assertSame(FileHeader.ChangeType.MODIFY, fh.getChangeType());
208 assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
209 assertFalse(fh.hasMetaDataChanges());
210 assertEquals("ee8a5a0", fh.getNewId().name());
211 assertNull(fh.getForwardBinaryHunk());
212 assertNull(fh.getReverseBinaryHunk());
213 assertEquals(1, fh.getHunks().size());
214 assertEquals(272, fh.getHunks().get(0).getOldImage().getStartLine());
215 }
216
217 @Test
218 public void testParse_GitBinaryDelta() throws IOException {
219 final Patch p = parseTestPatchFile();
220 assertEquals(1, p.getFiles().size());
221 assertTrue(p.getErrors().isEmpty());
222
223 final FileHeader fh = p.getFiles().get(0);
224 assertTrue(fh.getNewPath().startsWith("zero.bin"));
225 assertSame(FileHeader.ChangeType.MODIFY, fh.getChangeType());
226 assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
227 assertSame(FileMode.REGULAR_FILE, fh.getNewMode());
228
229 assertNotNull(fh.getOldId());
230 assertNotNull(fh.getNewId());
231 assertEquals("08e7df176454f3ee5eeda13efa0adaa54828dfd8", fh.getOldId()
232 .name());
233 assertEquals("d70d8710b6d32ff844af0ee7c247e4b4b051867f", fh.getNewId()
234 .name());
235
236 assertTrue(fh.getHunks().isEmpty());
237 assertFalse(fh.hasMetaDataChanges());
238
239 final BinaryHunk fwd = fh.getForwardBinaryHunk();
240 final BinaryHunk rev = fh.getReverseBinaryHunk();
241 assertNotNull(fwd);
242 assertNotNull(rev);
243 assertEquals(12, fwd.getSize());
244 assertEquals(11, rev.getSize());
245
246 assertSame(fh, fwd.getFileHeader());
247 assertSame(fh, rev.getFileHeader());
248
249 assertSame(BinaryHunk.Type.DELTA_DEFLATED, fwd.getType());
250 assertSame(BinaryHunk.Type.DELTA_DEFLATED, rev.getType());
251
252 assertEquals(496, fh.endOffset);
253 }
254
255 @Test
256 public void testParse_FixNoNewline() throws IOException {
257 final Patch p = parseTestPatchFile();
258 assertEquals(1, p.getFiles().size());
259 assertTrue(p.getErrors().isEmpty());
260
261 final FileHeader f = p.getFiles().get(0);
262
263 assertEquals("a", f.getNewPath());
264 assertEquals(252, f.startOffset);
265
266 assertEquals("2e65efe", f.getOldId().name());
267 assertEquals("f2ad6c7", f.getNewId().name());
268 assertSame(FileHeader.PatchType.UNIFIED, f.getPatchType());
269 assertSame(FileMode.REGULAR_FILE, f.getOldMode());
270 assertSame(FileMode.REGULAR_FILE, f.getNewMode());
271 assertEquals(1, f.getHunks().size());
272 {
273 final HunkHeader h = f.getHunks().get(0);
274 assertSame(f, h.getFileHeader());
275 assertEquals(317, h.startOffset);
276 assertEquals(1, h.getOldImage().getStartLine());
277 assertEquals(1, h.getOldImage().getLineCount());
278 assertEquals(1, h.getNewStartLine());
279 assertEquals(1, h.getNewLineCount());
280
281 assertEquals(0, h.getLinesContext());
282 assertEquals(1, h.getOldImage().getLinesAdded());
283 assertEquals(1, h.getOldImage().getLinesDeleted());
284 assertSame(f.getOldId(), h.getOldImage().getId());
285
286 assertEquals(363, h.endOffset);
287 }
288 }
289
290 @Test
291 public void testParse_AddNoNewline() throws IOException {
292 final Patch p = parseTestPatchFile();
293 assertEquals(1, p.getFiles().size());
294 assertTrue(p.getErrors().isEmpty());
295
296 final FileHeader f = p.getFiles().get(0);
297
298 assertEquals("a", f.getNewPath());
299 assertEquals(256, f.startOffset);
300
301 assertEquals("f2ad6c7", f.getOldId().name());
302 assertEquals("c59d9b6", f.getNewId().name());
303 assertSame(FileHeader.PatchType.UNIFIED, f.getPatchType());
304 assertSame(FileMode.REGULAR_FILE, f.getOldMode());
305 assertSame(FileMode.REGULAR_FILE, f.getNewMode());
306 assertEquals(1, f.getHunks().size());
307 {
308 final HunkHeader h = f.getHunks().get(0);
309 assertSame(f, h.getFileHeader());
310 assertEquals(321, h.startOffset);
311 assertEquals(1, h.getOldImage().getStartLine());
312 assertEquals(1, h.getOldImage().getLineCount());
313 assertEquals(1, h.getNewStartLine());
314 assertEquals(1, h.getNewLineCount());
315
316 assertEquals(0, h.getLinesContext());
317 assertEquals(1, h.getOldImage().getLinesAdded());
318 assertEquals(1, h.getOldImage().getLinesDeleted());
319 assertSame(f.getOldId(), h.getOldImage().getId());
320
321 assertEquals(367, h.endOffset);
322 }
323 }
324
325 private Patch parseTestPatchFile() throws IOException {
326 final String patchFile = JGitTestUtil.getName() + ".patch";
327 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
328 if (in == null) {
329 fail("No " + patchFile + " test vector");
330 return null;
331 }
332 final Patch p = new Patch();
333 p.parse(in);
334 return p;
335 }
336 }
337 }