diff --git a/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicateClasses.java b/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicateClasses.java index 70504c22..a527e8e7 100644 --- a/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicateClasses.java +++ b/src/main/java/org/apache/maven/plugins/enforcer/BanDuplicateClasses.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -151,7 +152,8 @@ else if ( file.isDirectory() ) for ( String name : FileUtils.getFileNames( file, null, null, false ) ) { getLog().debug( " " + name ); - checkAndAddName( o, name, classesSeen, duplicateClassNames, ignorableDependencies ); + checkAndAddName( o, name, () -> Files.newInputStream( file.toPath().resolve( name ) ), + classesSeen, duplicateClassNames, ignorableDependencies ); } } catch ( IOException e ) @@ -170,7 +172,9 @@ else if ( isJarFile( o ) ) for ( JarEntry entry : Collections.list( jar.entries() ) ) { String fileName = entry.getName(); - checkAndAddName( o, fileName, classesSeen, duplicateClassNames, ignorableDependencies ); + + checkAndAddName( o, fileName, () -> jar.getInputStream( entry ), + classesSeen, duplicateClassNames, ignorableDependencies ); } } } @@ -220,10 +224,10 @@ else if ( isJarFile( o ) ) } - private void checkAndAddName( Artifact artifact, String pathToClassFile, Map classesSeen, Set duplicateClasses, Collection ignores ) - throws EnforcerRuleException + throws EnforcerRuleException, IOException { if ( !pathToClassFile.endsWith( ".class" ) ) { @@ -244,7 +248,8 @@ private void checkAndAddName( Artifact artifact, String pathToClassFile, Map Files.newInputStream( tempDirectory.toPath().resolve( pathToClassFile ) ) ); } private File createTempDirectory( String uniqueIdStr ) diff --git a/src/test/java/org/apache/maven/plugins/enforcer/HasherTest.java b/src/test/java/org/apache/maven/plugins/enforcer/HasherTest.java deleted file mode 100644 index 7abf0318..00000000 --- a/src/test/java/org/apache/maven/plugins/enforcer/HasherTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package org.apache.maven.plugins.enforcer; - -import org.apache.maven.artifact.Artifact; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.junit.rules.TestName; - -import static org.junit.Assert.assertEquals; - -public class HasherTest -{ - private static final String PATH_TO_CLASS_FILE = HasherTest.class.getName().replace( '.', '/' ) + ".class"; - - @Rule - public TemporaryFolder temporaryFolder = new TemporaryFolder(); - - @Rule - public final TestName testName = new TestName(); - - private ClassFileHelper classFileHelper; - - @Before - public void beforeEachTest() - { - classFileHelper = new ClassFileHelper( temporaryFolder ); - } - - @Test - public void generateHashReturnsCorrectHashForFileInDirectory() throws Exception - { - ClassFile classFile = classFileHelper.createWithContent( PATH_TO_CLASS_FILE, "this is the file's contents" ); - Artifact artifact = classFile.getArtifactThisClassWasFoundIn(); - Hasher hasher = new Hasher( PATH_TO_CLASS_FILE ); - - String hash = hasher.generateHash( artifact ); - - assertEquals( "ae23844bc5db9bfad3fbbe5426d89dd3", hash ); - } - - @Test - public void generateHashReturnsCorrectHashForFileInJar() throws Exception - { - ClassFile classFile = classFileHelper.createJarWithContent( "temp.jar", PATH_TO_CLASS_FILE, "this is the file's contents" ); - Artifact artifact = classFile.getArtifactThisClassWasFoundIn(); - Hasher hasher = new Hasher( PATH_TO_CLASS_FILE ); - - String hash = hasher.generateHash( artifact ); - - assertEquals( "ae23844bc5db9bfad3fbbe5426d89dd3", hash ); - } -}