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.jdbc;
018:
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021:
022: import org.compass.core.Compass;
023: import org.compass.core.CompassDetachedHits;
024: import org.compass.core.CompassTemplate;
025: import org.compass.core.Resource;
026: import org.compass.core.config.CompassConfiguration;
027: import org.compass.core.config.CompassEnvironment;
028: import org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping;
029: import org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping;
030: import org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping;
031: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
032: import org.compass.gps.impl.SingleCompassGps;
033:
034: /**
035: * @author kimchy
036: */
037: public class ResultSetJdbcGpsDeviceTests extends
038: AbstractJdbcGpsDeviceTests {
039:
040: protected Compass compass;
041:
042: protected CompassTemplate compassTemplate;
043:
044: private ResultSetJdbcGpsDevice gpsDevice;
045:
046: private SingleCompassGps gps;
047:
048: protected void tearDown() throws Exception {
049: if (gps != null) {
050: gps.stop();
051: }
052: if (compass != null) {
053: compass.close();
054: }
055: super .tearDown();
056: }
057:
058: protected void setUpExactMappingNoMirror() throws Exception {
059: // set up the database mappings, since they are used both to generate
060: // the resource mappings and configure the jdbc gps device
061: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
062: mapping.setAlias("result-set");
063: mapping
064: .setSelectQuery("select "
065: + "p.id as parent_id, p.first_name as parent_first_name, p.last_name as parent_last_name, "
066: + "c.id as child_id, c.first_name as child_first_name, c.last_name child_last_name "
067: + "from parent p left join child c on p.id = c.parent_id");
068: mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
069: "parent_id"));
070: mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
071: "child_id"));
072: mapping.addDataMapping(new DataColumnToPropertyMapping(
073: "parent_first_name", "parent_first_name"));
074: mapping.addDataMapping(new DataColumnToPropertyMapping(
075: "parent_first_name", "first_name"));
076: mapping.addDataMapping(new DataColumnToPropertyMapping(
077: "child_first_name", "child_first_name"));
078: mapping.addDataMapping(new DataColumnToPropertyMapping(
079: "child_first_name", "first_name"));
080:
081: CompassConfiguration conf = new CompassConfiguration()
082: .setSetting(CompassEnvironment.CONNECTION,
083: "target/test-index");
084: conf.addMappingResover(new ResultSetResourceMappingResolver(
085: mapping, dataSource));
086: compass = conf.buildCompass();
087: compass.getSearchEngineIndexManager().deleteIndex();
088: compass.getSearchEngineIndexManager().verifyIndex();
089:
090: compassTemplate = new CompassTemplate(compass);
091:
092: gpsDevice = new ResultSetJdbcGpsDevice();
093: gpsDevice.setDataSource(dataSource);
094: gpsDevice.setName("resultSetJdbcDevice");
095: // setting up no mirroring, even though it should not mirror since we
096: // mapped no version columns
097: gpsDevice.setMirrorDataChanges(false);
098: gpsDevice.addMapping(mapping);
099:
100: gps = new SingleCompassGps(compass);
101: gps.addGpsDevice(gpsDevice);
102: gps.start();
103: }
104:
105: public void testExactMappingNoMirror() throws Exception {
106: setUpExactMappingNoMirror();
107: gps.index();
108: Resource r = compassTemplate
109: .getResource("result-set", "1", "1");
110: assertNotNull(r.getProperty("parent_id"));
111: assertNotNull(r.getProperty("parent_first_name"));
112: assertNotNull(r.getProperty("child_id"));
113: assertNotNull(r.getProperty("child_first_name"));
114: assertNotNull(r.getProperty("first_name"));
115: assertNull(r.getProperty("ID"));
116: assertNull(r.getProperty("FIRST_NAME"));
117: assertNull(r.getProperty("LAST_NAME"));
118: assertNotNull(r);
119: r = compassTemplate.getResource("result-set", "4", "6");
120: assertNotNull(r);
121: CompassDetachedHits hits = compassTemplate
122: .findWithDetach("parent");
123: assertEquals(6, hits.getLength());
124: }
125:
126: protected void setUpUnmappedMappingNoMirror() throws Exception {
127: // set up the database mappings, since they are used both to generate
128: // the resource mappings and configure the jdbc gps device
129: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping(
130: "result-set",
131: "select * from parent p left join child c on p.id = c.parent_id");
132: mapping.setIndexUnMappedColumns(true);
133: mapping.addIdMapping(new IdColumnToPropertyMapping(1,
134: "parent_id"));
135: mapping.addIdMapping(new IdColumnToPropertyMapping(5,
136: "child_id"));
137: mapping.addDataMapping(new DataColumnToPropertyMapping(2,
138: "parent_first_name"));
139: mapping.addDataMapping(new DataColumnToPropertyMapping(7,
140: "child_first_name"));
141:
142: CompassConfiguration conf = new CompassConfiguration()
143: .setSetting(CompassEnvironment.CONNECTION,
144: "target/test-index");
145: conf.addMappingResover(new ResultSetResourceMappingResolver(
146: mapping, this .dataSource));
147: compass = conf.buildCompass();
148: compass.getSearchEngineIndexManager().deleteIndex();
149: compass.getSearchEngineIndexManager().verifyIndex();
150:
151: compassTemplate = new CompassTemplate(compass);
152:
153: gpsDevice = new ResultSetJdbcGpsDevice();
154: gpsDevice.setDataSource(dataSource);
155: gpsDevice.setName("resultSetJdbcDevice");
156: // it should not mirror the data since we did not mapped any version
157: // columns
158: gpsDevice.setMirrorDataChanges(true);
159: gpsDevice.addMapping(mapping);
160:
161: gps = new SingleCompassGps(compass);
162: gps.addGpsDevice(gpsDevice);
163: gps.start();
164: }
165:
166: public void testUnmappedMappingNoMirror() throws Exception {
167: setUpUnmappedMappingNoMirror();
168: gps.index();
169: Resource r = compassTemplate
170: .getResource("result-set", "1", "1");
171: assertNotNull(r);
172: assertNotNull(r.getProperty("parent_id"));
173: assertNotNull(r.getProperty("parent_first_name"));
174: assertNotNull(r.getProperty("child_id"));
175: assertNotNull(r.getProperty("child_first_name"));
176: assertNotNull(r.getProperty("LAST_NAME"));
177: assertNull(r.getProperty("FIRST_NAME"));
178: r = compassTemplate.getResource("result-set", "4", "6");
179: assertNotNull(r);
180: CompassDetachedHits hits = compassTemplate
181: .findWithDetach("parent");
182: assertEquals(6, hits.getLength());
183: }
184:
185: protected void setUpExactMappingWithMirror() throws Exception {
186: // set up the database mappings, since they are used both to generate
187: // the resource mappings and configure the jdbc gps device
188: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
189: mapping.setAlias("result-set");
190: mapping
191: .setSelectQuery("select "
192: + "p.id as parent_id, p.first_name as parent_first_name, p.last_name as parent_last_name, p.version as parent_version, "
193: + "COALESCE(c.id, 0) as child_id, c.first_name as child_first_name, c.last_name child_last_name, COALESCE(c.version, 0) as child_version "
194: + "from parent p left join child c on p.id = c.parent_id");
195: mapping
196: .setVersionQuery("select p.id as parent_id, COALESCE(c.id, 0) as child_id, p.version as parent_version, COALESCE(c.version, 0) as child_version from parent p left join child c on p.id = c.parent_id");
197: mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
198: "parent_id", "p.id"));
199: mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
200: "child_id", "COALESCE(c.id, 0)"));
201: mapping.addDataMapping(new DataColumnToPropertyMapping(
202: "parent_first_name", "parent_first_name"));
203: mapping.addDataMapping(new DataColumnToPropertyMapping(
204: "child_first_name", "child_first_name"));
205: mapping.addVersionMapping(new VersionColumnMapping(
206: "parent_version"));
207: mapping.addVersionMapping(new VersionColumnMapping(
208: "child_version"));
209:
210: CompassConfiguration conf = new CompassConfiguration()
211: .setSetting(CompassEnvironment.CONNECTION,
212: "target/testindex");
213: conf.addMappingResover(new ResultSetResourceMappingResolver(
214: mapping, this .dataSource));
215: compass = conf.buildCompass();
216: compass.getSearchEngineIndexManager().deleteIndex();
217: compass.getSearchEngineIndexManager().verifyIndex();
218:
219: compassTemplate = new CompassTemplate(compass);
220:
221: gpsDevice = new ResultSetJdbcGpsDevice();
222: gpsDevice.setDataSource(dataSource);
223: gpsDevice.setName("resultSetJdbcDevice");
224: gpsDevice.setMirrorDataChanges(true);
225: gpsDevice.addMapping(mapping);
226:
227: gps = new SingleCompassGps(compass);
228: gps.addGpsDevice(gpsDevice);
229: gps.start();
230: }
231:
232: public void testExactMappingWithMirrorMockEvent() throws Exception {
233: setUpExactMappingWithMirror();
234: MockSnapshotEventListener eventListener = new MockSnapshotEventListener();
235: gpsDevice.setSnapshotEventListener(eventListener);
236: gps.index();
237: compassTemplate.loadResource("result-set", "1", "1");
238: Resource r = compassTemplate
239: .getResource("result-set", "4", "6");
240: assertNotNull(r);
241: CompassDetachedHits hits = compassTemplate
242: .findWithDetach("parent");
243: assertEquals(6, hits.getLength());
244:
245: eventListener.clear();
246: gpsDevice.performMirroring();
247: assertFalse(eventListener.isCreateAndUpdateCalled());
248: assertFalse(eventListener.isDeleteCalled());
249:
250: // test that create works
251: Connection con = JdbcUtils.getConnection(dataSource);
252: PreparedStatement ps = con
253: .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
254: ps.execute();
255: ps.close();
256: con.commit();
257: con.close();
258:
259: eventListener.clear();
260: gpsDevice.performMirroring();
261: assertTrue(eventListener.isCreateAndUpdateCalled());
262: assertTrue(eventListener.isCreateHappen());
263: assertFalse(eventListener.isUpdateHappen());
264: assertFalse(eventListener.isDeleteCalled());
265:
266: // test that update parent works
267: con = JdbcUtils.getConnection(dataSource);
268: ps = con
269: .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
270: ps.execute();
271: ps.close();
272: con.commit();
273: con.close();
274:
275: eventListener.clear();
276: gpsDevice.performMirroring();
277: assertTrue(eventListener.isCreateAndUpdateCalled());
278: assertTrue(eventListener.isUpdateHappen());
279: assertFalse(eventListener.isCreateHappen());
280: assertFalse(eventListener.isDeleteCalled());
281:
282: // test that update child works
283: con = JdbcUtils.getConnection(dataSource);
284: ps = con
285: .prepareStatement("update child set first_name = 'new first name', version = 2 where id = 1");
286: ps.execute();
287: ps.close();
288: con.commit();
289: con.close();
290:
291: eventListener.clear();
292: gpsDevice.performMirroring();
293: assertTrue(eventListener.isCreateAndUpdateCalled());
294: assertTrue(eventListener.isUpdateHappen());
295: assertFalse(eventListener.isCreateHappen());
296: assertFalse(eventListener.isDeleteCalled());
297:
298: // test that delete works
299: con = JdbcUtils.getConnection(dataSource);
300: ps = con.prepareStatement("delete from parent where id = 999");
301: ps.execute();
302: ps.close();
303: con.commit();
304: con.close();
305:
306: eventListener.clear();
307: gpsDevice.performMirroring();
308: assertTrue(eventListener.isDeleteCalled());
309: assertTrue(eventListener.isDeleteHappen());
310: assertFalse(eventListener.isCreateAndUpdateCalled());
311:
312: eventListener.clear();
313: gpsDevice.performMirroring();
314: assertFalse(eventListener.isCreateAndUpdateCalled());
315: assertFalse(eventListener.isDeleteCalled());
316: }
317:
318: public void testExactMappingWithMirror() throws Exception {
319: setUpExactMappingWithMirror();
320: gps.index();
321: compassTemplate.loadResource("result-set", "1", "1");
322: Resource r = compassTemplate
323: .getResource("result-set", "4", "6");
324: assertNotNull(r);
325: CompassDetachedHits hits = compassTemplate
326: .findWithDetach("parent");
327: assertEquals(6, hits.getLength());
328:
329: // test that create works
330: Connection con = JdbcUtils.getConnection(dataSource);
331: PreparedStatement ps = con
332: .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
333: ps.execute();
334: ps.close();
335: con.commit();
336: con.close();
337:
338: r = compassTemplate.getResource("result-set", "999", "0");
339: assertNull(r);
340: gpsDevice.performMirroring();
341: compassTemplate.loadResource("result-set", "999", "0");
342:
343: // test that update works
344: con = JdbcUtils.getConnection(dataSource);
345: ps = con
346: .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
347: ps.execute();
348: ps.close();
349: con.commit();
350: con.close();
351:
352: gpsDevice.performMirroring();
353: r = compassTemplate.loadResource("result-set", "1", "1");
354: assertEquals("new first name", r.getValue("parent_first_name"));
355:
356: // test that delete works
357: con = JdbcUtils.getConnection(dataSource);
358: ps = con.prepareStatement("delete from parent where id = 999");
359: ps.execute();
360: ps.close();
361: con.commit();
362: con.close();
363:
364: gpsDevice.performMirroring();
365: r = compassTemplate.getResource("result-set", "999", "0");
366: assertNull(r);
367: }
368: }
|