KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > flow > jms > JMSFlowTest


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.nmr.flow.jms;
18
19 import org.apache.activemq.broker.BrokerService;
20 import org.apache.activemq.xbean.BrokerFactoryBean;
21 import org.apache.servicemix.jbi.container.ActivationSpec;
22 import org.apache.servicemix.jbi.container.JBIContainer;
23 import org.apache.servicemix.jbi.nmr.flow.jms.JMSFlow;
24 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
25 import org.apache.servicemix.tck.ReceiverComponent;
26 import org.apache.servicemix.tck.SenderComponent;
27 import org.springframework.core.io.ClassPathResource;
28
29 import junit.framework.TestCase;
30
31 /**
32  *
33  * JMSFlowTest
34  */

35 public class JMSFlowTest extends TestCase {
36     JBIContainer senderContainer = new JBIContainer();
37     JBIContainer receiverContainer = new JBIContainer();
38     private SenderComponent sender;
39     private ReceiverComponent receiver;
40     private static final int NUM_MESSAGES = 10;
41     protected BrokerService broker;
42     
43     /*
44      * @see TestCase#setUp()
45      */

46     protected void setUp() throws Exception JavaDoc {
47         super.setUp();
48        
49         BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jbi/nmr/flow/jca/broker.xml"));
50         bfb.afterPropertiesSet();
51         broker = bfb.getBroker();
52         broker.start();
53         senderContainer.setName("senderContainer");
54         senderContainer.setFlowName("jms?jmsURL=tcp://localhost:61216");
55         senderContainer.init();
56         senderContainer.start();
57         Object JavaDoc senderFlow = senderContainer.getFlow();
58         assertTrue(senderFlow instanceof JMSFlow);
59         
60         
61         receiverContainer.setName("receiverContainer");
62         receiverContainer.setFlowName("jms?jmsURL=tcp://localhost:61216");
63         receiverContainer.init();
64         receiverContainer.start();
65         Object JavaDoc receiverFlow = receiverContainer.getFlow();
66         assertTrue(receiverFlow instanceof JMSFlow);
67
68         Thread.sleep(2000);
69         
70         receiver = new ReceiverComponent();
71         sender = new SenderComponent();
72         sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE));
73     }
74     
75     protected void tearDown() throws Exception JavaDoc{
76         super.tearDown();
77         senderContainer.shutDown();
78         receiverContainer.shutDown();
79         broker.stop();
80     }
81     
82     public void testInOnly() throws Exception JavaDoc {
83         senderContainer.activateComponent(new ActivationSpec("sender", sender));
84         receiverContainer.activateComponent(new ActivationSpec("receiver", receiver));
85         Thread.sleep(1000);
86
87         sender.sendMessages(NUM_MESSAGES);
88         Thread.sleep(3000);
89         receiver.getMessageList().assertMessagesReceived(NUM_MESSAGES);
90     }
91
92     public void testClusteredInOnly() throws Exception JavaDoc {
93         final SenderComponent sender = new SenderComponent();
94         final ReceiverComponent receiver1 = new ReceiverComponent();
95         final ReceiverComponent receiver2 = new ReceiverComponent();
96         sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE));
97
98         senderContainer.activateComponent(new ActivationSpec("sender", sender));
99         senderContainer.activateComponent(new ActivationSpec("receiver", receiver1));
100         receiverContainer.activateComponent(new ActivationSpec("receiver", receiver2));
101         Thread.sleep(1000);
102
103         sender.sendMessages(NUM_MESSAGES);
104         Thread.sleep(3000);
105         assertTrue(receiver1.getMessageList().hasReceivedMessage());
106         assertTrue(receiver2.getMessageList().hasReceivedMessage());
107         receiver1.getMessageList().flushMessages();
108         receiver2.getMessageList().flushMessages();
109         
110         senderContainer.deactivateComponent("receiver");
111         Thread.sleep(1000);
112         
113         sender.sendMessages(NUM_MESSAGES);
114         Thread.sleep(3000);
115         assertFalse(receiver1.getMessageList().hasReceivedMessage());
116         assertTrue(receiver2.getMessageList().hasReceivedMessage());
117         receiver1.getMessageList().flushMessages();
118         receiver2.getMessageList().flushMessages();
119         
120         senderContainer.activateComponent(new ActivationSpec("receiver", receiver1));
121         receiverContainer.deactivateComponent("receiver");
122         Thread.sleep(1000);
123         
124         sender.sendMessages(NUM_MESSAGES);
125         Thread.sleep(3000);
126         assertTrue(receiver1.getMessageList().hasReceivedMessage());
127         assertFalse(receiver2.getMessageList().hasReceivedMessage());
128         receiver1.getMessageList().flushMessages();
129         receiver2.getMessageList().flushMessages();
130     }
131     
132 }
133
Popular Tags