Coverage Report - org.apache.commons.betwixt.strategy.impl.propertysuppression.PackageSuppressor

Classes in this File Line Coverage Branch Coverage Complexity
PackageSuppressor
77% 
83% 
3

 1  
 /*
 2  
  * Copyright 2005 The Apache Software Foundation.
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */ 
 16  
 package org.apache.commons.betwixt.strategy.impl.propertysuppression;
 17  
 
 18  
 import org.apache.commons.betwixt.strategy.PropertySuppressionStrategy;
 19  
 
 20  
 /**
 21  
  * Suppresses properties based on the package of their type.
 22  
  * Limited regex is supported. If the package name ends in <code>.*</code>
 23  
  * them all child packages will be suppressed.
 24  
  * @author <a href='http://commons.apache.org'>Apache Commons Team</a> of the <a href='http://www.apache.org'>Apache Software Foundation</a>
 25  
  */
 26  
 public class PackageSuppressor extends PropertySuppressionStrategy {
 27  
 
 28  
     /** Package to be suppressed */
 29  
     private final String suppressedPackage;
 30  
     private final boolean exact;
 31  
     
 32  
     /**
 33  
      * Constructs a suppressor for packages.
 34  
      * @param suppressedPackage package name (for exact match)
 35  
      * or base package and <code>.*</code>to suppress children
 36  
      */
 37  26
     public  PackageSuppressor(String suppressedPackage) {
 38  26
         if (suppressedPackage.endsWith(".*")) {
 39  13
             exact = false;
 40  13
             suppressedPackage = suppressedPackage.substring(0, suppressedPackage.length()-2);
 41  
         }
 42  
         else
 43  
         {
 44  13
             exact =true;
 45  
         }
 46  26
         this.suppressedPackage = suppressedPackage;
 47  26
     }
 48  
     
 49  
     public boolean suppressProperty(Class classContainingTheProperty, Class propertyType, String propertyName) {
 50  104
         boolean result = false;
 51  104
         if (propertyType != null) {
 52  104
             Package propertyTypePackage = propertyType.getPackage();
 53  104
             if (propertyTypePackage != null) {
 54  104
                 String packageName = propertyTypePackage.getName();
 55  104
                 if (exact) {
 56  52
                     result = suppressedPackage.equals(packageName);
 57  
                 }
 58  52
                 else if (packageName != null)
 59  
                 {
 60  52
                     result = packageName.startsWith(suppressedPackage);
 61  
                 }
 62  
             }
 63  
         }
 64  104
         return result;
 65  
     }
 66  
 
 67  
     public String toString() {
 68  0
         StringBuffer buffer = new StringBuffer("Suppressing package " );
 69  0
         buffer.append(suppressedPackage);
 70  0
         if (exact) {
 71  0
             buffer.append("(exact)");
 72  
         }
 73  0
         return buffer.toString();
 74  
     }
 75  
 }