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
059package net.sourceforge.cobertura.ant;
060
061import java.util.HashSet;
062import java.util.Iterator;
063import java.util.Set;
064
065import 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 */
072public class CheckTask extends CommonMatchingTask
073{
074
075        private String dataFile = null;
076
077        final Set<Regex> regexes = new HashSet<Regex>();
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                        getJava().createArg().setValue("--datafile");
103                        getJava().createArg().setValue(dataFile);
104                }
105
106                if (branchRate != null) {
107                        getJava().createArg().setValue("--branch");
108                        getJava().createArg().setValue(branchRate);
109                }
110
111                if (lineRate != null) {
112                        getJava().createArg().setValue("--line");
113                        getJava().createArg().setValue(lineRate);
114                }
115
116                if (packageBranchRate != null) {
117                        getJava().createArg().setValue("--packagebranch");
118                        getJava().createArg().setValue(packageBranchRate);
119                }
120
121                if (packageLineRate != null) {
122                        getJava().createArg().setValue("--packageline");
123                        getJava().createArg().setValue(packageLineRate);
124                }
125
126                if (totalBranchRate != null) {
127                        getJava().createArg().setValue("--totalbranch");
128                        getJava().createArg().setValue(totalBranchRate);
129                }
130
131                if (totalLineRate != null) {
132                        getJava().createArg().setValue("--totalline");
133                        getJava().createArg().setValue(totalLineRate);
134                }
135
136                Iterator<Regex> iter = regexes.iterator();
137                while (iter.hasNext()) {
138                        getJava().createArg().setValue("--regex");
139                        getJava().createArg().setValue(iter.next().toString());
140                }
141
142                AntUtil.transferCoberturaDataFileProperty(getJava());
143                int returnCode = getJava().executeJava();
144
145                // Check the return code and print a message
146                if (returnCode == 0) {
147                        System.out.println("All checks passed.");
148                } else {
149                        if (haltOnFailure)
150                                throw new BuildException(
151                                                "Coverage check failed. See messages above.");
152                        else if (failureProperty != null)
153                                getProject().setProperty(failureProperty, "true");
154                        else
155                                System.err
156                                                .println("Coverage check failed. See messages above.");
157                }
158        }
159
160        public Regex createRegex()
161        {
162                Regex regex = new Regex();
163                regexes.add(regex);
164                return regex;
165        }
166
167        public void setDataFile(String dataFile)
168        {
169                this.dataFile = dataFile;
170        }
171
172        public void setBranchRate(String branchRate)
173        {
174                this.branchRate = branchRate;
175        }
176
177        public void setLineRate(String lineRate)
178        {
179                this.lineRate = lineRate;
180        }
181
182        public void setPackageBranchRate(String packageBranchRate)
183        {
184                this.packageBranchRate = packageBranchRate;
185        }
186
187        public void setPackageLineRate(String packageLineRate)
188        {
189                this.packageLineRate = packageLineRate;
190        }
191
192        public void setTotalBranchRate(String totalBranchRate)
193        {
194                this.totalBranchRate = totalBranchRate;
195        }
196
197        public void setTotalLineRate(String totalLineRate)
198        {
199                this.totalLineRate = totalLineRate;
200        }
201
202        public void setFailureProperty(String failureProperty)
203        {
204                this.failureProperty = failureProperty;
205        }
206
207        public void setHaltOnFailure(boolean haltOnFailure)
208        {
209                this.haltOnFailure = haltOnFailure;
210        }
211
212}