diff options
author | Evgeny Mandrikov <Godin@users.noreply.github.com> | 2016-05-14 09:33:09 +0200 |
---|---|---|
committer | Marc R. Hoffmann <hoffmann@mountainminds.com> | 2016-05-14 09:33:09 +0200 |
commit | fde254dd251e6afc1c8f1272efb1be2b6c767387 (patch) | |
tree | 047453eeadcb83653f60e651f573faae6974f1b7 /org.jacoco.core.test | |
parent | a6bcbe3f67cf2d4296562b6bfda227dab4236f75 (diff) | |
download | jacoco-fde254dd251e6afc1c8f1272efb1be2b6c767387.tar.gz |
Add location to IOException when unable to read input during analysis (#400)
Diffstat (limited to 'org.jacoco.core.test')
-rw-r--r-- | org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java | 126 |
1 files changed, 110 insertions, 16 deletions
diff --git a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java index bef10792..89ed34b4 100644 --- a/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java +++ b/org.jacoco.core.test/src/org/jacoco/core/analysis/AnalyzerTest.java @@ -15,11 +15,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; @@ -39,7 +41,6 @@ import org.jacoco.core.test.TargetLoader; import org.junit.Before; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; /** @@ -50,9 +51,6 @@ public class AnalyzerTest { @Rule public TemporaryFolder folder = new TemporaryFolder(); - @Rule - public ExpectedException expected = ExpectedException.none(); - private Analyzer analyzer; private Map<String, IClassCoverage> classes; @@ -114,13 +112,15 @@ public class AnalyzerTest { @Test public void testAnalyzeClass_Broken() throws IOException { - expected.expect(IOException.class); - expected.expectMessage("Error while analyzing Broken.class."); - final byte[] brokenclass = TargetLoader .getClassDataAsBytes(AnalyzerTest.class); brokenclass[10] = 0x23; - analyzer.analyzeClass(brokenclass, "Broken.class"); + try { + analyzer.analyzeClass(brokenclass, "Broken.class"); + fail("expected exception"); + } catch (IOException e) { + assertEquals("Error while analyzing Broken.class.", e.getMessage()); + } } @Test @@ -156,6 +156,40 @@ public class AnalyzerTest { assertEquals(0, count); } + /** + * Triggers exception in + * {@link Analyzer#analyzeAll(java.io.InputStream, String)}. + */ + @Test + public void testAnalyzeAll_Broken() throws IOException { + try { + analyzer.analyzeAll(new InputStream() { + @Override + public int read() throws IOException { + throw new IOException(); + } + }, "Test"); + fail("expected exception"); + } catch (IOException e) { + assertEquals("Error while analyzing Test.", e.getMessage()); + } + } + + /** + * Triggers exception in + * {@link Analyzer#analyzeGzip(java.io.InputStream, String)}. + */ + @Test + public void testAnalyzeAll_BrokenGZ() { + final byte[] buffer = new byte[] { 0x1f, (byte) 0x8b, 0x00, 0x00 }; + try { + analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.gz"); + fail("expected exception"); + } catch (IOException e) { + assertEquals("Error while analyzing Test.gz.", e.getMessage()); + } + } + @Test public void testAnalyzeAll_Pack200() throws IOException { final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream(); @@ -178,6 +212,23 @@ public class AnalyzerTest { assertClasses("org/jacoco/core/analysis/AnalyzerTest"); } + /** + * Triggers exception in + * {@link Analyzer#analyzePack200(java.io.InputStream, String)}. + */ + @Test + public void testAnalyzeAll_BrokenPack200() { + final byte[] buffer = new byte[] { (byte) 0xca, (byte) 0xfe, + (byte) 0xd0, 0x0d }; + try { + analyzer.analyzeAll(new ByteArrayInputStream(buffer), + "Test.pack200"); + fail("expected exception"); + } catch (IOException e) { + assertEquals("Error while analyzing Test.pack200.", e.getMessage()); + } + } + @Test public void testAnalyzeAll_Empty() throws IOException { final int count = analyzer.analyzeAll(new ByteArrayInputStream( @@ -205,22 +256,58 @@ public class AnalyzerTest { "org/jacoco/core/analysis/AnalyzerTest"); } - @Test(expected = IOException.class) - public void testAnalyzeAll_BrokenZip() throws IOException { + /** + * Triggers exception in + * {@link Analyzer#nextEntry(java.util.zip.ZipInputStream, String)}. + */ + @Test + public void testAnalyzeAll_BrokenZip() { + final byte[] buffer = new byte[30]; + buffer[0] = 0x50; + buffer[1] = 0x4b; + buffer[2] = 0x03; + buffer[3] = 0x04; + Arrays.fill(buffer, 4, buffer.length, (byte) 0x42); + try { + analyzer.analyzeAll(new ByteArrayInputStream(buffer), "Test.zip"); + fail("expected exception"); + } catch (IOException e) { + assertEquals("Error while analyzing Test.zip.", e.getMessage()); + } + } + + /** + * With JDK 5 triggers exception in + * {@link Analyzer#nextEntry(ZipInputStream, String)}, + * i.e. message will contain only "broken.zip". + * + * With JDK > 5 triggers exception in + * {@link Analyzer#analyzeAll(java.io.InputStream, String)}, + * i.e. message will contain only "broken.zip@brokenentry.txt". + */ + @Test + public void testAnalyzeAll_BrokenZipEntry() throws IOException { File file = new File(folder.getRoot(), "broken.zip"); OutputStream out = new FileOutputStream(file); ZipOutputStream zip = new ZipOutputStream(out); zip.putNextEntry(new ZipEntry("brokenentry.txt")); out.write(0x23); // Unexpected data here zip.close(); - analyzer.analyzeAll(file); + try { + analyzer.analyzeAll(file); + fail("expected exception"); + } catch (IOException e) { + assertTrue(e.getMessage().startsWith("Error while analyzing")); + assertTrue(e.getMessage().contains("broken.zip")); + } } + /** + * Triggers exception in + * {@link Analyzer#analyzeClass(java.io.InputStream, String)}. + */ @Test 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 ZipOutputStream zip = new ZipOutputStream(buffer); zip.putNextEntry(new ZipEntry( @@ -231,8 +318,15 @@ public class AnalyzerTest { zip.write(brokenclass); zip.finish(); - analyzer.analyzeAll(new ByteArrayInputStream(buffer.toByteArray()), - "test.zip"); + try { + analyzer.analyzeAll(new ByteArrayInputStream(buffer.toByteArray()), + "test.zip"); + fail("expected exception"); + } catch (IOException e) { + assertEquals( + "Error while analyzing test.zip@org/jacoco/core/analysis/AnalyzerTest.class.", + e.getMessage()); + } } private void createClassfile(final String dir, final Class<?> source) |