1 17 18 package org.apache.tools.ant.taskdefs.optional.sitraka; 19 20 import java.io.File ; 21 import java.io.FileWriter ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.util.Vector ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.Execute; 29 import org.apache.tools.ant.taskdefs.LogStreamHandler; 30 import org.apache.tools.ant.types.Commandline; 31 import org.apache.tools.ant.types.FileSet; 32 33 38 public class CovMerge extends CovBase { 39 40 41 private File tofile = null; 42 43 44 private Vector filesets = new Vector (); 45 46 private boolean verbose; 47 48 51 public void setTofile(File value) { 52 this.tofile = value; 53 } 54 55 59 public void setVerbose(boolean flag) { 60 this.verbose = flag; 61 } 62 63 66 public void addFileset(FileSet fs) { 67 filesets.addElement(fs); 68 } 69 70 72 public CovMerge() { 73 } 74 75 76 public void execute() throws BuildException { 77 checkOptions(); 78 79 File paramfile = createParamFile(); 80 try { 81 Commandline cmdl = new Commandline(); 82 cmdl.setExecutable(findExecutable("jpcovmerge")); 83 if (verbose) { 84 cmdl.createArgument().setValue("-v"); 85 } 86 cmdl.createArgument().setValue(getParamFileArgument() 87 + paramfile.getAbsolutePath()); 88 89 if (isJProbe4Plus()) { 90 cmdl.createArgument().setValue(tofile.getPath()); 93 } 94 95 LogStreamHandler handler 96 = new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN); 97 Execute exec = new Execute(handler); 98 log(cmdl.describeCommand(), Project.MSG_VERBOSE); 99 exec.setCommandline(cmdl.getCommandline()); 100 101 int exitValue = exec.execute(); 104 if (Execute.isFailure(exitValue)) { 105 throw new BuildException("JProbe Coverage Merging failed (" + exitValue + ")"); 106 } 107 } catch (IOException e) { 108 throw new BuildException("Failed to run JProbe Coverage Merge: " + e); 109 } finally { 110 paramfile.delete(); 112 } 113 } 114 115 116 protected void checkOptions() throws BuildException { 117 if (tofile == null) { 118 throw new BuildException("'tofile' attribute must be set."); 119 } 120 121 if (getHome() == null || !getHome().isDirectory()) { 123 throw new BuildException("Invalid home directory. Must point to JProbe home directory"); 124 } 125 File jar = findCoverageJar(); 126 if (!jar.exists()) { 127 throw new BuildException("Cannot find Coverage directory: " + getHome()); 128 } 129 } 130 131 132 protected File [] getSnapshots() { 133 Vector v = new Vector (); 134 final int size = filesets.size(); 135 for (int i = 0; i < size; i++) { 136 FileSet fs = (FileSet) filesets.elementAt(i); 137 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 138 ds.scan(); 139 String [] f = ds.getIncludedFiles(); 140 for (int j = 0; j < f.length; j++) { 141 String pathname = f[j]; 142 File file = new File (ds.getBasedir(), pathname); 143 file = getProject().resolveFile(file.getPath()); 144 v.addElement(file); 145 } 146 } 147 148 File [] files = new File [v.size()]; 149 v.copyInto(files); 150 return files; 151 } 152 153 154 158 protected File createParamFile() throws BuildException { 159 File [] snapshots = getSnapshots(); 160 File file = createTempFile("jpcovm"); 161 file.deleteOnExit(); 162 FileWriter fw = null; 163 try { 164 fw = new FileWriter (file); 165 PrintWriter pw = new PrintWriter (fw); 166 for (int i = 0; i < snapshots.length; i++) { 167 pw.println(snapshots[i].getAbsolutePath()); 168 } 169 if (!isJProbe4Plus()) { 170 pw.println(getProject().resolveFile(tofile.getPath())); 173 } 174 pw.flush(); 175 } catch (IOException e) { 176 throw new BuildException("I/O error while writing to " + file, e); 177 } finally { 178 if (fw != null) { 179 try { 180 fw.close(); 181 } catch (IOException ignored) { 182 } 183 } 184 } 185 return file; 186 } 187 188 } 189 | Popular Tags |