Skip to content

Commit

Permalink
Clarify semantic of location parameter.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchof committed Oct 12, 2015
1 parent 2c73d99 commit 0245366
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
29 changes: 13 additions & 16 deletions org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java
Expand Up @@ -15,7 +15,6 @@
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
Expand All @@ -40,6 +39,7 @@
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder; import org.junit.rules.TemporaryFolder;


/** /**
Expand All @@ -50,6 +50,9 @@ public class AnalyzerTest {
@Rule @Rule
public TemporaryFolder folder = new TemporaryFolder(); public TemporaryFolder folder = new TemporaryFolder();


@Rule
public ExpectedException expected = ExpectedException.none();

private Analyzer analyzer; private Analyzer analyzer;


private Map<String, IClassCoverage> classes; private Map<String, IClassCoverage> classes;
Expand Down Expand Up @@ -111,15 +114,13 @@ public void testAnalyzeClassNoIdMatch() throws IOException {


@Test @Test
public void testAnalyzeClass_Broken() throws IOException { public void testAnalyzeClass_Broken() throws IOException {
expected.expect(IOException.class);
expected.expectMessage("Error while analyzing Broken.class.");

final byte[] brokenclass = TargetLoader final byte[] brokenclass = TargetLoader
.getClassDataAsBytes(AnalyzerTest.class); .getClassDataAsBytes(AnalyzerTest.class);
brokenclass[10] = 0x23; brokenclass[10] = 0x23;
try { analyzer.analyzeClass(brokenclass, "Broken.class");
analyzer.analyzeClass(brokenclass, "Broken");
fail();
} catch (IOException e) {
assertEquals("Error while analyzing class Broken.", e.getMessage());
}
} }


@Test @Test
Expand Down Expand Up @@ -217,6 +218,9 @@ public void testAnalyzeAll_BrokenZip() throws IOException {


@Test @Test
public void testAnalyzeAll_BrokenClassFileInZip() throws IOException { public void testAnalyzeAll_BrokenClassFileInZip() throws IOException {
expected.expect(IOException.class);
expected.expectMessage("Error while analyzing test.zip@org/jacoco/core/analysis/AnalyzerTest.class.");

final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final ZipOutputStream zip = new ZipOutputStream(buffer); final ZipOutputStream zip = new ZipOutputStream(buffer);
zip.putNextEntry(new ZipEntry( zip.putNextEntry(new ZipEntry(
Expand All @@ -227,15 +231,8 @@ public void testAnalyzeAll_BrokenClassFileInZip() throws IOException {
zip.write(brokenclass); zip.write(brokenclass);
zip.finish(); zip.finish();


try { analyzer.analyzeAll(new ByteArrayInputStream(buffer.toByteArray()),
analyzer.analyzeAll(new ByteArrayInputStream(buffer.toByteArray()), "test.zip");
"test.zip");
fail();
} catch (IOException e) {
assertEquals(
"Error while analyzing class test.zip@org/jacoco/core/analysis/AnalyzerTest.class.",
e.getMessage());
}
} }


private void createClassfile(final String dir, final Class<?> source) private void createClassfile(final String dir, final Class<?> source)
Expand Down
46 changes: 23 additions & 23 deletions org.jacoco.core/src/org/jacoco/core/analysis/Analyzer.java
Expand Up @@ -112,17 +112,17 @@ public void analyzeClass(final ClassReader reader) {
* *
* @param buffer * @param buffer
* class definitions * class definitions
* @param name * @param location
* a name used for exception messages * a location description used for exception messages
* @throws IOException * @throws IOException
* if the class can't be analyzed * if the class can't be analyzed
*/ */
public void analyzeClass(final byte[] buffer, final String name) public void analyzeClass(final byte[] buffer, final String location)
throws IOException { throws IOException {
try { try {
analyzeClass(new ClassReader(buffer)); analyzeClass(new ClassReader(buffer));
} catch (final RuntimeException cause) { } catch (final RuntimeException cause) {
throw analyzerError(name, cause); throw analyzerError(location, cause);
} }
} }


Expand All @@ -131,24 +131,24 @@ public void analyzeClass(final byte[] buffer, final String name)
* *
* @param input * @param input
* stream to read class definition from * stream to read class definition from
* @param name * @param location
* a name used for exception messages * a location description used for exception messages
* @throws IOException * @throws IOException
* if the stream can't be read or the class can't be analyzed * if the stream can't be read or the class can't be analyzed
*/ */
public void analyzeClass(final InputStream input, final String name) public void analyzeClass(final InputStream input, final String location)
throws IOException { throws IOException {
try { try {
analyzeClass(new ClassReader(input)); analyzeClass(new ClassReader(input));
} catch (final RuntimeException e) { } catch (final RuntimeException e) {
throw analyzerError(name, e); throw analyzerError(location, e);
} }
} }


private IOException analyzerError(final String name, private IOException analyzerError(final String location,
final RuntimeException cause) { final RuntimeException cause) {
final IOException ex = new IOException(String.format( final IOException ex = new IOException(String.format(
"Error while analyzing class %s.", name)); "Error while analyzing %s.", location));
ex.initCause(cause); ex.initCause(cause);
return ex; return ex;
} }
Expand All @@ -161,25 +161,25 @@ private IOException analyzerError(final String name,
* *
* @param input * @param input
* input data * input data
* @param name * @param location
* a name used for exception messages * a location description used for exception messages
* @return number of class files found * @return number of class files found
* @throws IOException * @throws IOException
* if the stream can't be read or a class can't be analyzed * if the stream can't be read or a class can't be analyzed
*/ */
public int analyzeAll(final InputStream input, final String name) public int analyzeAll(final InputStream input, final String location)
throws IOException { throws IOException {
final ContentTypeDetector detector = new ContentTypeDetector(input); final ContentTypeDetector detector = new ContentTypeDetector(input);
switch (detector.getType()) { switch (detector.getType()) {
case ContentTypeDetector.CLASSFILE: case ContentTypeDetector.CLASSFILE:
analyzeClass(detector.getInputStream(), name); analyzeClass(detector.getInputStream(), location);
return 1; return 1;
case ContentTypeDetector.ZIPFILE: case ContentTypeDetector.ZIPFILE:
return analyzeZip(detector.getInputStream(), name); return analyzeZip(detector.getInputStream(), location);
case ContentTypeDetector.GZFILE: case ContentTypeDetector.GZFILE:
return analyzeGzip(detector.getInputStream(), name); return analyzeGzip(detector.getInputStream(), location);
case ContentTypeDetector.PACK200FILE: case ContentTypeDetector.PACK200FILE:
return analyzePack200(detector.getInputStream(), name); return analyzePack200(detector.getInputStream(), location);
default: default:
return 0; return 0;
} }
Expand Down Expand Up @@ -237,25 +237,25 @@ public int analyzeAll(final String path, final File basedir)
return count; return count;
} }


private int analyzeZip(final InputStream input, final String name) private int analyzeZip(final InputStream input, final String location)
throws IOException { throws IOException {
final ZipInputStream zip = new ZipInputStream(input); final ZipInputStream zip = new ZipInputStream(input);
ZipEntry entry; ZipEntry entry;
int count = 0; int count = 0;
while ((entry = zip.getNextEntry()) != null) { while ((entry = zip.getNextEntry()) != null) {
count += analyzeAll(zip, name + "@" + entry.getName()); count += analyzeAll(zip, location + "@" + entry.getName());
} }
return count; return count;
} }


private int analyzeGzip(final InputStream input, final String name) private int analyzeGzip(final InputStream input, final String location)
throws IOException { throws IOException {
return analyzeAll(new GZIPInputStream(input), name); return analyzeAll(new GZIPInputStream(input), location);
} }


private int analyzePack200(final InputStream input, final String name) private int analyzePack200(final InputStream input, final String location)
throws IOException { throws IOException {
return analyzeAll(Pack200Streams.unpack(input), name); return analyzeAll(Pack200Streams.unpack(input), location);
} }


} }

0 comments on commit 0245366

Please sign in to comment.