1 package org.apache.maven.plugins.assembly.archive;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }