View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
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.ByteArrayOutputStream;
22  import java.io.IOException;
23  
24  import org.eclipse.jgit.lib.Constants;
25  import org.eclipse.jgit.util.RawParseUtils;
26  import org.junit.Test;
27  
28  public class RawTextTest {
29  	@Test
30  	public void testEmpty() {
31  		final RawText r = new RawText(new byte[0]);
32  		assertEquals(0, r.size());
33  	}
34  
35  	@Test
36  	public void testNul() {
37  		String input = "foo-a\nf\0o-b\n";
38  		byte[] data = Constants.encodeASCII(input);
39  		final RawText a = new RawText(data);
40  		assertArrayEquals(a.content, data);
41  		assertEquals(2, a.size());
42  		assertEquals("foo-a\n", a.getString(0, 1, false));
43  		assertEquals("f\0o-b\n", a.getString(1, 2, false));
44  		assertEquals("foo-a", a.getString(0, 1, true));
45  		assertEquals("f\0o-b", a.getString(1, 2, true));
46  	}
47  
48  	@Test
49  	public void testCrLfTextYes() {
50  		assertTrue(RawText
51  				.isCrLfText(Constants.encodeASCII("line 1\r\nline 2\r\n")));
52  	}
53  
54  	@Test
55  	public void testCrLfTextNo() {
56  		assertFalse(
57  				RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\n")));
58  	}
59  
60  	@Test
61  	public void testCrLfTextBinary() {
62  		assertFalse(RawText
63  				.isCrLfText(Constants.encodeASCII("line 1\r\nline\0 2\r\n")));
64  	}
65  
66  	@Test
67  	public void testCrLfTextMixed() {
68  		assertTrue(RawText
69  				.isCrLfText(Constants.encodeASCII("line 1\nline 2\r\n")));
70  	}
71  
72  	@Test
73  	public void testCrLfTextCutShort() {
74  		assertFalse(
75  				RawText.isCrLfText(Constants.encodeASCII("line 1\nline 2\r")));
76  	}
77  
78  	@Test
79  	public void testEquals() {
80  		final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
81  		final RawText b = new RawText(Constants.encodeASCII("foo-b\nfoo-c\n"));
82  		RawTextComparator cmp = RawTextComparator.DEFAULT;
83  
84  		assertEquals(2, a.size());
85  		assertEquals(2, b.size());
86  
87  		// foo-a != foo-b
88  		assertFalse(cmp.equals(a, 0, b, 0));
89  		assertFalse(cmp.equals(b, 0, a, 0));
90  
91  		// foo-b == foo-b
92  		assertTrue(cmp.equals(a, 1, b, 0));
93  		assertTrue(cmp.equals(b, 0, a, 1));
94  	}
95  
96  	@Test
97  	public void testWriteLine1() throws IOException {
98  		final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b\n"));
99  		final ByteArrayOutputStream o = new ByteArrayOutputStream();
100 		a.writeLine(o, 0);
101 		final byte[] r = o.toByteArray();
102 		assertEquals("foo-a", RawParseUtils.decode(r));
103 	}
104 
105 	@Test
106 	public void testWriteLine2() throws IOException {
107 		final RawText a = new RawText(Constants.encodeASCII("foo-a\nfoo-b"));
108 		final ByteArrayOutputStream o = new ByteArrayOutputStream();
109 		a.writeLine(o, 1);
110 		final byte[] r = o.toByteArray();
111 		assertEquals("foo-b", RawParseUtils.decode(r));
112 	}
113 
114 	@Test
115 	public void testWriteLine3() throws IOException {
116 		final RawText a = new RawText(Constants.encodeASCII("a\n\nb\n"));
117 		final ByteArrayOutputStream o = new ByteArrayOutputStream();
118 		a.writeLine(o, 1);
119 		final byte[] r = o.toByteArray();
120 		assertEquals("", RawParseUtils.decode(r));
121 	}
122 
123 	@Test
124 	public void testComparatorReduceCommonStartEnd() {
125 		final RawTextComparator c = RawTextComparator.DEFAULT;
126 		Edit e;
127 
128 		e = c.reduceCommonStartEnd(t(""), t(""), new Edit(0, 0, 0, 0));
129 		assertEquals(new Edit(0, 0, 0, 0), e);
130 
131 		e = c.reduceCommonStartEnd(t("a"), t("b"), new Edit(0, 1, 0, 1));
132 		assertEquals(new Edit(0, 1, 0, 1), e);
133 
134 		e = c.reduceCommonStartEnd(t("a"), t("a"), new Edit(0, 1, 0, 1));
135 		assertEquals(new Edit(1, 1, 1, 1), e);
136 
137 		e = c.reduceCommonStartEnd(t("axB"), t("axC"), new Edit(0, 3, 0, 3));
138 		assertEquals(new Edit(2, 3, 2, 3), e);
139 
140 		e = c.reduceCommonStartEnd(t("Bxy"), t("Cxy"), new Edit(0, 3, 0, 3));
141 		assertEquals(new Edit(0, 1, 0, 1), e);
142 
143 		e = c.reduceCommonStartEnd(t("bc"), t("Abc"), new Edit(0, 2, 0, 3));
144 		assertEquals(new Edit(0, 0, 0, 1), e);
145 
146 		e = new Edit(0, 5, 0, 5);
147 		e = c.reduceCommonStartEnd(t("abQxy"), t("abRxy"), e);
148 		assertEquals(new Edit(2, 3, 2, 3), e);
149 
150 		RawText a = new RawText("p\na b\nQ\nc d\n".getBytes(UTF_8));
151 		RawText b = new RawText("p\na  b \nR\n c  d \n".getBytes(UTF_8));
152 		e = new Edit(0, 4, 0, 4);
153 		e = RawTextComparator.WS_IGNORE_ALL.reduceCommonStartEnd(a, b, e);
154 		assertEquals(new Edit(2, 3, 2, 3), e);
155 	}
156 
157 	@Test
158 	public void testComparatorReduceCommonStartEnd_EmptyLine() {
159 		RawText a;
160 		RawText b;
161 		Edit e;
162 
163 		a = new RawText("R\n y\n".getBytes(UTF_8));
164 		b = new RawText("S\n\n y\n".getBytes(UTF_8));
165 		e = new Edit(0, 2, 0, 3);
166 		e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
167 		assertEquals(new Edit(0, 1, 0, 2), e);
168 
169 		a = new RawText("S\n\n y\n".getBytes(UTF_8));
170 		b = new RawText("R\n y\n".getBytes(UTF_8));
171 		e = new Edit(0, 3, 0, 2);
172 		e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
173 		assertEquals(new Edit(0, 2, 0, 1), e);
174 	}
175 
176 	@Test
177 	public void testComparatorReduceCommonStartButLastLineNoEol() {
178 		RawText a;
179 		RawText b;
180 		Edit e;
181 		a = new RawText("start".getBytes(UTF_8));
182 		b = new RawText("start of line".getBytes(UTF_8));
183 		e = new Edit(0, 1, 0, 1);
184 		e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
185 		assertEquals(new Edit(0, 1, 0, 1), e);
186 	}
187 
188 	@Test
189 	public void testComparatorReduceCommonStartButLastLineNoEol_2() {
190 		RawText a;
191 		RawText b;
192 		Edit e;
193 		a = new RawText("start".getBytes(UTF_8));
194 		b = new RawText("start of\nlastline".getBytes(UTF_8));
195 		e = new Edit(0, 1, 0, 2);
196 		e = RawTextComparator.DEFAULT.reduceCommonStartEnd(a, b, e);
197 		assertEquals(new Edit(0, 1, 0, 2), e);
198 	}
199 
200 	@Test
201 	public void testLineDelimiter() throws Exception {
202 		RawText rt = new RawText(Constants.encodeASCII("foo\n"));
203 		assertEquals("\n", rt.getLineDelimiter());
204 		assertFalse(rt.isMissingNewlineAtEnd());
205 		rt = new RawText(Constants.encodeASCII("foo\r\n"));
206 		assertEquals("\r\n", rt.getLineDelimiter());
207 		assertFalse(rt.isMissingNewlineAtEnd());
208 
209 		rt = new RawText(Constants.encodeASCII("foo\nbar"));
210 		assertEquals("\n", rt.getLineDelimiter());
211 		assertTrue(rt.isMissingNewlineAtEnd());
212 		rt = new RawText(Constants.encodeASCII("foo\r\nbar"));
213 		assertEquals("\r\n", rt.getLineDelimiter());
214 		assertTrue(rt.isMissingNewlineAtEnd());
215 
216 		rt = new RawText(Constants.encodeASCII("foo\nbar\r\n"));
217 		assertEquals("\n", rt.getLineDelimiter());
218 		assertFalse(rt.isMissingNewlineAtEnd());
219 		rt = new RawText(Constants.encodeASCII("foo\r\nbar\n"));
220 		assertEquals("\r\n", rt.getLineDelimiter());
221 		assertFalse(rt.isMissingNewlineAtEnd());
222 
223 		rt = new RawText(Constants.encodeASCII("foo"));
224 		assertNull(rt.getLineDelimiter());
225 		assertTrue(rt.isMissingNewlineAtEnd());
226 
227 		rt = new RawText(Constants.encodeASCII(""));
228 		assertNull(rt.getLineDelimiter());
229 		assertTrue(rt.isMissingNewlineAtEnd());
230 
231 		rt = new RawText(Constants.encodeASCII("\n"));
232 		assertEquals("\n", rt.getLineDelimiter());
233 		assertFalse(rt.isMissingNewlineAtEnd());
234 
235 		rt = new RawText(Constants.encodeASCII("\r\n"));
236 		assertEquals("\r\n", rt.getLineDelimiter());
237 		assertFalse(rt.isMissingNewlineAtEnd());
238 	}
239 
240 	@Test
241 	public void testLineDelimiter2() throws Exception {
242 		RawText rt = new RawText(Constants.encodeASCII("\nfoo"));
243 		assertEquals("\n", rt.getLineDelimiter());
244 		assertTrue(rt.isMissingNewlineAtEnd());
245 	}
246 
247 	private static RawText t(String text) {
248 		StringBuilder r = new StringBuilder();
249 		for (int i = 0; i < text.length(); i++) {
250 			r.append(text.charAt(i));
251 			r.append('\n');
252 		}
253 		return new RawText(r.toString().getBytes(UTF_8));
254 	}
255 }