aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report.test/src/org/jacoco/report/InputStreamSourceFileLocatorTest.java
blob: 394ae928155e4aa05f535349aef7b6a99a12d094 (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
85
86
87
88
89
90
91
92
93
94
95
96
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.report;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

public class InputStreamSourceFileLocatorTest {

	private Map<String, byte[]> sources = new HashMap<String, byte[]>();

	class TestLocator extends InputStreamSourceFileLocator {

		protected TestLocator(String encoding, int tabWidth) {
			super(encoding, tabWidth);
		}

		@Override
		protected InputStream getSourceStream(String path) throws IOException {
			final byte[] bytes = sources.get(path);
			return bytes == null ? null : new ByteArrayInputStream(bytes);
		}
	}

	@Test
	public void testGetTabWidth() throws IOException {
		ISourceFileLocator locator = new TestLocator("UTF-8", 17);
		assertEquals(17, locator.getTabWidth());
	}

	@Test
	public void testGetSourceFileNegative() throws IOException {
		ISourceFileLocator locator = new TestLocator("UTF-8", 4);
		assertNull(locator.getSourceFile("org/jacoco/example", "Test.java"));
	}

	@Test
	public void testGetSourceFile() throws IOException {
		ISourceFileLocator locator = new TestLocator("UTF-8", 4);
		sources.put("org/jacoco/example/Test.java", "ÜÄö".getBytes("UTF-8"));
		assertContent("ÜÄö",
				locator.getSourceFile("org/jacoco/example", "Test.java"));
	}

	@Test
	public void testGetSourceFileDefaultPackage() throws IOException {
		ISourceFileLocator locator = new TestLocator("UTF-8", 4);
		sources.put("Test.java", "ÜÄö".getBytes("UTF-8"));
		assertContent("ÜÄö", locator.getSourceFile("", "Test.java"));
	}

	@Test
	public void testEncoding() throws IOException {
		ISourceFileLocator locator = new TestLocator("UTF-16", 4);
		sources.put("Test.java", "ÜÄö".getBytes("UTF-16"));
		assertContent("ÜÄö", locator.getSourceFile("", "Test.java"));
	}

	@Test
	public void testDefaultEncoding() throws IOException {
		ISourceFileLocator locator = new TestLocator(null, 4);
		sources.put("Test.java", "Hello World!".getBytes());
		assertContent("Hello World!", locator.getSourceFile("", "Test.java"));
	}

	private void assertContent(String expected, Reader source)
			throws IOException {
		assertNotNull(source);
		final BufferedReader buffer = new BufferedReader(source);
		assertEquals(expected, buffer.readLine());
		assertNull(buffer.readLine());
		buffer.close();
	}

}