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