1 package org.codehaus.mojo.fitnesse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.Writer;
20 import java.text.SimpleDateFormat;
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.commons.httpclient.UsernamePasswordCredentials;
27 import org.apache.maven.plugin.AbstractMojo;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.settings.Server;
30
31
32
33
34
35
36 public abstract class FitnesseAbstractMojo
37 extends AbstractMojo
38 {
39
40 public static final String FITNESSE_RESULT_PREFIX = "fitnesseResult";
41
42
43 public static final String OUTPUT_EXTENSION = "_output";
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 private List fitnesses;
65
66
67
68
69
70
71 private boolean failOnError;
72
73
74
75
76
77
78 private String dateFormat;
79
80
81
82
83
84
85
86
87 private List servers = new ArrayList();
88
89
90
91
92
93
94 protected String workingDir;
95
96
97
98
99 String cmdFitnessePage;
100
101
102
103
104 String cmdFitnesseHostName;
105
106
107
108
109 int cmdFitnessePort = -1;
110
111
112
113
114
115
116 void checkConfiguration()
117 throws MojoExecutionException
118 {
119 changeConfigWithCmdLineParameters();
120
121 if ( fitnesses == null || fitnesses.size() == 0 )
122 {
123 String errorMessage =
124 "Your should configure at least one Fitnesse server. "
125 + "Check your maven-fitnesse-plugin configuration.";
126 getLog().error( errorMessage );
127 throw new MojoExecutionException( errorMessage );
128 }
129 else
130 {
131 for ( Iterator tIt = fitnesses.iterator(); tIt.hasNext(); )
132 {
133 ( (Fitnesse) tIt.next() ).checkConfiguration();
134 }
135 }
136 }
137
138
139
140
141 private void changeConfigWithCmdLineParameters()
142 {
143 if ( cmdFitnesseHostName != null || cmdFitnessePort != -1 || cmdFitnessePage != null )
144 {
145 getLog().info( "Command line parameters detected, merging with pom configuration." );
146 Fitnesse tFit =
147 ( fitnesses != null && fitnesses.size() > 0 ? (Fitnesse) fitnesses.get( 0 )
148 : new Fitnesse( "localhost", Fitnesse.DEFAULT_FITNESSE_PORT, cmdFitnessePage ) );
149 fitnesses = new ArrayList();
150 fitnesses.add( tFit );
151 if ( cmdFitnessePage != null )
152 {
153 tFit.setPageName( cmdFitnessePage );
154 }
155 if ( cmdFitnesseHostName != null )
156 {
157 tFit.setHostName( cmdFitnesseHostName );
158 }
159 if ( cmdFitnessePort != -1 )
160 {
161 tFit.setPort( cmdFitnessePort );
162 }
163 getLog().info(
164 "using url=[http://" + tFit.getHostName() + ":" + tFit.getPort() + "/" + tFit.getPageName()
165 + "]" );
166 }
167 }
168
169
170
171
172
173
174 public void setFitnesses( List pFitnesses )
175 {
176 fitnesses = pFitnesses;
177 }
178
179
180
181
182
183
184
185 protected Fitnesse getFitnesse( int pPosition )
186 {
187 return (Fitnesse) fitnesses.get( pPosition );
188 }
189
190
191
192
193
194
195 protected int getFitnesseSize()
196 {
197 return fitnesses.size();
198 }
199
200
201
202
203
204
205
206
207 UsernamePasswordCredentials getCredential( String pServerId )
208 throws MojoExecutionException
209 {
210 UsernamePasswordCredentials tResult = null;
211 Server tServer;
212 for ( Iterator tEnum = servers.iterator(); tEnum.hasNext(); )
213 {
214 tServer = (Server) tEnum.next();
215 if ( pServerId.equals( tServer.getId() ) )
216 {
217 getLog().info( "Use login/password for user " + tServer.getUsername() );
218 tResult = new UsernamePasswordCredentials( tServer.getUsername(), tServer.getPassword() );
219 }
220 }
221 if ( tResult == null )
222 {
223 throw new MojoExecutionException( "Unable to find credential for ServerId=[" + pServerId
224 + "], you must define a <Server> tag in your settings.xml for this Id." );
225 }
226 return tResult;
227 }
228
229
230
231
232
233
234 public void addServer( Server pServer )
235 {
236 this.servers.add( pServer );
237 }
238
239
240
241
242
243
244 public boolean isFailOnError()
245 {
246 return failOnError;
247 }
248
249
250
251
252
253
254 public void setFailOnError( boolean failOnError )
255 {
256 this.failOnError = failOnError;
257 }
258
259
260
261
262
263
264
265
266
267
268
269
270 void transformHtml( InputStream pIn, Writer pOut, String pOutputFileName, String pStatus )
271 throws IOException, MojoExecutionException
272 {
273 String tHtml = FileUtil.getString( pIn );
274 int curPosStart = tHtml.indexOf( "<title>" ) + "<title>".length();
275 int curPosEnd = tHtml.indexOf( "</title>" );
276 pOut.write( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" " );
277 pOut.write( "\"http://www.w3.org/TR/html4/strict.DTD\">\r\n" );
278 pOut.write( "<html>\r\n" );
279 pOut.write( "\t<head>\r\n" );
280 pOut.write( "\t\t<title>" );
281 pOut.write( tHtml.substring( curPosStart, curPosEnd ) );
282 pOut.write( " [" );
283 pOut.write( getCurrentTimeAsString() );
284 pOut.write( "]</title>\r\n" );
285 pOut.write( "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"fitnesse_base.css\" " );
286 pOut.write( "media=\"screen\"/>\r\n" );
287 pOut.write( "\t\t<link rel=\"stylesheet\" type=\"text/css\" href=\"fitnesse_print.css\" " );
288 pOut.write( "media=\"print\"/>\r\n" );
289 pOut.write( "\t\t<script src=\"fitnesse.js\" type=\"text/javascript\"></script>\r\n" );
290 pOut.write( "\t</head>\r\n" );
291 pOut.write( "\t<body>\r\n" );
292 pOut.write( "\t\t<div id=\"execution-status\">\r\n" );
293 pOut.write( "\t\t\t<a href=\"" );
294 pOut.write( pOutputFileName );
295 pOut.write( "\"><img src=\"images/executionStatus/" );
296 pOut.write( getImage( pStatus ) );
297 pOut.write( "\"/></a>\r\n" );
298 pOut.write( "\t\t\t<br/>\r\n" );
299 pOut.write( "\t\t\t<a href=\"" );
300 pOut.write( pOutputFileName );
301 pOut.write( "\">Tests Executed " );
302 pOut.write( pStatus );
303 pOut.write( "</a>\r\n" );
304 pOut.write( "\t\t</div>\r\n" );
305 pOut.write( "\t\t<h3>Test executed on " + getCurrentTimeAsString() + "</h3>\r\n" );
306 curPosStart = tHtml.indexOf( "<div class=\"main\">" );
307 tHtml = tHtml.substring( curPosStart, tHtml.length() );
308 tHtml = tHtml.replaceAll( "/files/", "" );
309 curPosStart = tHtml.indexOf( "<div id=\"execution-status\">" );
310 curPosEnd = tHtml.indexOf( "</div>", curPosStart );
311 if ( curPosStart >= 0 && curPosEnd >= 0 )
312 {
313 pOut.write( tHtml.substring( 0, curPosStart ) );
314 pOut.write( tHtml.substring( curPosEnd + "</div>".length() + 2, tHtml.length() ) );
315 }
316 else
317 {
318 pOut.write( tHtml );
319 }
320 pOut.flush();
321 }
322
323
324
325
326
327
328
329
330 private String getImage( String pStatus )
331 throws MojoExecutionException
332 {
333 if ( FitnessePage.STATUS_OK.equals( pStatus ) )
334 {
335 return "ok.gif";
336 }
337 else if ( FitnessePage.STATUS_ERROR.equals( pStatus ) )
338 {
339 return "error.gif";
340 }
341 else if ( FitnessePage.STATUS_FAIL.equals( pStatus ) )
342 {
343 return "output.gif";
344 }
345 else
346 {
347 throw new MojoExecutionException( "Invalid status [" + pStatus + "]" );
348 }
349 }
350
351
352
353
354
355
356 protected String getCurrentTimeAsString()
357 {
358 SimpleDateFormat tFormat = new SimpleDateFormat( dateFormat );
359 return tFormat.format( new Date() );
360 }
361
362
363
364
365
366
367 public void setDateFormat( String pDateFormat )
368 {
369 dateFormat = pDateFormat;
370 }
371
372
373
374
375
376
377 public String getDateFormat()
378 {
379 return dateFormat;
380 }
381
382
383
384
385
386
387
388 String getTmpFileName( Fitnesse pServer )
389 {
390 return getResultFileName( pServer, "_tmp", "html" );
391 }
392
393
394
395
396
397
398
399 String getFinalFileName( Fitnesse pServer )
400 {
401 return getResultFileName( pServer, "", "html" );
402 }
403
404
405
406
407
408
409
410 abstract String getOutputFileName( Fitnesse pServer );
411
412
413
414
415
416
417
418 abstract String getOutputUrl( Fitnesse pServer );
419
420
421
422
423
424
425
426
427
428 protected String getResultFileName( Fitnesse pServer, String pPostfix, String pExtension )
429 {
430 return this.workingDir + "/" + FITNESSE_RESULT_PREFIX + "_" + pServer.getHostName() + "_"
431 + pServer.getPageName() + pPostfix + "." + pExtension;
432 }
433
434 }