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.spring.test.device.jdbc;
018:
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.SQLException;
022: import java.sql.Statement;
023: import javax.sql.DataSource;
024:
025: import junit.framework.TestCase;
026: import org.compass.core.Compass;
027: import org.compass.core.CompassDetachedHits;
028: import org.compass.core.CompassTemplate;
029: import org.compass.core.Resource;
030: import org.compass.gps.ActiveMirrorGpsDevice;
031: import org.compass.gps.CompassGps;
032: import org.compass.gps.device.jdbc.JdbcUtils;
033: import org.springframework.beans.factory.DisposableBean;
034: import org.springframework.context.ApplicationContext;
035: import org.springframework.context.support.ClassPathXmlApplicationContext;
036:
037: public class SpringJdbcGpsDeviceTests extends TestCase {
038:
039: private static final String DB_SETUP = ""
040: + "CREATE TABLE parent (id INTEGER NOT NULL IDENTITY PRIMARY KEY, first_name VARCHAR(30), last_name VARCHAR(30), version BIGINT NOT NULL );"
041: + "CREATE TABLE child (id INTEGER NOT NULL IDENTITY PRIMARY KEY, parent_id INTEGER NOT NULL, first_name VARCHAR(30), last_name VARCHAR(30), version BIGINT NOT NULL );"
042: + "alter table child add constraint fk_child_parent foreign key (parent_id) references parent(id);";
043:
044: private static final String[] DB_DATA = {
045: "INSERT INTO parent VALUES (1, 'parent first 1', 'last 1', 1);",
046: "INSERT INTO parent VALUES (2, 'parent first 2', 'last 2', 1);",
047: "INSERT INTO parent VALUES (3, 'parent first 3', 'last 3', 1);",
048: "INSERT INTO parent VALUES (4, 'parent first 4', 'last 4', 1);",
049: "INSERT INTO child VALUES (1, 1, 'child first 1 1', 'last 1 1', 1);",
050: "INSERT INTO child VALUES (2, 1, 'child first 1 2', 'last 1 2', 1);",
051: "INSERT INTO child VALUES (3, 1, 'child first 1 3', 'last 1 3', 1);",
052: "INSERT INTO child VALUES (4, 2, 'child first 2 1', 'last 2 1', 1);",
053: "INSERT INTO child VALUES (5, 3, 'child first 3 1', 'last 3 1', 1);",
054: "INSERT INTO child VALUES (6, 4, 'child first 3 2', 'last 3 2', 1);" };
055:
056: private static final String DB_TEARDOWN = "DROP TABLE child; DROP TABLE parent;";
057:
058: protected DataSource dataSource;
059:
060: private ApplicationContext dataSourceApplicationContext;
061:
062: protected void setUp() throws Exception {
063: dataSourceApplicationContext = new ClassPathXmlApplicationContext(
064: "org/compass/spring/test/device/jdbc/datasource-applicationContext.xml");
065: dataSource = (DataSource) dataSourceApplicationContext
066: .getBean("dataSource");
067: setUpDB();
068: setUpDBData();
069: }
070:
071: protected void tearDown() throws Exception {
072: tearDownDB();
073: ((DisposableBean) dataSourceApplicationContext).destroy();
074: }
075:
076: protected void setUpDB() throws SQLException {
077: try {
078: tearDownDB();
079: } catch (SQLException e) {
080: // do nothing
081: }
082: Connection con = dataSource.getConnection();
083: PreparedStatement ps = con.prepareStatement(DB_SETUP);
084: ps.execute();
085: ps.close();
086: con.close();
087: }
088:
089: protected void tearDownDB() throws SQLException {
090: Connection con = dataSource.getConnection();
091: PreparedStatement ps = con.prepareStatement(DB_TEARDOWN);
092: try {
093: ps.execute();
094: ps.close();
095: } finally {
096: con.close();
097: }
098: }
099:
100: protected void setUpDBData() throws SQLException {
101: Connection con = dataSource.getConnection();
102: Statement stmt = con.createStatement();
103: for (int i = 0; i < DB_DATA.length; i++) {
104: stmt.addBatch(DB_DATA[i]);
105: }
106: stmt.executeBatch();
107: stmt.close();
108: con.close();
109: }
110:
111: public void testResultSetJdbcDevice() throws Exception {
112: ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
113: new String[] { "org/compass/spring/test/device/jdbc/resultset-applicationContext.xml" },
114: dataSourceApplicationContext);
115:
116: CompassGps gps = (CompassGps) applicationContext.getBean("gps");
117: Compass compass = (Compass) applicationContext
118: .getBean("compass");
119: CompassTemplate compassTemplate = new CompassTemplate(compass);
120: ActiveMirrorGpsDevice gpsDevice = (ActiveMirrorGpsDevice) applicationContext
121: .getBean("jdbcGpsDevice");
122:
123: gps.index();
124: compassTemplate.loadResource("result-set", "1", "1");
125: Resource r = compassTemplate
126: .getResource("result-set", "4", "6");
127: assertNotNull(r);
128: CompassDetachedHits hits = compassTemplate
129: .findWithDetach("parent");
130: assertEquals(6, hits.getLength());
131:
132: // test that create works
133: Connection con = JdbcUtils.getConnection(dataSource);
134: PreparedStatement ps = con
135: .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
136: ps.execute();
137: ps.close();
138: con.commit();
139: con.close();
140:
141: r = compassTemplate.getResource("result-set", "999", "0");
142: assertNull(r);
143: gpsDevice.performMirroring();
144: compassTemplate.loadResource("result-set", "999", "0");
145:
146: // test that update works
147: con = JdbcUtils.getConnection(dataSource);
148: ps = con
149: .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
150: ps.execute();
151: ps.close();
152: con.commit();
153: con.close();
154:
155: gpsDevice.performMirroring();
156: r = compassTemplate.loadResource("result-set", "1", "1");
157: assertEquals("new first name", r.getValue("parent_first_name"));
158:
159: // test that delete works
160: con = JdbcUtils.getConnection(dataSource);
161: ps = con.prepareStatement("delete from parent where id = 999");
162: ps.execute();
163: ps.close();
164: con.commit();
165: con.close();
166:
167: gpsDevice.performMirroring();
168: r = compassTemplate.getResource("result-set", "999", "0");
169: assertNull(r);
170:
171: applicationContext.close();
172: }
173:
174: public void testTableJdbcDevice() throws Exception {
175: ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
176: new String[] { "org/compass/spring/test/device/jdbc/table-applicationContext.xml" },
177: dataSourceApplicationContext);
178:
179: CompassGps gps = (CompassGps) applicationContext.getBean("gps");
180: Compass compass = (Compass) applicationContext
181: .getBean("compass");
182: CompassTemplate compassTemplate = new CompassTemplate(compass);
183: ActiveMirrorGpsDevice gpsDevice = (ActiveMirrorGpsDevice) applicationContext
184: .getBean("jdbcGpsDevice");
185:
186: gps.index();
187: Resource r = compassTemplate.getResource("parent", "1");
188: assertNotNull(r);
189: assertNotNull(r.getProperty("ID"));
190: assertNotNull(r.getProperty("FIRST_NAME"));
191: assertNotNull(r.getProperty("LAST_NAME"));
192: CompassDetachedHits hits = compassTemplate
193: .findWithDetach("parent");
194: assertEquals(4, hits.getLength());
195: hits = compassTemplate.findWithDetach("child");
196: assertEquals(6, hits.getLength());
197:
198: // test that create works
199: Connection con = JdbcUtils.getConnection(dataSource);
200: PreparedStatement ps = con
201: .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
202: ps.execute();
203: ps.close();
204: con.commit();
205: con.close();
206:
207: r = compassTemplate.getResource("parent", "999");
208: assertNull(r);
209: gpsDevice.performMirroring();
210: compassTemplate.loadResource("parent", "999");
211:
212: gps.stop();
213: gps.start();
214: // test that update works
215: con = JdbcUtils.getConnection(dataSource);
216: ps = con
217: .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
218: ps.execute();
219: ps.close();
220: con.commit();
221: con.close();
222:
223: gpsDevice.performMirroring();
224: r = compassTemplate.loadResource("parent", "1");
225: assertEquals("new first name", r.getValue("FIRST_NAME"));
226:
227: // test that delete works
228: con = JdbcUtils.getConnection(dataSource);
229: ps = con.prepareStatement("delete from parent where id = 999");
230: ps.execute();
231: ps.close();
232: con.commit();
233: con.close();
234:
235: gpsDevice.performMirroring();
236: r = compassTemplate.getResource("parent", "999");
237: assertNull(r);
238:
239: applicationContext.close();
240: }
241: }
|