1 16 package scriptella; 17 18 import scriptella.configuration.ConfigurationEl; 19 import scriptella.core.ConnectionManager; 20 import scriptella.core.SqlTestHelper; 21 import scriptella.execution.EtlContext; 22 import scriptella.execution.EtlExecutor; 23 import scriptella.execution.EtlExecutorException; 24 import scriptella.execution.TestableEtlExecutor; 25 import scriptella.jdbc.QueryHelper; 26 import scriptella.spi.ParametersCallback; 27 import scriptella.spi.QueryCallback; 28 29 import java.sql.Connection ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 34 37 public class TxTest extends DBTestCase { 38 public void test() { 39 final Connection con = getConnection("txtest"); 40 final EtlExecutor se = newEtlExecutor("TxTest.xml"); 41 42 try { 43 se.execute(); 44 } catch (EtlExecutorException e) { 45 e.printStackTrace(); 46 fail("Scripts invoked in new tx must not fail the executor"); 47 } 48 49 QueryHelper s = new QueryHelper("select * from test"); 50 final int n[] = new int[]{0}; 51 52 s.execute(con, 53 new QueryCallback() { 54 public void processRow(final ParametersCallback evaluator) { 55 n[0]++; 56 assertEquals(n[0], evaluator.getParameter("ID")); 57 } 58 }); 59 assertEquals(n[0], 3); 60 } 61 62 public void test2() { 63 final java.sql.Connection con = getConnection("txtest2"); 64 final EtlExecutor se = newEtlExecutor("TxTest2.xml"); 65 66 try { 67 se.execute(); 68 } catch (EtlExecutorException e) { 69 e.printStackTrace(); 70 fail("Scripts invoked in new tx must not fail the executor"); 71 } 72 73 QueryHelper s = new QueryHelper("select * from test"); 74 final int n[] = new int[]{0}; 75 76 s.execute(con, 77 new QueryCallback() { 78 public void processRow(final ParametersCallback row) { 79 n[0]++; 80 assertEquals(n[0], row.getParameter("ID")); 81 } 82 }); 83 assertEquals(1, n[0]); 84 } 85 86 90 public void test3() { 91 final Connection con = getConnection("txtest3"); 92 ConfigurationEl conf = loadConfiguration("TxTest3.xml"); 93 final String failed[] = new String [1]; 94 final EtlExecutor se = new TestableEtlExecutor(conf) { 95 @Override 96 public void rollbackAll(final EtlContext ctx) { 97 failed[0] = "Script should not be rolled back"; 98 super.rollbackAll(ctx); 99 } 100 101 @Override 102 public void closeAll(final EtlContext ctx) { 103 final Map <String , ConnectionManager> connections = SqlTestHelper.getConnections(ctx.getSession()); 104 final ConnectionManager cf = connections.get("c1"); 105 final List <scriptella.spi.Connection> newConnections = SqlTestHelper.getNewConnections(cf); 106 107 if ((newConnections == null) || 108 (newConnections.size() != 1)) { 109 failed[0] = "Only one connection should be created for newtx script"; 110 } 111 112 if (SqlTestHelper.getConnection(cf) == null) { 113 failed[0] = "Connection should be initialized"; 114 } 115 116 super.closeAll(ctx); 117 } 118 }; 119 120 try { 121 se.execute(); 122 } catch (EtlExecutorException e) { 123 e.printStackTrace(); 124 fail("Scripts invoked in new tx must not fail the executor: " + 125 e.getMessage()); 126 } 127 128 if (failed[0] != null) { 129 fail(failed[0]); 130 } 131 132 QueryHelper s = new QueryHelper("select * from test2"); 133 134 s.execute(con, 135 new QueryCallback() { 136 public void processRow(final ParametersCallback row) { 137 fail("Table Test2 should have no rows"); 138 } 139 }); 140 } 141 } 142 | Popular Tags |