Debian Codegraph @

src/test/java/org/fest/reflect/beanproperty/Property_Test.java

raw
1 /*
2 * Created on Nov 23, 2009
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5 * License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
10 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
11 * governing permissions and limitations under the License.
12 *
13 * Copyright @2009 the original author or authors.
14 */
15 package org.fest.reflect.beanproperty;
16
17 import static org.fest.reflect.util.ExpectedFailures.expectIllegalArgumentException;
18 import static org.fest.reflect.util.ExpectedFailures.expectNullPointerException;
19 import static org.fest.reflect.util.ExpectedFailures.expectReflectionError;
20 import static org.fest.util.Lists.newArrayList;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24
25 import java.beans.PropertyDescriptor;
26 import java.util.List;
27
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import org.fest.reflect.Jedi;
32 import org.fest.reflect.Person;
33 import org.fest.reflect.reference.TypeRef;
34 import org.fest.test.CodeToTest;
35
36 /**
37 * Tests for the fluent interface for accessing properties.
38 *
39 * @author Alex Ruiz
40 */
41 public class Property_Test {
42
43 private Person person;
44
45 @Before
46 public void setUp() {
47 person = new Person("Luke");
48 }
49
50 @Test
51 public void should_throw_error_if_property_name_is_null() {
52 expectNullPointerException("The name of the property to access should not be null").on(new CodeToTest() {
53 public void run() {
54 PropertyName.startPropertyAccess(null);
55 }
56 });
57 }
58
59 @Test
60 public void should_throw_error_if_property_name_is_empty() {
61 expectIllegalArgumentException("The name of the property to access should not be empty").on(new CodeToTest() {
62 public void run() {
63 PropertyName.startPropertyAccess("");
64 }
65 });
66 }
67
68 @Test
69 public void should_throw_error_if_property_type_is_null() {
70 expectNullPointerException("The type of the property to access should not be null").on(new CodeToTest() {
71 public void run() {
72 PropertyName.startPropertyAccess("name").ofType((Class<?>) null);
73 }
74 });
75 }
76
77 @Test
78 public void should_throw_error_if_target_is_null() {
79 expectNullPointerException("Target should not be null").on(new CodeToTest() {
80 public void run() {
81 PropertyName.startPropertyAccess("name").ofType(String.class).in(null);
82 }
83 });
84 }
85
86 @Test
87 public void should_get_property_value() {
88 String personName = PropertyName.startPropertyAccess("name").ofType(String.class).in(person).get();
89 assertEquals("Luke", personName);
90 }
91
92 @Test
93 public void should_get_property_value_defined_in_object_class() {
94 Class<?> personClass = PropertyName.startPropertyAccess("class").ofType(Class.class).in(person).get();
95 assertEquals(Person.class, personClass);
96 }
97
98 @Test
99 public void should_set_property_value() {
100 PropertyName.startPropertyAccess("name").ofType(String.class).in(person).set("Leia");
101 assertEquals("Leia", person.getName());
102 }
103
104 @Test
105 public void should_return_real_property() {
106 PropertyDescriptor property = PropertyName.startPropertyAccess("name").ofType(String.class).in(person).info();
107 assertNotNull(property);
108 assertEquals("name", property.getName());
109 assertEquals(String.class, property.getPropertyType());
110 }
111
112 @Test
113 public void should_throw_error_if_wrong_property_type_was_specified() {
114 String message = "The type of the property 'name' in org.fest.reflect.Person should be <java.lang.Integer> but was <java.lang.String>";
115 expectReflectionError(message).on(new CodeToTest() {
116 public void run() {
117 PropertyName.startPropertyAccess("name").ofType(Integer.class).in(person).get();
118 }
119 });
120 }
121
122 @Test
123 public void should_throw_error_if_property_name_is_invalid() {
124 expectReflectionError("Unable to find property 'age' in org.fest.reflect.Person").on(new CodeToTest() {
125 public void run() {
126 PropertyName.startPropertyAccess("age").ofType(Integer.class).in(person);
127 }
128 });
129 }
130
131 @Test
132 public void should_get_property_in_super_type() {
133 Jedi jedi = new Jedi("Yoda");
134 String jediName = PropertyName.startPropertyAccess("name").ofType(String.class).in(jedi).get();
135 assertEquals("Yoda", jediName);
136 }
137
138 @Test
139 public void should_throw_error_if_TypeRef_is_null() {
140 expectNullPointerException("The type reference of the property to access should not be null").on(new CodeToTest() {
141 public void run() {
142 PropertyName.startPropertyAccess("name").ofType((TypeRef<?>) null);
143 }
144 });
145 }
146
147 @Test
148 public void should_use_TypeRef_to_read_property() {
149 Jedi jedi = new Jedi("Yoda");
150 jedi.addPower("heal");
151 List<String> powers = PropertyName.startPropertyAccess("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).get();
152 assertEquals(1, powers.size());
153 assertEquals("heal", powers.get(0));
154
155 }
156
157 @Test
158 public void should_use_TypeRef_to_write_property() {
159 Jedi jedi = new Jedi("Yoda");
160 List<String> powers = newArrayList("heal");
161 PropertyName.startPropertyAccess("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).set(powers);
162 assertEquals(1, jedi.powers().size());
163 assertEquals("heal", jedi.powers().get(0));
164 }
165 }