aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/util/IOTest.java
blob: 10a858fbfb5af95b0756499904b8d1039ec78da0 (plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.util;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

import org.junit.Test;

public class IOTest {

	private static final byte[] DATA = "abcdefghijklmnopqrstuvwxyz"
			.getBytes(StandardCharsets.US_ASCII);

	private byte[] initBuffer(int size) {
		byte[] buffer = new byte[size];
		for (int i = 0; i < size; i++) {
			buffer[i] = (byte) ('0' + (i % 10));
		}
		return buffer;
	}

	private int read(byte[] buffer, int from) throws IOException {
		try (InputStream in = new ByteArrayInputStream(DATA)) {
			return IO.readFully(in, buffer, from);
		}
	}

	@Test
	public void readFullyBufferShorter() throws Exception {
		byte[] buffer = initBuffer(9);
		int length = read(buffer, 0);
		assertEquals(buffer.length, length);
		assertArrayEquals(buffer, Arrays.copyOfRange(DATA, 0, length));
	}

	@Test
	public void readFullyBufferLonger() throws Exception {
		byte[] buffer = initBuffer(50);
		byte[] initial = Arrays.copyOf(buffer, buffer.length);
		int length = read(buffer, 0);
		assertEquals(DATA.length, length);
		assertArrayEquals(Arrays.copyOfRange(buffer, 0, length), DATA);
		assertArrayEquals(Arrays.copyOfRange(buffer, length, buffer.length),
				Arrays.copyOfRange(initial, length, initial.length));
	}

	@Test
	public void readFullyBufferShorterOffset() throws Exception {
		byte[] buffer = initBuffer(9);
		byte[] initial = Arrays.copyOf(buffer, buffer.length);
		int length = read(buffer, 6);
		assertEquals(3, length);
		assertArrayEquals(Arrays.copyOfRange(buffer, 0, 6),
				Arrays.copyOfRange(initial, 0, 6));
		assertArrayEquals(Arrays.copyOfRange(buffer, 6, buffer.length),
				Arrays.copyOfRange(DATA, 0, 3));
	}

	@Test
	public void readFullyBufferLongerOffset() throws Exception {
		byte[] buffer = initBuffer(50);
		byte[] initial = Arrays.copyOf(buffer, buffer.length);
		int length = read(buffer, 40);
		assertEquals(10, length);
		assertArrayEquals(Arrays.copyOfRange(buffer, 0, 40),
				Arrays.copyOfRange(initial, 0, 40));
		assertArrayEquals(Arrays.copyOfRange(buffer, 40, buffer.length),
				Arrays.copyOfRange(DATA, 0, 10));
	}
}