001 /*
002 * The Apache Software License, Version 1.1
003 *
004 * Copyright (C) 2000-2002 The Apache Software Foundation. All rights
005 * reserved.
006 * Copyright (C) 2003 jcoverage ltd.
007 * Copyright (C) 2005 Mark Doliner
008 * Copyright (C) 2005 Nathan Wilson
009 * Copyright (C) 2005 Alex Ruiz
010 *
011 * Redistribution and use in source and binary forms, with or without
012 * modification, are permitted provided that the following conditions
013 * are met:
014 *
015 * 1. Redistributions of source code must retain the above copyright
016 * notice, this list of conditions and the following disclaimer.
017 *
018 * 2. Redistributions in binary form must reproduce the above copyright
019 * notice, this list of conditions and the following disclaimer in
020 * the documentation and/or other materials provided with the
021 * distribution.
022 *
023 * 3. The end-user documentation included with the redistribution, if
024 * any, must include the following acknowlegement:
025 * "This product includes software developed by the
026 * Apache Software Foundation (http://www.apache.org/)."
027 * Alternately, this acknowlegement may appear in the software itself,
028 * if and wherever such third-party acknowlegements normally appear.
029 *
030 * 4. The names "Ant" and "Apache Software
031 * Foundation" must not be used to endorse or promote products derived
032 * from this software without prior written permission. For written
033 * permission, please contact apache@apache.org.
034 *
035 * 5. Products derived from this software may not be called "Apache"
036 * nor may "Apache" appear in their names without prior written
037 * permission of the Apache Group.
038 *
039 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
043 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050 * SUCH DAMAGE.
051 * ====================================================================
052 *
053 * This software consists of voluntary contributions made by many
054 * individuals on behalf of the Apache Software Foundation. For more
055 * information on the Apache Software Foundation, please see
056 * <http://www.apache.org/>.
057 */
058
059 package net.sourceforge.cobertura.ant;
060
061 import java.util.HashSet;
062 import java.util.Iterator;
063 import java.util.Set;
064
065 import org.apache.tools.ant.BuildException;
066
067 /**
068 * An ant task that can be used to optionally fail an ant build if
069 * the coverage percentage for lines or branches is below a certain,
070 * user specifiable threshold.
071 */
072 public class CheckTask extends CommonMatchingTask
073 {
074
075 private String dataFile = null;
076
077 final Set regexes = new HashSet();
078
079 private String branchRate = null;
080
081 private String lineRate = null;
082
083 private String packageBranchRate = null;
084
085 private String packageLineRate = null;
086
087 private String totalBranchRate = null;
088
089 private String totalLineRate = null;
090
091 private String failureProperty = null;
092
093 private boolean haltOnFailure = true;
094
095 public CheckTask() {
096 super("net.sourceforge.cobertura.check.Main");
097 }
098
099 public void execute() throws BuildException
100 {
101 if (dataFile != null)
102 {
103 getJava().createArg().setValue("--datafile");
104 getJava().createArg().setValue(dataFile);
105 }
106
107 if (branchRate != null)
108 {
109 getJava().createArg().setValue("--branch");
110 getJava().createArg().setValue(branchRate);
111 }
112
113 if (lineRate != null)
114 {
115 getJava().createArg().setValue("--line");
116 getJava().createArg().setValue(lineRate);
117 }
118
119 if (packageBranchRate != null)
120 {
121 getJava().createArg().setValue("--packagebranch");
122 getJava().createArg().setValue(packageBranchRate);
123 }
124
125 if (packageLineRate != null)
126 {
127 getJava().createArg().setValue("--packageline");
128 getJava().createArg().setValue(packageLineRate);
129 }
130
131 if (totalBranchRate != null)
132 {
133 getJava().createArg().setValue("--totalbranch");
134 getJava().createArg().setValue(totalBranchRate);
135 }
136
137 if (totalLineRate != null)
138 {
139 getJava().createArg().setValue("--totalline");
140 getJava().createArg().setValue(totalLineRate);
141 }
142
143 Iterator iter = regexes.iterator();
144 while (iter.hasNext())
145 {
146 getJava().createArg().setValue("--regex");
147 getJava().createArg().setValue(iter.next().toString());
148 }
149
150 AntUtil.transferCoberturaDataFileProperty(getJava());
151 int returnCode = getJava().executeJava();
152
153 // Check the return code and print a message
154 if (returnCode == 0)
155 {
156 System.out.println("All checks passed.");
157 }
158 else
159 {
160 if (haltOnFailure)
161 throw new BuildException(
162 "Coverage check failed. See messages above.");
163 else if (failureProperty != null)
164 getProject().setProperty(failureProperty, "true");
165 else
166 System.err
167 .println("Coverage check failed. See messages above.");
168 }
169 }
170
171 public Regex createRegex()
172 {
173 Regex regex = new Regex();
174 regexes.add(regex);
175 return regex;
176 }
177
178 public void setDataFile(String dataFile)
179 {
180 this.dataFile = dataFile;
181 }
182
183 public void setBranchRate(String branchRate)
184 {
185 this.branchRate = branchRate;
186 }
187
188 public void setLineRate(String lineRate)
189 {
190 this.lineRate = lineRate;
191 }
192
193 public void setPackageBranchRate(String packageBranchRate)
194 {
195 this.packageBranchRate = packageBranchRate;
196 }
197
198 public void setPackageLineRate(String packageLineRate)
199 {
200 this.packageLineRate = packageLineRate;
201 }
202
203 public void setTotalBranchRate(String totalBranchRate)
204 {
205 this.totalBranchRate = totalBranchRate;
206 }
207
208 public void setTotalLineRate(String totalLineRate)
209 {
210 this.totalLineRate = totalLineRate;
211 }
212
213 public void setFailureProperty(String failureProperty)
214 {
215 this.failureProperty = failureProperty;
216 }
217
218 public void setHaltOnFailure(boolean haltOnFailure)
219 {
220 this.haltOnFailure = haltOnFailure;
221 }
222
223 }