Skip to content

Commit 653bbbd

Browse files
author
jdcasey
committed
Fixing jar indexing, and adding a unit test.
Submitted By: Richard van der Hoff
1 parent bd834a4 commit 653bbbd

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
<contributor>
1313
<name>Dan Tran</name>
1414
</contributor>
15+
<contributor>
16+
<name>Richard van der Hoff</name>
17+
</contributor>
1518
</contributors>
1619
<dependencies>
1720
<dependency>

src/main/java/org/codehaus/plexus/archiver/jar/JarArchiver.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,15 +464,16 @@ private void createIndexList( ZipOutputStream zOut )
464464
cpEntries[ c++ ] = tok.nextToken();
465465
}
466466
}
467-
String[] indexJarEntries = (String[]) indexJars.toArray();
468-
for ( int i = 0; i < indexJarEntries.length; i++ )
467+
468+
for ( Iterator i = indexJars.iterator(); i.hasNext(); )
469469
{
470-
String name = findJarName( indexJarEntries[ i ], cpEntries );
470+
String indexJar = (String)i.next();
471+
String name = findJarName( indexJar, cpEntries );
471472
if ( name != null )
472473
{
473474
ArrayList dirs = new ArrayList();
474475
ArrayList files = new ArrayList();
475-
grabFilesAndDirs( indexJarEntries[ i ], dirs, files );
476+
grabFilesAndDirs( indexJar, dirs, files );
476477
if ( dirs.size() + files.size() > 0 )
477478
{
478479
writer.println( name );
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.codehaus.plexus.archiver.jar;
2+
3+
/*
4+
* Copyright 2006 The Apache Software Foundation
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
20+
import java.io.BufferedInputStream;
21+
22+
import org.codehaus.plexus.PlexusTestCase;
23+
import org.codehaus.plexus.archiver.Archiver;
24+
import org.codehaus.plexus.archiver.zip.ZipEntry;
25+
import org.codehaus.plexus.archiver.zip.ZipFile;
26+
27+
/**
28+
* @author Richard van der Hoff <[email protected]>
29+
* @version $Id$
30+
*/
31+
public class IndexTest extends PlexusTestCase {
32+
public void testCreateArchiveWithIndexedJars()
33+
throws Exception
34+
{
35+
/* create a dummy jar */
36+
JarArchiver archiver1 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
37+
archiver1.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), "one.txt" );
38+
archiver1.setDestFile( getTestFile( "target/output/archive1.jar" ) );
39+
archiver1.createArchive();
40+
41+
/* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first jar.
42+
*/
43+
Manifest m = new Manifest();
44+
Manifest.Attribute classpathAttr = new Manifest.Attribute( "Class-Path", "archive1.jar" );
45+
m.addConfiguredAttribute( classpathAttr );
46+
47+
JarArchiver archiver2 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
48+
archiver2.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), "two.txt" );
49+
archiver2.setIndex(true);
50+
archiver2.addConfiguredIndexJars(archiver1.getDestFile());
51+
archiver2.setDestFile( getTestFile( "target/output/archive2.jar" ) );
52+
archiver2.addConfiguredManifest(m);
53+
archiver2.createArchive();
54+
55+
// read the index file back and check it looks like it ought to
56+
ZipFile zf = new ZipFile( archiver2.getDestFile() );
57+
ZipEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
58+
assertNotNull(indexEntry);
59+
BufferedInputStream bis = new BufferedInputStream(zf.getInputStream(indexEntry));
60+
61+
byte buf[] = new byte[1024];
62+
int i = bis.read(buf);
63+
String res = new String(buf,0,i);
64+
assertEquals("JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n", res);
65+
}
66+
}

0 commit comments

Comments
 (0)