Coverage Report - org.doxla.spring.automock.resolver.SimplePatternMatchingMockResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
SimplePatternMatchingMockResolver
86% 
100% 
0
 
 1  
 /**
 2  
    Copyright 2007 Dan Oxlade
 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.doxla.spring.automock.resolver;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.HashSet;
 20  
 import java.util.Set;
 21  
 
 22  
 import org.doxla.spring.resource.util.ResouceToClassUtil;
 23  
 import org.springframework.beans.factory.annotation.Required;
 24  
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
 25  
 import org.springframework.core.io.Resource;
 26  
 
 27  
 /**
 28  
  * Resolves classes based on a simple regexp.
 29  
  * Set the regexp pattern to match on by setting the pattern property.
 30  
  * 
 31  
  * For example:
 32  
  * Setting the pattern property to 'classpath*:/org/doxla/spring/automock/*' 
 33  
  * will result in all classes in the org.doxla.spring.automock packing being
 34  
  * returned by invocations of the resolveClassesToMock method.
 35  
  * 
 36  
  * You should typically prefer {@link InterfaceOnlyPatternMatchingMockResolver}
 37  
  *  
 38  
  * @author danoxlade
 39  
  */
 40  
 public class SimplePatternMatchingMockResolver
 41  
         implements MockClassResolver{
 42  
 
 43  
         Resource[] resources;
 44  
         
 45  3
         public SimplePatternMatchingMockResolver() {
 46  3
         }
 47  
 
 48  
         public Collection<Class> resolveClassesToMock(ConfigurableListableBeanFactory beanFactory) {
 49  
                 try {
 50  3
                         return findMatchingClasses();
 51  0
                 } catch (ClassNotFoundException e) {
 52  0
                         throw new RuntimeException("The resources property contained a non class resource",e);
 53  
                 }
 54  
         }
 55  
         
 56  
         private Set<Class> findMatchingClasses() throws ClassNotFoundException {
 57  3
                 Set<Class> classes = new HashSet<Class>(resources.length);
 58  22
                 for (Resource resource : resources) {
 59  19
                         Class c = ResouceToClassUtil.resolveClassFromJarResouce(resource);
 60  19
                         if(accept(c)){
 61  7
                                 classes.add(c);
 62  
                         }
 63  
                 }
 64  
                 
 65  3
                 return classes;
 66  
         }
 67  
         
 68  
         /**
 69  
          * Hook point for subclasses
 70  
          * 
 71  
          * @param clazz
 72  
          * @return true if the clazz should be returned by the resolveClassesToMock method
 73  
          */
 74  
         boolean accept(Class clazz) {
 75  5
                 return true;
 76  
         }
 77  
 
 78  
         @Required
 79  
         public void setResources(Resource[] resources) {
 80  3
                 this.resources = resources;
 81  3
         }
 82  
 
 83  
 }