8000 Trac #119: Support for source folders in Ant report task. · jacoco/jacoco@8b5d6a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit

Permalink
Trac #119: Support for source folders in Ant report task.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchof committed Jan 18, 2012
1 parent f6898ee commit 8b5d6a3
Show file tree
Hide file tree
Showing 14 changed files with 830 additions and 93 deletions.
80 changes: 80 additions & 0 deletions org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*******************************************************************************
* Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.ant;

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.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileResource;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Unit tests for {@link AntFilesLocator}.
*/
public class AntFilesLocatorTest {

@Rule
public final TemporaryFolder folder = new TemporaryFolder();

private AntFilesLocator locator;

@Before
public void setup() {
locator = new AntFilesLocator("UTF-8", 4);
}

@Test
public void testGetSourceFileNegative() throws IOException {
assertNull(locator.getSourceFile("org/jacoco/somewhere",
"DoesNotExist.java"));
}

@Test
public void testGetSourceFile() throws IOException {
locator.add(createFile("org/jacoco/example/Test.java"));
final Reader source = locator.getSourceFile("org/jacoco/example",
"Test.java");
assertContent(source);
}

private Resource createFile(String path) throws IOException {
final File file = new File(folder.getRoot(), path);
file.getParentFile().mkdirs();
final Writer writer = new OutputStreamWriter(
new FileOutputStream(file), "UTF-8");
writer.write("Source");
writer.close();
return new FileResource(folder.getRoot(), path);
}

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

}
142 changes: 142 additions & 0 deletions org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*******************************************************************************
* Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.ant;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileResource;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Unit tests for {@link AntResourcesLocator}.
*/
public class AntResourcesLocatorTest {

@Rule
public final TemporaryFolder folder = new TemporaryFolder();

private AntResourcesLocator locator;

@Before
public void setup() {
locator = new AntResourcesLocator("UTF-8", 8);
}

@Test
public void testGetTabWidth() {
assertEquals(8, locator.getTabWidth());
}

@Test
public void testEmpty() {
assertTrue(locator.isEmpty());
}

@Test
public void testFile() throws IOException {
locator.add(createFile("org/jacoco/example/Test.java", "AAA"));

assertFalse(locator.isEmpty());
final Reader source = locator.getSourceFile("org/jacoco/example",
"Test.java");
assertContent("AAA", source);
}

@Test
public void testDirectory() throws IOException {
createFile("src/org/jacoco/example/Test.java", "AAA");
locator.add(new FileResource(folder.getRoot(), "src"));

assertFalse(locator.isEmpty());
final Reader source = locator.getSourceFile("org/jacoco/example",
"Test.java");
assertContent("AAA", source);
}

@Test
public void testFilePrecedence() throws IOException {
createFile("src/org/jacoco/example/Test.java", "DDD");
locator.add(new FileResource(folder.getRoot(), "src"));
locator.add(createFile("org/jacoco/example/Test.java", "FFF"));

final Reader source = locator.getSourceFile("org/jacoco/example",
"Test.java");
assertContent("FFF", source);
}

@Test
public void testDirectoryOrdering() throws IOException {
createFile("src1/org/jacoco/example/Test.java", "AAA");
locator.add(new FileResource(folder.getRoot(), "src1"));
createFile("src2/org/jacoco/example/Test.java", "BBB");
locator.add(new FileResource(folder.getRoot(), "src2"));
createFile("src3/org/jacoco/example/Test.java", "CCC");
locator.add(new FileResource(folder.getRoot(), "src3"));

final Reader source = locator.getSourceFile("org/jacoco/example",
"Test.java");
assertContent("AAA", source);
}

@Test
public void testAddAll() throws IOException {
List<Resource> resources = new ArrayList<Resource>();
resources.add(createFile("org/jacoco/example/Test1.java", "AAA"));
resources.add(createFile("org/jacoco/example/Test2.java", "BBB"));
locator.addAll(resources.iterator());

assertFalse(locator.isEmpty());
Reader source = locator.getSourceFile("org/jacoco/example",
"Test1.java");
assertContent("AAA", source);
source = locator.getSourceFile("org/jacoco/example", "Test2.java");
assertContent("BBB", source);
}

private Resource createFile(String path, String content 10000 ) throws IOException {
final File file = new File(folder.getRoot(), path);
file.getParentFile().mkdirs();
final Writer writer = new OutputStreamWriter(
new FileOutputStream(file), "UTF-8");
writer.write(content);
writer.close();
return new FileResource(folder.getRoot(), path);
}

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();
}

}
39 changes: 37 additions & 2 deletions org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
Contributors:
Brock Janiczak - initial API and implementation
Dominik Stadler - source folder support
$Id: $
-->
Expand Down Expand Up @@ -78,6 +79,24 @@
<au:assertLogContains level="warn" text="To enable source code annotation class files for bundle 'root' have to be compiled with debug information."/>
</target>

<target name="testReportWithSourceDirButNoDebug">
<java classname="org.jacoco.ant.RemoveDebugInfos" classpath="${java.class.path}" failonerror="true">
<arg value="${org.jacoco.ant.reportTaskTest.classes.dir}/org/jacoco/ant/TestTarget.class" />
<arg value="${temp.dir}/TestTarget.class" />
</java>
<jacoco:report>
<structure name="root">
<classfiles>
<fileset dir="${temp.dir}" id="*.class" />
</classfiles>
<sourcefiles>
<dirset dir="${org.jacoco.ant.reportTaskTest.sources.dir}/.." includes="src" />
</sourcefiles>
</structure>
</jacoco:report>
<au:assertLogContains level="warn" text="To enable source code annotation class files for bundle 'root' have to be compiled with debug information."/>
</target>

<!-- HTML Output -->

<target name="testReportHtmlNoDestdirOrDestfile">
Expand Down Expand Up @@ -189,7 +208,7 @@
<contains string="${testReportHtmlTabWidth.content}" substring="window['PR_TAB_WIDTH']=4"/>
</au:assertTrue>
</target>

<target name="testReportHtmlTabWidth">
<jacoco:report>
<structure name="Test">
Expand All @@ -208,7 +227,7 @@
<contains string="${testReportHtmlTabWidth.content}" substring="window['PR_TAB_WIDTH']=13"/>
</au:assertTrue>
</target>

<target name="testReportHtmlInvalidTabWidth">
<au:expectfailure expectedMessage="Tab width must be greater than 0">
<jacoco:report>
Expand Down Expand Up @@ -255,6 +274,22 @@
<au:assertFileExists file="${temp.dir}/default/TestTargetInDefault.java.html"/>
</target>

<target name="testReportHtmlWithSourcesDir">
<jacoco:report>
<structure name="Test">
<classfiles>
<fileset dir="${org.jacoco.ant.reportTaskTest.classes.dir}" includes="**/*.class"/>
</classfiles>
<sourcefiles>
<dirset dir="${org.jacoco.ant.reportTaskTest.sources.dir}/.." includes="src" />
</sourcefiles>
</structure>
<html destdir="${temp.dir}"/>
</jacoco:report>

<au:assertFileExists file="${temp.dir}/org.jacoco.ant/TestTarget.java.html"/>
<au:assertFileExists file="${temp.dir}/default/TestTargetInDefault.java.html"/>
</target>

<!-- CSV Output -->

Expand Down
56 changes: 56 additions & 0 deletions org.jacoco.ant/src/org/jacoco/ant/AntFilesLocator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marc R. Hoffmann - initial API and implementation
*
*******************************************************************************/
package org.jacoco.ant;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.tools.ant.types.Resource;
import org.jacoco.report.InputStreamSourceFileLocator;

/**
* Source locator based on Ant file resources.
*/
class AntFilesLocator extends InputStreamSourceFileLocator {

private final Map<String, Resource> resources;

public AntFilesLocator(final String encoding,
final int tabWidth) {
super(encoding, tabWidth);
this.resources = new HashMap<String, Resource>();
}

/**
* Adds the given file resource as a potential source file.
*
* @param file
* file resource to add
*/
void add(final Resource file) {
resources.put(file.getName().replace(File.separatorChar, '/'), file);
}

@Override
protected InputStream getSourceStream(final String path) throws IOException {
final Resource file = resources.get(path);
if (file == null) {
return null;
} else {
return file.getInputStream();
}
}

}
Loading

0 comments on commit 8b5d6a3

Please sign in to comment.
0