KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > DeliveryChannelImplTest


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.messaging;
18
19 import javax.jbi.messaging.DeliveryChannel;
20 import javax.jbi.messaging.ExchangeStatus;
21 import javax.jbi.messaging.InOut;
22 import javax.jbi.messaging.MessageExchangeFactory;
23 import javax.jbi.messaging.MessagingException;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.xml.namespace.QName JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.servicemix.components.util.ComponentSupport;
30 import org.apache.servicemix.jbi.container.ActivationSpec;
31 import org.apache.servicemix.jbi.container.JBIContainer;
32 import org.apache.servicemix.jbi.jaxp.StringSource;
33 import org.apache.servicemix.tck.SenderComponent;
34
35 public class DeliveryChannelImplTest extends TestCase {
36
37     protected JBIContainer container;
38     
39     protected void setUp() throws Exception JavaDoc {
40         container = new JBIContainer();
41         container.setEmbedded(true);
42         container.init();
43         container.start();
44     }
45     
46     protected void tearDown() throws Exception JavaDoc {
47         container.shutDown();
48     }
49     
50     public void testExchangeFactoryOnOpenChannel() throws Exception JavaDoc {
51         // Retrieve a delivery channel
52
TestComponent component = new TestComponent(null, null);
53         container.activateComponent(new ActivationSpec("component", component));
54         DeliveryChannel channel = component.getChannel();
55         // test
56
MessageExchangeFactory mef = channel.createExchangeFactory();
57         assertNotNull(mef);
58         assertNotNull(mef.createInOnlyExchange());
59     }
60     
61     public void testExchangeFactoryOnClosedChannel() throws Exception JavaDoc {
62         // Retrieve a delivery channel
63
TestComponent component = new TestComponent(null, null);
64         container.activateComponent(new ActivationSpec("component", component));
65         DeliveryChannel channel = component.getChannel();
66         // test
67
channel.close();
68         MessageExchangeFactory mef = channel.createExchangeFactory();
69         assertNotNull(mef);
70         try {
71             mef.createInOnlyExchange();
72             fail("Exchange creation should have failed (JBI: 5.5.2.1.4)");
73         } catch (MessagingException e) {
74             // expected
75
}
76     }
77     
78     public void testSendSyncOnSameComponent() throws Exception JavaDoc {
79         // Retrieve a delivery channel
80
TestComponent component = new TestComponent(new QName JavaDoc("service"), "endpoint");
81         container.activateComponent(new ActivationSpec("component", component));
82         final DeliveryChannel channel = component.getChannel();
83
84         // Create another thread
85
new Thread JavaDoc() {
86             public void run() {
87                 try {
88                     InOut me = (InOut) channel.accept(5000);
89                     NormalizedMessage nm = me.createMessage();
90                     nm.setContent(new StringSource("<response/>"));
91                     me.setOutMessage(nm);
92                     channel.sendSync(me);
93                 } catch (MessagingException e) {
94                     e.printStackTrace();
95                     fail("Exception caught: " + e);
96                 }
97             }
98         }.start();
99         
100         MessageExchangeFactory factory = channel.createExchangeFactoryForService(new QName JavaDoc("service"));
101         InOut me = factory.createInOutExchange();
102         NormalizedMessage nm = me.createMessage();
103         nm.setContent(new StringSource("<request/>"));
104         me.setInMessage(nm);
105         channel.sendSync(me);
106         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
107         me.setStatus(ExchangeStatus.DONE);
108         channel.send(me);
109     }
110     
111     public static class TestComponent extends ComponentSupport {
112         public TestComponent(QName JavaDoc service, String JavaDoc endpoint) {
113             super(service, endpoint);
114         }
115         public DeliveryChannel getChannel() throws MessagingException {
116             return getContext().getDeliveryChannel();
117         }
118     }
119     
120 }
121
Popular Tags