KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > TxTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * This class provides several tests for checking transaction attributes processing.
36  */

37 public class TxTest extends DBTestCase {
38     public void test() {
39         final Connection JavaDoc 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 JavaDoc 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     /**
87      * This test case validates newtx handling, i.e. only one extra connection should be opened for
88      * scripts with newtx=true attribute
89      */

90     public void test3() {
91         final Connection JavaDoc con = getConnection("txtest3");
92         ConfigurationEl conf = loadConfiguration("TxTest3.xml");
93         final String JavaDoc failed[] = new String JavaDoc[1];
94         final EtlExecutor se = new TestableEtlExecutor(conf) {
95             @Override JavaDoc
96             public void rollbackAll(final EtlContext ctx) {
97                 failed[0] = "Script should not be rolled back";
98                 super.rollbackAll(ctx);
99             }
100
101             @Override JavaDoc
102             public void closeAll(final EtlContext ctx) {
103                 final Map JavaDoc<String JavaDoc, ConnectionManager> connections = SqlTestHelper.getConnections(ctx.getSession());
104                 final ConnectionManager cf = connections.get("c1");
105                 final List JavaDoc<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