Search in sources :

Example 41 with AssumptionViolatedException

use of org.junit.AssumptionViolatedException in project jacoco by jacoco.

the class Pack200StreamsTest method pack_should_pack.

@Test
public void pack_should_pack() throws Exception {
    try {
        Class.forName("java.util.jar.Pack200");
    } catch (ClassNotFoundException e) {
        throw new AssumptionViolatedException("this test requires JDK with Pack200");
    }
    ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
    zipout.putNextEntry(new ZipEntry("Test.class"));
    zipout.write(TargetLoader.getClassDataAsBytes(getClass()));
    zipout.finish();
    ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
    Pack200Streams.pack(jarbuffer.toByteArray(), new NoCloseOutputStream(pack200buffer));
    jarbuffer.reset();
    final Object unpacker = Class.forName("java.util.jar.Pack200").getMethod("newUnpacker").invoke(null);
    Class.forName("java.util.jar.Pack200$Unpacker").getMethod("unpack", InputStream.class, JarOutputStream.class).invoke(unpacker, new ByteArrayInputStream(pack200buffer.toByteArray()), new JarOutputStream(jarbuffer));
    ZipInputStream zipin = new ZipInputStream(new ByteArrayInputStream(jarbuffer.toByteArray()));
    assertEquals("Test.class", zipin.getNextEntry().getName());
    assertNull(zipin.getNextEntry());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) AssumptionViolatedException(org.junit.AssumptionViolatedException) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 42 with AssumptionViolatedException

use of org.junit.AssumptionViolatedException in project jacoco by jacoco.

the class Pack200StreamsTest method unpack_should_unpack.

@Test
public void unpack_should_unpack() throws Exception {
    try {
        Class.forName("java.util.jar.Pack200");
    } catch (ClassNotFoundException e) {
        throw new AssumptionViolatedException("this test requires JDK with Pack200");
    }
    ByteArrayOutputStream jarbuffer = new ByteArrayOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(jarbuffer);
    zipout.putNextEntry(new ZipEntry("Test.class"));
    zipout.write(TargetLoader.getClassDataAsBytes(getClass()));
    zipout.finish();
    ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
    final Object packer = Class.forName("java.util.jar.Pack200").getMethod("newPacker").invoke(null);
    Class.forName("java.util.jar.Pack200$Packer").getMethod("pack", JarInputStream.class, OutputStream.class).invoke(packer, new JarInputStream(new ByteArrayInputStream(jarbuffer.toByteArray())), pack200buffer);
    InputStream result = Pack200Streams.unpack(new NoCloseInputStream(new ByteArrayInputStream(pack200buffer.toByteArray())));
    ZipInputStream zipin = new ZipInputStream(result);
    assertEquals("Test.class", zipin.getNextEntry().getName());
    assertNull(zipin.getNextEntry());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) AssumptionViolatedException(org.junit.AssumptionViolatedException) JarInputStream(java.util.jar.JarInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream) JarOutputStream(java.util.jar.JarOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 43 with AssumptionViolatedException

use of org.junit.AssumptionViolatedException in project jacoco by jacoco.

the class Pack200StreamsTest method pack_should_throw_IOException_when_Pack200_not_available_in_JDK.

@Test
public void pack_should_throw_IOException_when_Pack200_not_available_in_JDK() {
    try {
        Class.forName("java.util.jar.Pack200");
        throw new AssumptionViolatedException("this test requires JDK without Pack200");
    } catch (ClassNotFoundException ignore) {
    }
    try {
        Pack200Streams.pack(new byte[0], new ByteArrayOutputStream());
        fail("expected exception");
    } catch (IOException e) {
        assertNull(e.getMessage());
        assertTrue(e.getCause() instanceof ClassNotFoundException);
    }
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 44 with AssumptionViolatedException

use of org.junit.AssumptionViolatedException in project jacoco by jacoco.

the class Pack200StreamsTest method unpack_should_throw_IOException_when_can_not_read_from_InputStream.

@Test
public void unpack_should_throw_IOException_when_can_not_read_from_InputStream() {
    try {
        Class.forName("java.util.jar.Pack200");
    } catch (ClassNotFoundException e) {
        throw new AssumptionViolatedException("this test requires JDK with Pack200");
    }
    final InputStream inputStream = new BrokenInputStream();
    try {
        Pack200Streams.unpack(inputStream);
        fail("expected exception");
    } catch (IOException e) {
        assertTrue(e.getCause() instanceof IOException);
        assertEquals("fake broken input stream", e.getCause().getMessage());
    }
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 45 with AssumptionViolatedException

use of org.junit.AssumptionViolatedException in project jacoco by jacoco.

the class AnalyzerTest method testAnalyzeAll_Pack200.

@Test
public void testAnalyzeAll_Pack200() throws IOException {
    try {
        Class.forName("java.util.jar.Pack200");
    } catch (ClassNotFoundException e) {
        throw new AssumptionViolatedException("this test requires JDK with Pack200");
    }
    final ByteArrayOutputStream zipbuffer = new ByteArrayOutputStream();
    final ZipOutputStream zip = new ZipOutputStream(zipbuffer);
    zip.putNextEntry(new ZipEntry("org/jacoco/core/analysis/AnalyzerTest.class"));
    zip.write(TargetLoader.getClassDataAsBytes(AnalyzerTest.class));
    zip.finish();
    final ByteArrayOutputStream pack200buffer = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutput = new GZIPOutputStream(pack200buffer);
    Pack200Streams.pack(zipbuffer.toByteArray(), gzipOutput);
    gzipOutput.finish();
    final int count = analyzer.analyzeAll(new ByteArrayInputStream(pack200buffer.toByteArray()), "Test");
    assertEquals(1, count);
    assertClasses("org/jacoco/core/analysis/AnalyzerTest");
}
Also used : AssumptionViolatedException(org.junit.AssumptionViolatedException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

AssumptionViolatedException (org.junit.AssumptionViolatedException)79 Test (org.junit.Test)26 IOException (java.io.IOException)16 Statement (org.junit.runners.model.Statement)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Method (java.lang.reflect.Method)6 Set (java.util.Set)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 File (java.io.File)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 ZipEntry (java.util.zip.ZipEntry)5 ZipOutputStream (java.util.zip.ZipOutputStream)5 InputStream (java.io.InputStream)4 HashSet (java.util.HashSet)4 ZipInputStream (java.util.zip.ZipInputStream)4 FilterInputStream (java.io.FilterInputStream)3 UnknownHostException (java.net.UnknownHostException)3 JarInputStream (java.util.jar.JarInputStream)3 Configuration (org.apache.flink.configuration.Configuration)3 IInjectorProvider (org.eclipse.xtext.junit4.IInjectorProvider)3