View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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  		// foo-a != foo-b
77  		assertFalse(cmp.equals(a, 0, b, 0));
78  		assertFalse(cmp.equals(b, 0, a, 0));
79  
80  		// foo-b == foo-b
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 }