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 import java.util.Collections;
42 import java.util.List;
43
44
45
46
47
48
49 public abstract class AbstractWarProjectIT
50 {
51 protected Logger logger = LoggerFactory.getLogger( getClass() );
52
53
54
55
56
57
58 protected abstract String getWebappUrl();
59
60
61
62
63
64
65 protected abstract String getWarArtifactId();
66
67
68
69
70 private DefaultHttpClient httpClient;
71
72
73
74
75 protected Verifier verifier;
76
77
78
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
115
116
117
118
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 }