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.assertNotNull;
16import static org.junit.Assert.assertNull;
17
18import java.io.BufferedReader;
19import java.io.File;
20import java.io.FileOutputStream;
21import java.io.IOException;
22import java.io.OutputStreamWriter;
23import java.io.Reader;
24import java.io.Writer;
25
26import org.apache.tools.ant.types.Resource;
27import org.apache.tools.ant.types.resources.FileResource;
28import org.junit.Before;
29import org.junit.Rule;
30import org.junit.Test;
31import org.junit.rules.TemporaryFolder;
32
33/**
34 * Unit tests for {@link AntFilesLocator}.
35 */
36public class AntFilesLocatorTest {
37
38	@Rule
39	public final TemporaryFolder folder = new TemporaryFolder();
40
41	private AntFilesLocator locator;
42
43	@Before
44	public void setup() {
45		locator = new AntFilesLocator("UTF-8", 4);
46	}
47
48	@Test
49	public void testGetSourceFileNegative() throws IOException {
50		assertNull(locator.getSourceFile("org/jacoco/somewhere",
51				"DoesNotExist.java"));
52	}
53
54	@Test
55	public void testGetSourceFile() throws IOException {
56		locator.add(createFile("org/jacoco/example/Test.java"));
57		final Reader source = locator.getSourceFile("org/jacoco/example",
58				"Test.java");
59		assertContent(source);
60	}
61
62	private Resource createFile(String path) throws IOException {
63		final File file = new File(folder.getRoot(), path);
64		file.getParentFile().mkdirs();
65		final Writer writer = new OutputStreamWriter(
66				new FileOutputStream(file), "UTF-8");
67		writer.write("Source");
68		writer.close();
69		return new FileResource(folder.getRoot(), path);
70	}
71
72	private void assertContent(Reader source) throws IOException {
73		assertNotNull(source);
74		final BufferedReader buffer = new BufferedReader(source);
75		assertEquals("Source", buffer.readLine());
76		assertNull(buffer.readLine());
77		buffer.close();
78	}
79
80}
81