Coverage Report - com.stephenduncanjr.easymock.matcher.BeanProperty
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanProperty
87% 
100% 
0
 
 1  
 /*
 2  
  * Copyright 2006 Stephen Duncan Jr
 3  
  * 
 4  
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5  
  * use this file except in compliance with the License. You may obtain a copy of
 6  
  * the License at http://www.apache.org/licenses/LICENSE-2.0
 7  
  * 
 8  
  * Unless required by applicable law or agreed to in writing, software
 9  
  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 10  
  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 11  
  * License for the specific language governing permissions and limitations under
 12  
  * the License.
 13  
  */
 14  
 
 15  
 package com.stephenduncanjr.easymock.matcher;
 16  
 
 17  
 import java.lang.reflect.InvocationTargetException;
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 import java.util.Map.Entry;
 21  
 
 22  
 import org.apache.commons.beanutils.PropertyUtils;
 23  
 import org.easymock.IArgumentMatcher;
 24  
 
 25  
 /**
 26  
  * Matches based on a bean property.
 27  
  * 
 28  
  * @author stephen.duncan (Stephen C. Duncan Jr.
 29  
  *         <stephen.duncan@gmail.com>)
 30  
  * @since 1.0
 31  
  */
 32  
 public class BeanProperty implements IArgumentMatcher
 33  
 {
 34  
         /** Map of property names to values to match against. */
 35  
         private final Map<String, ?> expectedProperties;
 36  
 
 37  
         /**
 38  
          * Creates a new match for the given map of property names to values.
 39  
          * 
 40  
          * @param expectedProperties
 41  
          * @since 1.1
 42  
          */
 43  
         public BeanProperty(final Map<String, ?> expectedProperties)
 44  
         {
 45  7
                 super();
 46  7
                 this.expectedProperties = expectedProperties;
 47  7
         }
 48  
 
 49  
         /**
 50  
          * Creates a new matcher for the given property name and value.
 51  
          * 
 52  
          * @param expectedPropertyValue
 53  
          * @param propertyName
 54  
          */
 55  
         public BeanProperty(final String propertyName, final Object expectedPropertyValue)
 56  
         {
 57  13
                 super();
 58  13
                 final Map<String, Object> properties = new HashMap<String, Object>();
 59  13
                 properties.put(propertyName, expectedPropertyValue);
 60  13
                 this.expectedProperties = properties;
 61  13
         }
 62  
 
 63  
         /**
 64  
          * @see org.easymock.IArgumentMatcher#appendTo(java.lang.StringBuffer)
 65  
          */
 66  
         public void appendTo(final StringBuffer buffer)
 67  
         {
 68  6
                 buffer.append("propertyEq(");
 69  
 
 70  6
                 for(final Entry<String, ?> entry : this.expectedProperties.entrySet())
 71  
                 {
 72  8
                         buffer.append(entry.getKey());
 73  8
                         buffer.append("=");
 74  8
                         buffer.append(entry.getValue());
 75  8
                         buffer.append(", ");
 76  
                 }
 77  
 
 78  6
                 buffer.replace(buffer.length() - 2, buffer.length(), ")");
 79  6
         }
 80  
 
 81  
         /**
 82  
          * @see org.easymock.IArgumentMatcher#matches(java.lang.Object)
 83  
          */
 84  
         public boolean matches(final Object actual)
 85  
         {
 86  31
                 for(final Entry<String, ?> entry : this.expectedProperties.entrySet())
 87  
                 {
 88  
                         try
 89  
                         {
 90  35
                                 final Object actualValue = PropertyUtils.getProperty(actual, entry.getKey());
 91  28
                                 if(!(entry.getValue() == actualValue || entry.getValue().equals(actualValue)))
 92  
                                 {
 93  15
                                         return false;
 94  
                                 }
 95  
                         }
 96  0
                         catch(IllegalAccessException e)
 97  
                         {
 98  0
                                 return false;
 99  
                         }
 100  0
                         catch(InvocationTargetException e)
 101  
                         {
 102  0
                                 return false;
 103  
                         }
 104  4
                         catch(NoSuchMethodException e)
 105  
                         {
 106  4
                                 return false;
 107  
                         }
 108  3
                         catch(RuntimeException e)
 109  
                         {
 110  3
                                 return false;
 111  13
                         }
 112  
                 }
 113  
 
 114  9
                 return true;
 115  
         }
 116  
 }