View Javadoc
1   package org.codehaus.mojo.keytool.requests;
2   
3   /*
4    * Copyright 2005-2013 The Codehaus
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License" );
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.maven.shared.utils.cli.Commandline;
20  import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
21  import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
22  import org.codehaus.mojo.keytool.KeyTool;
23  import org.codehaus.mojo.keytool.KeyToolRequest;
24  import org.codehaus.mojo.keytool.UnsupportedKeyToolRequestException;
25  import org.codehaus.plexus.PlexusTestCase;
26  import org.junit.After;
27  import org.junit.Assert;
28  import org.junit.Before;
29  
30  import java.io.File;
31  import java.util.Arrays;
32  
33  /**
34   * abstract test of a keytool request.
35   *
36   * @author tchemit <chemit@codelutin.com>
37   * @since 1.1
38   */
39  public abstract class AbstractKeyToolRequestIT<R extends KeyToolRequest>
40      extends PlexusTestCase
41  {
42  
43      private static final long BUILD_TIMESTAMP = System.nanoTime();
44  
45      /**
46       * KeyTool to test keyTool requests.
47       */
48      protected KeyTool tool;
49  
50      protected File workingDirectory;
51  
52      protected KeyToolRequestFixtures requestFixtures;
53  
54      protected ResourceFixtures resourceFixtures;
55  
56      private final boolean supportedRequest;
57  
58      protected AbstractKeyToolRequestIT()
59      {
60          this( true );
61      }
62  
63      protected AbstractKeyToolRequestIT( boolean supportedRequest )
64      {
65          this.supportedRequest = supportedRequest;
66      }
67  
68      public abstract void testRequest()
69          throws Exception;
70  
71      protected final JavaToolResult consumeRequest( R request )
72          throws JavaToolException
73      {
74  
75          JavaToolResult result = null;
76  
77          if ( supportedRequest )
78          {
79              result = executeKeyToolRequest( request );
80          }
81          else
82          {
83              executeUnsupportedKeyToolRequest( request );
84          }
85          return result;
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      @Before
92      public void setUp()
93          throws Exception
94      {
95          super.setUp();
96  
97          String basedir = getBasedir();
98          workingDirectory = new File( basedir, "target" + File.separator + "surefire-workdir" + File.separator +
99              getClass().getName() + "_" + BUILD_TIMESTAMP );
100 
101         tool = (KeyTool) lookup( KeyTool.ROLE );
102 
103         Assert.assertNotNull( tool );
104 
105         requestFixtures = new KeyToolRequestFixtures();
106         resourceFixtures = new ResourceFixtures( workingDirectory );
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @After
113     public void tearDown()
114         throws Exception
115     {
116         super.tearDown();
117         tool = null;
118         requestFixtures = null;
119         resourceFixtures = null;
120     }
121 
122     protected JavaToolResult executeKeyToolRequest( KeyToolRequest request )
123         throws JavaToolException
124     {
125         Assert.assertNotNull( request );
126         JavaToolResult result = tool.execute( request );
127         System.out.println( result.getCommandline().toString() );
128         Assert.assertNotNull( result );
129         return result;
130     }
131 
132     protected void assertKeyToolResult( JavaToolResult result, String[] expectedCommandLineArguments,
133                                         int expectedExitCode )
134     {
135         assertKeyToolResult( result, expectedCommandLineArguments );
136 
137         assertEquals( "Differing exit code , required " + expectedExitCode + " but had : " + result.getExitCode(),
138                       expectedExitCode, result.getExitCode() );
139     }
140 
141     protected void assertKeyToolResult( JavaToolResult result, String[] expectedCommandLineArguments )
142     {
143 
144         Commandline commandline = result.getCommandline();
145         String[] arguments = commandline.getArguments();
146 
147         assertEquals(
148             "Differing number of arguments, required " + Arrays.asList( expectedCommandLineArguments ) + " but had : " +
149                 Arrays.asList( arguments ), expectedCommandLineArguments.length, arguments.length );
150         for ( int i = 0; i < arguments.length; i++ )
151         {
152             assertEquals( expectedCommandLineArguments[i], arguments[i] );
153         }
154     }
155 
156     protected void executeUnsupportedKeyToolRequest( KeyToolRequest request )
157         throws JavaToolException
158     {
159         Assert.assertNotNull( request );
160         try
161         {
162             tool.execute( request );
163             Assert.fail( "Request of type " + request.getClass().getName() + " is not supported." );
164         }
165         catch ( UnsupportedKeyToolRequestException e )
166         {
167             Assert.assertTrue( true );
168         }
169     }
170 }