001 /* 002 * Cobertura - http://cobertura.sourceforge.net/ 003 * 004 * Copyright (C) 2003 jcoverage ltd. 005 * Copyright (C) 2005 Mark Doliner <thekingant@users.sourceforge.net> 006 * 007 * Cobertura is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published 009 * by the Free Software Foundation; either version 2 of the License, 010 * or (at your option) any later version. 011 * 012 * Cobertura is distributed in the hope that it will be useful, but 013 * WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with Cobertura; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 020 * USA 021 */ 022 023 package net.sourceforge.cobertura.reporting; 024 025 import gnu.getopt.Getopt; 026 import gnu.getopt.LongOpt; 027 028 import java.io.File; 029 import java.io.FileInputStream; 030 import java.io.InputStream; 031 import java.io.ObjectInputStream; 032 import java.util.Map; 033 034 import net.sourceforge.cobertura.reporting.html.HTMLReport; 035 import net.sourceforge.cobertura.reporting.xml.XMLReport; 036 037 import org.apache.log4j.Logger; 038 039 public class Main 040 { 041 042 private static final Logger logger = Logger.getLogger(Main.class); 043 044 // TODO: make these not static? 045 static String format; 046 static File serializationFile; 047 static File sourceDir; 048 static File outputDir; 049 050 public static void main(String[] args) throws Exception 051 { 052 long startTime = System.currentTimeMillis(); 053 054 LongOpt[] longOpts = new LongOpt[4]; 055 longOpts[0] = new LongOpt("format", LongOpt.REQUIRED_ARGUMENT, null, 056 'f'); 057 longOpts[1] = new LongOpt("instrumentation", 058 LongOpt.REQUIRED_ARGUMENT, null, 'i'); 059 longOpts[2] = new LongOpt("output", LongOpt.REQUIRED_ARGUMENT, null, 060 'o'); 061 longOpts[3] = new LongOpt("source", LongOpt.REQUIRED_ARGUMENT, null, 062 's'); 063 064 Getopt g = new Getopt(Main.class.getName(), args, ":f:i:o:s:", 065 longOpts); 066 067 int c; 068 069 while ((c = g.getopt()) != -1) 070 { 071 switch (c) 072 { 073 case 'f': 074 format = new String(g.getOptarg()); 075 if (!format.equalsIgnoreCase("html") 076 && !format.equalsIgnoreCase("xml")) 077 { 078 throw new Exception("Error: format \"" + format 079 + "\" must be either html or xml"); 080 } 081 break; 082 083 case 'i': 084 serializationFile = new File(g.getOptarg()); 085 if (!serializationFile.exists()) 086 { 087 throw new Exception("Error: serialization file " 088 + serializationFile + " does not exist"); 089 } 090 if (serializationFile.isDirectory()) 091 { 092 throw new Exception("Error: serialization file " 093 + serializationFile 094 + " cannot be a directory"); 095 } 096 break; 097 098 case 'o': 099 outputDir = new File(g.getOptarg()); 100 if (outputDir.exists() && outputDir.isFile()) 101 { 102 throw new Exception("Error: destination directory " 103 + outputDir + " already exists and is a file"); 104 } 105 outputDir.mkdirs(); 106 break; 107 108 case 's': 109 sourceDir = new File(g.getOptarg()); 110 if (!sourceDir.exists()) 111 { 112 throw new Exception("Error: source directory " 113 + sourceDir + " does not exist"); 114 } 115 if (sourceDir.isFile()) 116 { 117 throw new Exception("Error: source directory " 118 + sourceDir 119 + " should be a directory, not a file"); 120 } 121 break; 122 } 123 } 124 125 if (logger.isDebugEnabled()) 126 { 127 logger.debug("format is " + format); 128 logger.debug("serializationFile is " 129 + serializationFile.getAbsolutePath()); 130 logger.debug("outputDir is " + outputDir.getAbsolutePath()); 131 logger.debug("sourceDir is " + sourceDir.getAbsolutePath()); 132 } 133 134 InputStream is = null; 135 ObjectInputStream objects = null; 136 try 137 { 138 is = new FileInputStream(serializationFile); 139 objects = new ObjectInputStream(is); 140 Map coverageData = (Map)objects.readObject(); 141 CoverageReport coverage = new CoverageReport(coverageData); 142 143 if (format.equalsIgnoreCase("xml")) 144 { 145 new XMLReport(coverage, outputDir, sourceDir); 146 } 147 else if (format.equalsIgnoreCase("html")) 148 { 149 new HTMLReport(coverage, outputDir, sourceDir); 150 } 151 } 152 finally 153 { 154 if (is != null) 155 is.close(); 156 if (objects != null) 157 objects.close(); 158 } 159 160 long stopTime = System.currentTimeMillis(); 161 System.out 162 .println("Reporting time: " + (stopTime - startTime) + "ms"); 163 } 164 165 }