1 package org.apache.maven.plugin.jar;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.MojoExecutionException;
23
24 import org.codehaus.plexus.util.StringUtils;
25 import org.codehaus.plexus.util.cli.Commandline;
26 import org.codehaus.plexus.util.cli.CommandLineException;
27 import org.codehaus.plexus.util.cli.StreamConsumer;
28
29 import junit.framework.TestCase;
30
31 import java.io.File;
32 import java.io.InputStream;
33 import java.io.IOException;
34
35 import java.util.Map;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.ArrayList;
39
40
41
42
43
44
45
46
47
48
49 public class JarSignVerifyMojoTest
50 extends TestCase
51 {
52 private MockJarSignVerifyMojo mojo;
53
54 static class MockJarSignVerifyMojo
55 extends JarSignVerifyMojo
56 {
57 public int executeResult;
58
59 public List commandLines = new ArrayList();
60
61 public String failureMsg;
62
63 public Map systemProperties = new HashMap();
64
65 public String lastOutLine;
66
67 protected int executeCommandLine( Commandline commandLine, InputStream inputStream, StreamConsumer systemOut,
68 StreamConsumer systemErr )
69 throws CommandLineException
70 {
71 commandLines.add( commandLine );
72 if ( failureMsg != null )
73 {
74 throw new CommandLineException( failureMsg );
75 }
76 if ( lastOutLine != null )
77 {
78 systemOut.consumeLine( lastOutLine );
79 }
80 return executeResult;
81 }
82
83 protected String getSystemProperty( String key )
84 {
85 return (String) systemProperties.get( key );
86 }
87 }
88
89
90 public void setUp()
91 throws IOException
92 {
93 mojo = new MockJarSignVerifyMojo();
94 mojo.executeResult = 0;
95
96 File basedir = new File( System.getProperty( "java.io.tmpdir" ) );
97 mojo.setBasedir( basedir );
98 mojo.setWorkingDir( basedir );
99 mojo.setJarPath( new File( "/tmp/signed/file-version.jar" ) );
100 }
101
102 public void tearDown()
103 {
104 mojo = null;
105 }
106
107 public void testPleaseMaven()
108 {
109 assertTrue( true );
110 }
111
112
113
114 public void testRunOK()
115 throws MojoExecutionException
116 {
117 mojo.lastOutLine = "jar verified.";
118
119 mojo.execute();
120
121 String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"};
122
123 checkMojo( expectedArguments );
124 }
125
126
127
128 public void testRunOKAllArguments()
129 throws MojoExecutionException
130 {
131 mojo.lastOutLine = "jar verified.";
132
133 mojo.setVerbose( true );
134 mojo.setCheckCerts( true );
135
136 mojo.execute();
137
138 String[] expectedArguments = {"-verify", "-verbose", "-certs", "/tmp/signed/file-version.jar"};
139
140 checkMojo( expectedArguments );
141 }
142
143
144
145 public void testRunFailureNeverHappens()
146 {
147 mojo.executeResult = 1;
148
149 try
150 {
151 mojo.execute();
152 fail( "expected failure" );
153 }
154 catch ( MojoExecutionException e )
155 {
156 assertTrue( e.getMessage().startsWith( "Result of " ) );
157 }
158
159 String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"};
160
161 checkMojo( expectedArguments );
162 }
163
164
165
166 public void testRunFailureVerifyFailed()
167 {
168 mojo.lastOutLine = "jar is unsigned.";
169
170 try
171 {
172 mojo.execute();
173 fail( "expected failure" );
174 }
175 catch ( MojoExecutionException e )
176 {
177 assertTrue( e.getMessage().startsWith( "Verify failed: jar is unsigned." ) );
178 }
179
180 String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"};
181
182 checkMojo( expectedArguments );
183 }
184
185
186
187 public void testRunError()
188 {
189 mojo.failureMsg = "simulated failure";
190
191 try
192 {
193 mojo.execute();
194 fail( "expected failure" );
195 }
196 catch ( MojoExecutionException e )
197 {
198 assertEquals( "command execution failed", e.getMessage() );
199 }
200
201 String[] expectedArguments = {"-verify", "/tmp/signed/file-version.jar"};
202
203 checkMojo( expectedArguments );
204 }
205
206 private void checkMojo( String[] expectedCommandLineArguments )
207 {
208 checkMojo( mojo, expectedCommandLineArguments );
209 }
210
211 static void checkMojo( MockJarSignVerifyMojo mojo, String[] expectedCommandLineArguments )
212 {
213 assertEquals( 1, mojo.commandLines.size() );
214 Commandline commandline = (Commandline) mojo.commandLines.get( 0 );
215 String[] arguments = commandline.getArguments();
216
217
218
219
220
221
222 assertEquals( "Differing number of arguments", expectedCommandLineArguments.length, arguments.length );
223 for ( int i = 0; i < arguments.length; i++ )
224 {
225 expectedCommandLineArguments[i] =
226 StringUtils.replace( expectedCommandLineArguments[i], "/", File.separator );
227 assertEquals( expectedCommandLineArguments[i], expectedCommandLineArguments[i], arguments[i] );
228 }
229 }
230 }