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.hibernate.scrollable;
018:
019: import javax.naming.Context;
020: import javax.naming.InitialContext;
021:
022: import junit.framework.TestCase;
023: import org.compass.core.Compass;
024: import org.compass.core.CompassDetachedHits;
025: import org.compass.core.CompassTemplate;
026: import org.compass.core.Resource;
027: import org.compass.core.config.CompassConfiguration;
028: import org.compass.core.config.CompassEnvironment;
029: import org.compass.gps.device.jdbc.ResultSetResourceMappingResolver;
030: import org.compass.gps.device.jdbc.mapping.DataColumnToPropertyMapping;
031: import org.compass.gps.device.jdbc.mapping.IdColumnToPropertyMapping;
032: import org.compass.gps.device.jdbc.mapping.ResultSetToResourceMapping;
033: import org.compass.gps.device.jdbc.mapping.VersionColumnMapping;
034: import org.compass.gps.impl.SingleCompassGps;
035: import org.hibernate.Session;
036: import org.hibernate.SessionFactory;
037: import org.hibernate.Transaction;
038: import org.hibernate.cfg.Configuration;
039: import org.hibernate.cfg.Environment;
040: import org.objectweb.jotm.Jotm;
041:
042: public class Hibernate3ScrollableResultsGpsDeviceTests extends TestCase {
043:
044: private Jotm jotm;
045:
046: protected Compass compass;
047:
048: protected CompassTemplate compassTemplate;
049:
050: private Hibernate3ScrollableResultsGpsDevice gpsDevice;
051:
052: private SingleCompassGps gps;
053:
054: private SessionFactory sessionFactory;
055:
056: private Parent parent1;
057: private Parent parent2;
058: private Parent parent3;
059: private Parent parent4;
060: private Child child1;
061: private Child child2;
062: private Child child3;
063: private Child child4;
064: private Child child5;
065: private Child child6;
066:
067: protected void setUp() throws Exception {
068: super .setUp();
069:
070: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
071: "com.sun.jndi.rmi.registry.RegistryContextFactory");
072: System
073: .setProperty(Context.PROVIDER_URL,
074: "rmi://localhost:1099");
075:
076: try {
077: java.rmi.registry.LocateRegistry.createRegistry(1099);
078: } catch (Exception e) {
079: // do nothing
080: }
081:
082: jotm = new Jotm(true, true);
083: Context ctx = new InitialContext();
084: ctx.rebind("java:comp/UserTransaction", jotm
085: .getUserTransaction());
086:
087: Configuration conf = new Configuration()
088: .configure(
089: "/org/compass/gps/device/hibernate/scrollable/hibernate3.cfg.xml")
090: .setProperty(Environment.HBM2DDL_AUTO, "create");
091: sessionFactory = conf.buildSessionFactory();
092:
093: // set up the initial set of data
094: Session s = sessionFactory.openSession();
095: Transaction tx = s.beginTransaction();
096:
097: parent1 = new Parent(1, "parent first 1", "last 1");
098: parent2 = new Parent(2, "parent first 2", "last 2");
099: parent3 = new Parent(3, "parent first 3", "last 3");
100: parent4 = new Parent(4, "parent first 4", "last 4");
101: child1 = new Child(1, parent1, "child first 1 1 ", "last 1 1");
102: child2 = new Child(2, parent1, "child first 1 2 ", "last 1 2");
103: child3 = new Child(3, parent1, "child first 1 3 ", "last 1 3");
104: child4 = new Child(4, parent2, "child first 2 1 ", "last 2 1");
105: child5 = new Child(5, parent3, "child first 3 1 ", "last 3 1");
106: child6 = new Child(6, parent4, "child first 4 1 ", "last 4 1");
107:
108: s.save(parent1);
109: s.save(parent2);
110: s.save(parent3);
111: s.save(parent4);
112: s.save(child1);
113: s.save(child2);
114: s.save(child3);
115: s.save(child4);
116: s.save(child5);
117: s.save(child6);
118:
119: tx.commit();
120: s.close();
121:
122: }
123:
124: protected void setUpExactMappingNoMirror() throws Exception {
125: // set up the database mappings, since they are used both to generate
126: // the resource mappings and configure the gps device
127: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
128: mapping.setAlias("result-set");
129: mapping
130: .setSelectQuery("select "
131: + "p.id as parent_id, p.firstName as parent_first_name, p.lastName as parent_last_name, "
132: + "c.id as child_id, c.firstName as child_first_name, c.lastName as child_last_name "
133: + "from Parent p left join p.children as c");
134: mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
135: "parent_id"));
136: mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
137: "child_id"));
138: mapping.addDataMapping(new DataColumnToPropertyMapping(
139: "parent_first_name", "parent_first_name"));
140: mapping.addDataMapping(new DataColumnToPropertyMapping(
141: "parent_first_name", "first_name"));
142: mapping.addDataMapping(new DataColumnToPropertyMapping(
143: "child_first_name", "child_first_name"));
144: mapping.addDataMapping(new DataColumnToPropertyMapping(
145: "child_first_name", "first_name"));
146:
147: CompassConfiguration conf = new CompassConfiguration()
148: .setSetting(CompassEnvironment.CONNECTION,
149: "target/test-index");
150: conf.addMappingResover(new ResultSetResourceMappingResolver(
151: mapping, null)); //TODO
152: compass = conf.buildCompass();
153: compass.getSearchEngineIndexManager().deleteIndex();
154: compass.getSearchEngineIndexManager().verifyIndex();
155:
156: compassTemplate = new CompassTemplate(compass);
157:
158: gpsDevice = new Hibernate3ScrollableResultsGpsDevice();
159: gpsDevice.setSessionFactory(sessionFactory);
160: gpsDevice.setName("hibernate3ScrollableResultsGpsDevice");
161: // setting up no mirroring, even though it should not mirror since we
162: // mapped no version columns
163: gpsDevice.setMirrorDataChanges(false);
164: gpsDevice.addMapping(mapping);
165:
166: gps = new SingleCompassGps(compass);
167: gps.addGpsDevice(gpsDevice);
168: gps.start();
169: }
170:
171: protected void tearDown() throws Exception {
172: sessionFactory.close();
173: if (gps != null) {
174: gps.stop();
175: }
176: if (compass != null) {
177: compass.close();
178: }
179: jotm.stop();
180: super .tearDown();
181: }
182:
183: public void testExactMappingNoMirror() throws Exception {
184: setUpExactMappingNoMirror();
185: gps.index();
186: Resource r = compassTemplate.getResource("result-set",
187: new String[] { "1", "1" });
188: assertNotNull(r.getProperty("parent_id"));
189: assertNotNull(r.getProperty("parent_first_name"));
190: assertNotNull(r.getProperty("child_id"));
191: assertNotNull(r.getProperty("child_first_name"));
192: assertNotNull(r.getProperty("first_name"));
193: assertNull(r.getProperty("ID"));
194: assertNull(r.getProperty("FIRST_NAME"));
195: assertNull(r.getProperty("LAST_NAME"));
196: assertNotNull(r);
197: r = compassTemplate.getResource("result-set", new String[] {
198: "4", "6" });
199: assertNotNull(r);
200: CompassDetachedHits hits = compassTemplate
201: .findWithDetach("parent");
202: assertEquals(6, hits.getLength());
203: }
204:
205: public void testUnmappedMappingNoMirror() throws Exception {
206: setUpUnmappedMappingNoMirror();
207: gps.index();
208: Resource r = compassTemplate.getResource("result-set",
209: new String[] { "1", "1" });
210: assertNotNull(r);
211: assertNotNull(r.getProperty("parent_id"));
212: assertNotNull(r.getProperty("parent_first_name"));
213: assertNotNull(r.getProperty("child_id"));
214: assertNotNull(r.getProperty("child_first_name"));
215: assertNotNull(r.getProperty("LAST_NAME"));
216: assertNull(r.getProperty("FIRST_NAME"));
217: r = compassTemplate.getResource("result-set", new String[] {
218: "4", "6" });
219: assertNotNull(r);
220: CompassDetachedHits hits = compassTemplate
221: .findWithDetach("parent");
222: assertEquals(6, hits.getLength());
223: }
224:
225: protected void setUpUnmappedMappingNoMirror() throws Exception {
226: // set up the database mappings, since they are used both to generate
227: // the resource mappings and configure the jdbc gps device
228: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping(
229: "result-set", "from Parent p left join p.children as c");
230:
231: mapping
232: .setSelectQuery("select "
233: + "p.id as parent_id, p.firstName as parent_first_name, p.lastName as parent_last_name, "
234: + "p.version as parent_version, c.id as child_id, c.version as child_version, c.firstName as FIRST_NAME, c.lastName as LAST_NAME "
235: + "from Parent p left join p.children as c");
236: mapping.setIndexUnMappedColumns(true);
237: mapping.addIdMapping(new IdColumnToPropertyMapping(0,
238: "parent_id"));
239: mapping.addIdMapping(new IdColumnToPropertyMapping(4,
240: "child_id"));
241: mapping.addDataMapping(new DataColumnToPropertyMapping(1,
242: "parent_first_name"));
243: mapping.addDataMapping(new DataColumnToPropertyMapping(6,
244: "child_first_name"));
245:
246: CompassConfiguration conf = new CompassConfiguration()
247: .setSetting(CompassEnvironment.CONNECTION,
248: "target/test-index");
249: conf.addMappingResover(new ResultSetResourceMappingResolver(
250: mapping, null)); //XXX
251: compass = conf.buildCompass();
252: compass.getSearchEngineIndexManager().deleteIndex();
253: compass.getSearchEngineIndexManager().verifyIndex();
254:
255: compassTemplate = new CompassTemplate(compass);
256:
257: gpsDevice = new Hibernate3ScrollableResultsGpsDevice();
258: gpsDevice.setSessionFactory(sessionFactory);
259: gpsDevice.setName("hibernate3ScrollableResultsGpsDevice");
260: // it should not mirror the data since we did not mapped any version
261: // columns
262: gpsDevice.setMirrorDataChanges(true);
263: gpsDevice.addMapping(mapping);
264:
265: gps = new SingleCompassGps(compass);
266: gps.addGpsDevice(gpsDevice);
267: gps.start();
268: }
269:
270: protected void setUpExactMappingWithMirror() throws Exception {
271: // set up the database mappings, since they are used both to generate
272: // the resource mappings and configure the jdbc gps device
273: ResultSetToResourceMapping mapping = new ResultSetToResourceMapping();
274: mapping.setAlias("result-set");
275: mapping
276: .setSelectQuery("select "
277: + "p.id as parent_id, p.firstName as parent_first_name, p.lastName as parent_last_name, p.version as parent_version, "
278: + "COALESCE(c.id, 0) as child_id, c.firstName as child_first_name, c.lastName as child_last_name, COALESCE(c.version, 0) as child_version "
279: + "from Parent p left join p.children as c");
280: mapping
281: .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 p.children as c");
282: mapping.addIdMapping(new IdColumnToPropertyMapping("parent_id",
283: "parent_id", "p.id"));
284: mapping.addIdMapping(new IdColumnToPropertyMapping("child_id",
285: "child_id", "COALESCE(c.id, 0)"));
286: mapping.addDataMapping(new DataColumnToPropertyMapping(
287: "parent_first_name", "parent_first_name"));
288: mapping.addDataMapping(new DataColumnToPropertyMapping(
289: "child_first_name", "child_first_name"));
290: mapping.addVersionMapping(new VersionColumnMapping(
291: "parent_version"));
292: mapping.addVersionMapping(new VersionColumnMapping(
293: "child_version"));
294:
295: CompassConfiguration conf = new CompassConfiguration()
296: .setSetting(CompassEnvironment.CONNECTION,
297: "target/testindex");
298: conf.addMappingResover(new ResultSetResourceMappingResolver(
299: mapping, null)); //XXX
300: compass = conf.buildCompass();
301: compass.getSearchEngineIndexManager().deleteIndex();
302: compass.getSearchEngineIndexManager().verifyIndex();
303:
304: compassTemplate = new CompassTemplate(compass);
305:
306: gpsDevice = new Hibernate3ScrollableResultsGpsDevice();
307: gpsDevice.setSessionFactory(sessionFactory);
308: gpsDevice.setName("hibernate3ScrollableResultsGpsDevice");
309: gpsDevice.setMirrorDataChanges(true);
310: gpsDevice.addMapping(mapping);
311:
312: gps = new SingleCompassGps(compass);
313: gps.addGpsDevice(gpsDevice);
314: gps.start();
315: }
316:
317: public void testExactMappingWithMirror() throws Exception {
318: setUpExactMappingWithMirror();
319: gps.index();
320: Resource r = compassTemplate.loadResource("result-set",
321: new String[] { "1", "1" });
322: r = compassTemplate.getResource("result-set", new String[] {
323: "4", "6" });
324: assertNotNull(r);
325: CompassDetachedHits hits = compassTemplate
326: .findWithDetach("parent");
327: assertEquals(6, hits.getLength());
328:
329: // test that create works
330: Session s = sessionFactory.openSession();
331: Transaction tx = s.beginTransaction();
332:
333: Parent newParent = new Parent(999, "parent first 999",
334: "last 999");
335: s.save(newParent);
336: tx.commit();
337: s.close();
338:
339: r = compassTemplate.getResource("result-set", new String[] {
340: "999", "0" });
341: assertNull(r);
342: gpsDevice.performMirroring();
343: r = compassTemplate.loadResource("result-set", new String[] {
344: "999", "0" });
345:
346: // test that update works
347: s = sessionFactory.openSession();
348: tx = s.beginTransaction();
349: Parent p = (Parent) s.get(Parent.class, new Integer(1));
350: p.setFirstName("new first name"); //XXX: version is increased automatically?
351: tx.commit();
352: s.close();
353:
354: gpsDevice.performMirroring();
355: r = compassTemplate.loadResource("result-set", new String[] {
356: "1", "1" });
357: assertEquals("new first name", r.getValue("parent_first_name"));
358:
359: // test that delete works
360: s = sessionFactory.openSession();
361: tx = s.beginTransaction();
362: p = (Parent) s.get(Parent.class, new Integer(999));
363: s.delete(p);
364: tx.commit();
365: s.close();
366:
367: gpsDevice.performMirroring();
368: r = compassTemplate.getResource("result-set", new String[] {
369: "999", "0" });
370: assertNull(r);
371: }
372:
373: }
|