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