KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > ant > CheckTask


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 JavaDoc;
62 import java.net.URLClassLoader JavaDoc;
63 import java.util.HashSet JavaDoc;
64 import java.util.Iterator JavaDoc;
65 import java.util.Set JavaDoc;
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 public class CheckTask extends MatchingTask
80 {
81
82     private String JavaDoc dataFile = null;
83
84     final Set JavaDoc regexes = new HashSet JavaDoc();
85
86     private String JavaDoc branchRate = null;
87
88     private String JavaDoc lineRate = null;
89
90     private String JavaDoc packageBranchRate = null;
91
92     private String JavaDoc packageLineRate = null;
93
94     private String JavaDoc totalBranchRate = null;
95
96     private String JavaDoc totalLineRate = null;
97
98     private String JavaDoc 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 JavaDoc 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 JavaDoc)
200             {
201                 URL JavaDoc[] earls = ((URLClassLoader JavaDoc)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 JavaDoc dataFile)
229     {
230         this.dataFile = dataFile;
231     }
232
233     public void setBranchRate(String JavaDoc branchRate)
234     {
235         this.branchRate = branchRate;
236     }
237
238     public void setLineRate(String JavaDoc lineRate)
239     {
240         this.lineRate = lineRate;
241     }
242
243     public void setPackageBranchRate(String JavaDoc packageBranchRate)
244     {
245         this.packageBranchRate = packageBranchRate;
246     }
247
248     public void setPackageLineRate(String JavaDoc packageLineRate)
249     {
250         this.packageLineRate = packageLineRate;
251     }
252
253     public void setTotalBranchRate(String JavaDoc totalBranchRate)
254     {
255         this.totalBranchRate = totalBranchRate;
256     }
257
258     public void setTotalLineRate(String JavaDoc totalLineRate)
259     {
260         this.totalLineRate = totalLineRate;
261     }
262
263     public void setFailureProperty(String JavaDoc 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