1
/* Copyright (c) 2000-2006 hamcrest.org
3
package org.hamcrest.beans;
5
import static org.hamcrest.MatcherAssert.assertThat;
6
import static org.hamcrest.beans.HasPropertyWithValue.hasProperty;
7
import static org.hamcrest.core.IsAnything.anything;
8
import static org.hamcrest.core.IsEqual.equalTo;
10
import java.beans.IntrospectionException;
11
import java.beans.PropertyDescriptor;
12
import java.beans.SimpleBeanInfo;
14
import org.hamcrest.AbstractMatcherTest;
15
import org.hamcrest.Description;
16
import org.hamcrest.Matcher;
17
import org.hamcrest.StringDescription;
18
import org.hamcrest.core.IsEqual;
21
* @author Iain McGinniss
23
* @author Steve Freeman
26
public class HasPropertyWithValueTest extends AbstractMatcherTest {
27
private final BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
29
private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo(
32
private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
35
protected Matcher<?> createMatcher() {
36
return hasProperty("irrelevant", anything());
39
public void testMatchesInfolessBeanWithMatchedNamedProperty() {
40
assertMatches("with property", hasProperty("property", equalTo("is expected")), shouldMatch);
41
assertMismatchDescription("property \"property\" was \"not expected\"",
42
hasProperty("property", equalTo("is expected")), shouldNotMatch);
45
public void testMatchesBeanWithInfoWithMatchedNamedProperty() {
46
assertMatches("with bean info", hasProperty("property", equalTo("with info")), beanWithInfo);
47
assertMismatchDescription("property \"property\" was \"with info\"",
48
hasProperty("property", equalTo("without info")), beanWithInfo);
51
public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
52
assertMismatchDescription("No property \"nonExistentProperty\"",
53
hasProperty("nonExistentProperty", anything()), shouldNotMatch);
56
public void testDoesNotMatchWriteOnlyProperty() {
57
assertMismatchDescription("property \"writeOnlyProperty\" is not readable",
58
hasProperty("writeOnlyProperty", anything()), shouldNotMatch);
61
public void testDescribeTo() {
62
assertDescription("hasProperty(\"property\", <true>)", hasProperty("property", equalTo(true)));
65
public void testMatchesPropertyAndValue() {
66
assertMatches("property with value", hasProperty( "property", anything()), beanWithInfo);
69
public void testDoesNotWriteMismatchIfPropertyMatches() {
70
Description description = new StringDescription();
71
hasProperty( "property", anything()).describeMismatch(beanWithInfo, description);
72
assertEquals("Expected mismatch description", "", description.toString());
75
public void testDescribesMissingPropertyMismatch() {
76
assertMismatchDescription("No property \"honk\"", hasProperty( "honk", anything()), shouldNotMatch);
79
public void testCanAccessAnAnonymousInnerClass() {
80
class X implements IX {
81
public int getTest() {
86
assertThat(new X(), HasPropertyWithValue.hasProperty("test", IsEqual.equalTo(1)));
93
public static class BeanWithoutInfo {
94
private String property;
96
public BeanWithoutInfo(String property) {
97
this.property = property;
100
public String getProperty() {
104
public void setProperty(String property) {
105
this.property = property;
108
public void setWriteOnlyProperty(float property) {
112
public String toString() {
113
return "[Person: " + property + "]";
117
public static class BeanWithInfo {
118
private final String propertyValue;
120
public BeanWithInfo(String propertyValue) {
121
this.propertyValue = propertyValue;
124
public String property() {
125
return propertyValue;
129
public static class BeanWithInfoBeanInfo extends SimpleBeanInfo {
131
public PropertyDescriptor[] getPropertyDescriptors() {
133
return new PropertyDescriptor[] {
134
new PropertyDescriptor("property", BeanWithInfo.class, "property", null)
136
} catch (IntrospectionException e) {
137
throw new RuntimeException("Introspection exception: " + e.getMessage());