Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate correct content of empty jars #112

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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(
("<plugin><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version
+ "</version></plugin>").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(
( "<plugin><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>"
+ version
+ "</version></plugin>" ).getBytes() );
jos.closeEntry();
}
return bos.toByteArray();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<String> 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<JarEntry> 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<String> names = new ArrayList<>();
try (JarFile jar = new JarFile( jarFile ) )
{
Enumeration<JarEntry> 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<String> names = new ArrayList<>();
try (JarFile jar = new JarFile( jarFile ) )
{
Enumeration<JarEntry> 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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>mrm-empty-jar</artifactId>
<version>1.0</version>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>localhost</groupId>
<artifactId>mrm-empty-plugin-jar</artifactId>
<version>1.0</version>
<packaging>maven-plugin</packaging>
</project>