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.collection;
018:
019: import junit.framework.TestCase;
020: import org.compass.core.Compass;
021: import org.compass.core.CompassTemplate;
022: import org.compass.core.config.CompassConfiguration;
023: import org.compass.gps.device.hibernate.CompassTransactionInterceptor;
024: import org.compass.gps.device.hibernate.HibernateGpsDevice;
025: import org.compass.gps.impl.SingleCompassGps;
026: import org.hibernate.Session;
027: import org.hibernate.SessionFactory;
028: import org.hibernate.Transaction;
029: import org.hibernate.cfg.Configuration;
030: import org.hibernate.cfg.Environment;
031: import org.hibernate.collection.PersistentCollection;
032:
033: /**
034: * @author kimchy
035: */
036: public class CollectionNonJtaTests extends TestCase {
037:
038: private Compass compass;
039:
040: private CompassTemplate compassTemplate;
041:
042: private SingleCompassGps compassGps;
043:
044: private SessionFactory sessionFactory;
045:
046: protected void setUp() throws Exception {
047:
048: CompassConfiguration cpConf = new CompassConfiguration()
049: .configure("/org/compass/gps/device/hibernate/collection/compass-nonjta.cfg.xml");
050: compass = cpConf.buildCompass();
051: compass.getSearchEngineIndexManager().deleteIndex();
052: compass.getSearchEngineIndexManager().verifyIndex();
053:
054: compassTemplate = new CompassTemplate(compass);
055:
056: compassGps = new SingleCompassGps(compass);
057:
058: Configuration conf = new Configuration()
059: .configure(
060: "/org/compass/gps/device/hibernate/collection/hibernate-nonjta.cfg.xml")
061: .setProperty(Environment.HBM2DDL_AUTO, "create");
062: sessionFactory = conf.buildSessionFactory();
063:
064: CompassTransactionInterceptor.injectInterceptor(sessionFactory,
065: new CompassTransactionInterceptor(compass));
066:
067: HibernateGpsDevice device = new HibernateGpsDevice();
068: device.setSessionFactory(sessionFactory);
069: device.setName("hibernateDevice");
070: compassGps.addGpsDevice(device);
071: compassGps.start();
072: }
073:
074: public void testCollection() {
075: Session s = sessionFactory.openSession();
076: Transaction tx = s.beginTransaction();
077: Parent parent = new Parent();
078: parent.setValue("value");
079: Child child1 = new Child("child1");
080: parent.addChild(child1);
081: Child child2 = new Child("child2");
082: parent.addChild(child2);
083: s.save(parent);
084: tx.commit();
085: s.close();
086:
087: parent = (Parent) compassTemplate.load(Parent.class, parent
088: .getId());
089: assertEquals("value", parent.getValue());
090:
091: assertEquals(1, compassTemplate.find("child1").length());
092:
093: s = sessionFactory.openSession();
094: tx = s.beginTransaction();
095: parent = (Parent) s.load(Parent.class, parent.getId());
096: assertEquals(false, ((PersistentCollection) parent.getChilds())
097: .wasInitialized());
098: parent.setValue("newvalue");
099: s.flush();
100: tx.commit();
101: s.close();
102:
103: s = sessionFactory.openSession();
104: tx = s.beginTransaction();
105: parent.setValue("newvalue1");
106: s.saveOrUpdate(parent);
107: tx.commit();
108: s.close();
109: }
110:
111: protected void tearDown() throws Exception {
112: sessionFactory.close();
113: compassGps.stop();
114: compass.close();
115: }
116:
117: }
|