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