diff --git a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java index 531e7a1f..d14b3253 100644 --- a/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java +++ b/mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java @@ -85,17 +85,14 @@ else if ( content instanceof File ) public static byte[] newEmptyJarContent() throws IOException { - byte[] emptyJar; - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - final Manifest manifest = new Manifest(); - manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); - manifest.getMainAttributes().putValue("Archiver-Version", "1.0"); - manifest.getMainAttributes().putValue("Created-By", "Mock Repository Maven Plugin"); - try (JarOutputStream jos = new JarOutputStream(bos, manifest)) { - emptyJar = bos.toByteArray(); - return emptyJar; - } - } + final Manifest manifest = new Manifest(); + manifest.getMainAttributes().putValue( "Manifest-Version", "1.0" ); + manifest.getMainAttributes().putValue( "Archiver-Version", "1.0" ); + manifest.getMainAttributes().putValue( "Created-By", "Mock Repository Maven Plugin" ); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + new JarOutputStream( bos, manifest ).close(); + return bos.toByteArray(); } /** @@ -111,23 +108,23 @@ public static byte[] newEmptyJarContent() public static byte[] newEmptyMavenPluginJarContent( String groupId, String artifactId, String version ) throws IOException { - byte[] emptyJar; - try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - final Manifest manifest = new Manifest(); - manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); - manifest.getMainAttributes().putValue("Archiver-Version", "1.0"); - manifest.getMainAttributes().putValue("Created-By", "Mock Repository Maven Plugin"); - try (JarOutputStream jos = new JarOutputStream(bos, manifest)) { - JarEntry entry = new JarEntry("META-INF/maven/plugin.xml"); - jos.putNextEntry(entry); - jos.write( - ("" + groupId + "" + artifactId + "" + version - + "").getBytes()); - jos.closeEntry(); - emptyJar = bos.toByteArray(); - return emptyJar; - } + final Manifest manifest = new Manifest(); + manifest.getMainAttributes().putValue( "Manifest-Version", "1.0" ); + manifest.getMainAttributes().putValue( "Archiver-Version", "1.0" ); + manifest.getMainAttributes().putValue( "Created-By", "Mock Repository Maven Plugin" ); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try ( JarOutputStream jos = new JarOutputStream( bos, manifest ) ) + { + JarEntry entry = new JarEntry( "META-INF/maven/plugin.xml" ); + jos.putNextEntry( entry ); + jos.write( + ( "" + groupId + "" + artifactId + "" + + version + + "" ).getBytes() ); + jos.closeEntry(); } + return bos.toByteArray(); } /** diff --git a/mrm-servlet/src/test/java/org/codehaus/mojo/mrm/impl/maven/MockArtifactStoreTest.java b/mrm-servlet/src/test/java/org/codehaus/mojo/mrm/impl/maven/MockArtifactStoreTest.java index 60bc4143..57740ae0 100644 --- a/mrm-servlet/src/test/java/org/codehaus/mojo/mrm/impl/maven/MockArtifactStoreTest.java +++ b/mrm-servlet/src/test/java/org/codehaus/mojo/mrm/impl/maven/MockArtifactStoreTest.java @@ -1,24 +1,33 @@ package org.codehaus.mojo.mrm.impl.maven; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; +import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; -import java.util.jar.JarInputStream; +import java.util.jar.JarFile; +import java.util.jar.Manifest; import org.apache.commons.io.IOUtils; import org.apache.maven.archetype.catalog.Archetype; import org.apache.maven.archetype.catalog.ArchetypeCatalog; import org.codehaus.mojo.mrm.api.maven.Artifact; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; public class MockArtifactStoreTest { + @Rule + public TemporaryFolder temporaryFolder= new TemporaryFolder(); // MMOCKRM-3 @Test @@ -70,22 +79,99 @@ public void testDirectoryContent() throws Exception assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/mrm-15/mrm-15-1.0.pom" ), artifactStore.get( pomArtifact ) ) ); Artifact mainArtifact = new Artifact( "localhost", "mrm-15", "1.0", "jar" ); - assertNotNull( artifactStore.get( mainArtifact ) ); + InputStream inputStreamJar = artifactStore.get( mainArtifact ); + assertNotNull( inputStreamJar ); List names = new ArrayList<>(); - try (JarInputStream jis = new JarInputStream( artifactStore.get( mainArtifact ) ) ) + File jarFile = temporaryFolder.newFile(); + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); + + try (JarFile jar = new JarFile( jarFile ) ) { - JarEntry jarEntry; - while ( ( jarEntry = jis.getNextJarEntry() ) != null ) + Enumeration entries = jar.entries(); + while ( entries.hasMoreElements() ) { - names.add( jarEntry.getName() ); + JarEntry entry = entries.nextElement(); + names.add( entry.getName() ); } + Manifest manifest = jar.getManifest(); + assertNotNull( manifest ); + assertEquals( 2, manifest.getMainAttributes().size()); } assertTrue( names.contains( "README.txt" ) ); } - + + @Test + public void testEmptyJarContent() throws Exception + { + MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-jar" ) ); + + Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "pom" ); + InputStream inputStreamPom = artifactStore.get( pomArtifact ); + assertNotNull( inputStreamPom ); + assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-jar/mrm-empty-jar-1.0.pom" ), + inputStreamPom ) ); + + Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "jar" ); + InputStream inputStreamJar = artifactStore.get( mainArtifact ); + assertNotNull( inputStreamJar ); + + File jarFile = temporaryFolder.newFile(); + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); + + List names = new ArrayList<>(); + try (JarFile jar = new JarFile( jarFile ) ) + { + Enumeration entries = jar.entries(); + while ( entries.hasMoreElements() ) + { + JarEntry entry = entries.nextElement(); + names.add( entry.getName() ); + } + Manifest manifest = jar.getManifest(); + assertNotNull( manifest ); + assertEquals( 3, manifest.getMainAttributes().size()); + } + assertTrue( names.contains( "META-INF/MANIFEST.MF" ) ); + } + + @Test + public void testEmptyPluginJarContent() throws Exception + { + MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-plugin-jar" ) ); + + Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "pom" ); + InputStream inputStreamPom = artifactStore.get( pomArtifact ); + assertNotNull( inputStreamPom ); + assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom" ), + inputStreamPom ) ); + + Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "jar" ); + InputStream inputStreamJar = artifactStore.get( mainArtifact ); + assertNotNull( inputStreamJar ); + + File jarFile = temporaryFolder.newFile(); + Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING ); + + List names = new ArrayList<>(); + try (JarFile jar = new JarFile( jarFile ) ) + { + Enumeration entries = jar.entries(); + while ( entries.hasMoreElements() ) + { + JarEntry entry = entries.nextElement(); + names.add( entry.getName() ); + } + Manifest manifest = jar.getManifest(); + assertNotNull( manifest ); + assertEquals( 3, manifest.getMainAttributes().size()); + } + assertTrue( names.contains( "META-INF/MANIFEST.MF" ) ); + assertTrue( names.contains( "META-INF/maven/plugin.xml" ) ); + } + @Test public void testDirectoryWithClassifierContent() throws Exception { diff --git a/mrm-servlet/src/test/resources/empty-jar/mrm-empty-jar-1.0.pom b/mrm-servlet/src/test/resources/empty-jar/mrm-empty-jar-1.0.pom new file mode 100644 index 00000000..12c2e9c5 --- /dev/null +++ b/mrm-servlet/src/test/resources/empty-jar/mrm-empty-jar-1.0.pom @@ -0,0 +1,6 @@ + + 4.0.0 + localhost + mrm-empty-jar + 1.0 + diff --git a/mrm-servlet/src/test/resources/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom b/mrm-servlet/src/test/resources/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom new file mode 100644 index 00000000..fa0077be --- /dev/null +++ b/mrm-servlet/src/test/resources/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom @@ -0,0 +1,7 @@ + + 4.0.0 + localhost + mrm-empty-plugin-jar + 1.0 + maven-plugin +