1 17 package org.apache.servicemix.store.jdbc; 18 19 import java.sql.Connection ; 20 21 import javax.resource.spi.ConnectionManager ; 22 import javax.resource.spi.ManagedConnectionFactory ; 23 import javax.sql.DataSource ; 24 import javax.sql.XADataSource ; 25 import javax.transaction.TransactionManager ; 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 dataSource; 45 private Connection connection; 46 private StoreFactory factory; 47 private TransactionManager tm; 48 49 protected void setUp() throws Exception { 50 TransactionManagerImpl exTransactionManager = new TransactionManagerImpl(600, new XidFactoryImpl(), null, null); 51 TransactionContextManager transactionContextManager = new TransactionContextManager(exTransactionManager, exTransactionManager); 52 tm = (TransactionManager ) new GeronimoTransactionManager(transactionContextManager); 53 54 ConnectionManager 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 mcf = new DerbyDataSourceMCF("target/testdb"); 64 dataSource = (DataSource ) 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 { 72 if (connection != null) { 73 connection.close(); 74 } 75 } 76 77 public void testStoreAndLoad() throws Exception { 78 Store store = factory.open("store"); 79 String id = store.store(new Integer (10)); 80 Integer i = (Integer ) 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 { 87 Store store = factory.open("store"); 88 tm.begin(); 89 String id = store.store(new Integer (10)); 90 Integer i = (Integer ) 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 { 98 Store store = factory.open("store"); 99 tm.begin(); 100 String id = store.store(new Integer (10)); 101 tm.commit(); 102 tm.begin(); 103 Integer i = (Integer ) 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 { 111 Store store = factory.open("store"); 112 tm.begin(); 113 String id = store.store(new Integer (10)); 114 tm.rollback(); 115 tm.begin(); 116 assertNull(store.load(id)); 117 tm.commit(); 118 } 119 120 public void testStoreRollbackAndLoadNonTx() throws Exception { 121 Store store = factory.open("store"); 122 tm.begin(); 123 String id = store.store(new Integer (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 dbName) { 131 super(createXADS(dbName), new AllExceptionsAreFatalSorter()); 132 } 133 public String getPassword() { 134 return null; 135 } 136 public String getUserName() { 137 return null; 138 } 139 protected static XADataSource createXADS(String dbName) { 140 EmbeddedXADataSource xads = new EmbeddedXADataSource(); 141 xads.setDatabaseName(dbName); 142 xads.setCreateDatabase("create"); 143 return xads; 144 } 145 } 146 147 } 148 | Popular Tags |