From 584024bd71e60b2a0643e078920e280fb35c389b Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Jan 2025 20:24:27 +0800 Subject: [PATCH 1/5] Don't share relocators in ServiceResourceTransformerTest --- .../ServiceResourceTransformerTest.java | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) 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..dcb62e73 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 @@ -23,16 +23,13 @@ import java.io.FileOutputStream; 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.Test; @@ -46,13 +43,10 @@ public class ServiceResourceTransformerTest { private final String NEWLINE = "\n"; - private List relocators = new ArrayList(); - @Test public void relocatedClasses() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, Arrays.asList("org.foo.exclude.*")); - relocators.add(relocator); String content = "org.foo.Service\norg.foo.exclude.OtherService\n"; byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8); @@ -61,7 +55,7 @@ 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, Collections.singletonList(relocator), 0); contentStream.close(); File tempJar = File.createTempFile("shade.", ".jar"); @@ -89,7 +83,6 @@ public void relocatedClasses() throws Exception { public void mergeRelocatedFiles() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*")); - relocators.add(relocator); String content = "org.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE; String contentShaded = "borg.foo.Service" + NEWLINE + "org.foo.exclude.OtherService" + NEWLINE; @@ -100,11 +93,11 @@ 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, Collections.singletonList(relocator), 0); } try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) { - xformer.processResource(contentResourceShaded, contentStream, relocators, 0); + xformer.processResource(contentResourceShaded, contentStream, Collections.singletonList(relocator), 0); } File tempJar = File.createTempFile("shade.", ".jar"); @@ -131,7 +124,6 @@ public void mergeRelocatedFiles() throws Exception { @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"); @@ -139,7 +131,7 @@ public void concatanationAppliedMultipleTimes() throws Exception { String contentResource = "META-INF/services/org.osgi.framework.launch.FrameworkFactory"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, relocators, 0); + xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); contentStream.close(); File tempJar = File.createTempFile("shade.", ".jar"); @@ -166,7 +158,6 @@ public void concatanationAppliedMultipleTimes() throws Exception { @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 +165,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, Collections.singletonList(relocator), 0); contentStream.close(); content = "org.blah.Service\n"; @@ -182,7 +173,7 @@ 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, Collections.singletonList(relocator), 0); contentStream.close(); File tempJar = File.createTempFile("shade.", ".jar"); From 070a720e1ea713f916dbde302638bbfbaf54abe8 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Jan 2025 20:33:49 +0800 Subject: [PATCH 2/5] Set up and clean up tempJar --- .../ServiceResourceTransformerTest.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) 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 dcb62e73..d7d9c04c 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 @@ -22,6 +22,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.Collections; @@ -31,6 +32,8 @@ import org.apache.commons.io.IOUtils; import org.apache.maven.plugins.shade.relocation.SimpleRelocator; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -42,6 +45,17 @@ */ public class ServiceResourceTransformerTest { private final String NEWLINE = "\n"; + private File tempJar; + + @Before + public void setup() throws IOException { + tempJar = File.createTempFile("shade.", ".jar"); + } + + @After + public void cleanup() { + tempJar.delete(); + } @Test public void relocatedClasses() throws Exception { @@ -58,8 +72,6 @@ public void relocatedClasses() throws Exception { xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); try (JarOutputStream jos = new JarOutputStream(fos)) { xformer.modifyOutputStream(jos); @@ -74,8 +86,6 @@ public void relocatedClasses() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @@ -100,8 +110,6 @@ public void mergeRelocatedFiles() throws Exception { xformer.processResource(contentResourceShaded, contentStream, Collections.singletonList(relocator), 0); } - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); try (JarOutputStream jos = new JarOutputStream(fos)) { xformer.modifyOutputStream(jos); @@ -116,8 +124,6 @@ public void mergeRelocatedFiles() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @@ -134,8 +140,6 @@ public void concatanationAppliedMultipleTimes() throws Exception { xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); try (JarOutputStream jos = new JarOutputStream(fos)) { xformer.modifyOutputStream(jos); @@ -150,8 +154,6 @@ public void concatanationAppliedMultipleTimes() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } @@ -176,8 +178,6 @@ public void concatenation() throws Exception { xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); contentStream.close(); - File tempJar = File.createTempFile("shade.", ".jar"); - tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream(tempJar); try (JarOutputStream jos = new JarOutputStream(fos)) { xformer.modifyOutputStream(jos); @@ -203,8 +203,6 @@ public void concatenation() throws Exception { } finally { jarFile.close(); } - } finally { - tempJar.delete(); } } } From b0e68c2522a197b04a4653f7c4db74bc0823d388 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Jan 2025 20:34:16 +0800 Subject: [PATCH 3/5] Cleanups --- .../ServiceResourceTransformerTest.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) 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 d7d9c04c..f0bb3eda 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 @@ -24,8 +24,6 @@ import java.io.InputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; @@ -36,6 +34,7 @@ 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; @@ -54,13 +53,14 @@ public void setup() throws IOException { @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.*")); + 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); @@ -69,7 +69,7 @@ public void relocatedClasses() throws Exception { String contentResourceShaded = "META-INF/services/borg.foo.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); FileOutputStream fos = new FileOutputStream(tempJar); @@ -81,7 +81,7 @@ 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(); @@ -92,7 +92,7 @@ public void relocatedClasses() throws Exception { @Test public void mergeRelocatedFiles() throws Exception { SimpleRelocator relocator = - new SimpleRelocator("org.foo", "borg.foo", null, Collections.singletonList("org.foo.exclude.*")); + 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; @@ -103,11 +103,11 @@ public void mergeRelocatedFiles() throws Exception { ServicesResourceTransformer xformer = new ServicesResourceTransformer(); try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) { - xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); } try (InputStream contentStream = new ByteArrayInputStream(contentBytes)) { - xformer.processResource(contentResourceShaded, contentStream, Collections.singletonList(relocator), 0); + xformer.processResource(contentResourceShaded, contentStream, singletonList(relocator), 0); } FileOutputStream fos = new FileOutputStream(tempJar); @@ -132,12 +132,12 @@ public void concatanationAppliedMultipleTimes() throws Exception { SimpleRelocator relocator = new SimpleRelocator("org.eclipse", "org.eclipse1234", null, null); 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, Collections.singletonList(relocator), 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); FileOutputStream fos = new FileOutputStream(tempJar); @@ -167,7 +167,7 @@ public void concatenation() throws Exception { String contentResource = "META-INF/services/org.something.another"; ServicesResourceTransformer xformer = new ServicesResourceTransformer(); - xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); content = "org.blah.Service\n"; @@ -175,7 +175,7 @@ public void concatenation() throws Exception { contentStream = new ByteArrayInputStream(contentBytes); contentResource = "META-INF/services/org.something.another"; - xformer.processResource(contentResource, contentStream, Collections.singletonList(relocator), 0); + xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); FileOutputStream fos = new FileOutputStream(tempJar); @@ -187,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; From 4efee052ea72ce5edf1362e447bb6a98de4774a9 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Jan 2025 20:38:49 +0800 Subject: [PATCH 4/5] Try fos with resource but fos --- .../resource/ServiceResourceTransformerTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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 f0bb3eda..0c81c311 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 @@ -72,8 +72,8 @@ public void relocatedClasses() throws Exception { xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - 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(); @@ -110,8 +110,8 @@ public void mergeRelocatedFiles() throws Exception { xformer.processResource(contentResourceShaded, contentStream, singletonList(relocator), 0); } - 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(); @@ -140,8 +140,8 @@ public void concatanationAppliedMultipleTimes() throws Exception { xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - 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(); @@ -178,8 +178,8 @@ public void concatenation() throws Exception { xformer.processResource(contentResource, contentStream, singletonList(relocator), 0); contentStream.close(); - 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(); From 980a100b91202d6969ecea6481575ced50ffdc05 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 29 Jan 2025 20:46:33 +0800 Subject: [PATCH 5/5] Fix style --- .../plugins/shade/resource/ServiceResourceTransformerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0c81c311..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,8 +21,8 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.nio.charset.StandardCharsets; import java.util.jar.JarEntry; import java.util.jar.JarFile;