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 org.apache.maven.archiver.MavenArchiveConfiguration;
23  import org.apache.maven.model.Model;
24  import org.apache.maven.plugin.assembly.testutils.MockManager;
25  import org.apache.maven.plugin.assembly.testutils.TestFileManager;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.archiver.Archiver;
28  import org.codehaus.plexus.archiver.ArchiverException;
29  import org.codehaus.plexus.archiver.jar.JarArchiver;
30  import org.codehaus.plexus.util.IOUtil;
31  import org.easymock.MockControl;
32  
33  import java.io.BufferedReader;
34  import java.io.File;
35  import java.io.IOException;
36  import java.io.InputStreamReader;
37  import java.io.StringWriter;
38  import java.net.JarURLConnection;
39  import java.net.URL;
40  import java.util.Collections;
41  
42  import junit.framework.TestCase;
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 ).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( 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" );
88  
89          config.setManifestFile( manifestFile );
90  
91          JarArchiver archiver = new JarArchiver();
92  
93          archiver.setArchiveFinalizers( Collections.singletonList( new ManifestCreationFinalizer(
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().indexOf( "Main-Class: Stuff" ) > -1 );
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.singletonList( new ManifestCreationFinalizer(
131                                                                                                  project,
132                                                                                                  config ) ) );
133 
134         File file = fileManager.createTempFile();
135 
136         archiver.setDestFile( file );
137 
138         archiver.createArchive();
139 
140         URL resource = new URL( "jar:file:" + file.getAbsolutePath() + "!/META-INF/MANIFEST.MF" );
141 
142         BufferedReader reader = new BufferedReader( new InputStreamReader( resource.openStream() ) );
143 
144         StringWriter writer = new StringWriter();
145 
146         IOUtil.copy( reader, writer );
147 
148         System.out.println( "Test Manifest:\n\n" + writer );
149 
150         assertTrue( writer.toString().indexOf( testKey + ": " + testValue ) > -1 );
151 
152         // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4823678
153         ( (JarURLConnection) resource.openConnection() ).getJarFile().close();
154     }
155 
156     private final class MockAndControlForArchiver
157     {
158         Archiver archiver;
159 
160         MockControl control;
161 
162         MockAndControlForArchiver( MockManager mm )
163         {
164             control = MockControl.createControl( Archiver.class );
165             mm.add( control );
166 
167             archiver = (Archiver) control.getMock();
168         }
169     }
170 
171 }