|
| 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