1 58 59 package net.sourceforge.cobertura.ant; 60 61 import java.net.URL ; 62 import java.net.URLClassLoader ; 63 import java.util.HashSet ; 64 import java.util.Iterator ; 65 import java.util.Set ; 66 67 import org.apache.tools.ant.AntClassLoader; 68 import org.apache.tools.ant.BuildException; 69 import org.apache.tools.ant.taskdefs.Java; 70 import org.apache.tools.ant.taskdefs.MatchingTask; 71 import org.apache.tools.ant.types.Path; 72 import org.apache.tools.ant.types.Reference; 73 74 79 public class CheckTask extends MatchingTask 80 { 81 82 private String dataFile = null; 83 84 final Set regexes = new HashSet (); 85 86 private String branchRate = null; 87 88 private String lineRate = null; 89 90 private String packageBranchRate = null; 91 92 private String packageLineRate = null; 93 94 private String totalBranchRate = null; 95 96 private String totalLineRate = null; 97 98 private String failureProperty = null; 99 100 private boolean haltOnFailure = true; 101 102 private Java java = null; 103 104 public void execute() throws BuildException 105 { 106 if (dataFile != null) 107 { 108 getJava().createArg().setValue("--datafile"); 109 getJava().createArg().setValue(dataFile); 110 } 111 112 if (branchRate != null) 113 { 114 getJava().createArg().setValue("--branch"); 115 getJava().createArg().setValue(branchRate); 116 } 117 118 if (lineRate != null) 119 { 120 getJava().createArg().setValue("--line"); 121 getJava().createArg().setValue(lineRate); 122 } 123 124 if (packageBranchRate != null) 125 { 126 getJava().createArg().setValue("--packagebranch"); 127 getJava().createArg().setValue(packageBranchRate); 128 } 129 130 if (packageLineRate != null) 131 { 132 getJava().createArg().setValue("--packageline"); 133 getJava().createArg().setValue(packageLineRate); 134 } 135 136 if (totalBranchRate != null) 137 { 138 getJava().createArg().setValue("--totalbranch"); 139 getJava().createArg().setValue(totalBranchRate); 140 } 141 142 if (totalLineRate != null) 143 { 144 getJava().createArg().setValue("--totalline"); 145 getJava().createArg().setValue(totalLineRate); 146 } 147 148 Iterator iter = regexes.iterator(); 149 while (iter.hasNext()) 150 { 151 getJava().createArg().setValue("--regex"); 152 getJava().createArg().setValue(iter.next().toString()); 153 } 154 155 AntUtil.transferCoberturaDataFileProperty(getJava()); 156 int returnCode = getJava().executeJava(); 157 158 if (returnCode == 0) 160 { 161 System.out.println("All checks passed."); 162 } 163 else 164 { 165 if (haltOnFailure) 166 throw new BuildException( 167 "Coverage check failed. See messages above."); 168 else if (failureProperty != null) 169 getProject().setProperty(failureProperty, "true"); 170 else 171 System.err 172 .println("Coverage check failed. See messages above."); 173 } 174 } 175 176 public Regex createRegex() 177 { 178 Regex regex = new Regex(); 179 regexes.add(regex); 180 return regex; 181 } 182 183 protected Java getJava() 184 { 185 if (java == null) 186 { 187 java = (Java)getProject().createTask("java"); 188 java.setTaskName(getTaskName()); 189 java.setClassname("net.sourceforge.cobertura.check.Main"); 190 java.setFork(true); 191 java.setDir(getProject().getBaseDir()); 192 193 if (getClass().getClassLoader() instanceof AntClassLoader) 194 { 195 createClasspath().setPath( 196 ((AntClassLoader)getClass().getClassLoader()) 197 .getClasspath()); 198 } 199 else if (getClass().getClassLoader() instanceof URLClassLoader ) 200 { 201 URL [] earls = ((URLClassLoader )getClass().getClassLoader()) 202 .getURLs(); 203 for (int i = 0; i < earls.length; i++) 204 { 205 createClasspath().setPath(earls[i].getFile()); 206 } 207 } 208 } 209 210 return java; 211 } 212 213 public Path createClasspath() 214 { 215 return getJava().createClasspath().createPath(); 216 } 217 218 public void setClasspath(Path classpath) 219 { 220 createClasspath().append(classpath); 221 } 222 223 public void setClasspathRef(Reference r) 224 { 225 createClasspath().setRefid(r); 226 } 227 228 public void setDataFile(String dataFile) 229 { 230 this.dataFile = dataFile; 231 } 232 233 public void setBranchRate(String branchRate) 234 { 235 this.branchRate = branchRate; 236 } 237 238 public void setLineRate(String lineRate) 239 { 240 this.lineRate = lineRate; 241 } 242 243 public void setPackageBranchRate(String packageBranchRate) 244 { 245 this.packageBranchRate = packageBranchRate; 246 } 247 248 public void setPackageLineRate(String packageLineRate) 249 { 250 this.packageLineRate = packageLineRate; 251 } 252 253 public void setTotalBranchRate(String totalBranchRate) 254 { 255 this.totalBranchRate = totalBranchRate; 256 } 257 258 public void setTotalLineRate(String totalLineRate) 259 { 260 this.totalLineRate = totalLineRate; 261 } 262 263 public void setFailureProperty(String failureProperty) 264 { 265 this.failureProperty = failureProperty; 266 } 267 268 public void setHaltOnFailure(boolean haltOnFailure) 269 { 270 this.haltOnFailure = haltOnFailure; 271 } 272 273 } 274 | Popular Tags |