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 package org.eclipse.jgit.patch;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertSame;
48 import static org.junit.Assert.assertTrue;
49 import static org.junit.Assert.fail;
50
51 import java.io.IOException;
52 import java.io.InputStream;
53
54 import org.eclipse.jgit.diff.DiffEntry;
55 import org.eclipse.jgit.junit.JGitTestUtil;
56 import org.eclipse.jgit.lib.FileMode;
57 import org.junit.Test;
58
59 public class PatchCcTest {
60 @Test
61 public void testParse_OneFileCc() throws IOException {
62 final Patch p = parseTestPatchFile();
63 assertEquals(1, p.getFiles().size());
64 assertTrue(p.getErrors().isEmpty());
65
66 final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
67
68 assertEquals("org.spearce.egit.ui/src/org/spearce/egit/ui/UIText.java",
69 cfh.getNewPath());
70 assertEquals(cfh.getNewPath(), cfh.getOldPath());
71
72 assertEquals(98, cfh.startOffset);
73
74 assertEquals(2, cfh.getParentCount());
75 assertSame(cfh.getOldId(0), cfh.getOldId());
76 assertEquals("169356b", cfh.getOldId(0).name());
77 assertEquals("dd8c317", cfh.getOldId(1).name());
78 assertEquals("fd85931", cfh.getNewId().name());
79
80 assertSame(cfh.getOldMode(0), cfh.getOldMode());
81 assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(0));
82 assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(1));
83 assertSame(FileMode.EXECUTABLE_FILE, cfh.getNewMode());
84 assertSame(FileHeader.ChangeType.MODIFY, cfh.getChangeType());
85 assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
86
87 assertEquals(1, cfh.getHunks().size());
88 {
89 final CombinedHunkHeader h = cfh.getHunks().get(0);
90
91 assertSame(cfh, h.getFileHeader());
92 assertEquals(346, h.startOffset);
93 assertEquals(764, h.endOffset);
94
95 assertSame(h.getOldImage(0), h.getOldImage());
96 assertSame(cfh.getOldId(0), h.getOldImage(0).getId());
97 assertSame(cfh.getOldId(1), h.getOldImage(1).getId());
98
99 assertEquals(55, h.getOldImage(0).getStartLine());
100 assertEquals(12, h.getOldImage(0).getLineCount());
101 assertEquals(3, h.getOldImage(0).getLinesAdded());
102 assertEquals(0, h.getOldImage(0).getLinesDeleted());
103
104 assertEquals(163, h.getOldImage(1).getStartLine());
105 assertEquals(13, h.getOldImage(1).getLineCount());
106 assertEquals(2, h.getOldImage(1).getLinesAdded());
107 assertEquals(0, h.getOldImage(1).getLinesDeleted());
108
109 assertEquals(163, h.getNewStartLine());
110 assertEquals(15, h.getNewLineCount());
111
112 assertEquals(10, h.getLinesContext());
113 }
114 }
115
116 @Test
117 public void testParse_CcNewFile() throws IOException {
118 final Patch p = parseTestPatchFile();
119 assertEquals(1, p.getFiles().size());
120 assertTrue(p.getErrors().isEmpty());
121
122 final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
123
124 assertSame(DiffEntry.DEV_NULL, cfh.getOldPath());
125 assertEquals("d", cfh.getNewPath());
126
127 assertEquals(187, cfh.startOffset);
128
129 assertEquals(2, cfh.getParentCount());
130 assertSame(cfh.getOldId(0), cfh.getOldId());
131 assertEquals("0000000", cfh.getOldId(0).name());
132 assertEquals("0000000", cfh.getOldId(1).name());
133 assertEquals("4bcfe98", cfh.getNewId().name());
134
135 assertSame(cfh.getOldMode(0), cfh.getOldMode());
136 assertSame(FileMode.MISSING, cfh.getOldMode(0));
137 assertSame(FileMode.MISSING, cfh.getOldMode(1));
138 assertSame(FileMode.REGULAR_FILE, cfh.getNewMode());
139 assertSame(FileHeader.ChangeType.ADD, cfh.getChangeType());
140 assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
141
142 assertEquals(1, cfh.getHunks().size());
143 {
144 final CombinedHunkHeader h = cfh.getHunks().get(0);
145
146 assertSame(cfh, h.getFileHeader());
147 assertEquals(273, h.startOffset);
148 assertEquals(300, h.endOffset);
149
150 assertSame(h.getOldImage(0), h.getOldImage());
151 assertSame(cfh.getOldId(0), h.getOldImage(0).getId());
152 assertSame(cfh.getOldId(1), h.getOldImage(1).getId());
153
154 assertEquals(1, h.getOldImage(0).getStartLine());
155 assertEquals(0, h.getOldImage(0).getLineCount());
156 assertEquals(1, h.getOldImage(0).getLinesAdded());
157 assertEquals(0, h.getOldImage(0).getLinesDeleted());
158
159 assertEquals(1, h.getOldImage(1).getStartLine());
160 assertEquals(0, h.getOldImage(1).getLineCount());
161 assertEquals(1, h.getOldImage(1).getLinesAdded());
162 assertEquals(0, h.getOldImage(1).getLinesDeleted());
163
164 assertEquals(1, h.getNewStartLine());
165 assertEquals(1, h.getNewLineCount());
166
167 assertEquals(0, h.getLinesContext());
168 }
169 }
170
171 @Test
172 public void testParse_CcDeleteFile() throws IOException {
173 final Patch p = parseTestPatchFile();
174 assertEquals(1, p.getFiles().size());
175 assertTrue(p.getErrors().isEmpty());
176
177 final CombinedFileHeader cfh = (CombinedFileHeader) p.getFiles().get(0);
178
179 assertEquals("a", cfh.getOldPath());
180 assertSame(DiffEntry.DEV_NULL, cfh.getNewPath());
181
182 assertEquals(187, cfh.startOffset);
183
184 assertEquals(2, cfh.getParentCount());
185 assertSame(cfh.getOldId(0), cfh.getOldId());
186 assertEquals("7898192", cfh.getOldId(0).name());
187 assertEquals("2e65efe", cfh.getOldId(1).name());
188 assertEquals("0000000", cfh.getNewId().name());
189
190 assertSame(cfh.getOldMode(0), cfh.getOldMode());
191 assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(0));
192 assertSame(FileMode.REGULAR_FILE, cfh.getOldMode(1));
193 assertSame(FileMode.MISSING, cfh.getNewMode());
194 assertSame(FileHeader.ChangeType.DELETE, cfh.getChangeType());
195 assertSame(FileHeader.PatchType.UNIFIED, cfh.getPatchType());
196
197 assertTrue(cfh.getHunks().isEmpty());
198 }
199
200 private Patch parseTestPatchFile() throws IOException {
201 final String patchFile = JGitTestUtil.getName() + ".patch";
202 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
203 if (in == null) {
204 fail("No " + patchFile + " test vector");
205 return null;
206 }
207 final Patch p = new Patch();
208 p.parse(in);
209 return p;
210 }
211 }
212 }