View Javadoc
1   /*
2    * Copyright (C) 2008-2009, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.util;
12  
13  import static java.nio.charset.StandardCharsets.ISO_8859_1;
14  import static org.junit.Assert.assertArrayEquals;
15  import static org.junit.Assert.assertNotNull;
16  import static org.junit.Assert.assertThrows;
17  
18  import org.eclipse.jgit.errors.BinaryBlobException;
19  import org.junit.Test;
20  
21  public class RawParseUtils_LineMapTest {
22  
23  	@Test
24  	public void testEmpty() throws Exception {
25  		final IntList map = RawParseUtils.lineMap(new byte[] {}, 0, 0);
26  		assertNotNull(map);
27  		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0}, asInts(map));
28  	}
29  
30  	@Test
31  	public void testOneBlankLine() throws Exception  {
32  		final IntList map = RawParseUtils.lineMap(new byte[] { '\n' }, 0, 1);
33  		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 1}, asInts(map));
34  	}
35  
36  	@Test
37  	public void testTwoLineFooBar() {
38  		final byte[] buf = "foo\nbar\n".getBytes(ISO_8859_1);
39  		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
40  		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
41  	}
42  
43  	@Test
44  	public void testTwoLineNoLF() {
45  		final byte[] buf = "foo\nbar".getBytes(ISO_8859_1);
46  		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
47  		assertArrayEquals(new int[]{Integer.MIN_VALUE, 0, 4, buf.length}, asInts(map));
48  	}
49  
50  	@Test
51  	public void testNulByte() {
52  		final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
53  		final IntList map = RawParseUtils.lineMap(buf, 3, buf.length);
54  		assertArrayEquals(new int[] { Integer.MIN_VALUE, 3, 7, buf.length },
55  				asInts(map));
56  	}
57  
58  	@Test
59  	public void testLineMapOrBinary() throws Exception {
60  		final byte[] buf = "xxxfoo\nb\0ar".getBytes(ISO_8859_1);
61  		assertThrows(BinaryBlobException.class,
62  				() -> RawParseUtils.lineMapOrBinary(buf, 3, buf.length));
63  	}
64  
65  	@Test
66  	public void testFourLineBlanks() {
67  		final byte[] buf = "foo\n\n\nbar\n".getBytes(ISO_8859_1);
68  		final IntList map = RawParseUtils.lineMap(buf, 0, buf.length);
69  
70  		assertArrayEquals(new int[]{
71  				Integer.MIN_VALUE, 0, 4, 5, 6, buf.length
72  		}, asInts(map));
73  	}
74  
75  	private int[] asInts(IntList l) {
76  		int[] result = new int[l.size()];
77  		for (int i = 0; i < l.size(); i++) {
78  			result[i] = l.get(i);
79  		}
80  		return result;
81  	}
82  }