KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > eip > ContentBasedRouterTxTest


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.eip;
18
19 import javax.jbi.messaging.ExchangeStatus;
20 import javax.jbi.messaging.InOnly;
21 import javax.jbi.messaging.InOut;
22 import javax.xml.namespace.QName JavaDoc;
23
24 import org.apache.servicemix.eip.patterns.ContentBasedRouter;
25 import org.apache.servicemix.eip.support.RoutingRule;
26 import org.apache.servicemix.eip.support.XPathPredicate;
27 import org.apache.servicemix.jbi.jaxp.SourceTransformer;
28 import org.apache.servicemix.tck.ReceiverComponent;
29 import org.w3c.dom.Node JavaDoc;
30
31 public class ContentBasedRouterTxTest extends AbstractEIPTransactionalTest {
32
33     private ContentBasedRouter router;
34
35     protected void setUp() throws Exception JavaDoc {
36         super.setUp();
37
38         router = new ContentBasedRouter();
39         router.setRules(new RoutingRule[] {
40                 new RoutingRule(
41                         new XPathPredicate("/hello/@id = '1'"),
42                         createServiceExchangeTarget(new QName JavaDoc("target1"))),
43                 new RoutingRule(
44                         new XPathPredicate("/hello/@id = '2'"),
45                         createServiceExchangeTarget(new QName JavaDoc("target2"))),
46                 new RoutingRule(
47                         null,
48                         createServiceExchangeTarget(new QName JavaDoc("target3")))
49         });
50         configurePattern(router);
51         activateComponent(router, "router");
52     }
53     
54     public void testInOnlySync() throws Exception JavaDoc {
55         ReceiverComponent rec1 = activateReceiver("target1");
56         ReceiverComponent rec2 = activateReceiver("target2");
57         ReceiverComponent rec3 = activateReceiver("target3");
58         
59         tm.begin();
60         
61         InOnly me = client.createInOnlyExchange();
62         me.setService(new QName JavaDoc("router"));
63         me.getInMessage().setContent(createSource("<hello id='1' />"));
64         client.sendSync(me);
65         assertEquals(ExchangeStatus.DONE, me.getStatus());
66         
67         rec1.getMessageList().assertMessagesReceived(1);
68         rec2.getMessageList().assertMessagesReceived(0);
69         rec3.getMessageList().assertMessagesReceived(0);
70
71         rec1.getMessageList().flushMessages();
72         rec2.getMessageList().flushMessages();
73         rec3.getMessageList().flushMessages();
74         
75         me = client.createInOnlyExchange();
76         me.setService(new QName JavaDoc("router"));
77         me.getInMessage().setContent(createSource("<hello id='2' />"));
78         client.sendSync(me);
79         assertEquals(ExchangeStatus.DONE, me.getStatus());
80         
81         rec1.getMessageList().assertMessagesReceived(0);
82         rec2.getMessageList().assertMessagesReceived(1);
83         rec3.getMessageList().assertMessagesReceived(0);
84
85         rec1.getMessageList().flushMessages();
86         rec2.getMessageList().flushMessages();
87         rec3.getMessageList().flushMessages();
88
89         me = client.createInOnlyExchange();
90         me.setService(new QName JavaDoc("router"));
91         me.getInMessage().setContent(createSource("<hello id='3' />"));
92         client.sendSync(me);
93         assertEquals(ExchangeStatus.DONE, me.getStatus());
94         
95         rec1.getMessageList().assertMessagesReceived(0);
96         rec2.getMessageList().assertMessagesReceived(0);
97         rec3.getMessageList().assertMessagesReceived(1);
98
99         tm.commit();
100     }
101
102     public void testInOnlyAsync() throws Exception JavaDoc {
103         ReceiverComponent rec1 = activateReceiver("target1");
104         ReceiverComponent rec2 = activateReceiver("target2");
105         ReceiverComponent rec3 = activateReceiver("target3");
106         
107         tm.begin();
108         
109         InOnly me = client.createInOnlyExchange();
110         me.setService(new QName JavaDoc("router"));
111         me.getInMessage().setContent(createSource("<hello id='1' />"));
112         client.send(me);
113         
114         me = client.createInOnlyExchange();
115         me.setService(new QName JavaDoc("router"));
116         me.getInMessage().setContent(createSource("<hello id='2' />"));
117         client.send(me);
118         
119         me = client.createInOnlyExchange();
120         me.setService(new QName JavaDoc("router"));
121         me.getInMessage().setContent(createSource("<hello id='3' />"));
122         client.send(me);
123         
124         tm.commit();
125         
126         me = (InOnly) client.receive();
127         assertEquals(ExchangeStatus.DONE, me.getStatus());
128         me = (InOnly) client.receive();
129         assertEquals(ExchangeStatus.DONE, me.getStatus());
130         me = (InOnly) client.receive();
131         assertEquals(ExchangeStatus.DONE, me.getStatus());
132         
133         rec1.getMessageList().assertMessagesReceived(1);
134         rec2.getMessageList().assertMessagesReceived(1);
135         rec3.getMessageList().assertMessagesReceived(1);
136     }
137
138     public void testInOutSync() throws Exception JavaDoc {
139         tm.begin();
140         
141         activateComponent(new ReturnMockComponent("<from1/>"), "target1");
142         activateComponent(new ReturnMockComponent("<from2/>"), "target2");
143         activateComponent(new ReturnMockComponent("<from3/>"), "target3");
144         
145         InOut me = client.createInOutExchange();
146         me.setService(new QName JavaDoc("router"));
147         me.getInMessage().setContent(createSource("<hello id='1' />"));
148         client.sendSync(me);
149         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
150         Node JavaDoc node = new SourceTransformer().toDOMNode(me.getOutMessage());
151         assertEquals("from1", node.getFirstChild().getNodeName());
152         client.done(me);
153         
154         me = client.createInOutExchange();
155         me.setService(new QName JavaDoc("router"));
156         me.getInMessage().setContent(createSource("<hello id='2' />"));
157         client.sendSync(me);
158         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
159         node = new SourceTransformer().toDOMNode(me.getOutMessage());
160         assertEquals("from2", node.getFirstChild().getNodeName());
161         client.done(me);
162         
163         me = client.createInOutExchange();
164         me.setService(new QName JavaDoc("router"));
165         me.getInMessage().setContent(createSource("<hello id='3' />"));
166         client.sendSync(me);
167         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
168         node = new SourceTransformer().toDOMNode(me.getOutMessage());
169         assertEquals("from3", node.getFirstChild().getNodeName());
170         client.done(me);
171         
172         tm.commit();
173     }
174
175     public void testInOutAsync() throws Exception JavaDoc {
176         activateComponent(new ReturnMockComponent("<from1/>"), "target1");
177         activateComponent(new ReturnMockComponent("<from2/>"), "target2");
178         activateComponent(new ReturnMockComponent("<from3/>"), "target3");
179         
180         tm.begin();
181         
182         InOut me = client.createInOutExchange();
183         me.setService(new QName JavaDoc("router"));
184         me.getInMessage().setContent(createSource("<hello id='1' />"));
185         client.send(me);
186         
187         tm.commit();
188         
189         me = (InOut) client.receive();
190         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
191         Node JavaDoc node = new SourceTransformer().toDOMNode(me.getOutMessage());
192         assertEquals("from1", node.getFirstChild().getNodeName());
193         client.done(me);
194         
195         tm.begin();
196         
197         me = client.createInOutExchange();
198         me.setService(new QName JavaDoc("router"));
199         me.getInMessage().setContent(createSource("<hello id='2' />"));
200         client.send(me);
201         
202         tm.commit();
203         
204         me = (InOut) client.receive();
205         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
206         node = new SourceTransformer().toDOMNode(me.getOutMessage());
207         assertEquals("from2", node.getFirstChild().getNodeName());
208         client.done(me);
209         
210         tm.begin();
211         
212         me = client.createInOutExchange();
213         me.setService(new QName JavaDoc("router"));
214         me.getInMessage().setContent(createSource("<hello id='3' />"));
215         client.send(me);
216         
217         tm.commit();
218         
219         me = (InOut) client.receive();
220         assertEquals(ExchangeStatus.ACTIVE, me.getStatus());
221         node = new SourceTransformer().toDOMNode(me.getOutMessage());
222         assertEquals("from3", node.getFirstChild().getNodeName());
223         client.done(me);
224     }
225
226 }
227
Popular Tags