1
2
3
4
5
6
7
8
9
10
11 package org.mule.test.integration.transport.jdbc;
12
13 import org.mule.api.MuleEventContext;
14 import org.mule.api.model.Model;
15 import org.mule.api.transport.Connector;
16 import org.mule.config.PoolingProfile;
17 import org.mule.model.seda.SedaModel;
18 import org.mule.tck.AbstractMuleTestCase;
19 import org.mule.tck.functional.FunctionalTestComponent;
20 import org.mule.tck.util.MuleDerbyTestUtils;
21 import org.mule.transport.jdbc.JdbcConnector;
22 import org.mule.transport.jdbc.JdbcUtils;
23
24 import java.sql.Connection;
25 import java.util.HashMap;
26 import java.util.Map;
27
28 import javax.sql.DataSource;
29 import javax.sql.XADataSource;
30
31 import org.apache.commons.dbutils.QueryRunner;
32 import org.apache.commons.dbutils.handlers.ArrayHandler;
33 import org.enhydra.jdbc.standard.StandardDataSource;
34
35 public abstract class AbstractJdbcFunctionalTestCase extends AbstractMuleTestCase
36 {
37
38 public static final String DEFAULT_IN_URI = "jdbc://getTest?type=1";
39 public static final String DEFAULT_OUT_URI = "jdbc://writeTest?type=2";
40 public static final String CONNECTOR_NAME = "testConnector";
41 public static final String DEFAULT_MESSAGE = "Test Message";
42
43 public static final String SQL_READ = "SELECT ID, TYPE, DATA, ACK, RESULT FROM TEST WHERE TYPE = #[type] AND ACK IS NULL";
44 public static final String SQL_ACK = "UPDATE TEST SET ACK = #[NOW] WHERE ID = #[id] AND TYPE = #[type] AND DATA = #[data]";
45 public static final String SQL_WRITE = "INSERT INTO TEST(TYPE, DATA, ACK, RESULT) VALUES(#[type], #[payload], NULL, NULL)";
46
47 public static String EMBEDDED_CONNECTION_STRING;
48 public static final String EMBEDDED_DRIVER_NAME = "org.apache.derby.jdbc.EmbeddedDriver";
49
50 public static String CLIENT_CONNECTION_STRING ;
51 public static final String CLIENT_DRIVER_NAME = "org.apache.derby.jdbc.ClientDriver";
52
53 protected Connector connector;
54 protected Model model;
55 protected DataSource dataSource;
56
57 private static boolean derbySetupDone = false;
58
59 protected void suitePreSetUp() throws Exception
60 {
61 if (!derbySetupDone)
62 {
63 String dbName = MuleDerbyTestUtils.loadDatabaseName("derby.properties", "database.name");
64
65 MuleDerbyTestUtils.defaultDerbyCleanAndInit("derby.properties", "database.name");
66 EMBEDDED_CONNECTION_STRING = "jdbc:derby:" + dbName;
67 CLIENT_CONNECTION_STRING = "jdbc:derby://localhost:1527/"+ dbName +";create=true";
68 derbySetupDone = true;
69 }
70 super.suitePreSetUp();
71 }
72
73 protected void doSetUp() throws Exception
74 {
75 SedaModel model = new SedaModel();
76 model.setName("main");
77 model.getPoolingProfile().setInitialisationPolicy(
78 PoolingProfile.INITIALISE_ONE);
79 muleContext.getRegistry().registerModel(model);
80
81 connector = createConnector();
82 muleContext.getRegistry().registerConnector(connector);
83
84 emptyTable();
85 }
86
87 protected void emptyTable() throws Exception
88 {
89 try
90 {
91 execSqlUpdate("DELETE FROM TEST");
92 }
93 catch (Exception e)
94 {
95 execSqlUpdate("CREATE TABLE TEST(ID INTEGER GENERATED BY DEFAULT AS IDENTITY(START WITH 0) NOT NULL PRIMARY KEY,TYPE INTEGER,DATA VARCHAR(255),ACK TIMESTAMP,RESULT VARCHAR(255))");
96 }
97 }
98
99 protected int execSqlUpdate(String sql) throws Exception
100 {
101 Connection con = null;
102 try
103 {
104 con = getConnection();
105 return new QueryRunner().update(con, sql);
106 }
107 finally
108 {
109 JdbcUtils.close(con);
110 }
111 }
112
113 protected Object[] execSqlQuery(String sql) throws Exception
114 {
115 Connection con = null;
116 try
117 {
118 con = getConnection();
119 return (Object[])new QueryRunner().query(con, sql, new ArrayHandler());
120 }
121 finally
122 {
123 JdbcUtils.close(con);
124 }
125 }
126
127 public static class JdbcFunctionalTestComponent extends FunctionalTestComponent
128 {
129 public Object onCall(MuleEventContext context) throws Exception
130 {
131 if (getEventCallback() != null)
132 {
133 getEventCallback().eventReceived(context, this);
134 }
135 Map map = (Map)context.getMessage().getPayload();
136 return map.get("data") + " Received";
137 }
138 }
139
140 public Connection getConnection() throws Exception
141 {
142 Object dataSource = getDataSource();
143 if (dataSource instanceof DataSource)
144 {
145 return ((DataSource)dataSource).getConnection();
146 }
147 else
148 {
149 return ((XADataSource)dataSource).getXAConnection().getConnection();
150 }
151 }
152
153 public DataSource getDataSource() throws Exception
154 {
155 if (dataSource == null)
156 {
157 dataSource = createDataSource();
158 }
159 return dataSource;
160 }
161
162 public Connector createConnector() throws Exception
163 {
164 JdbcConnector connector = new JdbcConnector(muleContext);
165 connector.setDataSource(getDataSource());
166 connector.setName(CONNECTOR_NAME);
167 connector.getDispatcherThreadingProfile().setDoThreading(false);
168 connector.setPollingFrequency(5000);
169
170 Map queries = new HashMap();
171 queries.put("getTest", SQL_READ);
172 queries.put("getTest.ack", SQL_ACK);
173 queries.put("writeTest", SQL_WRITE);
174 connector.setQueries(queries);
175
176 return connector;
177 }
178
179 protected String getInDest()
180 {
181 return DEFAULT_IN_URI;
182 }
183
184 protected String getOutDest()
185 {
186 return DEFAULT_OUT_URI;
187 }
188
189
190 protected DataSource createDataSource() throws Exception
191 {
192 return createEmbeddedDataSource();
193 }
194
195 protected DataSource createEmbeddedDataSource() throws Exception
196 {
197 StandardDataSource ds = new StandardDataSource();
198 ds.setDriverName(EMBEDDED_DRIVER_NAME);
199 ds.setUrl(EMBEDDED_CONNECTION_STRING);
200 return ds;
201 }
202
203 protected DataSource createClientDataSource() throws Exception
204 {
205 StandardDataSource ds = new StandardDataSource();
206 ds.setDriverName(CLIENT_DRIVER_NAME);
207 ds.setUrl(CLIENT_CONNECTION_STRING);
208 return ds;
209 }
210
211 }