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                  org.apache.commons.compress.archivers.zip.ZipFile.builder()
60                          .setFile(archiver2.getDestFile())
61                          .get();
62  
63          ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
64          assertNotNull(indexEntry);
65          InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));
66  
67          byte[] buf = new byte[1024];
68          int i = bis.read(buf);
69          String res = new String(buf, 0, i);
70          assertEquals(
71                  "JarIndex-Version: 1.0\n\narchive2.jar\ntwo.txt\n\narchive1.jar\none.txt\n\n",
72                  res.replaceAll("\r\n", "\n"));
73      }
74  
75      /**
76       * this is pretty much a duplicate of testCreateArchiveWithIndexedJars(), but adds some extra
77       * tests for files in META-INF
78       */
79      @Test
80      void testCreateArchiveWithIndexedJarsAndMetaInf() throws Exception {
81          /* create a dummy jar */
82          JarArchiver archiver1 = (JarArchiver) lookup(Archiver.class, "jar");
83          archiver1.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "one.txt");
84  
85          // add a file in the META-INF directory, as this previously didn't make it into the index
86          archiver1.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/foo");
87          archiver1.setDestFile(getTestFile("target/output/archive1.jar"));
88          archiver1.createArchive();
89  
90          /* create another dummy jar, with an index but nothing else in META-INF. Also checks non-leaf files. */
91          JarArchiver archiver3 = (JarArchiver) lookup(Archiver.class, "jar");
92          archiver3.addFile(getTestFile("src/test/resources/manifests/manifest1.mf"), "org/apache/maven/one.txt");
93          archiver3.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "META-INF/INDEX.LIST");
94          archiver3.setDestFile(getTestFile("target/output/archive3.jar"));
95          archiver3.createArchive();
96  
97          /* now create another jar, with an index, and whose manifest includes a Class-Path entry for the first two jars.
98           */
99          Manifest m = new Manifest();
100         Manifest.Attribute classpathAttr = new Manifest.Attribute("Class-Path", "archive1.jar archive3.jar");
101         m.addConfiguredAttribute(classpathAttr);
102 
103         JarArchiver archiver2 = (JarArchiver) lookup(Archiver.class, "jar");
104         archiver2.addFile(getTestFile("src/test/resources/manifests/manifest2.mf"), "two.txt");
105         archiver2.setIndex(true);
106         archiver2.addConfiguredIndexJars(archiver1.getDestFile());
107         archiver2.addConfiguredIndexJars(archiver3.getDestFile());
108         archiver2.setDestFile(getTestFile("target/output/archive2.jar"));
109         archiver2.addConfiguredManifest(m);
110         archiver2.createArchive();
111 
112         // read the index file back and check it looks like it ought to
113         org.apache.commons.compress.archivers.zip.ZipFile zf =
114                 org.apache.commons.compress.archivers.zip.ZipFile.builder()
115                         .setFile(archiver2.getDestFile())
116                         .get();
117 
118         ZipArchiveEntry indexEntry = zf.getEntry("META-INF/INDEX.LIST");
119         assertNotNull(indexEntry);
120         InputStream bis = bufferedInputStream(zf.getInputStream(indexEntry));
121 
122         byte[] buf = new byte[1024];
123         int i = bis.read(buf);
124         String res = new String(buf, 0, i);
125         // System.out.println(res);
126 
127         assertEquals(
128                 "JarIndex-Version: 1.0\n\n" + "archive2.jar\ntwo.txt\n\n" + "archive1.jar\nMETA-INF\none.txt\n\n"
129                         + "archive3.jar\norg\norg/apache\norg/apache/maven\n\n",
130                 res.replaceAll("\r\n", "\n"));
131     }
132 }