Debian Codegraph @

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

raw
1 /*
2 * Created on Apr 8, 2012
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 @2006-2009 the original author or authors.
14 */
15 package org.fest.reflect.field;
16
17 import static org.fest.reflect.core.Reflection.field;
18 import static org.fest.util.Lists.newArrayList;
19
20 import static org.junit.Assert.assertSame;
21 import static org.mockito.Mockito.*;
22
23 import java.util.List;
24
25 import org.junit.Assert;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.matchers.JUnitMatchers;
29 import org.junit.rules.ExpectedException;
30
31 import org.fest.reflect.exception.ReflectionError;
32 import org.fest.reflect.reference.TypeRef;
33
34 /**
35 * @author Ivan Hristov
36 *
37 */
38 public class NestedFieldTest {
39
40 @Rule
41 public ExpectedException expectedException = ExpectedException.none();
42
43 private static final class BusinessService {
44 private final NotificationService notificationService;
45
46 public BusinessService() {
47 this.notificationService = new NotificationService();
48 }
49
50 public void doLogic() {
51 notificationService.save();
52 }
53
54 public NotificationService getNotificationService() {
55 return notificationService;
56 }
57 // ...
58 }
59
60 private static final class NotificationService {
61 private final Logger logger = new Logger();
62 private final IClientStatusDao clientStatusDao;
63
64 public NotificationService() {
65 this.clientStatusDao = new ClientStatusDao();
66 }
67
68 // ...
69
70 public void save() {
71 clientStatusDao.update();
72 }
73
74 public Logger getLogger() {
75 return logger;
76 }
77 }
78
79 private static class Logger {
80 // ...
81 }
82
83 private static interface IClientStatusDao {
84 void update();
85 }
86
87 private static class ClientStatusDao implements IClientStatusDao {
88 private final Session session;
89 @SuppressWarnings("unused")
90 private final List<String> listOfNames = newArrayList("Ivan", "Joel", "Alex");
91
92 public ClientStatusDao() {
93 this.session = new SessionImpl();
94 }
95
96 public void update() {
97 session.manageSession();
98 }
99 }
100
101 private static class SessionImpl implements Session {
102 private final SessionMonitor sessionMonitor = new SessionMonitor();
103
104 public void manageSession() {
105 sessionMonitor.monitor();
106 // logic goes here
107 }
108
109 }
110
111 private static interface Session {
112 void manageSession();
113 }
114
115 private static class SessionMonitor {
116 public void monitor() {
117 // monitoring logic here
118 }
119 }
120
121 @Test
122 public void shouldSetOneLevelNestedLoggerField() {
123 // GIVEN
124 BusinessService businessService = new BusinessService();
125 Logger loggerMock = mock(Logger.class);
126
127 // WHEN
128 field("notificationService.logger").ofType(Logger.class).in(businessService).set(loggerMock);
129
130 // THEN
131 assertSame(businessService.getNotificationService().getLogger(), loggerMock);
132 }
133
134 @Test
135 public void shouldThrowExceptionBecauseOfWrongOrder() {
136
137 expectedException.expect(ReflectionError.class);
138 expectedException.expectMessage(JUnitMatchers.containsString(//
139 "Unable to find field 'clientStatusDao' in org.fest.reflect.field.NestedFieldTest$BusinessService"));
140
141 // GIVEN
142 BusinessService businessService = new BusinessService();
143 Session sessionMock = mock(Session.class);
144
145 field("clientStatusDao.notificationService.session").ofType(Session.class).in(businessService).set(sessionMock);
146
147 // WHEN
148 businessService.doLogic();
149
150 // THEN
151 verify(sessionMock, times(1)).manageSession();
152 }
153
154 @Test
155 public void shouldSetSecondLevelNestedSessionField() {
156 // GIVEN
157 BusinessService businessService = new BusinessService();
158 Session sessionMock = mock(Session.class);
159
160 field("notificationService.clientStatusDao.session").ofType(Session.class).in(businessService).set(sessionMock);
161
162 // WHEN
163 businessService.doLogic();
164
165 // THEN
166 verify(sessionMock, times(1)).manageSession();
167 }
168
169 @Test
170 public void shouldGetSecondLevelNestedListOfNamesField() {
171 // GIVEN
172 BusinessService businessService = new BusinessService();
173
174 // WHEN
175 List<String> listOfNames = field("notificationService.clientStatusDao.listOfNames").ofType(new TypeRef<List<String>>() {})
176 .in(businessService).get();
177
178 // THEN
179 Assert.assertThat(listOfNames, JUnitMatchers.hasItems("Ivan", "Joel", "Alex"));
180 }
181
182 @Test
183 public void shouldSetThirdLevelNestedSessionFactoryField() {
184 // GIVEN
185 BusinessService businessService = new BusinessService();
186 SessionMonitor sessionMonitorMock = mock(SessionMonitor.class);
187
188 field("notificationService.clientStatusDao.session.sessionMonitor").ofType(SessionMonitor.class)//
189 .in(businessService).set(sessionMonitorMock);
190
191 // WHEN
192 businessService.doLogic();
193
194 // THEN
195 verify(sessionMonitorMock, times(1)).monitor();
196 }
197 }