| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CheckTask |
|
| 2.066666666666667;2.067 |
| 1 | /* | |
| 2 | * The Apache Software License, Version 1.1 | |
| 3 | * | |
| 4 | * Copyright (C) 2000-2002 The Apache Software Foundation. All rights | |
| 5 | * reserved. | |
| 6 | * Copyright (C) 2003 jcoverage ltd. | |
| 7 | * Copyright (C) 2005 Mark Doliner | |
| 8 | * Copyright (C) 2005 Nathan Wilson | |
| 9 | * Copyright (C) 2005 Alex Ruiz | |
| 10 | * | |
| 11 | * Redistribution and use in source and binary forms, with or without | |
| 12 | * modification, are permitted provided that the following conditions | |
| 13 | * are met: | |
| 14 | * | |
| 15 | * 1. Redistributions of source code must retain the above copyright | |
| 16 | * notice, this list of conditions and the following disclaimer. | |
| 17 | * | |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 19 | * notice, this list of conditions and the following disclaimer in | |
| 20 | * the documentation and/or other materials provided with the | |
| 21 | * distribution. | |
| 22 | * | |
| 23 | * 3. The end-user documentation included with the redistribution, if | |
| 24 | * any, must include the following acknowlegement: | |
| 25 | * "This product includes software developed by the | |
| 26 | * Apache Software Foundation (http://www.apache.org/)." | |
| 27 | * Alternately, this acknowlegement may appear in the software itself, | |
| 28 | * if and wherever such third-party acknowlegements normally appear. | |
| 29 | * | |
| 30 | * 4. The names "Ant" and "Apache Software | |
| 31 | * Foundation" must not be used to endorse or promote products derived | |
| 32 | * from this software without prior written permission. For written | |
| 33 | * permission, please contact apache@apache.org. | |
| 34 | * | |
| 35 | * 5. Products derived from this software may not be called "Apache" | |
| 36 | * nor may "Apache" appear in their names without prior written | |
| 37 | * permission of the Apache Group. | |
| 38 | * | |
| 39 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |
| 40 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
| 41 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
| 42 | * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR | |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 45 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF | |
| 46 | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
| 47 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 48 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 49 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 50 | * SUCH DAMAGE. | |
| 51 | * ==================================================================== | |
| 52 | * | |
| 53 | * This software consists of voluntary contributions made by many | |
| 54 | * individuals on behalf of the Apache Software Foundation. For more | |
| 55 | * information on the Apache Software Foundation, please see | |
| 56 | * <http://www.apache.org/>. | |
| 57 | */ | |
| 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 | /** | |
| 75 | * An ant task that can be used to optionally fail an ant build if | |
| 76 | * the coverage percentage for lines or branches is below a certain, | |
| 77 | * user specifiable threshold. | |
| 78 | */ | |
| 79 | 0 | public class CheckTask extends MatchingTask |
| 80 | { | |
| 81 | ||
| 82 | 0 | private String dataFile = null; |
| 83 | ||
| 84 | 0 | final Set regexes = new HashSet(); |
| 85 | ||
| 86 | 0 | private String branchRate = null; |
| 87 | ||
| 88 | 0 | private String lineRate = null; |
| 89 | ||
| 90 | 0 | private String packageBranchRate = null; |
| 91 | ||
| 92 | 0 | private String packageLineRate = null; |
| 93 | ||
| 94 | 0 | private String totalBranchRate = null; |
| 95 | ||
| 96 | 0 | private String totalLineRate = null; |
| 97 | ||
| 98 | 0 | private String failureProperty = null; |
| 99 | ||
| 100 | 0 | private boolean haltOnFailure = true; |
| 101 | ||
| 102 | 0 | private Java java = null; |
| 103 | ||
| 104 | public void execute() throws BuildException | |
| 105 | { | |
| 106 | 0 | if (dataFile != null) |
| 107 | { | |
| 108 | 0 | getJava().createArg().setValue("--datafile"); |
| 109 | 0 | getJava().createArg().setValue(dataFile); |
| 110 | } | |
| 111 | ||
| 112 | 0 | if (branchRate != null) |
| 113 | { | |
| 114 | 0 | getJava().createArg().setValue("--branch"); |
| 115 | 0 | getJava().createArg().setValue(branchRate); |
| 116 | } | |
| 117 | ||
| 118 | 0 | if (lineRate != null) |
| 119 | { | |
| 120 | 0 | getJava().createArg().setValue("--line"); |
| 121 | 0 | getJava().createArg().setValue(lineRate); |
| 122 | } | |
| 123 | ||
| 124 | 0 | if (packageBranchRate != null) |
| 125 | { | |
| 126 | 0 | getJava().createArg().setValue("--packagebranch"); |
| 127 | 0 | getJava().createArg().setValue(packageBranchRate); |
| 128 | } | |
| 129 | ||
| 130 | 0 | if (packageLineRate != null) |
| 131 | { | |
| 132 | 0 | getJava().createArg().setValue("--packageline"); |
| 133 | 0 | getJava().createArg().setValue(packageLineRate); |
| 134 | } | |
| 135 | ||
| 136 | 0 | if (totalBranchRate != null) |
| 137 | { | |
| 138 | 0 | getJava().createArg().setValue("--totalbranch"); |
| 139 | 0 | getJava().createArg().setValue(totalBranchRate); |
| 140 | } | |
| 141 | ||
| 142 | 0 | if (totalLineRate != null) |
| 143 | { | |
| 144 | 0 | getJava().createArg().setValue("--totalline"); |
| 145 | 0 | getJava().createArg().setValue(totalLineRate); |
| 146 | } | |
| 147 | ||
| 148 | 0 | Iterator iter = regexes.iterator(); |
| 149 | 0 | while (iter.hasNext()) |
| 150 | { | |
| 151 | 0 | getJava().createArg().setValue("--regex"); |
| 152 | 0 | getJava().createArg().setValue(iter.next().toString()); |
| 153 | 0 | } |
| 154 | ||
| 155 | 0 | AntUtil.transferCoberturaDataFileProperty(getJava()); |
| 156 | 0 | int returnCode = getJava().executeJava(); |
| 157 | ||
| 158 | // Check the return code and print a message | |
| 159 | 0 | if (returnCode == 0) |
| 160 | { | |
| 161 | 0 | System.out.println("All checks passed."); |
| 162 | 0 | } |
| 163 | else | |
| 164 | { | |
| 165 | 0 | if (haltOnFailure) |
| 166 | 0 | throw new BuildException( |
| 167 | "Coverage check failed. See messages above."); | |
| 168 | 0 | else if (failureProperty != null) |
| 169 | 0 | getProject().setProperty(failureProperty, "true"); |
| 170 | else | |
| 171 | 0 | System.err |
| 172 | .println("Coverage check failed. See messages above."); | |
| 173 | } | |
| 174 | 0 | } |
| 175 | ||
| 176 | public Regex createRegex() | |
| 177 | { | |
| 178 | 0 | Regex regex = new Regex(); |
| 179 | 0 | regexes.add(regex); |
| 180 | 0 | return regex; |
| 181 | } | |
| 182 | ||
| 183 | protected Java getJava() | |
| 184 | { | |
| 185 | 0 | if (java == null) |
| 186 | { | |
| 187 | 0 | java = (Java)getProject().createTask("java"); |
| 188 | 0 | java.setTaskName(getTaskName()); |
| 189 | 0 | java.setClassname("net.sourceforge.cobertura.check.Main"); |
| 190 | 0 | java.setFork(true); |
| 191 | 0 | java.setDir(getProject().getBaseDir()); |
| 192 | ||
| 193 | 0 | if (getClass().getClassLoader() instanceof AntClassLoader) |
| 194 | { | |
| 195 | 0 | createClasspath().setPath( |
| 196 | ((AntClassLoader)getClass().getClassLoader()) | |
| 197 | .getClasspath()); | |
| 198 | 0 | } |
| 199 | 0 | else if (getClass().getClassLoader() instanceof URLClassLoader) |
| 200 | { | |
| 201 | 0 | URL[] earls = ((URLClassLoader)getClass().getClassLoader()) |
| 202 | .getURLs(); | |
| 203 | 0 | for (int i = 0; i < earls.length; i++) |
| 204 | { | |
| 205 | 0 | createClasspath().setPath(earls[i].getFile()); |
| 206 | } | |
| 207 | } | |
| 208 | } | |
| 209 | ||
| 210 | 0 | return java; |
| 211 | } | |
| 212 | ||
| 213 | public Path createClasspath() | |
| 214 | { | |
| 215 | 0 | return getJava().createClasspath().createPath(); |
| 216 | } | |
| 217 | ||
| 218 | public void setClasspath(Path classpath) | |
| 219 | { | |
| 220 | 0 | createClasspath().append(classpath); |
| 221 | 0 | } |
| 222 | ||
| 223 | public void setClasspathRef(Reference r) | |
| 224 | { | |
| 225 | 0 | createClasspath().setRefid(r); |
| 226 | 0 | } |
| 227 | ||
| 228 | public void setDataFile(String dataFile) | |
| 229 | { | |
| 230 | 0 | this.dataFile = dataFile; |
| 231 | 0 | } |
| 232 | ||
| 233 | public void setBranchRate(String branchRate) | |
| 234 | { | |
| 235 | 0 | this.branchRate = branchRate; |
| 236 | 0 | } |
| 237 | ||
| 238 | public void setLineRate(String lineRate) | |
| 239 | { | |
| 240 | 0 | this.lineRate = lineRate; |
| 241 | 0 | } |
| 242 | ||
| 243 | public void setPackageBranchRate(String packageBranchRate) | |
| 244 | { | |
| 245 | 0 | this.packageBranchRate = packageBranchRate; |
| 246 | 0 | } |
| 247 | ||
| 248 | public void setPackageLineRate(String packageLineRate) | |
| 249 | { | |
| 250 | 0 | this.packageLineRate = packageLineRate; |
| 251 | 0 | } |
| 252 | ||
| 253 | public void setTotalBranchRate(String totalBranchRate) | |
| 254 | { | |
| 255 | 0 | this.totalBranchRate = totalBranchRate; |
| 256 | 0 | } |
| 257 | ||
| 258 | public void setTotalLineRate(String totalLineRate) | |
| 259 | { | |
| 260 | 0 | this.totalLineRate = totalLineRate; |
| 261 | 0 | } |
| 262 | ||
| 263 | public void setFailureProperty(String failureProperty) | |
| 264 | { | |
| 265 | 0 | this.failureProperty = failureProperty; |
| 266 | 0 | } |
| 267 | ||
| 268 | public void setHaltOnFailure(boolean haltOnFailure) | |
| 269 | { | |
| 270 | 0 | this.haltOnFailure = haltOnFailure; |
| 271 | 0 | } |
| 272 | ||
| 273 | } |