~ubuntu-branches/ubuntu/raring/libhamcrest1.2-java/raring

« back to all changes in this revision

Viewing changes to hamcrest-unit-test/src/main/java/org/hamcrest/beans/HasPropertyWithValueTest.java

  • Committer: Package Import Robot
  • Author(s): Brian Thomason
  • Date: 2011-12-02 17:55:55 UTC
  • Revision ID: package-import@ubuntu.com-20111202175555-xuj86jbpi8mehr1o
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  Copyright (c) 2000-2006 hamcrest.org
 
2
 */
 
3
package org.hamcrest.beans;
 
4
 
 
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;
 
9
 
 
10
import java.beans.IntrospectionException;
 
11
import java.beans.PropertyDescriptor;
 
12
import java.beans.SimpleBeanInfo;
 
13
 
 
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;
 
19
 
 
20
/**
 
21
 * @author Iain McGinniss
 
22
 * @author Nat Pryce
 
23
 * @author Steve Freeman
 
24
 * @since 1.1.0
 
25
 */
 
26
public class HasPropertyWithValueTest extends AbstractMatcherTest {
 
27
  private final BeanWithoutInfo shouldMatch = new BeanWithoutInfo("is expected");
 
28
 
 
29
  private final BeanWithoutInfo shouldNotMatch = new BeanWithoutInfo(
 
30
      "not expected");
 
31
 
 
32
  private final BeanWithInfo beanWithInfo = new BeanWithInfo("with info");
 
33
 
 
34
  @Override
 
35
  protected Matcher<?> createMatcher() {
 
36
    return hasProperty("irrelevant", anything());
 
37
  }
 
38
 
 
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);
 
43
  }
 
44
 
 
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);
 
49
  }
 
50
 
 
51
  public void testDoesNotMatchInfolessBeanWithoutMatchedNamedProperty() {
 
52
    assertMismatchDescription("No property \"nonExistentProperty\"", 
 
53
                              hasProperty("nonExistentProperty", anything()), shouldNotMatch);
 
54
   }
 
55
 
 
56
  public void testDoesNotMatchWriteOnlyProperty() {
 
57
    assertMismatchDescription("property \"writeOnlyProperty\" is not readable",
 
58
                              hasProperty("writeOnlyProperty", anything()), shouldNotMatch); 
 
59
  }
 
60
 
 
61
  public void testDescribeTo() {
 
62
    assertDescription("hasProperty(\"property\", <true>)", hasProperty("property", equalTo(true)));
 
63
  }
 
64
 
 
65
  public void testMatchesPropertyAndValue() {
 
66
    assertMatches("property with value", hasProperty( "property", anything()), beanWithInfo);
 
67
  }
 
68
  
 
69
  public void testDoesNotWriteMismatchIfPropertyMatches() {
 
70
    Description description = new StringDescription();
 
71
    hasProperty( "property", anything()).describeMismatch(beanWithInfo, description);
 
72
    assertEquals("Expected mismatch description", "", description.toString());
 
73
  }
 
74
 
 
75
  public void testDescribesMissingPropertyMismatch() {
 
76
    assertMismatchDescription("No property \"honk\"", hasProperty( "honk", anything()), shouldNotMatch);
 
77
  }
 
78
 
 
79
  public void testCanAccessAnAnonymousInnerClass() {
 
80
    class X implements IX {
 
81
      public int getTest() {
 
82
        return 1;
 
83
      }
 
84
    }
 
85
 
 
86
    assertThat(new X(), HasPropertyWithValue.hasProperty("test", IsEqual.equalTo(1)));
 
87
  }
 
88
 
 
89
  interface IX {
 
90
    int getTest();
 
91
  }
 
92
 
 
93
  public static class BeanWithoutInfo {
 
94
    private String property;
 
95
 
 
96
    public BeanWithoutInfo(String property) {
 
97
      this.property = property;
 
98
    }
 
99
 
 
100
    public String getProperty() {
 
101
      return property;
 
102
    }
 
103
 
 
104
    public void setProperty(String property) {
 
105
      this.property = property;
 
106
    }
 
107
 
 
108
    public void setWriteOnlyProperty(float property) {
 
109
    }
 
110
 
 
111
    @Override
 
112
    public String toString() {
 
113
      return "[Person: " + property + "]";
 
114
    }
 
115
  }
 
116
 
 
117
  public static class BeanWithInfo {
 
118
    private final String propertyValue;
 
119
 
 
120
    public BeanWithInfo(String propertyValue) {
 
121
      this.propertyValue = propertyValue;
 
122
    }
 
123
 
 
124
    public String property() {
 
125
      return propertyValue;
 
126
    }
 
127
  }
 
128
 
 
129
  public static class BeanWithInfoBeanInfo extends SimpleBeanInfo {
 
130
    @Override
 
131
    public PropertyDescriptor[] getPropertyDescriptors() {
 
132
      try {
 
133
        return new PropertyDescriptor[] { 
 
134
            new PropertyDescriptor("property", BeanWithInfo.class, "property", null) 
 
135
          };
 
136
      } catch (IntrospectionException e) {
 
137
        throw new RuntimeException("Introspection exception: " + e.getMessage());
 
138
      }
 
139
    }
 
140
  }
 
141
}