Coverage Report - org.apache.tomcat.maven.it.AbstractWarProjectIT
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractWarProjectIT
0 %
0/57
0 %
0/6
1,538
AbstractWarProjectIT$1
0 %
0/3
N/A
1,538
 
 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  0
 public abstract class AbstractWarProjectIT
 48  
 {
 49  0
     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  0
         httpClient = new DefaultHttpClient();
 85  
 
 86  0
         final HttpParams params = httpClient.getParams();
 87  0
         HttpConnectionParams.setConnectionTimeout( params, getTimeout() );
 88  0
         HttpConnectionParams.setSoTimeout( params, getTimeout() );
 89  
 
 90  0
         webappHome = ResourceExtractor.simpleExtractResources( getClass(), "/" + getWarArtifactId() );
 91  0
         verifier = new Verifier( webappHome.getAbsolutePath() );
 92  
 
 93  0
         boolean debugVerifier = Boolean.getBoolean( "verifier.maven.debug" );
 94  
 
 95  0
         verifier.setMavenDebug( debugVerifier );
 96  0
         verifier.setDebugJvm( Boolean.getBoolean( "verifier.debugJvm" ) );
 97  0
         verifier.displayStreamBuffers();
 98  
 
 99  0
         verifier.deleteArtifact( "org.apache.tomcat.maven.it", getWarArtifactId(), "1.0-SNAPSHOT", "war" );
 100  0
     }
 101  
 
 102  
     @After
 103  
     public void tearDown()
 104  
         throws Exception
 105  
     {
 106  0
         httpClient.getConnectionManager().shutdown();
 107  0
         verifier.resetStreams();
 108  0
         verifier.deleteArtifact( "org.apache.tomcat.maven.it", getWarArtifactId(), "1.0-SNAPSHOT", "war" );
 109  0
     }
 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  0
         final String[] responseBodies = new String[]{ null };
 122  
 
 123  0
         final Thread thread = new Thread( "webapp-response-retriever" )
 124  0
         {
 125  
             @Override
 126  
             public void run()
 127  
             {
 128  0
                 responseBodies[0] = getResponseBody( getTimeout() );
 129  0
             }
 130  
         };
 131  
 
 132  0
         thread.start();
 133  
 
 134  0
         LOG.info( "Executing verify on " + webappHome.getAbsolutePath() );
 135  0
         verifier.executeGoal( "verify" );
 136  
 
 137  0
         verifier.displayStreamBuffers();
 138  
 
 139  0
         thread.join();
 140  
 
 141  0
         return responseBodies[0];
 142  
     }
 143  
 
 144  
     private String getResponseBody( int timeout )
 145  
     {
 146  0
         String responseBody = null;
 147  0
         final long startTime = System.currentTimeMillis();
 148  0
         final long endTime = startTime + timeout;
 149  0
         long currentTime = System.currentTimeMillis();
 150  
         try
 151  
         {
 152  0
             while ( pingUrl() != 200 && currentTime < endTime )
 153  
             {
 154  0
                 LOG.debug( "Ping..." );
 155  0
                 Thread.sleep( 500 );
 156  0
                 currentTime = System.currentTimeMillis();
 157  
             }
 158  0
             if ( currentTime < endTime )
 159  
             {
 160  0
                 responseBody = getResponseBody();
 161  0
                 LOG.debug( "Received: " + responseBody );
 162  
             }
 163  
             else
 164  
             {
 165  0
                 LOG.error( "Timeout met while trying to access web application." );
 166  
             }
 167  
         }
 168  0
         catch ( IOException e )
 169  
         {
 170  0
             LOG.error( "Exception while trying to access web application.", e );
 171  
         }
 172  0
         catch ( InterruptedException e )
 173  
         {
 174  0
             LOG.error( "Exception while trying to access web application.", e );
 175  0
         }
 176  0
         return responseBody;
 177  
     }
 178  
 
 179  
     private String getResponseBody()
 180  
         throws IOException
 181  
     {
 182  0
         HttpGet httpGet = new HttpGet( getWebappUrl() );
 183  0
         ResponseHandler<String> responseHandler = new BasicResponseHandler();
 184  0
         return httpClient.execute( httpGet, responseHandler );
 185  
     }
 186  
 
 187  
     private int pingUrl()
 188  
     {
 189  0
         final HttpHead httpHead = new HttpHead( getWebappUrl() );
 190  
         try
 191  
         {
 192  0
             final HttpResponse response = httpClient.execute( httpHead );
 193  0
             return response.getStatusLine().getStatusCode();
 194  
         }
 195  0
         catch ( IOException e )
 196  
         {
 197  0
             LOG.debug( "Ignoring exception while pinging URL " + httpHead.getURI(), e );
 198  0
             return -1;
 199  
         }
 200  
     }
 201  
 
 202  
     protected int getTimeout()
 203  
     {
 204  0
         return 15000;
 205  
     }
 206  
 
 207  
     protected static String getHttpItPort()
 208  
     {
 209  0
         return System.getProperty( "its.http.port" );
 210  
     }
 211  
 
 212  
     protected static String getHttpsItPort()
 213  
     {
 214  0
         return System.getProperty( "its.https.port" );
 215  
     }
 216  
 
 217  
     protected static String getAjpItPort()
 218  
     {
 219  0
         return System.getProperty( "its.ajp.port" );
 220  
     }
 221  
 
 222  
 }