-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trac #119: Support for source folders in Ant report task.
- Loading branch information
Showing
14 changed files
with
830 additions
and
93 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
org.jacoco.ant.test/src/org/jacoco/ant/AntFilesLocatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
142
org.jacoco.ant.test/src/org/jacoco/ant/AntResourcesLocatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
||
|
||
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.