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.EntityManagerFactory;
021: import javax.persistence.EntityTransaction;
022:
023: import junit.framework.TestCase;
024: import org.compass.annotations.config.CompassAnnotationsConfiguration;
025: import org.compass.core.Compass;
026: import org.compass.core.config.CompassConfiguration;
027: import org.compass.gps.CompassGps;
028: import org.compass.gps.impl.SingleCompassGps;
029:
030: /**
031: * @author kimchy
032: */
033: public abstract class AbstractJpaGpsDeviceTests extends TestCase {
034:
035: protected EntityManagerFactory entityManagerFactory;
036:
037: protected Compass compass;
038:
039: protected CompassGps compassGps;
040:
041: @Override
042: protected void setUp() throws Exception {
043: entityManagerFactory = doSetUpEntityManagerFactory();
044: setUpCompass();
045: setUpGps();
046: compassGps.start();
047: setUpDB();
048: }
049:
050: @Override
051: protected void tearDown() throws Exception {
052: tearDownDB();
053: compassGps.stop();
054: compass.close();
055: entityManagerFactory.close();
056:
057: try {
058: compass.getSearchEngineIndexManager().deleteIndex();
059: } catch (Exception e) {
060: e.printStackTrace();
061: }
062: if (compass.getSpellCheckManager() != null) {
063: try {
064: compass.getSpellCheckManager().deleteIndex();
065: } catch (Exception e) {
066: e.printStackTrace();
067: }
068: }
069: }
070:
071: protected abstract EntityManagerFactory doSetUpEntityManagerFactory();
072:
073: protected abstract void setUpCoreCompass(CompassConfiguration conf);
074:
075: protected void setUpCompass() {
076: CompassConfiguration cpConf = new CompassAnnotationsConfiguration()
077: .setConnection("target/test-index");
078: setUpCoreCompass(cpConf);
079: compass = cpConf.buildCompass();
080: compass.getSearchEngineIndexManager().deleteIndex();
081: compass.getSearchEngineIndexManager().verifyIndex();
082: }
083:
084: protected void setUpGps() {
085: compassGps = new SingleCompassGps(compass);
086: setUpGpsDevice();
087: }
088:
089: protected void setUpGpsDevice() {
090: JpaGpsDevice jpaGpsDevice = new JpaGpsDevice();
091: jpaGpsDevice.setName("jdoDevice");
092: jpaGpsDevice.setEntityManagerFactory(entityManagerFactory);
093: addDeviceSettings(jpaGpsDevice);
094: ((SingleCompassGps) compassGps).addGpsDevice(jpaGpsDevice);
095: }
096:
097: protected void addDeviceSettings(JpaGpsDevice device) {
098:
099: }
100:
101: protected void setUpDB() {
102: EntityManager entityManager = entityManagerFactory
103: .createEntityManager();
104: EntityTransaction entityTransaction = entityManager
105: .getTransaction();
106: entityTransaction.begin();
107: setUpDB(entityManager);
108: entityTransaction.commit();
109: entityManager.close();
110: }
111:
112: protected void setUpDB(EntityManager entityManager) {
113: }
114:
115: protected void tearDownDB() {
116: EntityManager entityManager = entityManagerFactory
117: .createEntityManager();
118: EntityTransaction entityTransaction = entityManager
119: .getTransaction();
120: entityTransaction.begin();
121: tearDownDB(entityManager);
122: entityTransaction.commit();
123: entityManager.close();
124: }
125:
126: protected void tearDownDB(EntityManager entityManager) {
127: }
128: }
|