diff --git a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java index bbb55fde..7e5d3a64 100644 --- a/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java +++ b/src/main/java/org/apache/maven/plugins/shade/resource/ServicesResourceTransformer.java @@ -71,8 +71,6 @@ public void processResource( String resource, InputStream is, final List c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" ); - Object o = c.newInstance(); - assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) ); + try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ) ) { + Class c = cl.loadClass( "hidden.org.apache.maven.plugins.shade.Lib" ); + Object o = c.newInstance(); + assertEquals( "foo.bar/baz", c.getDeclaredField( "CONSTANT" ).get( o ) ); + } } public void testShaderWithCustomShadedPattern() @@ -211,25 +212,26 @@ public void testShaderWithRelocatedClassname() s.shade( shadeRequest ); - URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ); - Class c = cl.loadClass( "_plexus.util.__StringUtils" ); - // first, ensure it works: - Object o = c.newInstance(); - assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null ) ); - - // now, check that its source file was rewritten: - final String[] source = { null }; - final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class" ) ); - classReader.accept( new ClassVisitor( Opcodes.ASM4 ) - { + try ( URLClassLoader cl = new URLClassLoader( new URL[] { file.toURI().toURL() } ) ) { + Class c = cl.loadClass( "_plexus.util.__StringUtils" ); + // first, ensure it works: + Object o = c.newInstance(); + assertEquals( "", c.getMethod( "clean", String.class ).invoke( o, (String) null ) ); + + // now, check that its source file was rewritten: + final String[] source = { null }; + final ClassReader classReader = new ClassReader( cl.getResourceAsStream( "_plexus/util/__StringUtils.class" ) ); + classReader.accept( new ClassVisitor( Opcodes.ASM4 ) + { @Override public void visitSource( String arg0, String arg1 ) { super.visitSource( arg0, arg1 ); source[0] = arg0; } - }, ClassReader.SKIP_CODE ); - assertEquals( "__StringUtils.java", source[0] ); + }, ClassReader.SKIP_CODE ); + assertEquals( "__StringUtils.java", source[0] ); + } } private void shaderWithPattern( String shadedPattern, File jar, String[] excludes ) diff --git a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java index 208bac5f..d5020acf 100644 --- a/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/shade/mojo/ShadeMojoTest.java @@ -99,14 +99,15 @@ public void testShaderWithExclusions() s.shade( shadeRequest ); - ClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } ); - Class c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" ); - - Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" ); - assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) ); - - Method method = c.getDeclaredMethod( "getClassRealmPackageImport" ); - assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) ); + try ( URLClassLoader cl = new URLClassLoader( new URL[]{ jarFile.toURI().toURL() } ) ) { + Class c = cl.loadClass( "org.apache.maven.plugins.shade.Lib" ); + + Field field = c.getDeclaredField( "CLASS_REALM_PACKAGE_IMPORT" ); + assertEquals( "org.codehaus.plexus.util.xml.pull", field.get( null ) ); + + Method method = c.getDeclaredMethod( "getClassRealmPackageImport" ); + assertEquals( "org.codehaus.plexus.util.xml.pull", method.invoke( null ) ); + } } /** 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 3bef2f98..b87252cc 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 @@ -66,28 +66,21 @@ public void relocatedClasses() throws Exception { File tempJar = File.createTempFile("shade.", ".jar"); tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream( tempJar ); - JarOutputStream jos = new JarOutputStream( fos ); - try { + try ( JarOutputStream jos = new JarOutputStream( fos ) ) { xformer.modifyOutputStream( jos ); jos.close(); - jos = null; + JarFile jarFile = new JarFile( tempJar ); JarEntry jarEntry = jarFile.getJarEntry( contentResourceShaded ); assertNotNull( jarEntry ); - InputStream entryStream = jarFile.getInputStream( jarEntry ); - try { + try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) { String xformedContent = IOUtils.toString( entryStream, "utf-8" ); assertEquals( "borg.foo.Service" + System.getProperty( "line.separator" ) + "org.foo.exclude.OtherService" + System.getProperty( "line.separator" ), xformedContent ); } finally { - IOUtils.closeQuietly( entryStream ); jarFile.close(); } } finally { - if (jos != null) - { - IOUtils.closeQuietly( jos ); - } tempJar.delete(); } } @@ -110,28 +103,20 @@ public void concatanationAppliedMultipleTimes() throws Exception { File tempJar = File.createTempFile("shade.", ".jar"); tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream( tempJar ); - JarOutputStream jos = new JarOutputStream( fos ); - try { + try ( JarOutputStream jos = new JarOutputStream( fos ) ) { xformer.modifyOutputStream( jos ); jos.close(); - jos = null; + JarFile jarFile = new JarFile( tempJar ); JarEntry jarEntry = jarFile.getJarEntry( contentResource ); assertNotNull( jarEntry ); - InputStream entryStream = jarFile.getInputStream( jarEntry ); - try { + try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) { String xformedContent = IOUtils.toString(entryStream, "utf-8"); assertEquals( "org.eclipse1234.osgi.launch.EquinoxFactory" + System.getProperty( "line.separator" ), xformedContent ); - } finally { - IOUtils.closeQuietly( entryStream ); jarFile.close(); } } finally { - if (jos != null) - { - IOUtils.closeQuietly( jos ); - } tempJar.delete(); } } @@ -161,16 +146,14 @@ public void concatenation() throws Exception { File tempJar = File.createTempFile("shade.", ".jar"); tempJar.deleteOnExit(); FileOutputStream fos = new FileOutputStream( tempJar ); - JarOutputStream jos = new JarOutputStream( fos ); - try { + try ( JarOutputStream jos = new JarOutputStream( fos ) ) { xformer.modifyOutputStream( jos ); jos.close(); - jos = null; + JarFile jarFile = new JarFile( tempJar ); JarEntry jarEntry = jarFile.getJarEntry( contentResource ); assertNotNull( jarEntry ); - InputStream entryStream = jarFile.getInputStream( jarEntry ); - try { + try ( InputStream entryStream = jarFile.getInputStream( jarEntry ) ) { String xformedContent = IOUtils.toString(entryStream, "utf-8"); // must be two lines, with our two classes. String[] classes = xformedContent.split("\r?\n"); @@ -189,14 +172,9 @@ else if ("borg.foo.Service".equals( name )) } assertTrue( h1 && h2 ); } finally { - IOUtils.closeQuietly( entryStream ); jarFile.close(); } } finally { - if (jos != null) - { - IOUtils.closeQuietly( jos ); - } tempJar.delete(); } } diff --git a/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java b/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java index f4f76521..1eaf2b1b 100644 --- a/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java +++ b/src/test/java/org/apache/maven/plugins/shade/resource/rule/TransformerTesterRule.java @@ -21,7 +21,6 @@ import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail;