aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.ant.test
diff options
context:
space:
mode:
authorMarc R. Hoffmann <hoffmann@mountainminds.com>2012-01-18 20:24:45 +0000
committerMarc R. Hoffmann <hoffmann@mountainminds.com>2012-01-18 20:24:45 +0000
commit8b5d6a399c8927362780dd9426b85c2432fd8d7a (patch)
tree33fd6472de9fb08f77ff570418f95821a99acb9d /org.jacoco.ant.test
parentf6898eece6c8ba3a4fee48189ab0a714b8d55cf2 (diff)
downloadjacoco-8b5d6a399c8927362780dd9426b85c2432fd8d7a.tar.gz
Trac #119: Support for source folders in Ant report task.
Diffstat (limited to 'org.jacoco.ant.test')
-rw-r--r--org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java80
-rw-r--r--org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java142
-rw-r--r--org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml39
3 files changed, 259 insertions, 2 deletions
diff --git a/org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java b/org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java
new file mode 100644
index 00000000..5bf6e409
--- /dev/null
+++ b/org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java
@@ -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();
+ }
+
+}
diff --git a/org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java b/org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java
new file mode 100644
index 00000000..700fbe4b
--- /dev/null
+++ b/org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java
@@ -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) 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();
+ }
+
+}
diff --git a/org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml b/org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml
index 30e7903b..783eeaa4 100644
--- a/org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml
+++ b/org.jacoco.ant.test/src/org/jacoco/ant/ReportTaskTest.xml
@@ -9,6 +9,7 @@
Contributors:
Brock Janiczak - initial API and implementation
+ Dominik Stadler - source folder support
$Id: $
-->
@@ -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">
@@ -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">
@@ -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>
@@ -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 -->