View Javadoc
1   package org.apache.maven.plugin.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.plugin.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( Collections.<ArchiveFinalizer>singletonList( new ManifestCreationFinalizer(
93                                                                                                   null,
94                                                                                                   project,
95                                                                                                   config ) ) );
96  
97          File file = fileManager.createTempFile();
98  
99          archiver.setDestFile( file );
100 
101         archiver.createArchive();
102 
103         URL resource = new URL( "jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF" );
104 
105         BufferedReader reader = new BufferedReader( new InputStreamReader( resource.openStream() ) );
106 
107         StringWriter writer = new StringWriter();
108 
109         IOUtil.copy( reader, writer );
110 
111         assertTrue(writer.toString().contains("Main-Class: Stuff"));
112 
113         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
114         ( (JarURLConnection) resource.openConnection() ).getJarFile().close();
115     }
116 
117     public void testShouldAddManifestEntriesWhenArchiverIsJarArchiver()
118         throws ArchiverException, IOException
119     {
120         MavenProject project = new MavenProject( new Model() );
121         MavenArchiveConfiguration config = new MavenArchiveConfiguration();
122 
123         String testKey = "Test-Key";
124         String testValue = "test-value";
125 
126         config.addManifestEntry( testKey, testValue );
127 
128         JarArchiver archiver = new JarArchiver();
129 
130         archiver.setArchiveFinalizers( Collections.<ArchiveFinalizer>singletonList( new ManifestCreationFinalizer(
131                                                                                                  null,
132                                                                                                  project,
133                                                                                                  config ) ) );
134 
135         File file = fileManager.createTempFile();
136 
137         archiver.setDestFile( file );
138 
139         archiver.createArchive();
140 
141         URL resource = new URL( "jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF" );
142 
143         BufferedReader reader = new BufferedReader( new InputStreamReader( resource.openStream() ) );
144 
145         StringWriter writer = new StringWriter();
146 
147         IOUtil.copy( reader, writer );
148 
149         System.out.println( "Test Manifest:\n\n" + writer );
150 
151         assertTrue(writer.toString().contains(testKey + ": " + testValue));
152 
153         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
154         ( (JarURLConnection) resource.openConnection() ).getJarFile().close();
155     }
156 
157     private final class MockAndControlForArchiver
158     {
159         final Archiver archiver;
160 
161 
162         MockAndControlForArchiver( EasyMockSupport mm )
163         {
164 
165             archiver = mm.createMock(Archiver.class);
166         }
167     }
168 
169 }