1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.beep4j.internal;
17
18 import net.sf.beep4j.ChannelHandler;
19 import net.sf.beep4j.Message;
20 import net.sf.beep4j.MessageStub;
21 import net.sf.beep4j.NullCloseChannelCallback;
22 import net.sf.beep4j.ReplyHandler;
23 import net.sf.beep4j.SessionHandler;
24 import net.sf.beep4j.internal.profile.BEEPError;
25 import net.sf.beep4j.internal.profile.ChannelManagementProfile;
26 import net.sf.beep4j.internal.profile.Greeting;
27
28 import org.jmock.Mock;
29 import org.jmock.MockObjectTestCase;
30
31 public class SessionImplTest extends MockObjectTestCase {
32
33 private Mock mappingMock;
34
35 private TransportMapping mapping;
36
37 private Mock sessionHandlerMock;
38
39 private SessionHandler sessionHandler;
40
41 @Override
42 protected void setUp() throws Exception {
43 super.setUp();
44
45 mappingMock = mock(TransportMapping.class);
46 mapping = (TransportMapping) mappingMock.proxy();
47
48 sessionHandlerMock = mock(SessionHandler.class);
49 sessionHandler = (SessionHandler) sessionHandlerMock.proxy();
50 }
51
52
53
54 public void testConnectionEstablished() throws Exception {
55
56 }
57
58 public void testExceptionCaught() throws Exception {
59
60 }
61
62 public void testMessageReceived() throws Exception {
63
64 }
65
66 public void testConnectionClosed() throws Exception {
67
68 }
69
70
71
72 public void testReceiveANS() throws Exception {
73
74 }
75
76 public void testReceiveNUL() throws Exception {
77
78 }
79
80 public void testReceiveMSG() throws Exception {
81 Mock profileMock = mock(ChannelManagementProfile.class);
82 final ChannelManagementProfile profile = (ChannelManagementProfile) profileMock.proxy();
83 Mock channelHandlerMock = mock(ChannelHandler.class);
84 ChannelHandler channelHandler = (ChannelHandler) channelHandlerMock.proxy();
85 Message message = new MessageStub();
86 Message greeting = new MessageStub();
87
88
89 mappingMock.expects(once()).method("channelStarted").with(eq(0));
90 profileMock.expects(once()).method("createChannelHandler").with(ANYTHING).will(returnValue(channelHandler));
91 profileMock.expects(once()).method("receivedGreeting").with(same(greeting));
92 channelHandlerMock.expects(once()).method("channelOpened").with(ANYTHING);
93 channelHandlerMock.expects(once()).method("messageReceived").with(same(message), ANYTHING);
94 sessionHandlerMock.expects(once()).method("sessionOpened").with(ANYTHING);
95
96
97 MessageHandler session = new SessionImpl(false, sessionHandler, mapping) {
98 @Override
99 protected ChannelManagementProfile createChannelManagementProfile(boolean initiator) {
100 return profile;
101 }
102 };
103 session.receiveRPY(0, 0, greeting);
104 session.receiveMSG(0, 0, message);
105 }
106
107 public void testReceiveInitialERR() throws Exception {
108 Mock profileMock = mock(ChannelManagementProfile.class);
109 final ChannelManagementProfile profile = (ChannelManagementProfile) profileMock.proxy();
110 Mock channelHandlerMock = mock(ChannelHandler.class);
111 ChannelHandler channelHandler = (ChannelHandler) channelHandlerMock.proxy();
112 Message message = new MessageStub();
113
114
115 mappingMock.expects(once()).method("channelStarted").with(eq(0));
116 mappingMock.expects(once()).method("closeTransport");
117 profileMock.expects(once()).method("createChannelHandler").with(ANYTHING).will(returnValue(channelHandler));
118 channelHandlerMock.expects(once()).method("channelOpened").with(ANYTHING);
119 sessionHandlerMock.expects(once()).method("sessionStartDeclined")
120 .with(eq(550), eq("still working"));
121 profileMock.expects(once()).method("receivedError")
122 .with(same(message))
123 .will(returnValue(new BEEPError(550, "still working")));
124
125
126 MessageHandler session = new SessionImpl(false, sessionHandler, mapping) {
127 @Override
128 protected ChannelManagementProfile createChannelManagementProfile(boolean initiator) {
129 return profile;
130 }
131 };
132 session.receiveERR(0, 0, message);
133 }
134
135 public void testReceiveInitialRPY() throws Exception {
136 Mock profileMock = mock(ChannelManagementProfile.class);
137 final ChannelManagementProfile profile = (ChannelManagementProfile) profileMock.proxy();
138 Mock channelHandlerMock = mock(ChannelHandler.class);
139 ChannelHandler channelHandler = (ChannelHandler) channelHandlerMock.proxy();
140 Message message = new MessageStub();
141
142
143 mappingMock.expects(once()).method("channelStarted").with(eq(0));
144 profileMock.expects(once()).method("createChannelHandler").with(ANYTHING).will(returnValue(channelHandler));
145 channelHandlerMock.expects(once()).method("channelOpened").with(ANYTHING);
146 profileMock.expects(once()).method("receivedGreeting")
147 .with(same(message))
148 .will(returnValue(new Greeting(new String[0], new String[0], new String[] { "abc" })));
149 sessionHandlerMock.expects(once()).method("sessionOpened").with(ANYTHING);
150
151
152 MessageHandler session = new SessionImpl(false, sessionHandler, mapping) {
153 @Override
154 protected ChannelManagementProfile createChannelManagementProfile(boolean initiator) {
155 return profile;
156 }
157 };
158 session.receiveRPY(0, 0, message);
159 }
160
161 public void testReceiveRPY() throws Exception {
162 Mock profileMock = mock(ChannelManagementProfile.class);
163 final ChannelManagementProfile profile = (ChannelManagementProfile) profileMock.proxy();
164 Mock channelHandlerMock = mock(ChannelHandler.class);
165 ChannelHandler channelHandler = (ChannelHandler) channelHandlerMock.proxy();
166 Mock replyListenerMock = mock(ReplyHandler.class);
167 ReplyHandler replyListener = (ReplyHandler) replyListenerMock.proxy();
168
169 Message greeting = new MessageStub();
170 Message message = new MessageStub();
171 Message reply = new MessageStub();
172
173
174 sessionHandlerMock.expects(once()).method("sessionOpened").with(ANYTHING);
175 mappingMock.expects(once()).method("channelStarted").with(eq(0));
176 profileMock.expects(once()).method("createChannelHandler").with(ANYTHING).will(returnValue(channelHandler));
177 channelHandlerMock.expects(once()).method("channelOpened").with(ANYTHING);
178 profileMock.expects(once()).method("receivedGreeting")
179 .with(same(greeting))
180 .will(returnValue(new Greeting(new String[0], new String[0], new String[] { "abc" })));
181
182 mappingMock.expects(once()).method("sendMSG").with(eq(0), eq(1), same(message));
183 replyListenerMock.expects(once()).method("receivedRPY").with(same(reply));
184
185
186 SessionImpl session = new SessionImpl(false, sessionHandler, mapping) {
187 @Override
188 protected ChannelManagementProfile createChannelManagementProfile(boolean initiator) {
189 return profile;
190 }
191 };
192 session.receiveRPY(0, 0, greeting);
193 session.sendMessage(0, message, replyListener);
194 session.receiveRPY(0, 1, reply);
195 }
196
197
198
199 public void testChannelStartRequested() throws Exception {
200
201 }
202
203 public void testChannelCloseRequested() throws Exception {
204
205 }
206
207 public void testSessionCloseRequested() throws Exception {
208
209 }
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248 public void testStartChannelWithFactory() throws Exception {
249
250 }
251
252 public void testClose() throws Exception {
253
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277 public void testRequestChannelClose() throws Exception {
278
279
280 Mock profileMock = mock(ChannelManagementProfile.class);
281 final ChannelManagementProfile profile = (ChannelManagementProfile) profileMock.proxy();
282 Mock channelHandlerMock = mock(ChannelHandler.class);
283 ChannelHandler channelHandler = (ChannelHandler) channelHandlerMock.proxy();
284
285
286 mappingMock.expects(once()).method("channelStarted").with(eq(0));
287 profileMock.expects(once()).method("createChannelHandler").with(ANYTHING).will(returnValue(channelHandler));
288 channelHandlerMock.expects(once()).method("channelOpened").with(ANYTHING);
289 profileMock.expects(once()).method("closeChannel").with(eq(0), ANYTHING);
290
291
292 InternalSession session = new SessionImpl(false, sessionHandler, mapping) {
293 @Override
294 protected ChannelManagementProfile createChannelManagementProfile(boolean initiator) {
295 return profile;
296 }
297 };
298
299 session.requestChannelClose(0, new NullCloseChannelCallback());
300 }
301
302 }