diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java index 693ba499..906358f9 100644 --- a/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/resource/ServiceResourceTransformerTest.java @@ -21,21 +21,20 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import org.apache.commons.io.IOUtils; -import org.apache.maven.plugins.shade.relocation.Relocator; import org.apache.maven.plugins.shade.relocation.SimpleRelocator; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import static java.util.Collections.singletonList; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -45,14 +44,23 @@ */ public class ServiceResourceTransformerTest { private final String NEWLINE = "\n"; + private File tempJar; - private List relocators = new ArrayList(); + @Before + public void setup() throws IOException { + tempJar = File.createTempFile("shade.", ".jar"); + } + + @After + public void cleanup() { + //noinspection ResultOfMethodCallIgnored + tempJar.delete(); + } @Test public void relocatedClasses() throws Exception { SimpleRelocator relocator = - new SimpleRelocator("org.foo", "borg.foo", null, Arrays.asList("org.foo.exclude.*")); - relocators.add(relocator); + new SimpleRelocator("org.foo", "borg.foo", null, singletonList("org.foo.exclude.*")); String content = "org.foo.Service\norg.foo.exclude.OtherService\n"; byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); @@ -61,13 +69,11 @@ public void relocatedClasses() throws Exception { String contentResourceShaded = "META-INF/services/borg.foo.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tempJar); - try (JarOutputStream jos = new JarOutputStream(fos)) { + try (FileOutputStream fos = new FileOutputStream(tempJar)) { + JarOutputStream jos = new JarOutputStream(fos); xformer.modifyOutputStream(jos); jos.close(); @@ -75,21 +81,18 @@ public void relocatedClasses() throws Exception { JarEntry jarEntry = jarFile.getJarEntry(contentResourceShaded); assertNotNull(jarEntry); try (InputStream entryStream = jarFile.getInputStream(jarEntry)) { - String xformedContent = IOUtils.toString(entryStream, "utf-8"); + String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8); assertEquals("borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE, xformedContent); } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @Test public void mergeRelocatedFiles() throws Exception { SimpleRelocator relocator = - new SimpleRelocator("org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*")); - relocators.add(relocator); + new SimpleRelocator("org.foo", "borg.foo", null, singletonList("org.foo.exclude.*")); String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE; String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE; @@ -100,17 +103,15 @@ public void mergeRelocatedFiles() throws Exception { ServicesResourceTransformer xformer = new ServicesResourceTransformer(); try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) { - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); } try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) { - xformer.processResource(contentResourceShaded, contentStream, relocators, 0); + xformer.processResource(contentResourceShaded, contentStream, singletonList(relocator), 0); } - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tempJar); - try (JarOutputStream jos = new JarOutputStream(fos)) { + try (FileOutputStream fos = new FileOutputStream(tempJar)) { + JarOutputStream jos = new JarOutputStream(fos); xformer.modifyOutputStream(jos); jos.close(); @@ -123,29 +124,24 @@ public void mergeRelocatedFiles() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @Test public void concatanationAppliedMultipleTimes() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.eclipse", "org.eclipse1234", null, null); - relocators.add(relocator); String content = "org.eclipse.osgi.launch.EquinoxFactory\n"; - byte[] contentBytes = content.getBytes("UTF-8"); + byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); InputStream contentStream = new ByteArrayInputStream(contentBytes); String contentResource = "META-INF/services/org.osgi.framework.launch.FrameworkFactory"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tempJar); - try (JarOutputStream jos = new JarOutputStream(fos)) { + try (FileOutputStream fos = new FileOutputStream(tempJar)) { + JarOutputStream jos = new JarOutputStream(fos); xformer.modifyOutputStream(jos); jos.close(); @@ -158,15 +154,12 @@ public void concatanationAppliedMultipleTimes() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @Test public void concatenation() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, null); - relocators.add(relocator); String content = "org.foo.Service\n"; byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); @@ -174,7 +167,7 @@ public void concatenation() throws Exception { String contentResource = "META-INF/services/org.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); content = "org.blah.Service\n"; @@ -182,13 +175,11 @@ public void concatenation() throws Exception { contentStream = new ByteArrayInputStream(contentBytes); contentResource = "META-INF/services/org.something.another"; - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); - FileOutputStream fos = new FileOutputStream(tempJar); - try (JarOutputStream jos = new JarOutputStream(fos)) { + try (FileOutputStream fos = new FileOutputStream(tempJar)) { + JarOutputStream jos = new JarOutputStream(fos); xformer.modifyOutputStream(jos); jos.close(); @@ -196,7 +187,7 @@ public void concatenation() throws Exception { JarEntry jarEntry = jarFile.getJarEntry(contentResource); assertNotNull(jarEntry); try (InputStream entryStream = jarFile.getInputStream(jarEntry)) { - String xformedContent = IOUtils.toString(entryStream, "utf-8"); + String xformedContent = IOUtils.toString(entryStream, StandardCharsets.UTF_8); // must be two lines, with our two classes. String[] classes = xformedContent.split("\r?\n"); boolean h1 = false; @@ -212,8 +203,6 @@ public void concatenation() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } }