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.simple;
018:
019: import junit.framework.Assert;
020: import org.compass.core.CompassHits;
021: import org.compass.core.CompassSession;
022: import org.compass.core.CompassTransaction;
023: import org.compass.core.config.CompassConfiguration;
024: import org.hibernate.Query;
025: import org.hibernate.Session;
026: import org.hibernate.SessionFactory;
027: import org.hibernate.Transaction;
028: import org.hibernate.cfg.Configuration;
029: import org.hibernate.cfg.Environment;
030:
031: /**
032: * @author kimchy
033: */
034: public abstract class AbstractSimpleHibernateGpsDeviceTests extends
035: AbstractHibernateGpsDeviceTests {
036:
037: protected void setUpCoreCompass(CompassConfiguration conf) {
038: conf.addClass(Simple.class).addClass(SimpleBase.class)
039: .addClass(SimpleExtend.class);
040: }
041:
042: protected String getHiberanteCfgLocation() {
043: return "/org/compass/gps/device/hibernate/simple/hibernate.cfg.xml";
044: }
045:
046: protected SessionFactory doSetUpSessionFactory() {
047: Configuration conf = new Configuration().configure(
048: getHiberanteCfgLocation()).setProperty(
049: Environment.HBM2DDL_AUTO, "create");
050: return conf.buildSessionFactory();
051: }
052:
053: protected void setUpDB(Session session) {
054: Simple simple = new Simple();
055: simple.setId(1);
056: simple.setValue("value1");
057: session.save(simple);
058: simple = new Simple();
059: simple.setId(2);
060: simple.setValue("value2");
061: session.save(simple);
062: simple = new Simple();
063: simple.setId(3);
064: simple.setValue("value3");
065: session.save(simple);
066:
067: SimpleBase simpleBase = new SimpleBase();
068: simpleBase.setId(1);
069: simpleBase.setValue("value");
070: session.save(simpleBase);
071:
072: SimpleExtend simpleExtend = new SimpleExtend();
073: simpleExtend.setId(2);
074: simpleExtend.setValue("value");
075: simpleExtend.setValueExtend("valueExtended");
076: session.save(simpleExtend);
077: }
078:
079: protected void tearDownDB(Session session) {
080: Query query = session.createQuery("delete from Simple");
081: query.executeUpdate();
082: query = session.createQuery("delete from SimpleBase");
083: query.executeUpdate();
084: }
085:
086: public void testSimple() throws Exception {
087: compassGps.index();
088:
089: CompassSession sess = compass.openSession();
090: CompassTransaction tr = sess.beginTransaction();
091:
092: Simple simple = sess.load(Simple.class, 1);
093: Assert.assertEquals("value1", simple.getValue());
094: simple = sess.load(Simple.class, 2);
095: Assert.assertEquals("value2", simple.getValue());
096:
097: CompassHits hits = sess.find("value1");
098: Assert.assertEquals(1, hits.length());
099:
100: tr.commit();
101: sess.close();
102: }
103:
104: public void testMirrorWithCommit() throws Exception {
105: compassGps.index();
106:
107: Session session = sessionFactory.openSession();
108: Transaction tr = session.beginTransaction();
109:
110: // insert a new one
111: Simple simple = new Simple();
112: simple.setId(4);
113: simple.setValue("value4");
114: session.save(simple);
115:
116: // delete the second one
117: simple = (Simple) session.load(Simple.class, 2);
118: session.delete(simple);
119:
120: // update the first one
121: simple = (Simple) session.load(Simple.class, 1);
122: simple.setValue("updatedValue1");
123: session.save(simple);
124:
125: session.flush();
126:
127: tr.commit();
128: session.close();
129:
130: CompassSession sess = compass.openSession();
131: CompassTransaction compassTransaction = sess.beginTransaction();
132:
133: simple = sess.load(Simple.class, 4);
134: assertEquals("value4", simple.getValue());
135:
136: simple = sess.get(Simple.class, 2);
137: assertNull(simple);
138:
139: simple = sess.load(Simple.class, 1);
140: assertEquals("updatedValue1", simple.getValue());
141:
142: compassTransaction.commit();
143: sess.close();
144: }
145:
146: public void testExtend() throws Exception {
147: compassGps.index();
148:
149: CompassSession sess = compass.openSession();
150: CompassTransaction tr = sess.beginTransaction();
151:
152: SimpleBase simpleBase = sess.load(SimpleBase.class, 1);
153: Assert.assertEquals("value", simpleBase.getValue());
154: SimpleExtend simpleExtend = sess.load(SimpleExtend.class, 2);
155: Assert.assertEquals("value", simpleExtend.getValue());
156: Assert.assertEquals("valueExtended", simpleExtend
157: .getValueExtend());
158:
159: CompassHits hits = sess.queryBuilder().queryString("value")
160: .toQuery().setSubIndexes(new String[] { "simple1" })
161: .hits();
162: assertEquals(2, hits.length());
163:
164: tr.commit();
165: sess.close();
166: }
167:
168: }
|