|  | 
| 18 | 18 |  */ | 
| 19 | 19 | package org.apache.maven.plugins.shade.resource; | 
| 20 | 20 | 
 | 
|  | 21 | +import java.io.ByteArrayInputStream; | 
|  | 22 | +import java.io.ByteArrayOutputStream; | 
|  | 23 | +import java.io.IOException; | 
|  | 24 | +import java.io.InputStream; | 
|  | 25 | +import java.util.Collections; | 
| 21 | 26 | import java.util.Locale; | 
|  | 27 | +import java.util.jar.JarInputStream; | 
|  | 28 | +import java.util.jar.JarOutputStream; | 
| 22 | 29 | 
 | 
| 23 | 30 | import org.junit.Before; | 
| 24 | 31 | import org.junit.Test; | 
| 25 | 32 | 
 | 
|  | 33 | +import static org.junit.Assert.assertEquals; | 
| 26 | 34 | import static org.junit.Assert.assertFalse; | 
| 27 | 35 | import static org.junit.Assert.assertTrue; | 
| 28 | 36 | 
 | 
| @@ -56,4 +64,37 @@ public void testCanTransformResource() { | 
| 56 | 64 |         assertTrue(transformer.canTransformResource("ABCDEFGHIJKLMNOPQRSTUVWXYZ")); | 
| 57 | 65 |         assertFalse(transformer.canTransformResource("META-INF/MANIFEST.MF")); | 
| 58 | 66 |     } | 
|  | 67 | + | 
|  | 68 | +    @Test | 
|  | 69 | +    public void testProcessResource() throws IOException { | 
|  | 70 | +        transformer.resource = "test-resource"; | 
|  | 71 | +        String firstLine = "first line"; | 
|  | 72 | +        String secondLine = "second line"; | 
|  | 73 | +        InputStream firstIs = new ByteArrayInputStream(firstLine.getBytes()); | 
|  | 74 | +        InputStream secondIs = new ByteArrayInputStream(secondLine.getBytes()); | 
|  | 75 | + | 
|  | 76 | +        transformer.processResource("", firstIs, Collections.emptyList()); | 
|  | 77 | +        transformer.processResource("", secondIs, Collections.emptyList()); | 
|  | 78 | + | 
|  | 79 | +        final ByteArrayOutputStream out = new ByteArrayOutputStream(); | 
|  | 80 | +        try (final JarOutputStream jarOutputStream = new JarOutputStream(out)) { | 
|  | 81 | +            transformer.modifyOutputStream(jarOutputStream); | 
|  | 82 | +        } | 
|  | 83 | + | 
|  | 84 | +        try (final JarInputStream jis = new JarInputStream(new ByteArrayInputStream(out.toByteArray()))) { | 
|  | 85 | +            assertEquals("test-resource", jis.getNextJarEntry().getName()); | 
|  | 86 | +            String result = read(jis); | 
|  | 87 | +            assertEquals(firstLine + "\n" + secondLine, result); | 
|  | 88 | +        } | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    private String read(final JarInputStream jar) throws IOException { | 
|  | 92 | +        final StringBuilder builder = new StringBuilder(); | 
|  | 93 | +        final byte[] buffer = new byte[512]; | 
|  | 94 | +        int read; | 
|  | 95 | +        while ((read = jar.read(buffer)) >= 0) { | 
|  | 96 | +            builder.append(new String(buffer, 0, read)); | 
|  | 97 | +        } | 
|  | 98 | +        return builder.toString(); | 
|  | 99 | +    } | 
| 59 | 100 | } | 
0 commit comments