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