KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmock > examples > website > BeanPropertyGetterMatcher


1 /* Copyright (c) 2000-2004 jMock.org
2  */

3 package org.jmock.examples.website;
4
5 import java.beans.BeanInfo JavaDoc;
6 import java.beans.IntrospectionException JavaDoc;
7 import java.beans.Introspector JavaDoc;
8 import java.beans.PropertyDescriptor JavaDoc;
9 import junit.framework.AssertionFailedError;
10 import org.jmock.core.Invocation;
11 import org.jmock.core.matcher.StatelessInvocationMatcher;
12
13
14 public class BeanPropertyGetterMatcher extends StatelessInvocationMatcher
15 {
16
17     public boolean matches( Invocation invocation ) {
18         Class JavaDoc beanClass = invocation.invokedMethod.getDeclaringClass();
19
20         try {
21             BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(beanClass);
22             PropertyDescriptor JavaDoc[] properties = beanInfo.getPropertyDescriptors();
23
24             for (int i = 0; i < properties.length; i++) {
25                 if (invocation.invokedMethod.equals(properties[i].getReadMethod())) return true;
26             }
27             return false;
28         }
29         catch (IntrospectionException JavaDoc ex) {
30             throw new AssertionFailedError("could not introspect bean class" + beanClass + ": " + ex.getMessage());
31         }
32     }
33
34     public StringBuffer JavaDoc describeTo( StringBuffer JavaDoc buffer ) {
35         return buffer.append("any bean property getter");
36     }
37 }
38
Popular Tags