01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.servicemix.components.jdbc;
18:
19: import java.sql.Connection;
20: import java.sql.Statement;
21:
22: import javax.jbi.messaging.InOut;
23: import javax.sql.DataSource;
24: import javax.xml.namespace.QName;
25:
26: import junit.framework.TestCase;
27:
28: import org.apache.commons.logging.Log;
29: import org.apache.commons.logging.LogFactory;
30: import org.apache.servicemix.client.DefaultServiceMixClient;
31: import org.apache.servicemix.jbi.container.JBIContainer;
32: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
33: import org.apache.servicemix.jbi.jaxp.StringSource;
34: import org.hsqldb.jdbc.jdbcDataSource;
35:
36: public class JdbcComponentTest extends TestCase {
37: private static transient Log log = LogFactory
38: .getLog(JdbcComponentTest.class);
39:
40: private JBIContainer jbi;
41: private DataSource dataSource;
42: private JdbcComponent jdbc;
43:
44: protected void setUp() throws Exception {
45: jdbcDataSource ds = new jdbcDataSource();
46: ds.setDatabase("jdbc:hsqldb:mem:aname");
47: ds.setUser("sa");
48: dataSource = ds;
49:
50: jbi = new JBIContainer();
51: jbi.setEmbedded(true);
52: jbi.init();
53: jbi.start();
54:
55: jdbc = new JdbcComponent();
56: jdbc.setService(new QName("urn:jdbc", "service"));
57: jdbc.setEndpoint("endpoint");
58: jdbc.setDataSource(dataSource);
59: jbi.activateComponent(jdbc, "jdbc");
60: }
61:
62: protected void tearDown() throws Exception {
63: jbi.shutDown();
64: }
65:
66: public void testInOut() throws Exception {
67: Connection con = dataSource.getConnection("sa", "");
68: Statement st = con.createStatement();
69: st
70: .execute("create table MyTable (id varchar(80) not null, name varchar(80))");
71: st.execute("insert into MyTable values ('1', 'One')");
72: st.execute("insert into MyTable values ('2', 'Two')");
73:
74: DefaultServiceMixClient client = new DefaultServiceMixClient(
75: jbi);
76: InOut me = client.createInOutExchange();
77: me.setService(new QName("urn:jdbc", "service"));
78: me.getInMessage().setContent(
79: new StringSource("<sql>select * from MyTable</sql>"));
80: client.sendSync(me);
81: String out = new SourceTransformer().contentToString(me
82: .getOutMessage());
83: log.info(out);
84: assertTrue(out.contains("One"));
85: assertTrue(out.contains("Two"));
86: }
87: }
|