• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *******************************************************************************/
12 package org.jacoco.ant;
13 
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertNotNull;
16 import static org.junit.Assert.assertNull;
17 
18 import java.io.BufferedReader;
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStreamWriter;
23 import java.io.Reader;
24 import java.io.Writer;
25 
26 import org.apache.tools.ant.types.Resource;
27 import org.apache.tools.ant.types.resources.FileResource;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.rules.TemporaryFolder;
32 
33 /**
34  * Unit tests for {@link AntFilesLocator}.
35  */
36 public class AntFilesLocatorTest {
37 
38 	@Rule
39 	public final TemporaryFolder folder = new TemporaryFolder();
40 
41 	private AntFilesLocator locator;
42 
43 	@Before
setup()44 	public void setup() {
45 		locator = new AntFilesLocator("UTF-8", 4);
46 	}
47 
48 	@Test
testGetSourceFileNegative()49 	public void testGetSourceFileNegative() throws IOException {
50 		assertNull(locator.getSourceFile("org/jacoco/somewhere",
51 				"DoesNotExist.java"));
52 	}
53 
54 	@Test
testGetSourceFile()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 
createFile(String path)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 
assertContent(Reader source)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