KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > nmr > PubSubTest


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;
18
19 import javax.jbi.messaging.MessageExchange;
20 import javax.xml.namespace.QName JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.apache.servicemix.jbi.container.ActivationSpec;
25 import org.apache.servicemix.jbi.container.JBIContainer;
26 import org.apache.servicemix.jbi.container.SubscriptionSpec;
27 import org.apache.servicemix.jbi.resolver.SubscriptionFilter;
28 import org.apache.servicemix.tck.ReceiverComponent;
29 import org.apache.servicemix.tck.SenderComponent;
30
31 public class PubSubTest extends TestCase {
32
33     private SenderComponent sender;
34     private JBIContainer container;
35
36     protected void setUp() throws Exception JavaDoc {
37         container = new JBIContainer();
38         container.setEmbedded(true);
39         container.setFlowName("seda");
40         container.init();
41         container.start();
42         
43         sender = new SenderComponent();
44         ActivationSpec as = new ActivationSpec("source",sender);
45         as.setService(new QName JavaDoc("http://www.test.com","source"));
46         as.setFailIfNoDestinationEndpoint(false);
47         container.activateComponent(as);
48     }
49
50     protected void tearDown() throws Exception JavaDoc {
51         container.shutDown();
52     }
53     
54     public void testPubSub() throws Exception JavaDoc {
55         ReceiverComponent recListener = new ReceiverComponent();
56         container.activateComponent(createReceiverAS("receiver",recListener));
57         sender.sendMessages(1);
58         recListener.getMessageList().assertMessagesReceived(1);
59     }
60     
61     public void testPubSubFiltered() throws Exception JavaDoc {
62         ReceiverComponent recListener = new ReceiverComponent();
63         container.activateComponent(createReceiverASFiltered("receiver",recListener));
64         sender.sendMessages(1, false);
65         recListener.getMessageList().assertMessagesReceived(1);
66     }
67
68     private ActivationSpec createReceiverAS(String JavaDoc id, Object JavaDoc component) {
69         ActivationSpec as = new ActivationSpec(id, component);
70         SubscriptionSpec ss = new SubscriptionSpec();
71         ss.setService(new QName JavaDoc("http://www.test.com","source"));
72         as.setSubscriptions(new SubscriptionSpec[] { ss });
73         as.setFailIfNoDestinationEndpoint(false);
74         return as;
75     }
76
77     private ActivationSpec createReceiverASFiltered(String JavaDoc id, Object JavaDoc component) {
78         ActivationSpec as = new ActivationSpec(id, component);
79         SubscriptionSpec ss = new SubscriptionSpec();
80         ss.setService(new QName JavaDoc("http://www.test.com","source"));
81         ss.setFilter(new Filter());
82         as.setSubscriptions(new SubscriptionSpec[] { ss });
83         as.setFailIfNoDestinationEndpoint(false);
84         return as;
85     }
86
87     public static class Filter implements SubscriptionFilter {
88
89         public boolean matches(MessageExchange arg0) {
90             System.out.println("Matches");
91             return true;
92         }
93         
94     }
95 }
96
Popular Tags