View Javadoc

1   package org.apache.tomcat.maven.it;
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  
23  import org.apache.http.HttpResponse;
24  import org.apache.http.client.ResponseHandler;
25  import org.apache.http.client.methods.HttpGet;
26  import org.apache.http.client.methods.HttpHead;
27  import org.apache.http.impl.client.BasicResponseHandler;
28  import org.apache.http.impl.client.DefaultHttpClient;
29  import org.apache.http.params.HttpConnectionParams;
30  import org.apache.http.params.HttpParams;
31  import org.apache.maven.it.VerificationException;
32  import org.apache.maven.it.Verifier;
33  import org.apache.maven.it.util.ResourceExtractor;
34  import org.junit.After;
35  import org.junit.Before;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  import java.io.File;
40  import java.io.IOException;
41  import java.util.Collections;
42  import java.util.List;
43  
44  /**
45   * Base class for all tests which have a war-project using the tomcat-maven-plugin below project-resources.
46   *
47   * @author Mark Michaelis
48   */
49  public abstract class AbstractWarProjectIT
50  {
51      protected Logger logger = LoggerFactory.getLogger( getClass() );
52  
53      /**
54       * This URL will be queried for content. It will also be used to wait for the startup of the webapp.
55       *
56       * @return the URL to ping
57       */
58      protected abstract String getWebappUrl();
59  
60      /**
61       * Artifact ID of the war project. Needed to uninstall any artifacts.
62       *
63       * @return artifact ID of the war project under test
64       */
65      protected abstract String getWarArtifactId();
66  
67      /**
68       * HttpClient to use to connect to the deployed web-application.
69       */
70      private DefaultHttpClient httpClient;
71  
72      /**
73       * Helper for Maven-Integration-Tests.
74       */
75      protected Verifier verifier;
76  
77      /**
78       * Where the war project got placed to.
79       */
80      protected File webappHome;
81  
82      @Before
83      public void setUp()
84          throws Exception
85      {
86          httpClient = new DefaultHttpClient();
87  
88          final HttpParams params = httpClient.getParams();
89          HttpConnectionParams.setConnectionTimeout( params, getTimeout() );
90          HttpConnectionParams.setSoTimeout( params, getTimeout() );
91  
92          webappHome = ResourceExtractor.simpleExtractResources( getClass(), "/" + getWarArtifactId() );
93          verifier = new Verifier( webappHome.getAbsolutePath() );
94  
95          boolean debugVerifier = Boolean.getBoolean( "verifier.maven.debug" );
96  
97          verifier.setMavenDebug( debugVerifier );
98          verifier.setDebugJvm( Boolean.getBoolean( "verifier.debugJvm" ) );
99          verifier.displayStreamBuffers();
100 
101         verifier.deleteArtifact( "org.apache.tomcat.maven.it", getWarArtifactId(), "1.0-SNAPSHOT", "war" );
102     }
103 
104     @After
105     public void tearDown()
106         throws Exception
107     {
108         httpClient.getConnectionManager().shutdown();
109         verifier.resetStreams();
110         verifier.deleteArtifact( "org.apache.tomcat.maven.it", getWarArtifactId(), "1.0-SNAPSHOT", "war" );
111     }
112 
113     /**
114      * Executes mvn verify and retrieves the response from the web application.
115      *
116      * @return the response given
117      * @throws VerificationException if the verifier failed to execute the goal
118      * @throws InterruptedException  if the execution got interrupted in some way
119      */
120     protected final String executeVerifyWithGet()
121         throws VerificationException, InterruptedException, IOException
122     {
123         final String[] responseBodies = new String[]{ null };
124 
125         final Thread thread = new Thread( "webapp-response-retriever" )
126         {
127             @Override
128             public void run()
129             {
130                 responseBodies[0] = getResponseBody( getTimeout() );
131             }
132         };
133 
134         thread.start();
135 
136         logger.info( "Executing verify on " + webappHome.getAbsolutePath() );
137 
138         verifier.setCliOptions( getCliOptions() );
139 
140         verifier.setLogFileName( "foo.log" );
141 
142         verifier.executeGoal( getGoal() );
143 
144         verifier.displayStreamBuffers();
145 
146         thread.join();
147 
148         return responseBodies[0];
149     }
150 
151 
152     protected String getGoal()
153     {
154         return "verify";
155     }
156 
157     protected List<String> getCliOptions()
158     {
159         return Collections.emptyList();
160     }
161 
162     private String getResponseBody( int timeout )
163     {
164         String responseBody = null;
165         final long startTime = System.currentTimeMillis();
166         final long endTime = startTime + timeout;
167         long currentTime = System.currentTimeMillis();
168         try
169         {
170             while ( pingUrl() != 200 && currentTime < endTime )
171             {
172                 logger.debug( "Ping..." );
173                 Thread.sleep( 500 );
174                 currentTime = System.currentTimeMillis();
175             }
176             if ( currentTime < endTime )
177             {
178                 responseBody = getResponseBody();
179                 logger.debug( "Received: " + responseBody );
180             }
181             else
182             {
183                 logger.error( "Timeout met while trying to access web application." );
184             }
185         }
186         catch ( IOException e )
187         {
188             logger.error( "Exception while trying to access web application.", e );
189         }
190         catch ( InterruptedException e )
191         {
192             logger.error( "Exception while trying to access web application.", e );
193         }
194         return responseBody;
195     }
196 
197     private String getResponseBody()
198         throws IOException
199     {
200         HttpGet httpGet = new HttpGet( getWebappUrl() );
201         ResponseHandler<String> responseHandler = new BasicResponseHandler();
202         return httpClient.execute( httpGet, responseHandler );
203     }
204 
205     private int pingUrl()
206     {
207         final HttpHead httpHead = new HttpHead( getWebappUrl() );
208         try
209         {
210             final HttpResponse response = httpClient.execute( httpHead );
211             return response.getStatusLine().getStatusCode();
212         }
213         catch ( IOException e )
214         {
215             logger.debug( "Ignoring exception while pinging URL " + httpHead.getURI(), e );
216             return -1;
217         }
218     }
219 
220     protected int getTimeout()
221     {
222         return 15000;
223     }
224 
225     protected static String getHttpItPort()
226     {
227         return System.getProperty( "its.http.port" );
228     }
229 
230     protected static String getHttpsItPort()
231     {
232         return System.getProperty( "its.https.port" );
233     }
234 
235     protected static String getAjpItPort()
236     {
237         return System.getProperty( "its.ajp.port" );
238     }
239 
240 }