1
2
3
4
5
6
7
8
9
10
11
12 package org.eclipse.jgit.diff;
13
14 import static java.nio.charset.StandardCharsets.UTF_8;
15 import static org.junit.Assert.assertArrayEquals;
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertFalse;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertTrue;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Arrays;
25
26 import org.eclipse.jgit.lib.Constants;
27 import org.eclipse.jgit.util.RawParseUtils;
28 import org.junit.Test;
29
30 public class RawTextTest {
31 @Test
32 public void testEmpty() {
33 final RawText r = new RawText(new byte[0]);
34 assertEquals(0, r.size());
35 }
36
37 @Test
38 public void testNul() {
39 String input = "foo-a\nf\0o-b\n";
40 byte[] data = Constants.encodeASCII(input);
41 final RawText a = new RawText(data);
42 assertArrayEquals(a.content, data);
43 assertEquals(2, a.size());
44 assertEquals("foo-a\n", a.getString(0, 1, false));
45 assertEquals("f\0o-b\n", a.getString(1, 2, false));
46 assertEquals("foo-a", a.getString(0, 1, true));
47 assertEquals("f\0o-b", a.getString(1, 2, true));
48 }
49
50 @Test
51 public void testCrLfTextYes() {
52 assertTrue(RawText
53 .isCrLfText(Constants.encodeASCII("line 1\r\nline 2\r\n")));
54 }
55
56 @Test
57 public void testCrLfTextNo() {
58 assertFalse(
59 RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\n")));
60 }
61
62 @Test
63 public void testCrLfTextBinary() {
64 assertFalse(RawText
65 .isCrLfText(Constants.encodeASCII("line 1\r\nline\0 2\r\n")));
66 }
67
68 @Test
69 public void testCrLfTextMixed() {
70 assertTrue(RawText
71 .isCrLfText(Constants.encodeASCII("line 1\nline 2\r\n")));
72 }
73
74 @Test
75 public void testCrLfTextCutShort() {
76 assertFalse(
77 RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\r")));
78 }
79
80 @Test
81 public void testEquals() {
82 final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
83 final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));
84 RawTextComparator cmp = RawTextComparator.DEFAULT;
85
86 assertEquals(2, a.size());
87 assertEquals(2, b.size());
88
89
90 assertFalse(cmp.equals(a, 0, b, 0));
91 assertFalse(cmp.equals(b, 0, a, 0));
92
93
94 assertTrue(cmp.equals(a, 1, b, 0));
95 assertTrue(cmp.equals(b, 0, a, 1));
96 }
97
98 @Test
99 public void testWriteLine1() throws IOException {
100 final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
101 final ByteArrayOutputStream o = new ByteArrayOutputStream();
102 a.writeLine(o, 0);
103 final byte[] r = o.toByteArray();
104 assertEquals("foo-a", RawParseUtils.decode(r));
105 }
106
107 @Test
108 public void testWriteLine2() throws IOException {
109 final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b"));
110 final ByteArrayOutputStream o = new ByteArrayOutputStream();
111 a.writeLine(o, 1);
112 final byte[] r = o.toByteArray();
113 assertEquals("foo-b", RawParseUtils.decode(r));
114 }
115
116 @Test
117 public void testWriteLine3() throws IOException {
118 final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n"));
119 final ByteArrayOutputStream o = new ByteArrayOutputStream();
120 a.writeLine(o, 1);
121 final byte[] r = o.toByteArray();
122 assertEquals("", RawParseUtils.decode(r));
123 }
124
125 @Test
126 public void testComparatorReduceCommonStartEnd() {
127 final RawTextComparator c = RawTextComparator.DEFAULT;
128 Edit e;
129
130 e = c.reduceCommonStartEnd(t(""), t(""), new Edit(0, 0, 0, 0));
131 assertEquals(new Edit(0, 0, 0, 0), e);
132
133 e = c.reduceCommonStartEnd(t("a"), t("b"), new Edit(0, 1, 0, 1));
134 assertEquals(new Edit(0, 1, 0, 1), e);
135
136 e = c.reduceCommonStartEnd(t("a"), t("a"), new Edit(0, 1, 0, 1));
137 assertEquals(new Edit(1, 1, 1, 1), e);
138
139 e = c.reduceCommonStartEnd(t("axB"), t("axC"), new Edit(0, 3, 0, 3));
140 assertEquals(new Edit(2, 3, 2, 3), e);
141
142 e = c.reduceCommonStartEnd(t("Bxy"), t("Cxy"), new Edit(0, 3, 0, 3));
143 assertEquals(new Edit(0, 1, 0, 1), e);
144
145 e = c.reduceCommonStartEnd(t("bc"), t("Abc"), new Edit(0, 2, 0, 3));
146 assertEquals(new Edit(0, 0, 0, 1), e);
147
148 e = new Edit(0, 5, 0, 5);
149 e = c.reduceCommonStartEnd(t("abQxy"), t("abRxy"), e);
150 assertEquals(new Edit(2, 3, 2, 3), e);
151
152 RawText a = new RawText("p\na b\nQ\nc d\n".getBytes(UTF_8));
153 RawText b = new RawText("p\na b \nR\n c d \n".getBytes(UTF_8));
154 e = new Edit(0, 4, 0, 4);
155 e = RawTextComparator.WS_IGNORE_ALL.reduceCommonStartEnd(a, b, e);
156 assertEquals(new Edit(2, 3, 2, 3), e);
157 }
158
159 @Test
160 public void testComparatorReduceCommonStartEnd_EmptyLine() {
161 RawText a;
162 RawText b;
163 Edit e;
164
165 a = new RawText("R\n y\n".getBytes(UTF_8));
166 b = new RawText("S\n\n y\n".getBytes(UTF_8));
167 e = new Edit(0, 2, 0, 3);
168 e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
169 assertEquals(new Edit(0, 1, 0, 2), e);
170
171 a = new RawText("S\n\n y\n".getBytes(UTF_8));
172 b = new RawText("R\n y\n".getBytes(UTF_8));
173 e = new Edit(0, 3, 0, 2);
174 e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
175 assertEquals(new Edit(0, 2, 0, 1), e);
176 }
177
178 @Test
179 public void testComparatorReduceCommonStartButLastLineNoEol() {
180 RawText a;
181 RawText b;
182 Edit e;
183 a = new RawText("start".getBytes(UTF_8));
184 b = new RawText("start of line".getBytes(UTF_8));
185 e = new Edit(0, 1, 0, 1);
186 e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
187 assertEquals(new Edit(0, 1, 0, 1), e);
188 }
189
190 @Test
191 public void testComparatorReduceCommonStartButLastLineNoEol_2() {
192 RawText a;
193 RawText b;
194 Edit e;
195 a = new RawText("start".getBytes(UTF_8));
196 b = new RawText("start of\nlastline".getBytes(UTF_8));
197 e = new Edit(0, 1, 0, 2);
198 e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
199 assertEquals(new Edit(0, 1, 0, 2), e);
200 }
201
202 @Test
203 public void testLineDelimiter() throws Exception {
204 RawText rt = new RawText(Constants.encodeASCII("foo\n"));
205 assertEquals("\n", rt.getLineDelimiter());
206 assertFalse(rt.isMissingNewlineAtEnd());
207 rt = new RawText(Constants.encodeASCII("foo\r\n"));
208 assertEquals("\r\n", rt.getLineDelimiter());
209 assertFalse(rt.isMissingNewlineAtEnd());
210
211 rt = new RawText(Constants.encodeASCII("foo\nbar"));
212 assertEquals("\n", rt.getLineDelimiter());
213 assertTrue(rt.isMissingNewlineAtEnd());
214 rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
215 assertEquals("\r\n", rt.getLineDelimiter());
216 assertTrue(rt.isMissingNewlineAtEnd());
217
218 rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
219 assertEquals("\n", rt.getLineDelimiter());
220 assertFalse(rt.isMissingNewlineAtEnd());
221 rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
222 assertEquals("\r\n", rt.getLineDelimiter());
223 assertFalse(rt.isMissingNewlineAtEnd());
224
225 rt = new RawText(Constants.encodeASCII("foo"));
226 assertNull(rt.getLineDelimiter());
227 assertTrue(rt.isMissingNewlineAtEnd());
228
229 rt = new RawText(Constants.encodeASCII(""));
230 assertNull(rt.getLineDelimiter());
231 assertTrue(rt.isMissingNewlineAtEnd());
232
233 rt = new RawText(Constants.encodeASCII("\n"));
234 assertEquals("\n", rt.getLineDelimiter());
235 assertFalse(rt.isMissingNewlineAtEnd());
236
237 rt = new RawText(Constants.encodeASCII("\r\n"));
238 assertEquals("\r\n", rt.getLineDelimiter());
239 assertFalse(rt.isMissingNewlineAtEnd());
240 }
241
242 @Test
243 public void testLineDelimiter2() throws Exception {
244 RawText rt = new RawText(Constants.encodeASCII("\nfoo"));
245 assertEquals("\n", rt.getLineDelimiter());
246 assertTrue(rt.isMissingNewlineAtEnd());
247 }
248
249 @Test
250 public void testCrAtLimit() throws Exception {
251 int limit = RawText.getBufferSize();
252 byte[] data = new byte[RawText.getBufferSize() + 2];
253 data[0] = 'A';
254 for (int i = 1; i < limit - 1; i++) {
255 if (i % 7 == 0) {
256 data[i] = '\n';
257 } else {
258 data[i] = (byte) ('A' + i % 7);
259 }
260 }
261 data[limit - 1] = '\r';
262 data[limit] = '\n';
263 data[limit + 1] = 'A';
264 assertTrue(RawText.isBinary(data, limit, true));
265 assertFalse(RawText.isBinary(data, limit, false));
266 assertFalse(RawText.isBinary(data, data.length, true));
267 byte[] buf = Arrays.copyOf(data, limit);
268 try (ByteArrayInputStream in = new ByteArrayInputStream(buf)) {
269 assertTrue(RawText.isBinary(in));
270 }
271 byte[] buf2 = Arrays.copyOf(data, limit + 1);
272 try (ByteArrayInputStream in = new ByteArrayInputStream(buf2)) {
273 assertFalse(RawText.isBinary(in));
274 }
275 byte[] buf3 = Arrays.copyOf(data, limit + 2);
276 try (ByteArrayInputStream in = new ByteArrayInputStream(buf3)) {
277 assertFalse(RawText.isBinary(in));
278 }
279 }
280
281 private static RawText t(String text) {
282 StringBuilder r = new StringBuilder();
283 for (int i = 0; i < text.length(); i++) {
284 r.append(text.charAt(i));
285 r.append('\n');
286 }
287 return new RawText(r.toString().getBytes(UTF_8));
288 }
289 }