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.TableToResourceMapping;
030: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
031: import org.compass.gps.device.jdbc.snapshot.FSJdbcSnapshotPersister;
032: import org.compass.gps.impl.SingleCompassGps;
033:
034: /**
035: *
036: * @author kimchy
037: *
038: */
039: public class TableJdbcGpsDeviceTests extends AbstractJdbcGpsDeviceTests {
040:
041: protected Compass compass;
042:
043: protected CompassTemplate compassTemplate;
044:
045: private ResultSetJdbcGpsDevice gpsDevice;
046:
047: private SingleCompassGps gps;
048:
049: protected void tearDown() throws Exception {
050: gps.stop();
051: compass.close();
052: super .tearDown();
053: }
054:
055: protected void setUpDefinedExactMapping() throws Exception {
056: // set up the database mappings, since they are used both to generate
057: // the resource mappings and configure the jdbc gps device
058: TableToResourceMapping parentMapping = new TableToResourceMapping(
059: "PARENT", "parent");
060: parentMapping.setIndexUnMappedColumns(false);
061: parentMapping.addDataMapping(new DataColumnToPropertyMapping(
062: "first_name", "first_name"));
063: TableToResourceMapping childMapping = new TableToResourceMapping(
064: "CHILD", "child");
065: childMapping.addDataMapping(new DataColumnToPropertyMapping(
066: "first_name", "first_name"));
067: childMapping.setIndexUnMappedColumns(false);
068:
069: CompassConfiguration conf = new CompassConfiguration()
070: .setSetting(CompassEnvironment.CONNECTION,
071: "target/test-index");
072: conf.addMappingResover(new ResultSetResourceMappingResolver(
073: parentMapping, dataSource));
074: conf.addMappingResover(new ResultSetResourceMappingResolver(
075: childMapping, dataSource));
076: compass = conf.buildCompass();
077: compass.getSearchEngineIndexManager().deleteIndex();
078: compass.getSearchEngineIndexManager().verifyIndex();
079:
080: compassTemplate = new CompassTemplate(compass);
081:
082: gpsDevice = new ResultSetJdbcGpsDevice();
083: gpsDevice.setDataSource(dataSource);
084: gpsDevice.setName("tableJdbcDevice");
085: gpsDevice.addMapping(parentMapping);
086: gpsDevice.addMapping(childMapping);
087:
088: gps = new SingleCompassGps(compass);
089: gps.addGpsDevice(gpsDevice);
090: gps.start();
091: }
092:
093: public void testDefinedExactMapping() throws Exception {
094: setUpDefinedExactMapping();
095: gps.index();
096: Resource r = compassTemplate.getResource("parent", "1");
097: assertNotNull(r);
098: assertNotNull(r.getProperty("ID"));
099: assertNotNull(r.getProperty("first_name"));
100: assertNull(r.getProperty("FIRST_NAME"));
101: assertNull(r.getProperty("LAST_NAME"));
102: CompassDetachedHits hits = compassTemplate
103: .findWithDetach("parent");
104: assertEquals(4, hits.getLength());
105: hits = compassTemplate.findWithDetach("child");
106: assertEquals(6, hits.getLength());
107: }
108:
109: protected void setUpAutomaticMapping() throws Exception {
110: // set up the database mappings, since they are used both to generate
111: // the resource mappings and configure the jdbc gps device
112: TableToResourceMapping parentMapping = new TableToResourceMapping(
113: "parent", "parent");
114: parentMapping.addVersionMapping(new VersionColumnMapping(
115: "version"));
116: parentMapping.setIndexUnMappedColumns(true);
117:
118: TableToResourceMapping childMapping = new TableToResourceMapping(
119: "child", "child");
120: childMapping.addVersionMapping(new VersionColumnMapping(
121: "version"));
122: childMapping.setIndexUnMappedColumns(true);
123:
124: CompassConfiguration conf = new CompassConfiguration()
125: .setSetting(CompassEnvironment.CONNECTION,
126: "target/testindex");
127: conf.addMappingResover(new ResultSetResourceMappingResolver(
128: parentMapping, dataSource));
129: conf.addMappingResover(new ResultSetResourceMappingResolver(
130: childMapping, dataSource));
131: compass = conf.buildCompass();
132: compass.getSearchEngineIndexManager().deleteIndex();
133: compass.getSearchEngineIndexManager().verifyIndex();
134:
135: compassTemplate = new CompassTemplate(compass);
136:
137: gpsDevice = new ResultSetJdbcGpsDevice();
138: gpsDevice.setSnapshotPersister(new FSJdbcSnapshotPersister(
139: "target/testindex/snapshot"));
140: gpsDevice.setDataSource(dataSource);
141: gpsDevice.setName("tableJdbcDevice");
142: gpsDevice.addMapping(parentMapping);
143: gpsDevice.addMapping(childMapping);
144:
145: gps = new SingleCompassGps(compass);
146: gps.addGpsDevice(gpsDevice);
147: gps.start();
148: }
149:
150: public void testAutomaticMappingAndFSPersister() throws Exception {
151: setUpAutomaticMapping();
152: gps.index();
153: Resource r = compassTemplate.getResource("parent", "1");
154: assertNotNull(r);
155: assertNotNull(r.getProperty("ID"));
156: assertNotNull(r.getProperty("FIRST_NAME"));
157: assertNotNull(r.getProperty("LAST_NAME"));
158: CompassDetachedHits hits = compassTemplate
159: .findWithDetach("parent");
160: assertEquals(4, hits.getLength());
161: hits = compassTemplate.findWithDetach("child");
162: assertEquals(6, hits.getLength());
163: }
164:
165: public void testAutomaticMappingWithMirroringAndFSPersister()
166: throws Exception {
167: setUpAutomaticMapping();
168: gpsDevice.setMirrorDataChanges(true);
169: gps.index();
170: Resource r = compassTemplate.getResource("parent", "1");
171: assertNotNull(r);
172: assertNotNull(r.getProperty("ID"));
173: assertNotNull(r.getProperty("FIRST_NAME"));
174: assertNotNull(r.getProperty("LAST_NAME"));
175: CompassDetachedHits hits = compassTemplate
176: .findWithDetach("parent");
177: assertEquals(4, hits.getLength());
178: hits = compassTemplate.findWithDetach("child");
179: assertEquals(6, hits.getLength());
180:
181: // test that create works
182: Connection con = JdbcUtils.getConnection(dataSource);
183: PreparedStatement ps = con
184: .prepareStatement("INSERT INTO parent VALUES (999, 'parent first 999', 'last 999', 1);");
185: ps.execute();
186: ps.close();
187: con.commit();
188: con.close();
189:
190: r = compassTemplate.getResource("parent", "999");
191: assertNull(r);
192: gpsDevice.performMirroring();
193: compassTemplate.loadResource("parent", "999");
194:
195: gps.stop();
196: gps.start();
197: // test that update works
198: con = JdbcUtils.getConnection(dataSource);
199: ps = con
200: .prepareStatement("update parent set first_name = 'new first name', version = 2 where id = 1");
201: ps.execute();
202: ps.close();
203: con.commit();
204: con.close();
205:
206: gpsDevice.performMirroring();
207: r = compassTemplate.loadResource("parent", "1");
208: assertEquals("new first name", r.getValue("FIRST_NAME"));
209:
210: // test that delete works
211: con = JdbcUtils.getConnection(dataSource);
212: ps = con.prepareStatement("delete from parent where id = 999");
213: ps.execute();
214: ps.close();
215: con.commit();
216: con.close();
217:
218: gpsDevice.performMirroring();
219: r = compassTemplate.getResource("parent", "999");
220: assertNull(r);
221: }
222: }
|