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
45 package org.eclipse.jgit.diff;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.junit.Assert.fail;
49
50 import java.io.ByteArrayOutputStream;
51 import java.io.IOException;
52 import java.io.InputStream;
53
54 import org.eclipse.jgit.junit.JGitTestUtil;
55 import org.eclipse.jgit.patch.FileHeader;
56 import org.eclipse.jgit.patch.Patch;
57 import org.eclipse.jgit.util.RawParseUtils;
58 import org.junit.Before;
59 import org.junit.Test;
60
61 public class DiffFormatterReflowTest {
62 private RawText a;
63
64 private RawText b;
65
66 private FileHeader file;
67
68 private ByteArrayOutputStream out;
69
70 private DiffFormatter fmt;
71
72 @Before
73 public void setUp() throws Exception {
74 out = new ByteArrayOutputStream();
75 fmt = new DiffFormatter(out);
76 }
77
78 @Test
79 public void testNegativeContextFails() throws IOException {
80 init("X");
81 try {
82 fmt.setContext(-1);
83 fail("accepted negative context");
84 } catch (IllegalArgumentException e) {
85
86 }
87 }
88
89 @Test
90 public void testContext0() throws IOException {
91 init("X");
92 fmt.setContext(0);
93 assertFormatted();
94 }
95
96 @Test
97 public void testContext1() throws IOException {
98 init("X");
99 fmt.setContext(1);
100 assertFormatted();
101 }
102
103 @Test
104 public void testContext3() throws IOException {
105 init("X");
106 fmt.setContext(3);
107 assertFormatted();
108 }
109
110 @Test
111 public void testContext5() throws IOException {
112 init("X");
113 fmt.setContext(5);
114 assertFormatted();
115 }
116
117 @Test
118 public void testContext10() throws IOException {
119 init("X");
120 fmt.setContext(10);
121 assertFormatted();
122 }
123
124 @Test
125 public void testContext100() throws IOException {
126 init("X");
127 fmt.setContext(100);
128 assertFormatted();
129 }
130
131 @Test
132 public void testEmpty1() throws IOException {
133 init("E");
134 assertFormatted("E.patch");
135 }
136
137 @Test
138 public void testNoNewLine1() throws IOException {
139 init("Y");
140 assertFormatted("Y.patch");
141 }
142
143 @Test
144 public void testNoNewLine2() throws IOException {
145 init("Z");
146 assertFormatted("Z.patch");
147 }
148
149 private void init(String name) throws IOException {
150 a = new RawText(readFile(name + "_PreImage"));
151 b = new RawText(readFile(name + "_PostImage"));
152 file = parseTestPatchFile(name + ".patch").getFiles().get(0);
153 }
154
155 private void assertFormatted() throws IOException {
156 assertFormatted(JGitTestUtil.getName() + ".out");
157 }
158
159 private void assertFormatted(String name) throws IOException {
160 fmt.format(file, a, b);
161 final String exp = RawParseUtils.decode(readFile(name));
162 assertEquals(exp, RawParseUtils.decode(out.toByteArray()));
163 }
164
165 private byte[] readFile(String patchFile) throws IOException {
166 final InputStream in = getClass().getResourceAsStream(patchFile);
167 if (in == null) {
168 fail("No " + patchFile + " test vector");
169 return null;
170 }
171 try {
172 final byte[] buf = new byte[1024];
173 final ByteArrayOutputStream temp = new ByteArrayOutputStream();
174 int n;
175 while ((n = in.read(buf)) > 0)
176 temp.write(buf, 0, n);
177 return temp.toByteArray();
178 } finally {
179 in.close();
180 }
181 }
182
183 private Patch parseTestPatchFile(String patchFile) throws IOException {
184 try (InputStream in = getClass().getResourceAsStream(patchFile)) {
185 if (in == null) {
186 fail("No " + patchFile + " test vector");
187 return null;
188 }
189 final Patch p = new Patch();
190 p.parse(in);
191 return p;
192 }
193 }
194 }