1   package org.apache.tomcat.maven.it;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
44  
45  
46  
47  public abstract class AbstractWarProjectIT
48  {
49      private static final Logger LOG = LoggerFactory.getLogger( AbstractWarProjectIT.class );
50  
51      
52  
53  
54  
55  
56      protected abstract String getWebappUrl();
57  
58      
59  
60  
61  
62  
63      protected abstract String getWarArtifactId();
64  
65      
66  
67  
68      private DefaultHttpClient httpClient;
69  
70      
71  
72  
73      protected Verifier verifier;
74  
75      
76  
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 
113 
114 
115 
116 
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 }