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