KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > transport > JoramConnectionTest


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: JoramConnectionTest.java 12:34:54 PM ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.jbi.transport;
23
24 import java.util.concurrent.ArrayBlockingQueue JavaDoc;
25
26 import javax.jms.Connection JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.easymock.EasyMock;
32
33 import junit.framework.TestCase;
34
35 import org.objectweb.petals.jbi.transport.mock.MockInitialContext;
36
37 /**
38  * Tests of the JoramConnection
39  *
40  * @author ddesjardins - eBMWebsourcing
41  */

42 public class JoramConnectionTest extends TestCase {
43
44     protected MockInitialContext initialContext;
45
46     protected JoramConnection joramConnection;
47
48     public void setUp() throws NamingException JavaDoc {
49         initialContext = new MockInitialContext();
50         joramConnection = new JoramConnection(1, 16200, "petals", "petals", "127.0.0.1",
51             initialContext);
52     }
53 /*
54     public void testGetQueueConnectionFactory() {
55         QueueConnectionFactory queueConnectionFactory = EasyMock
56             .createMock(QueueConnectionFactory.class);
57         joramConnection.qcf = queueConnectionFactory;
58         assertEquals(queueConnectionFactory, joramConnection
59             .getQueueConnectionFactory());
60     }
61
62     public void testInit() throws Exception {
63         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
64             new Method[] {
65                 JoramConnection.class.getDeclaredMethod("connectAdminModule",
66                     new Class[] {}),
67                 JoramConnection.class.getDeclaredMethod("disconnectAdminModule",
68                     new Class[] {}),
69                 JoramConnection.class.getDeclaredMethod(
70                     "setupQueueConnectionFactory", new Class[] {}),
71                 JoramConnection.class.getDeclaredMethod("setupConnectionFactory",
72                     new Class[] {}),
73                 JoramConnection.class
74                     .getDeclaredMethod("setupQueue", new Class[] {})});
75         QueueConnectionFactory queueConnectionFactory = EasyMock
76             .createMock(QueueConnectionFactory.class);
77         Queue queue = EasyMock.createMock(Queue.class);
78
79         joramConnection.connectAdminModule();
80         EasyMock.expect(joramConnection.setupConnectionFactory()).andReturn(
81             EasyMock.createMock(ConnectionFactory.class)).anyTimes();
82         EasyMock.expect(joramConnection.setupQueueConnectionFactory()).andReturn(
83             queueConnectionFactory).anyTimes();
84         EasyMock.expect(joramConnection.setupQueue()).andReturn(queue).anyTimes();
85         joramConnection.disconnectAdminModule();
86
87         EasyMock.replay(joramConnection);
88
89         joramConnection.init();
90
91         assertEquals(joramConnection.qcf, queueConnectionFactory);
92         assertEquals(joramConnection.queue, queue);
93     }
94
95     public void testInitException() throws SecurityException,
96         NoSuchMethodException, ConnectException, UnknownHostException,
97         AdminException, NamingException {
98         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
99             new Method[] {
100                 JoramConnection.class.getDeclaredMethod("connectAdminModule",
101                     new Class[] {}),
102                 JoramConnection.class.getDeclaredMethod("disconnectAdminModule",
103                     new Class[] {}),
104                 JoramConnection.class.getDeclaredMethod(
105                     "setupQueueConnectionFactory", new Class[] {}),
106                 JoramConnection.class.getDeclaredMethod("setupConnectionFactory",
107                     new Class[] {}),
108                 JoramConnection.class
109                     .getDeclaredMethod("setupQueue", new Class[] {})});
110
111         joramConnection.connectAdminModule();
112         EasyMock.expect(joramConnection.setupConnectionFactory()).andThrow(
113             new NamingException(
114                 "problem while the setup of the Connection factory"))
115             .anyTimes();
116
117         EasyMock.replay(joramConnection);
118
119         try {
120             joramConnection.init();
121             fail();
122         } catch (Exception e) {
123             // do nothing
124         }
125     }
126
127     @SuppressWarnings("unchecked")
128     public void testSetupConnectionFactory() throws ConnectException,
129         NamingException, SecurityException, NoSuchMethodException {
130         InitialContext initialContext = EasyMock
131             .createMock(InitialContext.class);
132         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
133             new Method[] {JoramConnection.class.getDeclaredMethod(
134                 "createConnectionFactory", new Class[] {})});
135         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
136         ConnectionFactory connectionFactory = EasyMock
137             .createMock(ConnectionFactory.class);
138
139         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
140         EasyMock.expect(joramConnection.createConnectionFactory()).andReturn(
141             connectionFactory).anyTimes();
142         initialContext.bind("JoramConnectionFactory", connectionFactory);
143
144         EasyMock.replay(initialContext);
145         EasyMock.replay(joramConnection);
146
147         joramConnection.ictx = initialContext;
148
149         assertEquals(joramConnection.setupConnectionFactory(), connectionFactory);
150     }
151
152     @SuppressWarnings("unchecked")
153     public void testSetupConnectionFactoryBound() throws ConnectException,
154         NamingException, SecurityException, NoSuchMethodException {
155         InitialContext initialContext = EasyMock
156             .createMock(InitialContext.class);
157         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
158             new Method[] {JoramConnection.class.getDeclaredMethod(
159                 "createConnectionFactory", new Class[] {})});
160         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
161         List<NameClassPair> names = new ArrayList<NameClassPair>();
162         names.add(new NameClassPair("JoramConnectionFactory",
163             ConnectionFactory.class.getName()));
164         list1.setElems(names);
165
166         ConnectionFactory connectionFactory = EasyMock
167             .createMock(ConnectionFactory.class);
168
169         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
170         EasyMock.expect(joramConnection.createConnectionFactory()).andReturn(
171             connectionFactory).anyTimes();
172         EasyMock.expect(initialContext.lookup(JoramConnection.CONNECTION_FACTORY))
173             .andReturn(connectionFactory).anyTimes();
174
175         EasyMock.replay(initialContext);
176         EasyMock.replay(joramConnection);
177
178         joramConnection.ictx = initialContext;
179
180         assertEquals(joramConnection.setupConnectionFactory(), connectionFactory);
181     }
182
183     @SuppressWarnings( {"unchecked", "static-access"})
184     public void testSetupQueue() throws Exception {
185         InitialContext initialContext = EasyMock
186             .createMock(InitialContext.class);
187         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
188         Queue queue = EasyMock.createMock(Queue.class);
189         JoramConnection.class.getDeclaredMethods();
190
191         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
192             new Method[] {JoramConnection.class.getDeclaredMethod("createQueue",
193                 new Class[] {String.class})});
194
195         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
196         EasyMock.expect(joramConnection.createQueue("JoramConnection-0"))
197             .andReturn(queue).anyTimes();
198
199         initialContext.bind("JoramConnection-0", queue);
200         queue.setFreeReading();
201         queue.setFreeWriting();
202
203         EasyMock.replay(initialContext);
204         EasyMock.replay(queue);
205         EasyMock.replay(joramConnection);
206
207         joramConnection.id = 0;
208         joramConnection.ictx = initialContext;
209
210         assertEquals(joramConnection.setupQueue(), queue);
211     }
212
213     @SuppressWarnings( {"unchecked", "static-access"})
214     public void testSetupQueueBound() throws Exception {
215         InitialContext initialContext = EasyMock
216             .createMock(InitialContext.class);
217         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
218         Queue queue = EasyMock.createMock(Queue.class);
219         JoramConnection.class.getDeclaredMethods();
220         List<NameClassPair> names = new ArrayList<NameClassPair>();
221         names.add(new NameClassPair("JoramConnection-0", ConnectionFactory.class
222             .getName()));
223         list1.setElems(names);
224
225         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
226             new Method[] {JoramConnection.class.getDeclaredMethod("createQueue",
227                 new Class[] {String.class})});
228
229         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
230         EasyMock.expect(joramConnection.createQueue("JoramConnection-0"))
231             .andReturn(queue).anyTimes();
232         EasyMock.expect(initialContext.lookup("JoramConnection-0")).andReturn(queue)
233             .anyTimes();
234
235         initialContext.bind("JoramConnection-0", queue);
236         queue.setFreeReading();
237         queue.setFreeWriting();
238
239         EasyMock.replay(initialContext);
240         EasyMock.replay(queue);
241         EasyMock.replay(joramConnection);
242
243         joramConnection.id = 0;
244         joramConnection.ictx = initialContext;
245
246         assertEquals(joramConnection.setupQueue(), queue);
247     }
248
249     @SuppressWarnings( {"unchecked", "static-access"})
250     public void testSetupQueueConnectionFactoryBound() throws Exception {
251         InitialContext initialContext = EasyMock
252             .createMock(InitialContext.class);
253         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
254         QueueConnectionFactory queueConnectionFactory = EasyMock
255             .createMock(QueueConnectionFactory.class);
256         List<NameClassPair> names = new ArrayList<NameClassPair>();
257         names.add(new NameClassPair(JoramConnection.QUEUE_CONNECTION_FACTORY,
258             ConnectionFactory.class.getName()));
259         list1.setElems(names);
260
261         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
262             new Method[] {JoramConnection.class.getDeclaredMethod(
263                 "createQueueConnectionFactory", new Class[0])});
264
265         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
266         EasyMock.expect(
267             initialContext.lookup(JoramConnection.QUEUE_CONNECTION_FACTORY))
268             .andReturn(queueConnectionFactory).anyTimes();
269
270         EasyMock.replay(initialContext);
271         EasyMock.replay(joramConnection);
272         EasyMock.replay(queueConnectionFactory);
273
274         joramConnection.id = 0;
275         joramConnection.ictx = initialContext;
276
277         assertEquals(joramConnection.setupQueueConnectionFactory(),
278             queueConnectionFactory);
279     }
280
281     @SuppressWarnings( {"unchecked", "static-access"})
282     public void testSetupQueueConnectionFactory() throws Exception {
283         InitialContext initialContext = EasyMock
284             .createMock(InitialContext.class);
285         MockNamingEnumeration<NameClassPair> list1 = new MockNamingEnumeration<NameClassPair>();
286         QueueConnectionFactory queueConnectionFactory = EasyMock
287             .createMock(QueueConnectionFactory.class);
288         JoramConnection.class.getDeclaredMethods();
289
290         JoramConnection joramConnection = EasyMock.createMock(JoramConnection.class,
291             new Method[] {JoramConnection.class.getDeclaredMethod(
292                 "createQueueConnectionFactory", new Class[0])});
293
294         EasyMock.expect(initialContext.list("/")).andReturn(list1).anyTimes();
295         EasyMock.expect(joramConnection.createQueueConnectionFactory()).andReturn(
296             queueConnectionFactory).anyTimes();
297         
298         initialContext.bind(JoramConnection.QUEUE_CONNECTION_FACTORY,
299             queueConnectionFactory);
300
301         EasyMock.replay(initialContext);
302         EasyMock.replay(joramConnection);
303         EasyMock.replay(queueConnectionFactory);
304
305         joramConnection.id = 0;
306         joramConnection.ictx = initialContext;
307
308         assertEquals(joramConnection.setupQueueConnectionFactory(),
309             queueConnectionFactory);
310     }
311
312     public void testStartListening() throws JMSException {
313         MessageListener messageListener = EasyMock
314             .createMock(MessageListener.class);
315         QueueConnectionFactory queueConnectionFactory = EasyMock
316             .createMock(QueueConnectionFactory.class);
317         QueueConnection queueConnection = EasyMock
318             .createMock(QueueConnection.class);
319         QueueSession queueSession = EasyMock.createMock(QueueSession.class);
320         QueueReceiver queueReceiver = EasyMock.createMock(QueueReceiver.class);
321
322         EasyMock.expect(
323             queueConnectionFactory.createQueueConnection("petals", "petals"))
324             .andReturn(queueConnection).anyTimes();
325         EasyMock.expect(queueConnection.createQueueSession(true, 0)).andReturn(
326             queueSession).anyTimes();
327         EasyMock.expect(queueSession.createReceiver(null)).andReturn(
328             queueReceiver).anyTimes();
329         queueReceiver.setMessageListener(messageListener);
330         queueConnection.start();
331
332         EasyMock.replay(messageListener);
333         EasyMock.replay(queueConnectionFactory);
334         EasyMock.replay(queueConnection);
335         EasyMock.replay(queueSession);
336         EasyMock.replay(queueReceiver);
337
338         joramConnection.qcf = queueConnectionFactory;
339
340         joramConnection.startListening(messageListener);
341     }
342 */

343     public void testStop() throws JMSException JavaDoc, NamingException JavaDoc {
344         Session JavaDoc session = EasyMock.createMock(Session JavaDoc.class);
345         Connection JavaDoc conn= EasyMock.createMock(Connection JavaDoc.class);
346
347         session.close();
348         conn.stop();
349
350         EasyMock.replay(session);
351         EasyMock.replay(conn);
352
353         joramConnection.sessionsPool = new ArrayBlockingQueue JavaDoc<Session JavaDoc>(1);
354         joramConnection.session = session;
355         joramConnection.connection = conn;
356         joramConnection.stop();
357     }
358 }
359
Popular Tags