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 junit.framework.TestCase;
23  import org.apache.maven.archiver.MavenArchiveConfiguration;
24  import org.apache.maven.model.Model;
25  import org.apache.maven.plugins.assembly.testutils.TestFileManager;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.archiver.ArchiveFinalizer;
28  import org.codehaus.plexus.archiver.Archiver;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.JarArchiver;
31  import org.codehaus.plexus.util.IOUtil;
32  import org.easymock.classextension.EasyMockSupport;
33  
34  import java.io.BufferedReader;
35  import java.io.File;
36  import java.io.IOException;
37  import java.io.InputStreamReader;
38  import java.io.StringWriter;
39  import java.net.JarURLConnection;
40  import java.net.URL;
41  import java.util.Collections;
42  
43  public class ManifestCreationFinalizerTest
44      extends TestCase
45  {
46  
47      private final TestFileManager fileManager = new TestFileManager( "manifest-finalizer.test.", ".jar" );
48  
49      public void tearDown()
50          throws IOException
51      {
52          fileManager.cleanUp();
53      }
54  
55      public void testShouldDoNothingWhenArchiveConfigIsNull()
56          throws ArchiverException
57      {
58          new ManifestCreationFinalizer( null, null, null ).finalizeArchiveCreation( null );
59      }
60  
61      public void testShouldDoNothingWhenArchiverIsNotJarArchiver()
62          throws ArchiverException
63      {
64          EasyMockSupport mm = new EasyMockSupport();
65  
66          MockAndControlForArchiver macArchiver = new MockAndControlForArchiver( mm );
67  
68          MavenProject project = new MavenProject( new Model() );
69          MavenArchiveConfiguration config = new MavenArchiveConfiguration();
70  
71          mm.replayAll();
72  
73          new ManifestCreationFinalizer( null, project, config ).finalizeArchiveCreation( macArchiver.archiver );
74  
75          mm.verifyAll();
76      }
77  
78      public void testShouldAddManifestWhenArchiverIsJarArchiver()
79          throws ArchiverException, IOException
80      {
81          MavenProject project = new MavenProject( new Model() );
82          MavenArchiveConfiguration config = new MavenArchiveConfiguration();
83  
84          File tempDir = fileManager.createTempDir();
85  
86          File manifestFile = fileManager.createFile( tempDir, "MANIFEST.MF", "Main-Class: Stuff\n" );
87  
88          config.setManifestFile( manifestFile );
89  
90          JarArchiver archiver = new JarArchiver();
91  
92          archiver.setArchiveFinalizers(
93              Collections.<ArchiveFinalizer>singletonList( new ManifestCreationFinalizer( null, project, config ) ) );
94  
95          File file = fileManager.createTempFile();
96  
97          archiver.setDestFile( file );
98  
99          archiver.createArchive();
100 
101         URL resource = new URL( "jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF" );
102 
103         BufferedReader reader = new BufferedReader( new InputStreamReader( resource.openStream() ) );
104 
105         StringWriter writer = new StringWriter();
106 
107         IOUtil.copy( reader, writer );
108 
109         assertTrue( writer.toString().contains( "Main-Class: Stuff" ) );
110 
111         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
112         ( (JarURLConnection) resource.openConnection() ).getJarFile().close();
113     }
114 
115     public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver()
116         throws ArchiverException, IOException
117     {
118         MavenProject project = new MavenProject( new Model() );
119         MavenArchiveConfiguration config = new MavenArchiveConfiguration();
120 
121         String testKey = "Test-Key";
122         String testValue = "test-value";
123 
124         config.addManifestEntry( testKey, testValue );
125 
126         JarArchiver archiver = new JarArchiver();
127 
128         archiver.setArchiveFinalizers(
129             Collections.<ArchiveFinalizer>singletonList( new ManifestCreationFinalizer( null, project, config ) ) );
130 
131         File file = fileManager.createTempFile();
132 
133         archiver.setDestFile( file );
134 
135         archiver.createArchive();
136 
137         URL resource = new URL( "jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF" );
138 
139         BufferedReader reader = new BufferedReader( new InputStreamReader( resource.openStream() ) );
140 
141         StringWriter writer = new StringWriter();
142 
143         IOUtil.copy( reader, writer );
144 
145         System.out.println( "Test Manifest:\n\n" + writer );
146 
147         assertTrue( writer.toString().contains( testKey + ": " + testValue ) );
148 
149         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
150         ( (JarURLConnection) resource.openConnection() ).getJarFile().close();
151     }
152 
153     private final class MockAndControlForArchiver
154     {
155         final Archiver archiver;
156 
157 
158         MockAndControlForArchiver( EasyMockSupport mm )
159         {
160 
161             archiver = mm.createMock( Archiver.class );
162         }
163     }
164 
165 }