View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.archive;
20  
21  import java.io.BufferedReader;
22  import java.io.File;
23  import java.io.InputStreamReader;
24  import java.io.StringWriter;
25  import java.net.JarURLConnection;
26  import java.net.URL;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  import java.nio.file.Path;
30  import java.util.Collections;
31  
32  import org.apache.commons.io.IOUtils;
33  import org.apache.maven.archiver.MavenArchiveConfiguration;
34  import org.apache.maven.model.Model;
35  import org.apache.maven.project.MavenProject;
36  import org.codehaus.plexus.archiver.jar.JarArchiver;
37  import org.junit.Rule;
38  import org.junit.Test;
39  import org.junit.rules.TemporaryFolder;
40  import org.junit.runner.RunWith;
41  import org.mockito.junit.MockitoJUnitRunner;
42  
43  import static org.junit.Assert.assertTrue;
44  
45  @RunWith(MockitoJUnitRunner.class)
46  public class ManifestCreationFinalizerTest {
47  
48      @Rule
49      public TemporaryFolder temporaryFolder = new TemporaryFolder();
50  
51      @Test
52      public void testShouldDoNothingWhenArchiveConfigIsNull() throws Exception {
53          new ManifestCreationFinalizer(null, null, null).finalizeArchiveCreation(null);
54      }
55  
56      @Test
57      public void testShouldDoNothingWhenArchiverIsNotJarArchiver() throws Exception {
58          MavenProject project = new MavenProject(new Model());
59          MavenArchiveConfiguration config = new MavenArchiveConfiguration();
60  
61          new ManifestCreationFinalizer(null, project, config).finalizeArchiveCreation(null);
62      }
63  
64      @Test
65      public void testShouldAddManifestWhenArchiverIsJarArchiver() throws Exception {
66          MavenProject project = new MavenProject(new Model());
67          MavenArchiveConfiguration config = new MavenArchiveConfiguration();
68  
69          File tempDir = temporaryFolder.getRoot();
70  
71          Path manifestFile = tempDir.toPath().resolve("MANIFEST.MF");
72  
73          Files.write(manifestFile, Collections.singletonList("Main-Class: Stuff\n"), StandardCharsets.UTF_8);
74  
75          config.setManifestFile(manifestFile.toFile());
76  
77          JarArchiver archiver = new JarArchiver();
78  
79          archiver.setArchiveFinalizers(Collections.singletonList(new ManifestCreationFinalizer(null, project, config)));
80  
81          File file = temporaryFolder.newFile();
82  
83          archiver.setDestFile(file);
84  
85          archiver.createArchive();
86  
87          URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
88  
89          BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
90  
91          StringWriter writer = new StringWriter();
92  
93          IOUtils.copy(reader, writer);
94  
95          assertTrue(writer.toString().contains("Main-Class: Stuff"));
96  
97          // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
98          ((JarURLConnection) resource.openConnection()).getJarFile().close();
99      }
100 
101     @Test
102     public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver() throws Exception {
103         MavenProject project = new MavenProject(new Model());
104         MavenArchiveConfiguration config = new MavenArchiveConfiguration();
105 
106         String testKey = "Test-Key";
107         String testValue = "test-value";
108 
109         config.addManifestEntry(testKey, testValue);
110 
111         JarArchiver archiver = new JarArchiver();
112 
113         archiver.setArchiveFinalizers(Collections.singletonList(new ManifestCreationFinalizer(null, project, config)));
114 
115         File file = temporaryFolder.newFile();
116 
117         archiver.setDestFile(file);
118 
119         archiver.createArchive();
120 
121         URL resource = new URL("jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF");
122 
123         BufferedReader reader = new BufferedReader(new InputStreamReader(resource.openStream()));
124 
125         StringWriter writer = new StringWriter();
126 
127         IOUtils.copy(reader, writer);
128 
129         assertTrue(writer.toString().contains(testKey + ": " + testValue));
130 
131         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
132         ((JarURLConnection) resource.openConnection()).getJarFile().close();
133     }
134 }