1   /*
2    *  Copyright 2006 Simon Raess
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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  	// --> test TransportContext methods <--
53  	
54  	public void testConnectionEstablished() throws Exception {
55  		// TODO: method stub
56  	}
57  	
58  	public void testExceptionCaught() throws Exception {
59  		// nothing to be checked, yet
60  	}
61  	
62  	public void testMessageReceived() throws Exception {
63  		// TODO: method stub
64  	}
65  	
66  	public void testConnectionClosed() throws Exception {
67  		// TODO: method stub
68  	}
69  	
70  	// --> test MessageHandler methods <--
71  	
72  	public void testReceiveANS() throws Exception {
73  		// TODO: method stub
74  	}
75  	
76  	public void testReceiveNUL() throws Exception {
77  		// TODO: method stub
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  		// define expectations
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  		// test
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 		// define expectations
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 		// test
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 		// define expectations
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 		// test
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 		// define expectations
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 		// test
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 	// --> test SessionManager methods <--
198 	
199 	public void testChannelStartRequested() throws Exception {
200 		// TODO: method stub
201 	}
202 	
203 	public void testChannelCloseRequested() throws Exception {
204 		// TODO: method stub
205 	}
206 	
207 	public void testSessionCloseRequested() throws Exception {
208 		// TODO: method stub
209 	}
210 	
211 	// --> test Session methods <--
212 	
213 	// TODO: reimplement this test
214 //	public void testStartChannelWithUri() throws Exception {
215 //		Mock handlerMock = mock(ChannelHandler.class);
216 //		ChannelHandler handler = (ChannelHandler) handlerMock.proxy();
217 //		
218 //		String profile = "http://example.org/profile/echo";
219 //		
220 //		// define expectations
221 //		mappingMock.expects(once()).method("channelStarted").with(eq(0));
222 //		mappingMock.expects(once()).method("sendMSG")
223 //				.with(eq(0), eq(1), eq(createStartChannelMessage(2, new ProfileInfo(profile))));
224 //
225 //		// test
226 //		InternalSession session = new SessionImpl(false, sessionHandler, mapping);
227 //		session.startChannel(profile, handler);
228 //	}
229 	
230 	// TODO: reimplement this test
231 //	public void testStartChannelWithProfileInfo() throws Exception {
232 //		Mock handlerMock = mock(ChannelHandler.class);
233 //		ChannelHandler handler = (ChannelHandler) handlerMock.proxy();
234 //		
235 //		String profile = "http://example.org/profile/echo";
236 //		ProfileInfo info = new ProfileInfo(profile, "abc");
237 //		
238 //		// define expectations
239 //		mappingMock.expects(once()).method("channelStarted").with(eq(0));
240 //		mappingMock.expects(once()).method("sendMSG")
241 //				.with(eq(0), eq(1), eq(createStartChannelMessage(2, info)));
242 //
243 //		// test
244 //		InternalSession session = new SessionImpl(false, sessionHandler, mapping);
245 //		session.startChannel(info, handler);
246 //	}
247 	
248 	public void testStartChannelWithFactory() throws Exception {
249 		// TODO: method stub
250 	}
251 	
252 	public void testClose() throws Exception {
253 		// TODO: method stub
254 	}
255 	
256 	// --> test InternalSession methods <--
257 	
258 	// TODO: reimplement this test
259 //	public void testSendMessage() throws Exception {
260 //		Message m1 = new MessageStub();
261 //		Message m2 = new MessageStub();
262 //		Message m3 = new MessageStub();
263 //		
264 //		// define expectations
265 //		mappingMock.expects(once()).method("channelStarted").with(eq(0));
266 //		mappingMock.expects(once()).method("sendMSG").with(eq(0), eq(1), same(m1));
267 //		mappingMock.expects(once()).method("sendMSG").with(eq(0), eq(2), same(m2));
268 //		mappingMock.expects(once()).method("sendMSG").with(eq(0), eq(3), same(m3));
269 //
270 //		// test
271 //		InternalSession session = new SessionImpl(false, sessionHandler, mapping);
272 //		session.sendMessage(0, m1, new NullReplyListener());
273 //		session.sendMessage(0, m2, new NullReplyListener());
274 //		session.sendMessage(0, m3, new NullReplyListener());
275 //	}
276 	
277 	public void testRequestChannelClose() throws Exception {
278 		// TODO: test channel close of channel other than channel 0
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 		// define expectations
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 		// test
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 }