View Javadoc
1   /*
2    * #%L
3    * Mojo's Maven plugin for Cobertura
4    * %%
5    * Copyright (C) 2005 - 2013 Codehaus
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package org.codehaus.mojo.cobertura.configuration;
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  /**
28   * The Configuration for the Instrumentation.
29   *
30   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
31   */
32  public class ConfigInstrumentation
33  {
34      private File basedir;
35  
36      private List<String> excludes;
37  
38      private List<String> ignores;
39  
40      private List<String> includes;
41  
42      private String maxmem;
43  
44      private boolean ignoreTrivial;
45  
46      private List<String> ignoreMethodAnnotations;
47  
48      /**
49       * Construct a new ConfigInstrumentation object.
50       */
51      public ConfigInstrumentation()
52      {
53          this.includes = new ArrayList<String>();
54          this.excludes = new ArrayList<String>();
55          this.ignores = new ArrayList<String>();
56          this.ignoreTrivial = false;
57          this.ignoreMethodAnnotations = new ArrayList<String>();
58  
59          this.basedir = new File( System.getProperty( "user.dir" ) );
60  
61          if ( MaxHeapSizeUtil.getInstance().envHasMavenMaxMemSetting() )
62          {
63              maxmem = MaxHeapSizeUtil.getInstance().getMavenMaxMemSetting();
64          }
65          else
66          {
67              this.maxmem = "64m";
68          }
69      }
70  
71      /**
72       * Add an Exclude to the underlying list.
73       *
74       * @param exclude the exlude string.
75       */
76      public void addExclude( String exclude )
77      {
78          this.excludes.add( exclude );
79      }
80  
81      /**
82       * Add an Ignore to the underlying list.
83       *
84       * @param ignore the ignore string.
85       */
86      public void addIgnore( String ignore )
87      {
88          this.ignores.add( ignore );
89      }
90  
91  
92      /**
93       * Add an IgnoreMethodAnnotation to the underlying list.
94       *
95       * @param ignoreMethodAnnotation the ignore string.
96       */
97      public void addIgnoreMethodAnnotation( String ignoreMethodAnnotation )
98      {
99          this.ignoreMethodAnnotations.add( ignoreMethodAnnotation );
100     }
101 
102     /**
103      * Add an Include ot the underlying list.
104      *
105      * @param include the include string.
106      */
107     public void addInclude( String include )
108     {
109         this.includes.add( include );
110     }
111 
112     /**
113      * @return Returns the basedir.
114      */
115     public File getBasedir()
116     {
117         return basedir;
118     }
119 
120     /**
121      * Get the Exclude List.
122      *
123      * @return the exlude list.
124      */
125     public List<String> getExcludes()
126     {
127         return excludes;
128     }
129 
130     /**
131      * Get the Ignore List.
132      *
133      * @return the ignore list.
134      */
135     public List<String> getIgnores()
136     {
137         return ignores;
138     }
139 
140     /**
141      * Get the Include List.
142      *
143      * @return the include list.
144      */
145     public List<String> getIncludes()
146     {
147         return includes;
148     }
149 
150     /**
151      * @param basedir The basedir to set.
152      */
153     public void setBasedir( File basedir )
154     {
155         this.basedir = basedir;
156     }
157 
158     /**
159      * Get the maxmem setting.
160      *
161      * @return the maxmem setting.
162      */
163     public String getMaxmem()
164     {
165         return maxmem;
166     }
167 
168     /**
169      * Sets the max memory for the JVM used to run the <code>InstrumentationTask</code> task.
170      * The format is "<value><size>". Ex: "64m" where 64 is the value, and "m" denotes megabytes.
171      *
172      * @param maxmem the value to which maxmem will be set
173      */
174     public void setMaxmem( String maxmem )
175     {
176         this.maxmem = maxmem;
177     }
178 
179     /**
180      * Get the ignoreTrivial setting.
181      *
182      * @return the ignoreTrivial setting.
183      */
184     public boolean getIgnoreTrivial()
185     {
186         return ignoreTrivial;
187     }
188 
189     /**
190      * IgnoreTrivial switch that tells Cobertura to ignore the following in the coverage report:
191      * Getter methods that simply read a class field; Setter methods that set a class field;
192      * Constructors that only set class fields and call a super class constructor.
193      *
194      * @param ignoreTrivial enable ignoreTrivial
195      */
196     public void setIgnoreTrivial( boolean ignoreTrivial )
197     {
198         this.ignoreTrivial = ignoreTrivial;
199     }
200 
201     /**
202      * Get the ignoreMethodAnnotations setting.
203      *
204      * @return the ignoreMethodAnnotations setting.
205      */
206     public List<String> getIgnoreMethodAnnotations()
207     {
208         return ignoreMethodAnnotations;
209     }
210 
211     /**
212      * IgnoreMethodAnnotation switch used to specify an annotation that, when present on a method,
213      * will cause Cobertura to ignore the method in the coverage report.
214      *
215      * @param ignoreMethodAnnotations
216      */
217     public void setIgnoreMethodAnnotations( List<String> ignoreMethodAnnotations )
218     {
219         this.ignoreMethodAnnotations = ignoreMethodAnnotations;
220     }
221 
222 
223     /**
224      * {@inheritDoc}
225      */
226     public String toString()
227     {
228         StringBuffer sb = new StringBuffer();
229         sb.append( "<ConfigInstrumentation" );
230 
231         sb.append( " basedir=\"" );
232         if ( this.basedir != null )
233         {
234             sb.append( basedir.getAbsolutePath() );
235         }
236         sb.append( "\"" );
237 
238         if ( !this.includes.isEmpty() )
239         {
240             sb.append( " includes=\"" );
241             Iterator<String> it = this.includes.iterator();
242             while ( it.hasNext() )
243             {
244                 String include = (String) it.next();
245                 sb.append( include );
246 
247                 if ( it.hasNext() )
248                 {
249                     sb.append( " " );
250                 }
251             }
252             sb.append( "\"" );
253         }
254 
255         if ( !this.excludes.isEmpty() )
256         {
257             sb.append( " excludes=\"" );
258             Iterator<String> it = this.excludes.iterator();
259             while ( it.hasNext() )
260             {
261                 String exclude = (String) it.next();
262                 sb.append( exclude );
263                 if ( it.hasNext() )
264                 {
265                     sb.append( " " );
266                 }
267             }
268             sb.append( "\"" );
269         }
270 
271         if ( !this.ignores.isEmpty() )
272         {
273             sb.append( " ignores=\"" );
274             Iterator<String> it = this.ignores.iterator();
275             while ( it.hasNext() )
276             {
277                 String ignore = (String) it.next();
278                 sb.append( ignore );
279                 if ( it.hasNext() )
280                 {
281                     sb.append( " " );
282                 }
283             }
284             sb.append( "\"" );
285         }
286 
287         if ( 0 != getMaxmem().length() )
288         {
289             sb.append( " maxmem=\"" );
290             sb.append( getMaxmem() );
291             sb.append( "\"" );
292         }
293 
294         sb.append( " ignoreTrivial=\"" );
295         sb.append( getIgnoreTrivial() );
296         sb.append( "\"" );
297 
298         if ( !this.ignoreMethodAnnotations.isEmpty() )
299         {
300             sb.append( " ignoreMethodAnnotations=\"" );
301             Iterator<String> it = this.ignoreMethodAnnotations.iterator();
302             while ( it.hasNext() )
303             {
304                 String ignore = (String) it.next();
305                 sb.append( ignore );
306                 if ( it.hasNext() )
307                 {
308                     sb.append( " " );
309                 }
310             }
311             sb.append( "\"" );
312         }
313 
314         return sb.append( " />" ).toString();
315     }
316 
317 }