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  
25  /**
26   * @author Emmanuel Venisse
27   * @version $Id: Package.java 170200 2005-05-15 06:24:19Z brett $
28   */
29  public class Package
30  {
31      private List classes;
32      private String name = "";
33      private String directory;
34  
35      public Package(String name)
36      {
37          this.name = name;
38          classes = new ArrayList();
39      }
40  
41      public void setName(String name)
42      {
43          if (name == null)
44          {
45              System.out.println("Package == null");
46          }
47          this.name = name;
48      }
49  
50      public String getName()
51      {
52          return name;
53      }
54  
55      public void setDirectory(String directory)
56      {
57          this.directory = directory;
58      }
59  
60      public String getDirectory()
61      {
62          return directory;
63      }
64  
65      public boolean contains(Clazz theClass)
66      {
67          return classes.contains(theClass);
68      }
69  
70      public void addClass(Clazz theClass)
71      {
72          classes.add(theClass);
73      }
74  
75      public List getClasses()
76      {
77          return classes;
78      }
79  
80      public List getClassesSortedByName()
81      {
82          ClazzComparator comp = new ClazzComparator();
83          Collections.sort(classes, comp);
84          return classes;
85      }
86  
87      public String getCoveredPercentLine()
88      {
89          return String.valueOf(getLineCoverage());
90      }
91  
92      public String getCoveredPercentBranch()
93      {
94      	double pckgLines = 0.00d;
95      	double total = 0.00d;
96  
97          if (getLineCoverage() > 0.00d)
98          {
99              for (Iterator iter = getClasses().iterator(); iter.hasNext(); )
100             {
101                 Clazz theClass = (Clazz) iter.next();
102                 int classLines = theClass.getLines().size();
103                 double rate = 0;
104                 try
105                 {
106                     rate = new Double(theClass.getBranchRate()).floatValue();
107                 }
108                 catch(NumberFormatException e)
109                 {
110                     rate = 0;
111                 }
112                 total += (rate*classLines);
113                 pckgLines += classLines;
114             }
115 
116             total /= pckgLines;
117         }
118 
119         return String.valueOf(total);
120     }
121 
122     private double getLineCoverage()
123     {
124         double pckgLines = 0.00d;
125         double pckgTestedLines = 0.00d;        
126         for (Iterator iter = getClasses().iterator(); iter.hasNext(); )
127         {
128             Clazz theClass = (Clazz) iter.next();
129             int classLines = theClass.getLines().size();
130             double rate = 0;
131             try
132 			{
133                 rate = new Double(theClass.getLineRate()).floatValue();
134             }
135             catch(NumberFormatException e)
136             {
137                 rate = 0;
138             }
139             pckgTestedLines += (rate * classLines);
140             pckgLines += classLines;
141         }
142 
143         return (pckgTestedLines / pckgLines);
144     }
145 }