KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > examples > AbstractSpringTestSupport


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.examples;
18
19 import javax.jbi.component.Component;
20
21 import junit.framework.TestCase;
22
23 import org.apache.servicemix.components.util.ComponentAdaptor;
24 import org.apache.servicemix.jbi.container.SpringJBIContainer;
25 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl;
26 import org.apache.servicemix.tck.MessageList;
27 import org.apache.servicemix.tck.Receiver;
28 import org.apache.servicemix.tck.Sender;
29 import org.springframework.context.support.AbstractXmlApplicationContext;
30
31 /**
32  * A base class for spring related test cases
33  *
34  * @version $Revision: 426415 $
35  */

36 public abstract class AbstractSpringTestSupport extends TestCase {
37     protected SpringJBIContainer jbi;
38     protected AbstractXmlApplicationContext context;
39     protected int messageCount = 1;
40
41     protected void setUp() throws Exception JavaDoc {
42
43         context = createBeanFactory();
44         //context.setXmlValidating(false);
45

46         // lets force the JBI container to be constructed first
47
jbi = (SpringJBIContainer) context.getBean("jbi");
48         assertNotNull("JBI Container not found in spring!", jbi);
49
50     }
51     
52     protected void tearDown() throws Exception JavaDoc {
53         if (context != null) {
54             context.close();
55         }
56     }
57
58     protected abstract AbstractXmlApplicationContext createBeanFactory();
59
60     public void testSendingAndReceivingMessagesUsingSpring() throws Exception JavaDoc {
61         Sender sender = getSender();
62         assertNotNull(sender);
63         assertNotNull(getReceiver());
64
65         sender.sendMessages(messageCount);
66
67         MessageList messageList = getReceivedMessageList();
68         messageList.assertMessagesReceived(messageCount);
69     }
70
71     protected Sender getSender() {
72         Object JavaDoc cmp = getComponent("sender");
73         if (cmp instanceof ComponentAdaptor) {
74             cmp = ((ComponentAdaptor) cmp).getLifeCycle();
75         }
76         return (Sender) cmp;
77     }
78
79     protected Receiver getReceiver() {
80         Object JavaDoc cmp = getComponent("receiver");
81         if (cmp instanceof ComponentAdaptor) {
82             cmp = ((ComponentAdaptor) cmp).getLifeCycle();
83         }
84         return (Receiver) cmp;
85     }
86
87     protected MessageList getReceivedMessageList() {
88         return getReceiver().getMessageList();
89     }
90     
91     protected Component getComponent(String JavaDoc name) {
92         ComponentMBeanImpl lcc = jbi.getComponent(name);
93         return lcc != null ? lcc.getComponent() : null;
94     }
95
96     protected Object JavaDoc getBean(String JavaDoc name) {
97         Object JavaDoc value = jbi.getBean(name);
98         if (value == null) {
99             value = context.getBean(name);
100         }
101         assertNotNull(name + " not found in JBI container!", value);
102         return value;
103     }
104 }
105
Popular Tags