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 java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.Properties;
023:
024: import junit.framework.TestCase;
025: import org.compass.core.Compass;
026: import org.compass.core.config.CompassConfiguration;
027: import org.compass.gps.CompassGps;
028: import org.compass.gps.device.hibernate.HibernateGpsDevice;
029: import org.compass.gps.impl.SingleCompassGps;
030: import org.hibernate.Session;
031: import org.hibernate.SessionFactory;
032: import org.hibernate.Transaction;
033:
034: /**
035: * @author kimchy
036: */
037: public abstract class AbstractHibernateGpsDeviceTests extends TestCase {
038:
039: protected SessionFactory sessionFactory;
040:
041: protected Compass compass;
042:
043: protected HibernateGpsDevice hibernateGpsDevice;
044:
045: protected CompassGps compassGps;
046:
047: protected void setUp() throws Exception {
048: sessionFactory = doSetUpSessionFactory();
049: setUpCompass();
050: setUpGps();
051: compassGps.start();
052: setUpDB();
053: }
054:
055: protected void tearDown() throws Exception {
056: tearDownDB();
057: compassGps.stop();
058: compass.close();
059: sessionFactory.close();
060:
061: try {
062: compass.getSearchEngineIndexManager().deleteIndex();
063: } catch (Exception e) {
064: e.printStackTrace();
065: }
066: if (compass.getSpellCheckManager() != null) {
067: try {
068: compass.getSpellCheckManager().deleteIndex();
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: }
073: }
074:
075: protected void doTearDown() throws Exception {
076:
077: }
078:
079: protected abstract SessionFactory doSetUpSessionFactory();
080:
081: protected abstract void setUpCoreCompass(CompassConfiguration conf);
082:
083: protected void setUpCompass() throws IOException {
084: CompassConfiguration cpConf = new CompassConfiguration()
085: .setConnection("target/test-index");
086: File testPropsFile = new File("compass.test.properties");
087: if (testPropsFile.exists()) {
088: Properties testProps = new Properties();
089: testProps.load(new FileInputStream(testPropsFile));
090: cpConf.getSettings().addSettings(testProps);
091: }
092: setUpCoreCompass(cpConf);
093: compass = cpConf.buildCompass();
094: compass.getSearchEngineIndexManager().deleteIndex();
095: compass.getSearchEngineIndexManager().verifyIndex();
096: }
097:
098: protected void setUpGps() {
099: compassGps = new SingleCompassGps(compass);
100: setUpGpsDevice();
101: }
102:
103: protected void setUpGpsDevice() {
104: hibernateGpsDevice = new HibernateGpsDevice();
105: hibernateGpsDevice.setName("jdoDevice");
106: hibernateGpsDevice.setSessionFactory(sessionFactory);
107: addDeviceSettings(hibernateGpsDevice);
108: compassGps.addGpsDevice(hibernateGpsDevice);
109: }
110:
111: protected void addDeviceSettings(HibernateGpsDevice device) {
112:
113: }
114:
115: protected void setUpDB() {
116: Session session = sessionFactory.openSession();
117: Transaction transaction = session.beginTransaction();
118: setUpDB(session);
119: transaction.commit();
120: session.close();
121: }
122:
123: protected void setUpDB(Session session) {
124: }
125:
126: protected void tearDownDB() {
127: Session session = sessionFactory.openSession();
128: Transaction transaction = session.beginTransaction();
129: tearDownDB(session);
130: transaction.commit();
131: session.close();
132: }
133:
134: protected void tearDownDB(Session session) {
135: }
136: }
|