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.assertNull;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.assertTrue;
50 import static org.junit.Assert.fail;
51
52 import java.io.IOException;
53 import java.io.InputStream;
54
55 import org.eclipse.jgit.junit.JGitTestUtil;
56 import org.junit.Test;
57
58 public class PatchErrorTest {
59 @Test
60 public void testError_DisconnectedHunk() throws IOException {
61 final Patch p = parseTestPatchFile();
62 assertEquals(1, p.getFiles().size());
63 {
64 final FileHeader fh = p.getFiles().get(0);
65 assertEquals(
66 "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
67 fh.getNewPath());
68 assertEquals(1, fh.getHunks().size());
69 }
70
71 assertEquals(1, p.getErrors().size());
72 final FormatError e = p.getErrors().get(0);
73 assertSame(FormatError.Severity.ERROR, e.getSeverity());
74 assertEquals("Hunk disconnected from file", e.getMessage());
75 assertEquals(18, e.getOffset());
76 assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
77 }
78
79 @Test
80 public void testError_TruncatedOld() throws IOException {
81 final Patch p = parseTestPatchFile();
82 assertEquals(1, p.getFiles().size());
83 assertEquals(1, p.getErrors().size());
84
85 final FormatError e = p.getErrors().get(0);
86 assertSame(FormatError.Severity.ERROR, e.getSeverity());
87 assertEquals("Truncated hunk, at least 1 old lines is missing", e
88 .getMessage());
89 assertEquals(313, e.getOffset());
90 assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
91 }
92
93 @Test
94 public void testError_TruncatedNew() throws IOException {
95 final Patch p = parseTestPatchFile();
96 assertEquals(1, p.getFiles().size());
97 assertEquals(1, p.getErrors().size());
98
99 final FormatError e = p.getErrors().get(0);
100 assertSame(FormatError.Severity.ERROR, e.getSeverity());
101 assertEquals("Truncated hunk, at least 1 new lines is missing", e
102 .getMessage());
103 assertEquals(313, e.getOffset());
104 assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
105 }
106
107 @Test
108 public void testError_BodyTooLong() throws IOException {
109 final Patch p = parseTestPatchFile();
110 assertEquals(1, p.getFiles().size());
111 assertEquals(1, p.getErrors().size());
112
113 final FormatError e = p.getErrors().get(0);
114 assertSame(FormatError.Severity.WARNING, e.getSeverity());
115 assertEquals("Hunk header 4:11 does not match body line count of 4:12",
116 e.getMessage());
117 assertEquals(349, e.getOffset());
118 assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
119 }
120
121 @Test
122 public void testError_GarbageBetweenFiles() throws IOException {
123 final Patch p = parseTestPatchFile();
124 assertEquals(2, p.getFiles().size());
125 {
126 final FileHeader fh = p.getFiles().get(0);
127 assertEquals(
128 "org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
129 fh.getNewPath());
130 assertEquals(1, fh.getHunks().size());
131 }
132 {
133 final FileHeader fh = p.getFiles().get(1);
134 assertEquals(
135 "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
136 fh.getNewPath());
137 assertEquals(1, fh.getHunks().size());
138 }
139
140 assertEquals(1, p.getErrors().size());
141 final FormatError e = p.getErrors().get(0);
142 assertSame(FormatError.Severity.WARNING, e.getSeverity());
143 assertEquals("Unexpected hunk trailer", e.getMessage());
144 assertEquals(926, e.getOffset());
145 assertEquals("I AM NOT HERE\n", e.getLineText());
146 }
147
148 @Test
149 public void testError_GitBinaryNoForwardHunk() throws IOException {
150 final Patch p = parseTestPatchFile();
151 assertEquals(2, p.getFiles().size());
152 {
153 final FileHeader fh = p.getFiles().get(0);
154 assertEquals("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
155 .getNewPath());
156 assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
157 assertTrue(fh.getHunks().isEmpty());
158 assertNull(fh.getForwardBinaryHunk());
159 }
160 {
161 final FileHeader fh = p.getFiles().get(1);
162 assertEquals("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
163 .getNewPath());
164 assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
165 assertTrue(fh.getHunks().isEmpty());
166 assertNull(fh.getForwardBinaryHunk());
167 }
168
169 assertEquals(1, p.getErrors().size());
170 final FormatError e = p.getErrors().get(0);
171 assertSame(FormatError.Severity.ERROR, e.getSeverity());
172 assertEquals("Missing forward-image in GIT binary patch", e
173 .getMessage());
174 assertEquals(297, e.getOffset());
175 assertEquals("\n", e.getLineText());
176 }
177
178 private Patch parseTestPatchFile() throws IOException {
179 final String patchFile = JGitTestUtil.getName() + ".patch";
180 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
181 if (in == null) {
182 fail("No " + patchFile + " test vector");
183 return null;
184 }
185 final Patch p = new Patch();
186 p.parse(in);
187 return p;
188 }
189 }
190 }