Clover coverage report - Maven Jalopy Plugin - 1.5
Coverage timestamp: Sun May 7 2006 20:56:30 PDT
file stats: LOC: 564   Methods: 31
NCLOC: 243   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JalopyBean.java 40% 80.2% 96.8% 78%
coverage coverage
 1    package org.apache.maven.jalopy;
 2   
 3    /* ====================================================================
 4    * Copyright 2006 The Apache Software Foundation.
 5    *
 6    * Licensed under the Apache License, Version 2.0 (the "License");
 7    * you may not use this file except in compliance with the License.
 8    * You may obtain a copy of the License at
 9    *
 10    * http://www.apache.org/licenses/LICENSE-2.0
 11    *
 12    * Unless required by applicable law or agreed to in writing, software
 13    * distributed under the License is distributed on an "AS IS" BASIS,
 14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15    * See the License for the specific language governing permissions and
 16    * limitations under the License.
 17    * ====================================================================
 18    */
 19   
 20    import de.hunsicker.jalopy.Jalopy;
 21    import de.hunsicker.jalopy.storage.Convention;
 22    import de.hunsicker.jalopy.storage.History;
 23   
 24    import java.io.File;
 25    import java.io.FileNotFoundException;
 26    import java.io.IOException;
 27    import java.io.InputStream;
 28   
 29    import org.apache.commons.logging.Log;
 30    import org.apache.commons.logging.LogFactory;
 31   
 32    import org.codehaus.plexus.util.DirectoryScanner;
 33    import org.codehaus.plexus.util.StringUtils;
 34   
 35   
 36    /**
 37    * A bean to format all source files according to code conventions.
 38    *
 39    * @author jruiz@exist.com
 40    * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
 41    * @author <a href="mailto:ltheussl@apache.org">Lukas Theussl</a>
 42    * @version $Id: JalopyBean.java 398096 2006-04-29 06:05:18Z ltheussl $
 43    */
 44   
 45    public class JalopyBean
 46    {
 47    /** Log. */
 48    private static final Log LOG = LogFactory.getLog( JalopyBean.class );
 49   
 50    /**
 51    * Sets the file format of the output files.
 52    * The file format controls what end of line character is used.
 53    * Either one of "UNIX", "DOS", "MAC", "DEFAULT" or "AUTO" can be used.
 54    */
 55    private String fileFormat;
 56   
 57    /**
 58    * Specifies the history policy to use.
 59    * Either one of "COMMENT", "FILE" or "NONE" can be used.
 60    */
 61    private String historyPolicy;
 62   
 63    /**
 64    * Indicates whether a run should be held if errors occured.
 65    */
 66    private boolean failOnError;
 67   
 68    /**
 69    * Indicates whether to enable the code inspector.
 70    */
 71    private boolean inspect;
 72   
 73    /**
 74    * Indicates whether keep backups of formatted source files.
 75    */
 76    private boolean backup;
 77   
 78    /**
 79    * Indicates whether to force formatting of source files.
 80    */
 81    private boolean force;
 82   
 83    /**
 84    * The source directory.
 85    */
 86    private File sourceDirectory;
 87   
 88    /**
 89    * The test source directory.
 90    */
 91    private File testSourceDirectory;
 92   
 93    /**
 94    * Sets the preferences file to use - given either relative to
 95    * the project's basedir or as an absolute local path or internet address.
 96    * If omitted, the current preferences are used, if available.
 97    * Otherwise the Jalopy build-in defaults will be used.
 98    */
 99    private File convention;
 100   
 101    /**
 102    * For Source Directory. Specifies a fileset source file to format.
 103    * This is a comma- or space-separated list of patterns of files.
 104    */
 105    private String srcIncludesPattern;
 106   
 107    /**
 108    * For Source Directory. Source files excluded from format.
 109    * This is a comma- or space-separated list of patterns of files.
 110    */
 111    private String srcExcludesPattern;
 112   
 113    /**
 114    * For Test Directory. Specifies a fileset source file to format.
 115    * This is a comma- or space-separated list of patterns of files.
 116    */
 117    private String testIncludesPattern;
 118   
 119    /**
 120    * For Test Directory. Source files excluded from format.
 121    * This is a comma- or space-separated list of patterns of files.
 122    */
 123    private String testExcludesPattern;
 124   
 125    /**
 126    * The main method: starts formatting of source files.
 127    *
 128    * @throws Exception Exception
 129    */
 130  1 public void execute() throws Exception
 131    {
 132   
 133  1 LOG.info("[jalopy] Jalopy Java Source Code Formatter " + Jalopy.getVersion());
 134   
 135  1 try
 136    {
 137  1 if ( getSourceDirectory().exists() )
 138    {
 139  1 String[] filesToFormat = getIncludedFiles( getSourceDirectory(), getSrcIncludesPattern(),
 140    getSrcExcludesPattern() );
 141   
 142  1 LOG.info( "[jalopy] Format " + filesToFormat.length + " source files in " + getSourceDirectory().toString() );
 143  1 formatFiles( getSourceDirectory(), filesToFormat );
 144  1 LOG.info( "[jalopy] " + filesToFormat.length + " source files formatted" );
 145    }
 146   
 147  1 if ( getTestSourceDirectory().exists() )
 148    {
 149  1 String[] filesToFormat = getIncludedFiles( getTestSourceDirectory(), getTestIncludesPattern(),
 150    getTestExcludesPattern() );
 151   
 152  1 LOG.info( "[jalopy] Format " + filesToFormat.length + " source files in " + getTestSourceDirectory().toString() );
 153  1 formatFiles( getTestSourceDirectory(), filesToFormat );
 154  1 LOG.info( "[jalopy] " + filesToFormat.length + " source files formatted" );
 155    }
 156    }
 157    catch ( Exception e )
 158    {
 159  0 e.printStackTrace();
 160    }
 161    }
 162   
 163    /**
 164    * Returns a string array of files to format.
 165    *
 166    * @param directory The base directory
 167    * @param includes The includes pattern
 168    * @param excludes The excludes pattern
 169    * @return String[]
 170    */
 171  2 private String[] getIncludedFiles( File directory, String includes, String excludes )
 172    {
 173  2 DirectoryScanner scanner = new DirectoryScanner();
 174   
 175  2 scanner.setBasedir( directory );
 176   
 177  2 scanner.setIncludes( StringUtils.split( includes, "," ) );
 178   
 179  2 scanner.setExcludes( StringUtils.split( excludes, "," ) );
 180   
 181  2 scanner.scan();
 182   
 183  2 String[] filesToFormat = scanner.getIncludedFiles();
 184   
 185  2 return filesToFormat;
 186    }
 187   
 188    /**
 189    * Formats the source files.
 190    *
 191    * @param directory The base directory
 192    * @param filesToFormat A string array of files to format
 193    * @throws FileNotFoundException FileNotFoundException
 194    * @throws Exception Exception
 195    */
 196  2 private void formatFiles( File directory, String[] filesToFormat )
 197    throws FileNotFoundException, Exception
 198    {
 199  2 Jalopy jalopy = createJalopy();
 200   
 201  2 for ( int i = 0; i < filesToFormat.length; i++ )
 202    {
 203  4 File currentFile = new File( directory, filesToFormat[i] );
 204   
 205  4 jalopy.setInput( currentFile );
 206   
 207  4 jalopy.setOutput( currentFile );
 208   
 209  4 if ( isBackup() )
 210    {
 211  0 jalopy.setBackupDirectory( currentFile.getParentFile() );
 212    }
 213   
 214  4 jalopy.format();
 215   
 216  4 logMessage( jalopy, currentFile );
 217    }
 218    }
 219   
 220    /**
 221    * Logs a message depending on the jalopy exit status.
 222    *
 223    * @param jalopy The jalopy instance
 224    * @param currentFile The file whose status is to be logged
 225    * @throws Exception Exception
 226    */
 227  4 private void logMessage( Jalopy jalopy, File currentFile )
 228    throws Exception
 229    {
 230   
 231  4 if ( jalopy.getState() == Jalopy.State.OK )
 232    {
 233  4 LOG.debug( "[jalopy] " + currentFile + " formatted correctly." );
 234    }
 235  0 else if ( jalopy.getState() == Jalopy.State.WARN )
 236    {
 237  0 LOG.warn( "[jalopy] " + currentFile + " formatted with warnings." );
 238    }
 239  0 else if ( jalopy.getState() == Jalopy.State.ERROR )
 240    {
 241  0 LOG.error( "[jalopy] " + currentFile + " could not be formatted." );
 242   
 243  0 if ( isFailOnError() )
 244    {
 245  0 throw new Exception( currentFile + " could not be formatted." );
 246    }
 247    }
 248    else
 249    {
 250  0 LOG.warn( "[jalopy] " + currentFile + " formatted with unknown state." );
 251    }
 252    }
 253   
 254    /**
 255    * Creates a new instance of a Jalopy formatter with all bean variables being set.
 256    *
 257    * @return Jalopy
 258    */
 259  2 private Jalopy createJalopy()
 260    {
 261  2 Jalopy jalopy = new Jalopy();
 262   
 263  2 try
 264    {
 265  2 if ( ( convention != null ) && convention.exists() )
 266    {
 267  2 LOG.debug( "[jalopy] Using convention file : " + convention.getAbsoluteFile() );
 268  2 Jalopy.setConvention( convention );
 269    }
 270    else
 271    {
 272  0 LOG.warn( "[jalopy] Convention file does not exist: " + convention.getAbsoluteFile() );
 273  0 LOG.warn( "[jalopy] Using default convention : jalopy.xml");
 274  0 InputStream in = this.getClass().getClassLoader().getResourceAsStream( "plugin-resources/jalopy.xml" );
 275  0 Convention.importSettings( in, Convention.EXTENSION_XML );
 276    }
 277    }
 278    catch ( IOException ex )
 279    {
 280  0 ex.printStackTrace();
 281    }
 282   
 283  2 jalopy.setFileFormat( getFileFormat() );
 284   
 285  2 jalopy.setInspect( isInspect() );
 286   
 287  2 if ( !getHistoryPolicy().equalsIgnoreCase( "none" ) )
 288    {
 289  0 jalopy.setHistoryPolicy( History.Policy.valueOf( getHistoryPolicy() ) );
 290    }
 291   
 292    // TODO: what are the allowed values for historyMethod?
 293    //Convention settings = Convention.getInstance();
 294    //History.Method historyMethod = History.Method.valueOf( settings.get( ConventionKeys.HISTORY_METHOD,
 295    // ConventionDefaults.HISTORY_METHOD ) );
 296    //jalopy.setHistoryMethod( historyMethod );
 297   
 298  2 jalopy.setBackup( isBackup() );
 299   
 300  2 jalopy.setForce( isForce() );
 301   
 302  2 return jalopy;
 303    }
 304   
 305    /**
 306    * Returns the fileFormat property.
 307    *
 308    * @return String
 309    */
 310  3 public String getFileFormat()
 311    {
 312  3 return fileFormat;
 313    }
 314   
 315    /**
 316    * Sets the fileFormat property.
 317    *
 318    * @param newfileFormat The new property
 319    */
 320  2 public void setFileFormat( String newfileFormat )
 321    {
 322  2 this.fileFormat = newfileFormat;
 323    }
 324   
 325    /**
 326    * Returns the failOnError property.
 327    *
 328    * @return boolean
 329    */
 330  1 public boolean isFailOnError()
 331    {
 332  1 return failOnError;
 333    }
 334   
 335    /**
 336    * Sets the failOnError property.
 337    *
 338    * @param newfailOnError The new property
 339    */
 340  2 public void setFailOnError( boolean newfailOnError )
 341    {
 342  2 this.failOnError = newfailOnError;
 343    }
 344   
 345    /**
 346    * Returns the inspect property.
 347    *
 348    * @return boolean
 349    */
 350  2 public boolean isInspect()
 351    {
 352  2 return inspect;
 353    }
 354   
 355    /**
 356    * Sets the inspect property.
 357    *
 358    * @param newinspect The new property
 359    */
 360  1 public void setInspect( boolean newinspect )
 361    {
 362  1 this.inspect = newinspect;
 363    }
 364   
 365    /**
 366    * Returns the backup property.
 367    *
 368    * @return boolean
 369    */
 370  6 public boolean isBackup()
 371    {
 372  6 return backup;
 373    }
 374   
 375    /**
 376    * Sets the backup property.
 377    *
 378    * @param newbackup The new property
 379    */
 380  1 public void setBackup( boolean newbackup )
 381    {
 382  1 this.backup = newbackup;
 383    }
 384   
 385    /**
 386    * Returns the force property.
 387    *
 388    * @return boolean
 389    */
 390  2 public boolean isForce()
 391    {
 392  2 return force;
 393    }
 394   
 395    /**
 396    * Sets the force property.
 397    *
 398    * @param newforce The new property
 399    */
 400  1 public void setForce( boolean newforce )
 401    {
 402  1 this.force = newforce;
 403    }
 404   
 405    /**
 406    * Returns the sourceDirectory property.
 407    *
 408    * @return File
 409    */
 410  5 public File getSourceDirectory()
 411    {
 412  5 return sourceDirectory;
 413    }
 414   
 415    /**
 416    * Sets the sourceDirectory property.
 417    *
 418    * @param newsourceDirectory The new property
 419    */
 420  2 public void setSourceDirectory( File newsourceDirectory )
 421    {
 422  2 this.sourceDirectory = newsourceDirectory;
 423    }
 424   
 425    /**
 426    * Returns the srcIncludesPattern property.
 427    *
 428    * @return String
 429    */
 430  2 public String getSrcIncludesPattern()
 431    {
 432  2 return srcIncludesPattern;
 433    }
 434   
 435    /**
 436    * Sets the srcIncludesPattern property.
 437    *
 438    * @param newsrcIncludesPattern The new property
 439    */
 440  2 public void setSrcIncludesPattern( String newsrcIncludesPattern )
 441    {
 442  2 this.srcIncludesPattern = newsrcIncludesPattern;
 443    }
 444   
 445    /**
 446    * Returns the srcExcludesPattern property.
 447    *
 448    * @return String
 449    */
 450  2 public String getSrcExcludesPattern()
 451    {
 452  2 return srcExcludesPattern;
 453    }
 454   
 455    /**
 456    * Sets the srcExcludesPattern property.
 457    *
 458    * @param newsrcExcludesPattern The new property
 459    */
 460  2 public void setSrcExcludesPattern( String newsrcExcludesPattern )
 461    {
 462  2 this.srcExcludesPattern = newsrcExcludesPattern;
 463    }
 464   
 465    /**
 466    * Returns the historyPolicy property.
 467    *
 468    * @return String
 469    */
 470  3 public String getHistoryPolicy()
 471    {
 472  3 return historyPolicy;
 473    }
 474   
 475    /**
 476    * Sets the historyPolicy property.
 477    *
 478    * @param newhistoryPolicy The new property
 479    */
 480  2 public void setHistoryPolicy( String newhistoryPolicy )
 481    {
 482  2 this.historyPolicy = newhistoryPolicy;
 483    }
 484   
 485    /**
 486    * Returns the convention property.
 487    *
 488    * @return File
 489    */
 490  0 public File getConvention()
 491    {
 492  0 return convention;
 493    }
 494   
 495    /**
 496    * Sets the convention property.
 497    *
 498    * @param newconvention The new property
 499    */
 500  1 public void setConvention( File newconvention )
 501    {
 502  1 this.convention = newconvention;
 503    }
 504   
 505    /**
 506    * Returns the testSourceDirectory property.
 507    *
 508    * @return File
 509    */
 510  4 public File getTestSourceDirectory()
 511    {
 512  4 return testSourceDirectory;
 513    }
 514   
 515    /**
 516    * Sets the testSourceDirectory property.
 517    *
 518    * @param newtestSourceDirectory The new property
 519    */
 520  1 public void setTestSourceDirectory( File newtestSourceDirectory )
 521    {
 522  1 this.testSourceDirectory = newtestSourceDirectory;
 523    }
 524   
 525    /**
 526    * Returns the testIncludesPattern property.
 527    *
 528    * @return String
 529    */
 530  1 public String getTestIncludesPattern()
 531    {
 532  1 return testIncludesPattern;
 533    }
 534   
 535    /**
 536    * Sets the testIncludesPattern property.
 537    *
 538    * @param newtestIncludesPattern The new property
 539    */
 540  1 public void setTestIncludesPattern( String newtestIncludesPattern )
 541    {
 542  1 this.testIncludesPattern = newtestIncludesPattern;
 543    }
 544   
 545    /**
 546    * Returns the testExcludesPattern property.
 547    *
 548    * @return String
 549    */
 550  1 public String getTestExcludesPattern()
 551    {
 552  1 return testExcludesPattern;
 553    }
 554   
 555    /**
 556    * Sets the testExcludesPattern property.
 557    *
 558    * @param newtestExcludesPattern The new property
 559    */
 560  1 public void setTestExcludesPattern( String newtestExcludesPattern )
 561    {
 562  1 this.testExcludesPattern = newtestExcludesPattern;
 563    }
 564    }