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  
21  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
22  import org.codehaus.plexus.archiver.Archiver;
23  import org.codehaus.plexus.archiver.TestSupport;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.codehaus.plexus.archiver.util.Streams.bufferedInputStream;
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertNotNull;
29  
30  /**
31   * @author <a href="mailto:richardv@mxtelecom.com">Richard van der Hoff</a>
32   */
33  class IndexTest extends TestSupport {
34  
35      @Test
36      void testCreateArchiveWithIndexedJars() throws Exception {
37          /* create a dummy jar */
38          JarArchiver archiver1 = (JarArchiver) lookup(Archiver.class, "jar");
39          archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
40          archiver1.setDestFile(getTestFile("target/output/archive1.jar"));
41          archiver1.createArchive();
42  
43          /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first jar.
44           */
45          Manifest m = new Manifest();
46          Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar");
47          m.addConfiguredAttribute(classpathAttr);
48  
49          JarArchiver archiver2 = (JarArchiver) lookup(Archiver.class, "jar");
50          archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt");
51          archiver2.setIndex(true);
52          archiver2.addConfiguredIndexJars(archiver1.getDestFile());
53          archiver2.setDestFile(getTestFile("target/output/archive2.jar"));
54          archiver2.addConfiguredManifest(m);
55          archiver2.createArchive();
56  
57          // read the index file back and check it looks like it ought to
58          org.apache.commons.compress.archivers.zip.ZipFile zf =
59                  new org.apache.commons.compress.archivers.zip.ZipFile(archiver2.getDestFile());
60  
61          ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
62          assertNotNull(indexEntry);
63          InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));
64  
65          byte buf[] = new byte[1024];
66          int i = bis.read(buf);
67          String res = new String(buf, 0, i);
68          assertEquals(
69                  "JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n",
70                  res.replaceAll("\r\n", "\n"));
71      }
72  
73      /**
74       * this is pretty much a duplicate of testCreateArchiveWithIndexedJars(), but adds some extra
75       * tests for files in META-INF
76       */
77      @Test
78      void testCreateArchiveWithIndexedJarsAndMetaInf() throws Exception {
79          /* create a dummy jar */
80          JarArchiver archiver1 = (JarArchiver) lookup(Archiver.class, "jar");
81          archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
82  
83          // add a file in the META-INF directory, as this previously didn't make it into the index
84          archiver1.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/foo");
85          archiver1.setDestFile(getTestFile("target/output/archive1.jar"));
86          archiver1.createArchive();
87  
88          /* create another dummy jar, with an index but nothing else in META-INF. Also checks non-leaf files. */
89          JarArchiver archiver3 = (JarArchiver) lookup(Archiver.class, "jar");
90          archiver3.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "org/apache/maven/one.txt");
91          archiver3.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/INDEX.LIST");
92          archiver3.setDestFile(getTestFile("target/output/archive3.jar"));
93          archiver3.createArchive();
94  
95          /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first two jars.
96           */
97          Manifest m = new Manifest();
98          Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar archive3.jar");
99          m.addConfiguredAttribute(classpathAttr);
100 
101         JarArchiver archiver2 = (JarArchiver) lookup(Archiver.class, "jar");
102         archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt");
103         archiver2.setIndex(true);
104         archiver2.addConfiguredIndexJars(archiver1.getDestFile());
105         archiver2.addConfiguredIndexJars(archiver3.getDestFile());
106         archiver2.setDestFile(getTestFile("target/output/archive2.jar"));
107         archiver2.addConfiguredManifest(m);
108         archiver2.createArchive();
109 
110         // read the index file back and check it looks like it ought to
111         org.apache.commons.compress.archivers.zip.ZipFile zf =
112                 new org.apache.commons.compress.archivers.zip.ZipFile(archiver2.getDestFile());
113 
114         ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
115         assertNotNull(indexEntry);
116         InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));
117 
118         byte buf[] = new byte[1024];
119         int i = bis.read(buf);
120         String res = new String(buf, 0, i);
121         // System.out.println(res);
122 
123         assertEquals(
124                 "JarIndex-Version: 1.0\n\n" + "archive2.jar\ntwo.txt\n\n" + "archive1.jar\nMETA-INF\none.txt\n\n"
125                         + "archive3.jar\norg\norg/apache\norg/apache/maven\n\n",
126                 res.replaceAll("\r\n", "\n"));
127     }
128 }