001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.gps.device.ibatis;
018:
019: import java.io.Reader;
020: import java.sql.Connection;
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024:
025: import com.ibatis.common.resources.Resources;
026: import com.ibatis.sqlmap.client.SqlMapClient;
027: import com.ibatis.sqlmap.client.SqlMapClientBuilder;
028: import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
029: import com.ibatis.sqlmap.engine.transaction.TransactionManager;
030: import com.ibatis.sqlmap.engine.transaction.external.ExternalTransactionConfig;
031: import junit.framework.TestCase;
032: import org.compass.core.Compass;
033: import org.compass.core.CompassHits;
034: import org.compass.core.CompassSession;
035: import org.compass.core.CompassTransaction;
036: import org.compass.core.config.CompassConfiguration;
037: import org.compass.gps.device.jdbc.datasource.SingleConnectionDataSource;
038: import org.compass.gps.impl.SingleCompassGps;
039:
040: /**
041: *
042: * @author kimchy
043: *
044: */
045: public class SqlMapClientTests extends TestCase {
046:
047: private static final String DB_SETUP = ""
048: + "CREATE TABLE contact (contactid INTEGER NOT NULL IDENTITY PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30));";
049:
050: private static final String[] DB_DATA = {
051: "INSERT INTO contact VALUES (1, 'first 1', 'last 1');",
052: "INSERT INTO contact VALUES (2, 'first 2', 'last 2');",
053: "INSERT INTO contact VALUES (3, 'first 3', 'last 3');",
054: "INSERT INTO contact VALUES (4, 'first 4', 'last 4');" };
055:
056: private static final String DB_TEARDOWN = "DROP TABLE contact;";
057:
058: protected SingleConnectionDataSource dataSource;
059:
060: private SqlMapClient sqlMapClient;
061:
062: private SingleCompassGps compassGps;
063:
064: private Compass compass;
065:
066: protected void setUp() throws Exception {
067: dataSource = new SingleConnectionDataSource(
068: "org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:test", "sa",
069: "", true);
070:
071: setUpDB();
072: setUpDBData();
073:
074: Reader configReader = Resources
075: .getResourceAsReader("org/compass/gps/device/ibatis/SqlMapConfig.xml");
076: sqlMapClient = SqlMapClientBuilder
077: .buildSqlMapClient(configReader);
078:
079: ExternalTransactionConfig transactionConfig = new ExternalTransactionConfig();
080: transactionConfig.setDataSource(dataSource);
081:
082: ExtendedSqlMapClient extendedClient = (ExtendedSqlMapClient) this .sqlMapClient;
083: transactionConfig
084: .setMaximumConcurrentTransactions(extendedClient
085: .getDelegate().getMaxTransactions());
086: extendedClient.getDelegate().setTxManager(
087: new TransactionManager(transactionConfig));
088:
089: CompassConfiguration cpConf = new CompassConfiguration()
090: .configure("/org/compass/gps/device/ibatis/compass.cfg.xml");
091: compass = cpConf.buildCompass();
092: compass.getSearchEngineIndexManager().deleteIndex();
093: compass.getSearchEngineIndexManager().verifyIndex();
094:
095: compassGps = new SingleCompassGps(compass);
096: SqlMapClientGpsDevice gpsDevice = new SqlMapClientGpsDevice(
097: "sqlMap", sqlMapClient, new String[] { "getContacts" });
098: compassGps.addGpsDevice(gpsDevice);
099: compassGps.start();
100: }
101:
102: protected void tearDown() throws Exception {
103: compassGps.stop();
104: compass.close();
105: tearDownDB();
106: dataSource.destroy();
107: }
108:
109: protected void setUpDB() throws SQLException {
110: Connection con = dataSource.getConnection();
111: PreparedStatement ps = con.prepareStatement(DB_SETUP);
112: ps.execute();
113: ps.close();
114: con.close();
115: }
116:
117: protected void tearDownDB() throws SQLException {
118: Connection con = dataSource.getConnection();
119: PreparedStatement ps = con.prepareStatement(DB_TEARDOWN);
120: ps.execute();
121: ps.close();
122: con.close();
123: }
124:
125: protected void setUpDBData() throws SQLException {
126: Connection con = dataSource.getConnection();
127: Statement stmt = con.createStatement();
128: for (int i = 0; i < DB_DATA.length; i++) {
129: stmt.addBatch(DB_DATA[i]);
130: }
131: stmt.executeBatch();
132: stmt.close();
133: con.close();
134: }
135:
136: public void testSqlMapClient() throws Exception {
137: compassGps.index();
138:
139: CompassSession session = compass.openSession();
140: CompassTransaction tr = session.beginTransaction();
141:
142: CompassHits hits = session.find("first");
143: assertEquals(4, hits.length());
144:
145: tr.commit();
146: session.close();
147: }
148: }
|