Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 238   Methods: 5
NCLOC: 100   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
BuildNumber.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs;
 55   
 
 56   
 import java.io.File;
 57   
 import java.io.FileInputStream;
 58   
 import java.io.FileOutputStream;
 59   
 import java.io.IOException;
 60   
 import java.util.Properties;
 61   
 import org.apache.tools.ant.BuildException;
 62   
 import org.apache.tools.ant.Task;
 63   
 import org.apache.tools.ant.util.FileUtils;
 64   
 
 65   
 /**
 66   
  * Read, increment, and write a build number in a file
 67   
  * It will first
 68   
  * attempt to read a build number from a file, then set the property
 69   
  * "build.number" to the value that was read in (or 0 if no such value). Then
 70   
  * it will increment the build number by one and write it back out into the
 71   
  * file.
 72   
  *
 73   
  * @author <a href="mailto:peter@apache.org">Peter Donald</a>
 74   
  * @version $Revision: 1.10 $ $Date: 2003/02/10 14:13:34 $
 75   
  * @since Ant 1.5
 76   
  * @ant.task name="buildnumber"
 77   
  */
 78   
 public class BuildNumber
 79   
      extends Task {
 80   
     /**
 81   
      * The name of the property in which the build number is stored.
 82   
      */
 83   
     private static final String DEFAULT_PROPERTY_NAME = "build.number";
 84   
 
 85   
     /** The default filename to use if no file specified.  */
 86   
     private static final String DEFAULT_FILENAME = DEFAULT_PROPERTY_NAME;
 87   
 
 88   
     /** The File in which the build number is stored.  */
 89   
     private File m_file;
 90   
 
 91   
 
 92   
     /**
 93   
      * The file in which the build number is stored. Defaults to
 94   
      * "build.number" if not specified.
 95   
      *
 96   
      * @param file the file in which build number is stored.
 97   
      */
 98  0
     public void setFile(final File file) {
 99  0
         m_file = file;
 100   
     }
 101   
 
 102   
 
 103   
     /**
 104   
      * Run task.
 105   
      *
 106   
      * @exception BuildException if an error occurs
 107   
      */
 108  0
     public void execute()
 109   
          throws BuildException {
 110  0
         File savedFile = m_file;// may be altered in validate
 111   
 
 112  0
         validate();
 113   
 
 114  0
         final Properties properties = loadProperties();
 115  0
         final int buildNumber = getBuildNumber(properties);
 116   
 
 117  0
         properties.put(DEFAULT_PROPERTY_NAME,
 118   
             String.valueOf(buildNumber + 1));
 119   
 
 120   
         // Write the properties file back out
 121  0
         FileOutputStream output = null;
 122   
 
 123  0
         try {
 124  0
             output = new FileOutputStream(m_file);
 125   
 
 126  0
             final String header = "Build Number for ANT. Do not edit!";
 127   
 
 128  0
             properties.save(output, header);
 129   
         } catch (final IOException ioe) {
 130  0
             final String message = "Error while writing " + m_file;
 131   
 
 132  0
             throw new BuildException(message, ioe);
 133   
         } finally {
 134  0
             if (null != output) {
 135  0
                 try {
 136  0
                     output.close();
 137   
                 } catch (final IOException ioe) {
 138   
                 }
 139   
             }
 140  0
             m_file = savedFile;
 141   
         }
 142   
 
 143   
         //Finally set the property
 144  0
         getProject().setNewProperty(DEFAULT_PROPERTY_NAME,
 145   
             String.valueOf(buildNumber));
 146   
     }
 147   
 
 148   
 
 149   
     /**
 150   
      * Utility method to retrieve build number from properties object.
 151   
      *
 152   
      * @param properties the properties to retrieve build number from
 153   
      * @return the build number or if no number in properties object
 154   
      * @throws BuildException if build.number property is not an integer
 155   
      */
 156  0
     private int getBuildNumber(final Properties properties)
 157   
          throws BuildException {
 158  0
         final String buildNumber =
 159   
             properties.getProperty(DEFAULT_PROPERTY_NAME, "0").trim();
 160   
 
 161   
         // Try parsing the line into an integer.
 162  0
         try {
 163  0
             return Integer.parseInt(buildNumber);
 164   
         } catch (final NumberFormatException nfe) {
 165  0
             final String message =
 166   
                 m_file + " contains a non integer build number: " + buildNumber;
 167   
 
 168  0
             throw new BuildException(message, nfe);
 169   
         }
 170   
     }
 171   
 
 172   
 
 173   
     /**
 174   
      * Utility method to load properties from file.
 175   
      *
 176   
      * @return the loaded properties
 177   
      * @throws BuildException
 178   
      */
 179  0
     private Properties loadProperties()
 180   
          throws BuildException {
 181  0
         FileInputStream input = null;
 182   
 
 183  0
         try {
 184  0
             final Properties properties = new Properties();
 185   
 
 186  0
             input = new FileInputStream(m_file);
 187  0
             properties.load(input);
 188  0
             return properties;
 189   
         } catch (final IOException ioe) {
 190  0
             throw new BuildException(ioe);
 191   
         } finally {
 192  0
             if (null != input) {
 193  0
                 try {
 194  0
                     input.close();
 195   
                 } catch (final IOException ioe) {
 196   
                 }
 197   
             }
 198   
         }
 199   
     }
 200   
 
 201   
 
 202   
     /**
 203   
      * Validate that the task parameters are valid.
 204   
      *
 205   
      * @throws BuildException if parameters are invalid
 206   
      */
 207  0
     private void validate()
 208   
          throws BuildException {
 209  0
         if (null == m_file) {
 210  0
             m_file = getProject().resolveFile(DEFAULT_FILENAME);
 211   
         }
 212   
 
 213  0
         if (!m_file.exists()) {
 214  0
             try {
 215  0
                 FileUtils.newFileUtils().createNewFile(m_file);
 216   
             } catch (final IOException ioe) {
 217  0
                 final String message =
 218   
                     m_file + " doesn't exist and new file can't be created.";
 219   
 
 220  0
                 throw new BuildException(message, ioe);
 221   
             }
 222   
         }
 223   
 
 224  0
         if (!m_file.canRead()) {
 225  0
             final String message = "Unable to read from " + m_file + ".";
 226   
 
 227  0
             throw new BuildException(message);
 228   
         }
 229   
 
 230  0
         if (!m_file.canWrite()) {
 231  0
             final String message = "Unable to write to " + m_file + ".";
 232   
 
 233  0
             throw new BuildException(message);
 234   
         }
 235   
     }
 236   
 }
 237   
 
 238