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