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 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.CompassTemplate;
025: import org.compass.core.config.CompassConfiguration;
026: import org.compass.gps.device.hibernate.HibernateGpsDevice;
027: import org.compass.gps.impl.SingleCompassGps;
028: import org.hibernate.Session;
029: import org.hibernate.SessionFactory;
030: import org.hibernate.Transaction;
031: import org.hibernate.cfg.Configuration;
032: import org.hibernate.cfg.Environment;
033: import org.objectweb.jotm.Jotm;
034:
035: /**
036: * @author kimchy
037: */
038: public class CollectionTests extends TestCase {
039:
040: private Jotm jotm;
041:
042: private Compass compass;
043:
044: private CompassTemplate compassTemplate;
045:
046: private SingleCompassGps compassGps;
047:
048: private SessionFactory sessionFactory;
049:
050: protected void setUp() throws Exception {
051: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
052: "com.sun.jndi.rmi.registry.RegistryContextFactory");
053: System
054: .setProperty(Context.PROVIDER_URL,
055: "rmi://localhost:1099");
056:
057: try {
058: java.rmi.registry.LocateRegistry.createRegistry(1099);
059: } catch (Exception e) {
060:
061: }
062:
063: jotm = new Jotm(true, true);
064: Context ctx = new InitialContext();
065: ctx.rebind("java:comp/UserTransaction", jotm
066: .getUserTransaction());
067:
068: CompassConfiguration cpConf = new CompassConfiguration()
069: .configure("/org/compass/gps/device/hibernate/collection/compass.cfg.xml");
070: compass = cpConf.buildCompass();
071: compass.getSearchEngineIndexManager().deleteIndex();
072: compass.getSearchEngineIndexManager().verifyIndex();
073:
074: compassTemplate = new CompassTemplate(compass);
075:
076: compassGps = new SingleCompassGps(compass);
077:
078: Configuration conf = new Configuration()
079: .configure(
080: "/org/compass/gps/device/hibernate/collection/hibernate.cfg.xml")
081: .setProperty(Environment.HBM2DDL_AUTO, "create");
082: sessionFactory = conf.buildSessionFactory();
083:
084: HibernateGpsDevice device = new HibernateGpsDevice();
085: device.setSessionFactory(sessionFactory);
086: device.setName("hibernateDevice");
087: compassGps.addGpsDevice(device);
088: compassGps.start();
089: }
090:
091: public void testCollection() {
092:
093: Session s = sessionFactory.openSession();
094: Transaction tx = s.beginTransaction();
095: Parent parent = new Parent();
096: parent.setValue("value");
097: Child child1 = new Child("child1");
098: parent.addChild(child1);
099: Child child2 = new Child("child2");
100: parent.addChild(child2);
101: s.save(parent);
102: tx.commit();
103: s.close();
104:
105: parent = (Parent) compassTemplate.load(Parent.class, parent
106: .getId());
107: assertEquals("value", parent.getValue());
108:
109: assertEquals(1, compassTemplate.find("child1").length());
110:
111: s = sessionFactory.openSession();
112: tx = s.beginTransaction();
113: parent = (Parent) s.load(Parent.class, parent.getId());
114: parent.setValue("newvalue");
115: s.flush();
116: tx.commit();
117: s.close();
118:
119: s = sessionFactory.openSession();
120: tx = s.beginTransaction();
121: parent.setValue("newvalue1");
122: s.saveOrUpdate(parent);
123: tx.commit();
124: s.close();
125: }
126:
127: protected void tearDown() throws Exception {
128: sessionFactory.close();
129: compassGps.stop();
130: compass.close();
131: jotm.stop();
132: }
133: }
|