View Javadoc

1   package org.apache.maven.plugin.jar;
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.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   * These unit tests only check whether the generated command lines are correct.
42   * Really running the command would mean checking the results, which is too painful and not really a unit test.
43   * It would probably require to 'jarsigner -verify' the resulting signed jar and I believe it would make the code
44   * too complex with very few benefits.
45   *
46   * @author Jerome Lacoste <jerome@coffeebreaks.org>
47   * @version $Id: JarSignVerifyMojoTest.java 728546 2008-12-21 22:56:51Z bentmann $
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          // it doesn't really matter if the paths are not cross-platform, we don't execute the command lines anyway
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         // isn't there an assertEquals for arrays?
217         /*
218         for (int i = 0; i < arguments.length; i++ ) {
219             System.out.println( arguments[ i ] );
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 }