View Javadoc

1   package org.apache.maven.jcoveragereport;
2   
3   /* ====================================================================
4    *   Copyright 2001-2004 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.HashMap;
26  
27  /**
28   * @author Emmanuel Venisse
29   * @version $Id: Coverage.java 170200 2005-05-15 06:24:19Z brett $
30   */
31  public class Coverage
32  {
33      private List classes;
34      private String srcDirectory;
35      private Map packageMap;
36  
37      public Coverage()
38      {
39          classes = new ArrayList();
40          packageMap = new HashMap();
41      }
42  
43      public void setClasses(List classes)
44      {
45          this.classes = classes;
46      }
47  
48      public void addClass(Clazz theClass)
49      {
50          classes.add(theClass);
51          Package pkg;
52          String packageName = theClass.getPackageName();
53          if (!packageMap.containsKey(packageName))
54          {
55              pkg = new Package(packageName);
56          }
57          else
58          {
59              pkg = (Package) packageMap.get(packageName);
60          }
61          if (!pkg.contains(theClass))
62          {
63              pkg.setDirectory(theClass.getFile().substring(0, theClass.getFile().lastIndexOf("/")));
64              pkg.addClass(theClass);
65              packageMap.put(packageName, pkg);
66          }
67      }
68  
69      public List getClasses()
70      {
71          return classes;
72      }
73  
74      public List getClassesSortedByName()
75      {
76          ClazzComparator comp = new ClazzComparator();
77          Collections.sort(classes, comp);
78          return classes;
79      }
80  
81      public void setSrcDirectory(String srcDirectory)
82      {
83          this.srcDirectory = srcDirectory;
84      }
85  
86      public String getSrcDirectory()
87      {
88          return srcDirectory;
89      }
90  
91      public List getPackages()
92      {
93          return new ArrayList(packageMap.values());
94      }
95  
96      public List getPackagesSortedByName()
97      {
98          PackageComparator comp = new PackageComparator();
99          List packages = getPackages();
100         Collections.sort(packages, comp);
101         return packages;
102     }
103 
104     public List getSubPackage(Package thePackage)
105     {
106         ArrayList subPkgList = new ArrayList();
107         for (Iterator iter = getPackagesSortedByName().iterator(); iter.hasNext(); )
108         {
109             Package pkg = (Package) iter.next();
110             if (pkg.getName().startsWith(thePackage.getName())
111                 && !pkg.getName().equals(thePackage.getName()))
112             {
113                 subPkgList.add(pkg);
114             }
115         }
116         return subPkgList;
117     }
118 
119     public String getCoveredPercentLine()
120     {
121         return String.valueOf(getLineCoverage());
122     }
123 
124     public String getCoveredPercentBranch()
125     {
126     	double totalLines = 0.00d;
127     	double total = 0.00d;
128 
129         if (getLineCoverage() > 0.00d)
130         {
131             for (Iterator iter = getClasses().iterator(); iter.hasNext(); )
132             {
133                 Clazz theClass = (Clazz) iter.next();
134                 int classLines = theClass.getLines().size();
135                 double rate = 0;
136                 try
137                 {
138                     rate = new Double(theClass.getBranchRate()).floatValue();
139                 }
140                 catch(NumberFormatException e)
141                 {
142                     rate = 0;
143                 }
144                 total += (rate*classLines);
145                 totalLines += classLines;
146             }
147 
148             total /= totalLines;
149         }
150 
151         return String.valueOf(total);
152     }
153     
154     private double getLineCoverage()
155     {
156         double totalLines = 0.00d;
157         double totalTestedLines = 0.00d;        
158         for (Iterator iter = getClasses().iterator(); iter.hasNext(); )
159         {
160             Clazz theClass = (Clazz) iter.next();
161             int classLines = theClass.getLines().size();
162             double rate = 0;
163             try
164 			{
165                 rate = new Double(theClass.getLineRate()).floatValue();
166             }
167             catch(NumberFormatException e)
168             {
169                 rate = 0;
170             }
171             totalTestedLines += (rate * classLines);
172             totalLines += classLines;
173         }
174 
175         return (totalTestedLines / totalLines);
176 
177     }
178 }