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.tcp;
17  
18  import junit.framework.TestCase;
19  import net.sf.beep4j.Message;
20  import net.sf.beep4j.MessageStub;
21  import net.sf.beep4j.internal.TransportMapping;
22  import net.sf.beep4j.transport.Transport;
23  
24  import org.easymock.MockControl;
25  
26  public class TCPMappingTest extends TestCase {
27  	
28  	private MockControl transportCtrl;
29  	
30  	private Transport transport;
31  	
32  	private MockControl controllerCtrl;
33  	
34  	private ChannelController controller;
35  	
36  	private MockControl factoryCtrl;
37  	
38  	private ChannelControllerFactory factory;
39  	
40  	@Override
41  	protected void setUp() throws Exception {
42  		transportCtrl = MockControl.createControl(Transport.class);
43  		transport = (Transport) transportCtrl.getMock();
44  		factoryCtrl = MockControl.createControl(ChannelControllerFactory.class);
45  		factory = (ChannelControllerFactory) factoryCtrl.getMock();
46  		controllerCtrl = MockControl.createControl(ChannelController.class);
47  		controller = (ChannelController) controllerCtrl.getMock();
48  		
49  		factory.createChannelController(0, transport);
50  		factoryCtrl.setReturnValue(controller);
51  	}
52  	
53  	private void replay() {
54  		transportCtrl.replay();
55  		factoryCtrl.replay();
56  		controllerCtrl.replay();
57  	}
58  
59  	private void verify() {
60  		transportCtrl.verify();
61  		factoryCtrl.verify();
62  		controllerCtrl.verify();
63  	}
64  	
65  	public void testReceiveFrame() throws Exception {
66  		TransportMapping mapping = new TCPMapping(transport, factory);
67  		
68  		// define expectations
69  		controller.checkFrame(0, 50);
70  		controller.frameReceived(0, 50);
71  		
72  		replay();
73  		
74  		mapping.channelStarted(0);
75  		mapping.checkFrame(0, 0, 50);
76  		mapping.frameReceived(0, 0, 50);
77  		
78  		verify();
79  	}
80  	
81  	public void testProcessMappingFrame() throws Exception {
82  		TransportMapping mapping = new TCPMapping(transport, factory, 50);
83  		
84  		// define expectations
85  		controller.updateSendWindow(0, 4096);
86  		
87  		replay();
88  		
89  		mapping.channelStarted(0);
90  		mapping.processMappingFrame("SEQ 0 0 4096".split(" "));
91  		
92  		verify();
93  	}
94  	
95  	public void testStartCloseChannel() throws Exception {
96  		TransportMapping mapping = new TCPMapping(transport, factory);
97  		
98  		// define expectations
99  		replay();
100 		
101 		// test
102 		mapping.channelStarted(0);
103 		mapping.channelClosed(0);
104 		
105 		try {
106 			mapping.checkFrame(0, 0, 50);
107 			fail("channel 0 is closed, does not have a controller");
108 		} catch (Exception e) {
109 			// expected
110 			// TODO: catch proper exception
111 		}
112 		
113 		// verify
114 		verify();
115 	}
116 	
117 	public void testSendANS() throws Exception {
118 		TransportMapping mapping = new TCPMapping(transport, factory);
119 		Message message = new MessageStub();
120 		
121 		// define expectations
122 		controller.sendANS(0, 0, message);
123 		replay();
124 		
125 		// test
126 		mapping.channelStarted(0);
127 		mapping.sendANS(0, 0, 0, message);
128 		
129 		// verify
130 		verify();
131 	}
132 
133 	public void testSendNUL() throws Exception {
134 		TransportMapping mapping = new TCPMapping(transport, factory);
135 		
136 		// define expectations
137 		controller.sendNUL(0);
138 		replay();
139 		
140 		// test
141 		mapping.channelStarted(0);
142 		mapping.sendNUL(0, 0);
143 		
144 		// verify
145 		verify();
146 	}
147 	
148 	public void testSendERR() throws Exception {
149 		TransportMapping mapping = new TCPMapping(transport, factory);
150 		Message message = new MessageStub();
151 		
152 		// define expectations
153 		controller.sendERR(0, message);
154 		replay();
155 		
156 		// test
157 		mapping.channelStarted(0);
158 		mapping.sendERR(0, 0, message);
159 		
160 		// verify
161 		verify();
162 	}
163 	
164 	public void testSendMSG() throws Exception {
165 		TransportMapping mapping = new TCPMapping(transport, factory);
166 		Message message = new MessageStub();
167 		
168 		// define expectations
169 		controller.sendMSG(0, message);
170 		replay();
171 		
172 		// test
173 		mapping.channelStarted(0);
174 		mapping.sendMSG(0, 0, message);
175 		
176 		// verify
177 		verify();
178 	}
179 	
180 	public void testSendRPY() throws Exception {
181 		TransportMapping mapping = new TCPMapping(transport, factory);
182 		Message message = new MessageStub();
183 		
184 		// define expectations
185 		controller.sendRPY(0, message);
186 		replay();
187 		
188 		// test
189 		mapping.channelStarted(0);
190 		mapping.sendRPY(0, 0, message);
191 		
192 		// verify
193 		verify();
194 	}
195 	
196 	public void testCloseTransport() throws Exception {
197 		TransportMapping mapping = new TCPMapping(transport, factory);
198 		
199 		// define expectations
200 		transport.closeTransport();
201 		replay();
202 		
203 		// test
204 		mapping.channelStarted(0);
205 		mapping.closeTransport();
206 		
207 		// verify
208 		verify();
209 	}
210 	
211 }