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.net.URL;
062import java.net.URLClassLoader;
063import java.util.HashSet;
064import java.util.Iterator;
065import java.util.Set;
066
067import org.apache.tools.ant.AntClassLoader;
068import org.apache.tools.ant.BuildException;
069import org.apache.tools.ant.taskdefs.Java;
070import org.apache.tools.ant.taskdefs.MatchingTask;
071import org.apache.tools.ant.types.Path;
072import org.apache.tools.ant.types.Reference;
073
074/**
075 * An ant task that can be used to optionally fail an ant build if
076 * the coverage percentage for lines or branches is below a certain,
077 * user specifiable threshold.
078 */
079public class CheckTask extends MatchingTask
080{
081
082        private String dataFile = null;
083
084        final Set regexes = new HashSet();
085
086        private String branchRate = null;
087
088        private String lineRate = null;
089
090        private String packageBranchRate = null;
091
092        private String packageLineRate = null;
093
094        private String totalBranchRate = null;
095
096        private String totalLineRate = null;
097
098        private String failureProperty = null;
099
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                // Check the return code and print a message
159                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}