View Javadoc
1   /*
2    * Copyright  2006 The Apache Software Foundation
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *
16   */
17  package org.codehaus.plexus.archiver.jar;
18  
19  import java.io.InputStream;
20  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
21  import org.codehaus.plexus.PlexusTestCase;
22  import org.codehaus.plexus.archiver.Archiver;
23  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
24  
25  /**
26   * @author Richard van der Hoff <richardv@mxtelecom.com>
27   */
28  public class IndexTest extends PlexusTestCase
29  {
30  
31      public void testCreateArchiveWithIndexedJars()
32          throws Exception
33      {
34          /* create a dummy jar */
35          JarArchiver archiver1 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
36          archiver1.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), "one.txt" );
37          archiver1.setDestFile( getTestFile( "target/output/archive1.jar" ) );
38          archiver1.createArchive();
39  
40          /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first jar.
41           */
42          Manifest m = new Manifest();
43          Manifest.Attribute classpathAttr = new Manifest.Attribute( "Class-Path", "archive1.jar" );
44          m.addConfiguredAttribute( classpathAttr );
45  
46          JarArchiver archiver2 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
47          archiver2.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), "two.txt" );
48          archiver2.setIndex( true );
49          archiver2.addConfiguredIndexJars( archiver1.getDestFile() );
50          archiver2.setDestFile( getTestFile( "target/output/archive2.jar" ) );
51          archiver2.addConfiguredManifest( m );
52          archiver2.createArchive();
53  
54          // read the index file back and check it looks like it ought to
55          org.apache.commons.compress.archivers.zip.ZipFile zf =
56              new org.apache.commons.compress.archivers.zip.ZipFile( archiver2.getDestFile() );
57  
58          ZipArchiveEntry indexEntry = zf.getEntry( "META-INF/INDEX.LIST" );
59          assertNotNull( indexEntry );
60          InputStream bis = bufferedInputStream( zf.getInputStream( indexEntry ) );
61  
62          byte buf[] = new byte[ 1024 ];
63          int i = bis.read( buf );
64          String res = new String( buf, 0, i );
65          assertEquals( "JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n",
66                        res.replaceAll( "\r\n", "\n" ) );
67  
68      }
69  
70      /**
71       * this is pretty much a duplicate of testCreateArchiveWithIndexedJars(), but adds some extra
72       * tests for files in META-INF
73       */
74      public void testCreateArchiveWithIndexedJarsAndMetaInf()
75          throws Exception
76      {
77          /* create a dummy jar */
78          JarArchiver archiver1 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
79          archiver1.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), "one.txt" );
80  
81          // add a file in the META-INF directory, as this previously didn't make it into the index
82          archiver1.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), "META-INF/foo" );
83          archiver1.setDestFile( getTestFile( "target/output/archive1.jar" ) );
84          archiver1.createArchive();
85  
86          /* create another dummy jar, with an index but nothing else in META-INF. Also checks non-leaf files. */
87          JarArchiver archiver3 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
88          archiver3.addFile( getTestFile( "src/test/resources/manifests/manifest1.mf" ), "org/apache/maven/one.txt" );
89          archiver3.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), "META-INF/INDEX.LIST" );
90          archiver3.setDestFile( getTestFile( "target/output/archive3.jar" ) );
91          archiver3.createArchive();
92  
93          /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first two jars.
94           */
95          Manifest m = new Manifest();
96          Manifest.Attribute classpathAttr = new Manifest.Attribute( "Class-Path", "archive1.jar archive3.jar" );
97          m.addConfiguredAttribute( classpathAttr );
98  
99          JarArchiver archiver2 = (JarArchiver) lookup( Archiver.ROLE, "jar" );
100         archiver2.addFile( getTestFile( "src/test/resources/manifests/manifest2.mf" ), "two.txt" );
101         archiver2.setIndex( true );
102         archiver2.addConfiguredIndexJars( archiver1.getDestFile() );
103         archiver2.addConfiguredIndexJars( archiver3.getDestFile() );
104         archiver2.setDestFile( getTestFile( "target/output/archive2.jar" ) );
105         archiver2.addConfiguredManifest( m );
106         archiver2.createArchive();
107 
108         // read the index file back and check it looks like it ought to
109         org.apache.commons.compress.archivers.zip.ZipFile zf =
110             new org.apache.commons.compress.archivers.zip.ZipFile( archiver2.getDestFile() );
111 
112         ZipArchiveEntry indexEntry = zf.getEntry( "META-INF/INDEX.LIST" );
113         assertNotNull( indexEntry );
114         InputStream bis = bufferedInputStream( zf.getInputStream( indexEntry ) );
115 
116         byte buf[] = new byte[ 1024 ];
117         int i = bis.read( buf );
118         String res = new String( buf, 0, i );
119         //System.out.println(res);
120 
121         assertEquals( "JarIndex-Version: 1.0\n\n" + "archive2.jar\ntwo.txt\n\n" + "archive1.jar\nMETA-INF\none.txt\n\n"
122                           + "archive3.jar\norg\norg/apache\norg/apache/maven\n\n", res.replaceAll( "\r\n", "\n" ) );
123     }
124 
125 }