Debian Codegraph @

src/test/java/org/fest/reflect/field/Field_staticField_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.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.Test;
28
29 import org.fest.reflect.Jedi;
30 import org.fest.reflect.Person;
31 import org.fest.reflect.reference.TypeRef;
32 import org.fest.test.CodeToTest;
33
34 /**
35 * Tests for the fluent interface for accessing static fields.
36 *
37 * @author Alex Ruiz
38 */
39 public class Field_staticField_Test {
40
41 @Test
42 public void should_throw_error_if_static_field_name_is_null() {
43 expectNullPointerException("The name of the static field to access should not be null").on(new CodeToTest() {
44 public void run() {
45 StaticFieldName.beginStaticFieldAccess(null);
46 }
47 });
48 }
49
50 @Test
51 public void should_throw_error_if_static_field_name_is_empty() {
52 expectIllegalArgumentException("The name of the static field to access should not be empty").on(new CodeToTest() {
53 public void run() {
54 StaticFieldName.beginStaticFieldAccess("");
55 }
56 });
57 }
58
59 @Test
60 public void should_throw_error_if_static_field_type_is_null() {
61 expectNullPointerException("The type of the static field to access should not be null").on(new CodeToTest() {
62 public void run() {
63 StaticFieldName.beginStaticFieldAccess("name").ofType((Class<?>) null);
64 }
65 });
66 }
67
68 @Test
69 public void should_throw_error_if_target_is_null() {
70 expectNullPointerException("Target should not be null").on(new CodeToTest() {
71 public void run() {
72 StaticFieldName.beginStaticFieldAccess("age").ofType(int.class).in(null);
73 }
74 });
75 }
76
77 @Test
78 public void should_get_static_field_value() {
79 Person.setCount(6);
80 int count = StaticFieldName.beginStaticFieldAccess("count").ofType(int.class).in(Person.class).get();
81 assertEquals(6, count);
82 }
83
84 @Test
85 public void should_set_static_field_value() {
86 StaticFieldName.beginStaticFieldAccess("count").ofType(int.class).in(Person.class).set(8);
87 assertEquals(8, Person.getCount());
88 }
89
90 @Test
91 public void should_return_real_static_field() {
92 java.lang.reflect.Field field = StaticFieldName.beginStaticFieldAccess("count").ofType(int.class).in(Person.class).info();
93 assertNotNull(field);
94 assertEquals("count", field.getName());
95 assertEquals(int.class, field.getType());
96
97 }
98
99 @Test
100 public void should_throw_error_if_wrong_static_field_type_was_specified() {
101 String msg = "The type of the field 'count' in org.fest.reflect.Person should be <java.lang.Float> but was <int>";
102 expectReflectionError(msg).on(new CodeToTest() {
103 public void run() {
104 StaticFieldName.beginStaticFieldAccess("count").ofType(Float.class).in(Person.class).get();
105 }
106 });
107 }
108
109 @Test
110 public void should_throw_error_if_static_field_name_is_invalid() {
111 expectReflectionError("Unable to find field 'age' in org.fest.reflect.Person").on(new CodeToTest() {
112 public void run() {
113 StaticFieldName.beginStaticFieldAccess("age").ofType(int.class).in(Person.class);
114 }
115 });
116 }
117
118 @Test
119 public void should_get_static_field_in_super_type() {
120 Person.setCount(8);
121 int count = StaticFieldName.beginStaticFieldAccess("count").ofType(int.class).in(Person.class).get();
122 assertEquals(8, count);
123 }
124
125 @Test
126 public void should_throw_error_if_TypeRef_is_null() {
127 String msg = "The type reference of the static field to access should not be null";
128 expectNullPointerException(msg).on(new CodeToTest() {
129 public void run() {
130 StaticFieldName.beginStaticFieldAccess("name").ofType((TypeRef<?>) null);
131 }
132 });
133 }
134
135 @Test
136 public void should_use_TypeRef_to_read_static_field() {
137 Jedi.addCommonPower("jump");
138 List<String> powers = StaticFieldName.beginStaticFieldAccess("commonPowers").ofType(new TypeRef<List<String>>() {})
139 .in(Jedi.class).get();
140 assertEquals(1, powers.size());
141 assertEquals("jump", powers.get(0));
142 }
143
144 @Test
145 public void should_use_TypeRef_to_write_static_field() {
146 List<String> powers = newArrayList("jump");
147 StaticFieldName.beginStaticFieldAccess("commonPowers").ofType(new TypeRef<List<String>>() {}).in(Jedi.class).set(powers);
148 assertEquals(1, Jedi.commonPowers().size());
149 assertEquals("jump", Jedi.commonPowers().get(0));
150 }
151 }