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