Coverage Report - net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
CoverageDataFileHandler
N/A
N/A
5.2
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2003 jcoverage ltd.
 5  
  * Copyright (C) 2005 Mark Doliner
 6  
  * Copyright (C) 2007 Joakim Erdfelt
 7  
  *
 8  
  * Cobertura is free software; you can redistribute it and/or modify
 9  
  * it under the terms of the GNU General Public License as published
 10  
  * by the Free Software Foundation; either version 2 of the License,
 11  
  * or (at your option) any later version.
 12  
  *
 13  
  * Cobertura is distributed in the hope that it will be useful, but
 14  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 16  
  * General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU General Public License
 19  
  * along with Cobertura; if not, write to the Free Software
 20  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 21  
  * USA
 22  
  */
 23  
 
 24  
 package net.sourceforge.cobertura.coveragedata;
 25  
 
 26  
 import net.sourceforge.cobertura.util.ConfigurationUtil;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.FileInputStream;
 30  
 import java.io.FileOutputStream;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.io.ObjectInputStream;
 34  
 import java.io.ObjectOutputStream;
 35  
 import java.io.OutputStream;
 36  
 
 37  
 /**
 38  
  * This contains methods used for reading and writing the
 39  
  * "cobertura.ser" file.
 40  
  */
 41  
 public abstract class CoverageDataFileHandler implements HasBeenInstrumented
 42  
 {
 43  
         private static File defaultFile = null;
 44  
 
 45  
         public static File getDefaultDataFile()
 46  
         {
 47  
                 // return cached defaultFile
 48  
                 if (defaultFile != null) 
 49  
                 {
 50  
                         return defaultFile;
 51  
                 }
 52  
 
 53  
                 // load and cache datafile configuration
 54  
                 ConfigurationUtil config = new ConfigurationUtil();
 55  
                 defaultFile = new File(config.getDatafile());
 56  
         
 57  
                 return defaultFile;
 58  
         }
 59  
 
 60  
         public static ProjectData loadCoverageData(File dataFile)
 61  
         {
 62  
                 InputStream is = null;
 63  
 
 64  
                 //System.out.println("Cobertura: Loading coverage data from " + dataFile.getAbsolutePath());
 65  
                 try
 66  
                 {
 67  
                         is = new FileInputStream(dataFile);
 68  
                         return loadCoverageData(is);
 69  
                 }
 70  
                 catch (IOException e)
 71  
                 {
 72  
                         System.err.println("Cobertura: Error reading file "
 73  
                                         + dataFile.getAbsolutePath() + ": "
 74  
                                         + e.getLocalizedMessage());
 75  
                         return null;
 76  
                 }
 77  
                 finally
 78  
                 {
 79  
                         if (is != null)
 80  
                                 try
 81  
                                 {
 82  
                                         is.close();
 83  
                                 }
 84  
                                 catch (IOException e)
 85  
                                 {
 86  
                                         System.err.println("Cobertura: Error closing file "
 87  
                                                         + dataFile.getAbsolutePath() + ": "
 88  
                                                         + e.getLocalizedMessage());
 89  
                                 }
 90  
                 }
 91  
         }
 92  
 
 93  
         private static ProjectData loadCoverageData(InputStream dataFile) throws IOException
 94  
         {
 95  
                 ObjectInputStream objects = null;
 96  
 
 97  
                 try
 98  
                 {
 99  
                         objects = new ObjectInputStream(dataFile);
 100  
                         ProjectData projectData = (ProjectData)objects.readObject();
 101  
                         System.out.println("Cobertura: Loaded information on "
 102  
                                         + projectData.getNumberOfClasses() + " classes.");
 103  
                         return projectData;
 104  
                 }
 105  
                 catch (IOException e) {
 106  
                         throw e;
 107  
                 }
 108  
                 catch (Exception e)
 109  
                 {
 110  
                         System.err.println("Cobertura: Error reading from object stream.");
 111  
                         e.printStackTrace();
 112  
                         return null;
 113  
                 }
 114  
                 finally
 115  
                 {
 116  
                         if (objects != null)
 117  
                         {
 118  
                                 try
 119  
                                 {
 120  
                                         objects.close();
 121  
                                 }
 122  
                                 catch (IOException e)
 123  
                                 {
 124  
                                         System.err
 125  
                                                         .println("Cobertura: Error closing object stream.");
 126  
                                         e.printStackTrace();
 127  
                                 }
 128  
                         }
 129  
                 }
 130  
         }
 131  
 
 132  
         public static void saveCoverageData(ProjectData projectData,
 133  
                         File dataFile)
 134  
         {
 135  
                 FileOutputStream os = null;
 136  
 
 137  
                 //System.out.println("Cobertura: Saving coverage data to " + dataFile.getAbsolutePath());
 138  
                 try
 139  
                 {
 140  
                         File dataDir = dataFile.getParentFile();
 141  
                         if( (dataDir != null) && !dataDir.exists() )
 142  
                         {
 143  
                                 dataDir.mkdirs();
 144  
                         }
 145  
                         os = new FileOutputStream(dataFile);
 146  
                         saveCoverageData(projectData, os);
 147  
                 }
 148  
                 catch (IOException e)
 149  
                 {
 150  
                         System.err.println("Cobertura: Error writing file "
 151  
                                         + dataFile.getAbsolutePath());
 152  
                         e.printStackTrace();
 153  
                 }
 154  
                 finally
 155  
                 {
 156  
                         if (os != null)
 157  
                         {
 158  
                                 try
 159  
                                 {
 160  
                                         os.close();
 161  
                                 }
 162  
                                 catch (IOException e)
 163  
                                 {
 164  
                                         System.err.println("Cobertura: Error closing file "
 165  
                                                         + dataFile.getAbsolutePath());
 166  
                                         e.printStackTrace();
 167  
                                 }
 168  
                         }
 169  
                 }
 170  
         }
 171  
 
 172  
         private static void saveCoverageData(ProjectData projectData,
 173  
                         OutputStream dataFile)
 174  
         {
 175  
                 ObjectOutputStream objects = null;
 176  
         
 177  
                 try
 178  
                 {
 179  
                         objects = new ObjectOutputStream(dataFile);
 180  
                         objects.writeObject(projectData);
 181  
                         System.out.println("Cobertura: Saved information on " + projectData.getNumberOfClasses() + " classes.");
 182  
                 }
 183  
                 catch (IOException e)
 184  
                 {
 185  
                         System.err.println("Cobertura: Error writing to object stream.");
 186  
                         e.printStackTrace();
 187  
                 }
 188  
                 finally
 189  
                 {
 190  
                         if (objects != null)
 191  
                         {
 192  
                                 try
 193  
                                 {
 194  
                                         objects.close();
 195  
                                 }
 196  
                                 catch (IOException e)
 197  
                                 {
 198  
                                         System.err
 199  
                                                         .println("Cobertura: Error closing object stream.");
 200  
                                         e.printStackTrace();
 201  
                                 }
 202  
                         }
 203  
                 }
 204  
         }
 205  
 
 206  
 }