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