KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.geronimo.transaction.ExtendedTransactionManager;
20 import org.apache.geronimo.transaction.context.TransactionContextManager;
21 import org.apache.servicemix.jbi.RuntimeJBIException;
22 import org.apache.servicemix.jbi.container.ActivationSpec;
23 import org.apache.servicemix.jbi.container.JBIContainer;
24 import org.apache.servicemix.jbi.nmr.flow.Flow;
25 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
26 import org.apache.servicemix.tck.AsyncReceiverPojo;
27 import org.apache.servicemix.tck.Receiver;
28 import org.apache.servicemix.tck.ReceiverComponent;
29 import org.apache.servicemix.tck.SenderComponent;
30 import org.jencks.factory.GeronimoTransactionManagerFactoryBean;
31 import org.jencks.factory.TransactionContextManagerFactoryBean;
32 import org.jencks.factory.TransactionManagerFactoryBean;
33 import org.springframework.transaction.TransactionStatus;
34 import org.springframework.transaction.jta.JtaTransactionManager;
35 import org.springframework.transaction.support.TransactionCallback;
36 import org.springframework.transaction.support.TransactionTemplate;
37
38 import javax.jbi.JBIException;
39 import javax.transaction.TransactionManager JavaDoc;
40 import javax.transaction.UserTransaction JavaDoc;
41
42 import junit.framework.TestCase;
43
44 /**
45  * @version $Revision: 426415 $
46  */

47 public abstract class AbstractTransactionTest extends TestCase {
48
49     protected static final int NUM_MESSAGES = 10;
50     
51     protected TransactionTemplate tt;
52     protected TransactionManager tm;
53     protected TransactionContextManager tcm;
54     protected JBIContainer senderContainer;
55     
56     /*
57      * @see TestCase#setUp()
58      */

59     protected void setUp() throws Exception JavaDoc {
60         super.setUp();
61         createTransactionLayer();
62         senderContainer = createJbiContainer("senderContainer");
63     }
64     
65     protected void tearDown() throws Exception JavaDoc {
66         senderContainer.shutDown();
67     }
68     
69     protected void createTransactionLayer() throws Exception JavaDoc {
70         TransactionManagerFactoryBean tmcf = new TransactionManagerFactoryBean();
71         tmcf.afterPropertiesSet();
72         ExtendedTransactionManager etm = (ExtendedTransactionManager) tmcf.getObject();
73         TransactionContextManagerFactoryBean tcmfb = new TransactionContextManagerFactoryBean();
74         tcmfb.setTransactionManager(etm);
75         tcmfb.afterPropertiesSet();
76         tcm = (TransactionContextManager) tcmfb.getObject();
77         GeronimoTransactionManagerFactoryBean gtmfb = new GeronimoTransactionManagerFactoryBean();
78         gtmfb.setTransactionContextManager(tcm);
79         gtmfb.afterPropertiesSet();
80         tm = (TransactionManager) gtmfb.getObject();
81         tt = new TransactionTemplate(new JtaTransactionManager((UserTransaction JavaDoc) tm));
82     }
83     
84     protected JBIContainer createJbiContainer(String JavaDoc name) throws Exception JavaDoc {
85         JBIContainer container = new JBIContainer();
86         container.setTransactionManager(tm);
87         container.setName(name);
88         container.setFlow(createFlow());
89         container.setAutoEnlistInTransaction(true);
90         container.setEmbedded(true);
91         container.init();
92         container.start();
93         return container;
94     }
95     
96     protected abstract Flow createFlow();
97     
98     protected void runSimpleTest(final boolean syncSend, final boolean syncReceive) throws Exception JavaDoc {
99         final SenderComponent sender = new SenderComponent();
100         sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE));
101         final Receiver receiver;
102         if (syncReceive) {
103             receiver = new ReceiverComponent();
104         } else {
105             receiver = new AsyncReceiverPojo();
106         }
107
108         senderContainer.activateComponent(new ActivationSpec("sender", sender));
109         senderContainer.activateComponent(new ActivationSpec("receiver", receiver));
110         
111         tt.execute(new TransactionCallback() {
112             public Object JavaDoc doInTransaction(TransactionStatus status) {
113                 try {
114                     sender.sendMessages(NUM_MESSAGES, syncSend);
115                 } catch (JBIException e) {
116                     throw new RuntimeJBIException(e);
117                 }
118                 return null;
119             }
120         });
121         receiver.getMessageList().assertMessagesReceived(NUM_MESSAGES);
122     }
123     
124     public void testSyncSendSyncReceive() throws Exception JavaDoc {
125         runSimpleTest(true, true);
126     }
127
128     public void testAsyncSendSyncReceive() throws Exception JavaDoc {
129         runSimpleTest(false, true);
130     }
131
132     public void testSyncSendAsyncReceive() throws Exception JavaDoc {
133         runSimpleTest(true, false);
134     }
135
136     public void testAsyncSendAsyncReceive() throws Exception JavaDoc {
137         runSimpleTest(false, false);
138     }
139
140 }
141
Popular Tags