Skip to content

Commit 4f2b7b7

Browse files
Generate correct content of empty jars
- automatically generated empty jars had wrong content due to read output before stream close - ByteArrayOutputStream needn't be closed - add test for empty jar and empty plugin jar
1 parent 9de675b commit 4f2b7b7

File tree

4 files changed

+134
-38
lines changed

4 files changed

+134
-38
lines changed

mrm-servlet/src/main/java/org/codehaus/mojo/mrm/impl/Utils.java

+24-27
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,14 @@ else if ( content instanceof File )
8585
public static byte[] newEmptyJarContent()
8686
throws IOException
8787
{
88-
byte[] emptyJar;
89-
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
90-
final Manifest manifest = new Manifest();
91-
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
92-
manifest.getMainAttributes().putValue("Archiver-Version", "1.0");
93-
manifest.getMainAttributes().putValue("Created-By", "Mock Repository Maven Plugin");
94-
try (JarOutputStream jos = new JarOutputStream(bos, manifest)) {
95-
emptyJar = bos.toByteArray();
96-
return emptyJar;
97-
}
98-
}
88+
final Manifest manifest = new Manifest();
89+
manifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
90+
manifest.getMainAttributes().putValue( "Archiver-Version", "1.0" );
91+
manifest.getMainAttributes().putValue( "Created-By", "Mock Repository Maven Plugin" );
92+
93+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
94+
new JarOutputStream( bos, manifest ).close();
95+
return bos.toByteArray();
9996
}
10097

10198
/**
@@ -111,23 +108,23 @@ public static byte[] newEmptyJarContent()
111108
public static byte[] newEmptyMavenPluginJarContent( String groupId, String artifactId, String version )
112109
throws IOException
113110
{
114-
byte[] emptyJar;
115-
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
116-
final Manifest manifest = new Manifest();
117-
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
118-
manifest.getMainAttributes().putValue("Archiver-Version", "1.0");
119-
manifest.getMainAttributes().putValue("Created-By", "Mock Repository Maven Plugin");
120-
try (JarOutputStream jos = new JarOutputStream(bos, manifest)) {
121-
JarEntry entry = new JarEntry("META-INF/maven/plugin.xml");
122-
jos.putNextEntry(entry);
123-
jos.write(
124-
("<plugin><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>" + version
125-
+ "</version></plugin>").getBytes());
126-
jos.closeEntry();
127-
emptyJar = bos.toByteArray();
128-
return emptyJar;
129-
}
111+
final Manifest manifest = new Manifest();
112+
manifest.getMainAttributes().putValue( "Manifest-Version", "1.0" );
113+
manifest.getMainAttributes().putValue( "Archiver-Version", "1.0" );
114+
manifest.getMainAttributes().putValue( "Created-By", "Mock Repository Maven Plugin" );
115+
116+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
117+
try ( JarOutputStream jos = new JarOutputStream( bos, manifest ) )
118+
{
119+
JarEntry entry = new JarEntry( "META-INF/maven/plugin.xml" );
120+
jos.putNextEntry( entry );
121+
jos.write(
122+
( "<plugin><groupId>" + groupId + "</groupId><artifactId>" + artifactId + "</artifactId><version>"
123+
+ version
124+
+ "</version></plugin>" ).getBytes() );
125+
jos.closeEntry();
130126
}
127+
return bos.toByteArray();
131128
}
132129

133130
/**

mrm-servlet/src/test/java/org/codehaus/mojo/mrm/impl/maven/MockArtifactStoreTest.java

+97-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package org.codehaus.mojo.mrm.impl.maven;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.Assert.assertNotNull;
5-
import static org.junit.Assert.assertTrue;
6-
73
import java.io.File;
84
import java.io.FileInputStream;
5+
import java.io.InputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.StandardCopyOption;
98
import java.util.ArrayList;
9+
import java.util.Enumeration;
1010
import java.util.List;
1111
import java.util.jar.JarEntry;
12-
import java.util.jar.JarInputStream;
12+
import java.util.jar.JarFile;
13+
import java.util.jar.Manifest;
1314

1415
import org.apache.commons.io.IOUtils;
1516
import org.apache.maven.archetype.catalog.Archetype;
1617
import org.apache.maven.archetype.catalog.ArchetypeCatalog;
1718
import org.codehaus.mojo.mrm.api.maven.Artifact;
19+
import org.junit.Rule;
1820
import org.junit.Test;
21+
import org.junit.rules.TemporaryFolder;
22+
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertNotNull;
25+
import static org.junit.Assert.assertTrue;
1926

2027
public class MockArtifactStoreTest
2128
{
29+
@Rule
30+
public TemporaryFolder temporaryFolder= new TemporaryFolder();
2231

2332
// MMOCKRM-3
2433
@Test
@@ -70,22 +79,99 @@ public void testDirectoryContent() throws Exception
7079
assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/mrm-15/mrm-15-1.0.pom" ), artifactStore.get( pomArtifact ) ) );
7180

7281
Artifact mainArtifact = new Artifact( "localhost", "mrm-15", "1.0", "jar" );
73-
assertNotNull( artifactStore.get( mainArtifact ) );
82+
InputStream inputStreamJar = artifactStore.get( mainArtifact );
83+
assertNotNull( inputStreamJar );
7484

7585
List<String> names = new ArrayList<>();
7686

77-
try (JarInputStream jis = new JarInputStream( artifactStore.get( mainArtifact ) ) )
87+
File jarFile = temporaryFolder.newFile();
88+
Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
89+
90+
try (JarFile jar = new JarFile( jarFile ) )
7891
{
79-
JarEntry jarEntry;
80-
while ( ( jarEntry = jis.getNextJarEntry() ) != null )
92+
Enumeration<JarEntry> entries = jar.entries();
93+
while ( entries.hasMoreElements() )
8194
{
82-
names.add( jarEntry.getName() );
95+
JarEntry entry = entries.nextElement();
96+
names.add( entry.getName() );
8397
}
98+
Manifest manifest = jar.getManifest();
99+
assertNotNull( manifest );
100+
assertEquals( 2, manifest.getMainAttributes().size());
84101
}
85102

86103
assertTrue( names.contains( "README.txt" ) );
87104
}
88-
105+
106+
@Test
107+
public void testEmptyJarContent() throws Exception
108+
{
109+
MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-jar" ) );
110+
111+
Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "pom" );
112+
InputStream inputStreamPom = artifactStore.get( pomArtifact );
113+
assertNotNull( inputStreamPom );
114+
assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-jar/mrm-empty-jar-1.0.pom" ),
115+
inputStreamPom ) );
116+
117+
Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-jar", "1.0", "jar" );
118+
InputStream inputStreamJar = artifactStore.get( mainArtifact );
119+
assertNotNull( inputStreamJar );
120+
121+
File jarFile = temporaryFolder.newFile();
122+
Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
123+
124+
List<String> names = new ArrayList<>();
125+
try (JarFile jar = new JarFile( jarFile ) )
126+
{
127+
Enumeration<JarEntry> entries = jar.entries();
128+
while ( entries.hasMoreElements() )
129+
{
130+
JarEntry entry = entries.nextElement();
131+
names.add( entry.getName() );
132+
}
133+
Manifest manifest = jar.getManifest();
134+
assertNotNull( manifest );
135+
assertEquals( 3, manifest.getMainAttributes().size());
136+
}
137+
assertTrue( names.contains( "META-INF/MANIFEST.MF" ) );
138+
}
139+
140+
@Test
141+
public void testEmptyPluginJarContent() throws Exception
142+
{
143+
MockArtifactStore artifactStore = new MockArtifactStore( new File( "target/test-classes/empty-plugin-jar" ) );
144+
145+
Artifact pomArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "pom" );
146+
InputStream inputStreamPom = artifactStore.get( pomArtifact );
147+
assertNotNull( inputStreamPom );
148+
assertTrue( "Content equals", IOUtils.contentEquals( new FileInputStream( "target/test-classes/empty-plugin-jar/mrm-empty-plugin-jar-1.0.pom" ),
149+
inputStreamPom ) );
150+
151+
Artifact mainArtifact = new Artifact( "localhost", "mrm-empty-plugin-jar", "1.0", "jar" );
152+
InputStream inputStreamJar = artifactStore.get( mainArtifact );
153+
assertNotNull( inputStreamJar );
154+
155+
File jarFile = temporaryFolder.newFile();
156+
Files.copy( inputStreamJar, jarFile.toPath(), StandardCopyOption.REPLACE_EXISTING );
157+
158+
List<String> names = new ArrayList<>();
159+
try (JarFile jar = new JarFile( jarFile ) )
160+
{
161+
Enumeration<JarEntry> entries = jar.entries();
162+
while ( entries.hasMoreElements() )
163+
{
164+
JarEntry entry = entries.nextElement();
165+
names.add( entry.getName() );
166+
}
167+
Manifest manifest = jar.getManifest();
168+
assertNotNull( manifest );
169+
assertEquals( 3, manifest.getMainAttributes().size());
170+
}
171+
assertTrue( names.contains( "META-INF/MANIFEST.MF" ) );
172+
assertTrue( names.contains( "META-INF/maven/plugin.xml" ) );
173+
}
174+
89175
@Test
90176
public void testDirectoryWithClassifierContent() throws Exception
91177
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>mrm-empty-jar</artifactId>
5+
<version>1.0</version>
6+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>localhost</groupId>
4+
<artifactId>mrm-empty-plugin-jar</artifactId>
5+
<version>1.0</version>
6+
<packaging>maven-plugin</packaging>
7+
</project>

0 commit comments

Comments
 (0)