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