KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > capture > EmmaTextResultsInterpreter


1 /**
2  * $Id: EmmaTextResultsInterpreter.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The GNU LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.capture;
30
31 import java.io.BufferedReader JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.Reader JavaDoc;
34
35 import org.apache.tools.ant.BuildException;
36
37 import com.idaremedia.antx.helpers.Tk;
38
39 /**
40  * Our default Ant &lt;emma/report&gt; text output log interpreter. This interpreter
41  * assumes that the text output is for all levels (up to method) sorted in the default
42  * order using weighted units. Results are determined by first looking at what EMMA
43  * says passed. Each "no pass" metric is interpreted as a single error. If a coverage
44  * metric passes EMMA's criteria, it is checked for our warning criteria which is 75%
45  * or better coverage. Any coverage below 75% is considered a warning.
46  * <p/>
47  * <b>Example Usage:</b><pre>
48  * &lt;typedef name="emma-checker"
49  * classname="com.idaremedia.antx.capture.EmmaTextResultsInterpreter"/&gt;
50  * ...
51  * &lt;emma enabled="yes"&gt;
52  * &lt;report sourcepath="${java.sources}"&gt;
53  * &lt;fileset refid="emma.files"/&gt;
54  * &lt;txt outfile="coverage.txt" depth="all"/&gt;
55  * &lt;/report&gt;
56  * &lt;/emma&gt;
57  * ...
58  * &lt;evaluatelogged logfile="coverage.txt" <b>interpreter="emma-checker"</b>
59  * maxerrors="0" maxwarnings="1" prefix="emma."/&gt;
60  * &lt;domatch property="emma.result"&gt;
61  * &lt;equals value="CLEAN"&gt;
62  * ...
63  * &lt;otherwise&gt;
64  * ...
65  *
66  * </pre>
67  *
68  * @since JWare/AntX 0.5
69  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
70  * @version 0.5
71  * @.safety single
72  * @.group api,helper
73  **/

74
75 public final class EmmaTextResultsInterpreter implements LogInterpreter
76 {
77     private static final String JavaDoc HEADER0= "[class, %]\t[method, %]\t[block, %]\t[line, %]";
78
79     /**
80      * Initializes a new EMMA text result file interpreter.
81      **/

82     public EmmaTextResultsInterpreter()
83     {
84         super();
85     }
86
87
88     private int beginning(String JavaDoc s, int end)
89     {
90         int start = end;
91         int i = end;
92         while (i>=0) {
93             if (!Character.isDigit(s.charAt(i))) {
94                 break;
95             }
96             start = i;
97             i--;
98         }
99         return start;
100     }
101
102
103     private boolean below75percent(String JavaDoc s, int resultend)
104     {
105         int i = s.lastIndexOf('%',resultend);
106         if (i>0) {
107             int j = i;
108             i = beginning(s,j-1);
109             int percent = Tk.integerFrom(s.substring(i,j),Tk.NO_INT);
110             if (percent!=Tk.NO_INT) {
111                 return percent<75;
112             }
113         }
114         return false;
115     }
116
117
118     public String JavaDoc interpret(Reader JavaDoc inputr, InterpretParameters config)
119         throws BuildException
120     {
121         String JavaDoc result = PROBLEM;
122         int nUnderCovered = 0;
123         int nBelow75 = 0;
124
125         try {
126             BufferedReader JavaDoc in = new BufferedReader JavaDoc(inputr,512);
127             String JavaDoc line;
128             while ((line=in.readLine())!=null) {
129                 if (line.startsWith(HEADER0)) {
130                     String JavaDoc resultsline = in.readLine();
131                     if (resultsline!=null) {
132                         result = CLEAN;
133                         int i,from=resultsline.length()-1;
134                         while(from>0) {
135                             i= resultsline.lastIndexOf(')',from);
136                             if (i<0) {
137                                 break;
138                             }
139                             if (resultsline.charAt(i+1)=='!') {
140                                 nUnderCovered++;
141                             } else if (below75percent(resultsline,i)) {
142                                 nBelow75++;
143                             }
144                             from = i-1;
145                             i = resultsline.lastIndexOf('(',from);
146                             if (i>0) {
147                                 from = i-1;
148                             }
149                         }
150                     }
151                     break;
152                 }
153             }
154             in.close();
155         } catch(IOException JavaDoc ioX) {
156             throw new BuildException(ioX);
157         }
158
159         if (nUnderCovered!=0) {
160             if (nUnderCovered>config.getMaxErrors()) {
161                 result = FAILURE;
162             } else {
163                 result = PROBLEM;
164             }
165         }
166         else if (nBelow75!=0) {
167             if (nBelow75>config.getMaxWarnings()) {
168                 result = FAILURE;
169             } else {
170                 result = PROBLEM;
171             }
172         }
173
174         ResultsHelper.set(config,nUnderCovered,nBelow75,result);
175         return result;
176     }
177 }
178
179 /* end-of-EmmaTextResultsInterpreter.java */
Popular Tags