Debian Codegraph @

src/test/java/org/fest/reflect/field/Field_field_Test.java

raw
1 /*
2 * Created on May 18, 2007
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 @2007-2009 the original author or authors.
14 */
15 package org.fest.reflect.field;
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.util.List;
26
27 import org.junit.Before;
28 import org.junit.Test;
29
30 import org.fest.reflect.Jedi;
31 import org.fest.reflect.Person;
32 import org.fest.reflect.reference.TypeRef;
33 import org.fest.test.CodeToTest;
34
35 /**
36 * Tests for the fluent interface for accessing fields.
37 *
38 * @author Alex Ruiz
39 */
40 public class Field_field_Test {
41
42 private Person person;
43
44 @Before
45 public void setUp() {
46 person = new Person("Luke");
47 }
48
49 @Test
50 public void should_throw_error_if_field_name_is_null() {
51 expectNullPointerException("The name of the field to access should not be null").on(new CodeToTest() {
52 public void run() {
53 FieldName.beginFieldAccess(null);
54 }
55 });
56 }
57
58 @Test
59 public void should_throw_error_if_field_name_is_empty() {
60 expectIllegalArgumentException("The name of the field to access should not be empty").on(new CodeToTest() {
61 public void run() {
62 FieldName.beginFieldAccess("");
63 }
64 });
65 }
66
67 @Test
68 public void should_throw_error_if_field_type_is_null() {
69 expectNullPointerException("The type of the field to access should not be null").on(new CodeToTest() {
70 public void run() {
71 FieldName.beginFieldAccess("name").ofType((Class<?>) null);
72 }
73 });
74 }
75
76 @Test
77 public void should_throw_error_if_target_is_null() {
78 expectNullPointerException("Target should not be null").on(new CodeToTest() {
79 public void run() {
80 FieldName.beginFieldAccess("name").ofType(String.class).in(null);
81 }
82 });
83 }
84
85 @Test
86 public void should_get_field_value() {
87 String personName = FieldName.beginFieldAccess("name").ofType(String.class).in(person).get();
88 assertEquals("Luke", personName);
89 }
90
91 @Test
92 public void should_set_field_value() {
93 FieldName.beginFieldAccess("name").ofType(String.class).in(person).set("Leia");
94 assertEquals("Leia", person.getName());
95 }
96
97 @Test
98 public void should_return_real_field() {
99 java.lang.reflect.Field field = FieldName.beginFieldAccess("name").ofType(String.class).in(person).info();
100 assertNotNull(field);
101 assertEquals("name", field.getName());
102 assertEquals(String.class, field.getType());
103 }
104
105 @Test
106 public void should_throw_error_if_wrong_field_type_was_specified() {
107 String msg = "The type of the field 'name' in org.fest.reflect.Person should be <java.lang.Integer> but was <java.lang.String>";
108 expectReflectionError(msg).on(new CodeToTest() {
109 public void run() {
110 FieldName.beginFieldAccess("name").ofType(Integer.class).in(person).get();
111 }
112 });
113 }
114
115 @Test
116 public void should_throw_error_if_field_name_is_invalid() {
117 expectReflectionError("Unable to find field 'age' in org.fest.reflect.Person").on(new CodeToTest() {
118 public void run() {
119 FieldName.beginFieldAccess("age").ofType(Integer.class).in(person);
120 }
121 });
122 }
123
124 @Test
125 public void should_get_field_in_super_type() {
126 Jedi jedi = new Jedi("Yoda");
127 String jediName = FieldName.beginFieldAccess("name").ofType(String.class).in(jedi).get();
128 assertEquals("Yoda", jediName);
129 }
130
131 @Test
132 public void should_throw_error_if_TypeRef_is_null() {
133 expectNullPointerException("The type reference of the field to access should not be null").on(new CodeToTest() {
134 public void run() {
135 FieldName.beginFieldAccess("name").ofType((TypeRef<?>) null);
136 }
137 });
138 }
139
140 @Test
141 public void should_use_TypeRef_to_read_field() {
142 Jedi jedi = new Jedi("Yoda");
143 jedi.addPower("heal");
144 List<String> powers = FieldName.beginFieldAccess("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).get();
145 assertEquals(1, powers.size());
146 assertEquals("heal", powers.get(0));
147 }
148
149 @Test
150 public void should_use_TypeRef_to_write_field() {
151 Jedi jedi = new Jedi("Yoda");
152 List<String> powers = newArrayList("heal");
153 FieldName.beginFieldAccess("powers").ofType(new TypeRef<List<String>>() {}).in(jedi).set(powers);
154 assertEquals(1, jedi.powers().size());
155 assertEquals("heal", jedi.powers().get(0));
156 }
157 }