1/*******************************************************************************
2 * Copyright (c) 2009, 2015 Mountainminds GmbH & Co. KG and Contributors
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.ant;
13
14import static org.junit.Assert.assertEquals;
15import static org.junit.Assert.assertFalse;
16import static org.junit.Assert.assertNotNull;
17import static org.junit.Assert.assertNull;
18import static org.junit.Assert.assertTrue;
19
20import java.io.BufferedReader;
21import java.io.File;
22import java.io.FileOutputStream;
23import java.io.IOException;
24import java.io.OutputStreamWriter;
25import java.io.Reader;
26import java.io.Writer;
27import java.util.ArrayList;
28import java.util.List;
29
30import org.apache.tools.ant.types.Resource;
31import org.apache.tools.ant.types.resources.FileResource;
32import org.junit.Before;
33import org.junit.Rule;
34import org.junit.Test;
35import org.junit.rules.TemporaryFolder;
36
37/**
38 * Unit tests for {@link AntResourcesLocator}.
39 */
40public class AntResourcesLocatorTest {
41
42	@Rule
43	public final TemporaryFolder folder = new TemporaryFolder();
44
45	private AntResourcesLocator locator;
46
47	@Before
48	public void setup() {
49		locator = new AntResourcesLocator("UTF-8", 8);
50	}
51
52	@Test
53	public void testGetTabWidth() {
54		assertEquals(8, locator.getTabWidth());
55	}
56
57	@Test
58	public void testEmpty() {
59		assertTrue(locator.isEmpty());
60	}
61
62	@Test
63	public void testFile() throws IOException {
64		locator.add(createFile("org/jacoco/example/Test.java", "AAA"));
65
66		assertFalse(locator.isEmpty());
67		final Reader source = locator.getSourceFile("org/jacoco/example",
68				"Test.java");
69		assertContent("AAA", source);
70	}
71
72	@Test
73	public void testDirectory() throws IOException {
74		createFile("src/org/jacoco/example/Test.java", "AAA");
75		locator.add(new FileResource(folder.getRoot(), "src"));
76
77		assertFalse(locator.isEmpty());
78		final Reader source = locator.getSourceFile("org/jacoco/example",
79				"Test.java");
80		assertContent("AAA", source);
81	}
82
83	@Test
84	public void testFilePrecedence() throws IOException {
85		createFile("src/org/jacoco/example/Test.java", "DDD");
86		locator.add(new FileResource(folder.getRoot(), "src"));
87		locator.add(createFile("org/jacoco/example/Test.java", "FFF"));
88
89		final Reader source = locator.getSourceFile("org/jacoco/example",
90				"Test.java");
91		assertContent("FFF", source);
92	}
93
94	@Test
95	public void testDirectoryOrdering() throws IOException {
96		createFile("src1/org/jacoco/example/Test.java", "AAA");
97		locator.add(new FileResource(folder.getRoot(), "src1"));
98		createFile("src2/org/jacoco/example/Test.java", "BBB");
99		locator.add(new FileResource(folder.getRoot(), "src2"));
100		createFile("src3/org/jacoco/example/Test.java", "CCC");
101		locator.add(new FileResource(folder.getRoot(), "src3"));
102
103		final Reader source = locator.getSourceFile("org/jacoco/example",
104				"Test.java");
105		assertContent("AAA", source);
106	}
107
108	@Test
109	public void testAddAll() throws IOException {
110		List<Resource> resources = new ArrayList<Resource>();
111		resources.add(createFile("org/jacoco/example/Test1.java", "AAA"));
112		resources.add(createFile("org/jacoco/example/Test2.java", "BBB"));
113		locator.addAll(resources.iterator());
114
115		assertFalse(locator.isEmpty());
116		Reader source = locator.getSourceFile("org/jacoco/example",
117				"Test1.java");
118		assertContent("AAA", source);
119		source = locator.getSourceFile("org/jacoco/example", "Test2.java");
120		assertContent("BBB", source);
121	}
122
123	private Resource createFile(String path, String content) throws IOException {
124		final File file = new File(folder.getRoot(), path);
125		file.getParentFile().mkdirs();
126		final Writer writer = new OutputStreamWriter(
127				new FileOutputStream(file), "UTF-8");
128		writer.write(content);
129		writer.close();
130		return new FileResource(folder.getRoot(), path);
131	}
132
133	private void assertContent(String expected, Reader source)
134			throws IOException {
135		assertNotNull(source);
136		final BufferedReader buffer = new BufferedReader(source);
137		assertEquals(expected, buffer.readLine());
138		assertNull(buffer.readLine());
139		buffer.close();
140	}
141
142}
143