KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > store > jdbc > JdbcStoreTransactionalTest


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.store.jdbc;
18
19 import java.sql.Connection JavaDoc;
20
21 import javax.resource.spi.ConnectionManager JavaDoc;
22 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
23 import javax.sql.DataSource JavaDoc;
24 import javax.sql.XADataSource JavaDoc;
25 import javax.transaction.TransactionManager JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.apache.derby.jdbc.EmbeddedXADataSource;
30 import org.apache.geronimo.connector.outbound.GenericConnectionManager;
31 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.NoPool;
32 import org.apache.geronimo.connector.outbound.connectionmanagerconfig.XATransactions;
33 import org.apache.geronimo.transaction.context.GeronimoTransactionManager;
34 import org.apache.geronimo.transaction.context.TransactionContextManager;
35 import org.apache.geronimo.transaction.manager.TransactionManagerImpl;
36 import org.apache.geronimo.transaction.manager.XidFactoryImpl;
37 import org.apache.servicemix.store.Store;
38 import org.apache.servicemix.store.StoreFactory;
39 import org.tranql.connector.AllExceptionsAreFatalSorter;
40 import org.tranql.connector.jdbc.AbstractXADataSourceMCF;
41
42 public class JdbcStoreTransactionalTest extends TestCase {
43
44     private DataSource JavaDoc dataSource;
45     private Connection JavaDoc connection;
46     private StoreFactory factory;
47     private TransactionManager JavaDoc tm;
48
49     protected void setUp() throws Exception JavaDoc {
50         TransactionManagerImpl exTransactionManager = new TransactionManagerImpl(600, new XidFactoryImpl(), null, null);
51         TransactionContextManager transactionContextManager = new TransactionContextManager(exTransactionManager, exTransactionManager);
52         tm = (TransactionManager JavaDoc) new GeronimoTransactionManager(transactionContextManager);
53         
54         // Create an embedded database for testing tx results when commit / rollback
55
ConnectionManager JavaDoc cm = new GenericConnectionManager(
56                         new XATransactions(true, true),
57                         new NoPool(),
58                         false,
59                         null,
60                         transactionContextManager,
61                         "connectionManager",
62                         GenericConnectionManager.class.getClassLoader());
63         ManagedConnectionFactory JavaDoc mcf = new DerbyDataSourceMCF("target/testdb");
64         dataSource = (DataSource JavaDoc) mcf.createConnectionFactory(cm);
65         JdbcStoreFactory f = new JdbcStoreFactory();
66         f.setTransactional(true);
67         f.setDataSource(dataSource);
68         factory = f;
69     }
70     
71     protected void tearDown() throws Exception JavaDoc {
72         if (connection != null) {
73             connection.close();
74         }
75     }
76     
77     public void testStoreAndLoad() throws Exception JavaDoc {
78         Store store = factory.open("store");
79         String JavaDoc id = store.store(new Integer JavaDoc(10));
80         Integer JavaDoc i = (Integer JavaDoc) store.load(id);
81         assertEquals(10, i.intValue());
82         assertNull(store.load(id));
83         assertNull(store.load("a"));
84     }
85
86     public void testStoreAndLoadInOneTx() throws Exception JavaDoc {
87         Store store = factory.open("store");
88         tm.begin();
89         String JavaDoc id = store.store(new Integer JavaDoc(10));
90         Integer JavaDoc i = (Integer JavaDoc) store.load(id);
91         assertEquals(10, i.intValue());
92         assertNull(store.load(id));
93         assertNull(store.load("a"));
94         tm.commit();
95     }
96
97     public void testStoreAndLoadInTwoTx() throws Exception JavaDoc {
98         Store store = factory.open("store");
99         tm.begin();
100         String JavaDoc id = store.store(new Integer JavaDoc(10));
101         tm.commit();
102         tm.begin();
103         Integer JavaDoc i = (Integer JavaDoc) store.load(id);
104         assertEquals(10, i.intValue());
105         assertNull(store.load(id));
106         tm.commit();
107         assertNull(store.load("a"));
108     }
109
110     public void testStoreRollbackAndLoad() throws Exception JavaDoc {
111         Store store = factory.open("store");
112         tm.begin();
113         String JavaDoc id = store.store(new Integer JavaDoc(10));
114         tm.rollback();
115         tm.begin();
116         assertNull(store.load(id));
117         tm.commit();
118     }
119
120     public void testStoreRollbackAndLoadNonTx() throws Exception JavaDoc {
121         Store store = factory.open("store");
122         tm.begin();
123         String JavaDoc id = store.store(new Integer JavaDoc(10));
124         tm.rollback();
125         assertNull(store.load(id));
126     }
127
128     public static class DerbyDataSourceMCF extends AbstractXADataSourceMCF {
129         private static final long serialVersionUID = 7971682207810098396L;
130         protected DerbyDataSourceMCF(String JavaDoc dbName) {
131             super(createXADS(dbName), new AllExceptionsAreFatalSorter());
132         }
133         public String JavaDoc getPassword() {
134             return null;
135         }
136         public String JavaDoc getUserName() {
137             return null;
138         }
139         protected static XADataSource JavaDoc createXADS(String JavaDoc dbName) {
140             EmbeddedXADataSource xads = new EmbeddedXADataSource();
141             xads.setDatabaseName(dbName);
142             xads.setCreateDatabase("create");
143             return xads;
144         }
145     }
146     
147 }
148
Popular Tags