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.jdo;
018:
019: import junit.framework.TestCase;
020: import org.compass.core.Compass;
021: import org.compass.core.config.CompassConfiguration;
022: import org.compass.gps.impl.SingleCompassGps;
023:
024: import javax.jdo.JDOHelper;
025: import javax.jdo.PersistenceManager;
026: import javax.jdo.PersistenceManagerFactory;
027: import javax.jdo.Transaction;
028: import java.util.Properties;
029:
030: /**
031: * @author kimchy
032: */
033: public abstract class AbstractJdoGpsDeviceTests extends TestCase {
034:
035: protected PersistenceManagerFactory persistenceManagerFactory;
036:
037: private Compass compass;
038:
039: protected SingleCompassGps compassGps;
040:
041: private Product product;
042:
043: private Book book;
044:
045: protected void setUp() throws Exception {
046: Properties properties = new Properties();
047:
048: properties.setProperty(
049: "javax.jdo.PersistenceManagerFactoryClass",
050: "org.jpox.PersistenceManagerFactoryImpl");
051: properties.setProperty("javax.jdo.option.ConnectionDriverName",
052: "org.hsqldb.jdbcDriver");
053: properties.setProperty("javax.jdo.option.ConnectionURL",
054: "jdbc:hsqldb:mem:test");
055: properties.setProperty("javax.jdo.option.ConnectionUserName",
056: "sa");
057: properties.setProperty("javax.jdo.option.ConnectionPassword",
058: "");
059: properties.setProperty("org.jpox.autoCreateSchema", "true");
060: properties.setProperty("org.jpox.validateTables", "false");
061: properties.setProperty("org.jpox.validateConstraints", "false");
062:
063: persistenceManagerFactory = JDOHelper
064: .getPersistenceManagerFactory(properties);
065: setUpCoreCompass();
066: setUpGps();
067: setUpGpsDevice();
068: compassGps.start();
069: setUpDB();
070: }
071:
072: protected void tearDown() throws Exception {
073: tearDownDB();
074: compassGps.stop();
075: compass.close();
076: persistenceManagerFactory.close();
077: }
078:
079: protected void setUpGpsDevice() {
080: JdoGpsDevice jdoGpsDevice = new JdoGpsDevice();
081: jdoGpsDevice.setName("jdoDevice");
082: jdoGpsDevice
083: .setPersistenceManagerFactory(persistenceManagerFactory);
084: compassGps.addGpsDevice(jdoGpsDevice);
085: }
086:
087: protected void setUpGps() {
088: compassGps = new SingleCompassGps(compass);
089: }
090:
091: protected void setUpCoreCompass() {
092: CompassConfiguration cpConf = new CompassConfiguration()
093: .configure("/org/compass/gps/device/jdo/compass.cfg.xml");
094: compass = cpConf.buildCompass();
095: compass.getSearchEngineIndexManager().deleteIndex();
096: compass.getSearchEngineIndexManager().verifyIndex();
097: }
098:
099: protected void setUpDB() throws Exception {
100: PersistenceManager pm = persistenceManagerFactory
101: .getPersistenceManager();
102: Transaction tx = pm.currentTransaction();
103: try {
104: tx.begin();
105: product = new Product("Sony Discman",
106: "A standard discman from Sony", 49.99);
107: book = new Book("Lord of the Rings by Tolkien",
108: "The classic story", 49.99, "JRR Tolkien",
109: "12345678", "MyBooks Factory");
110: pm.makePersistent(product);
111: pm.makePersistent(book);
112: tx.commit();
113: } finally {
114: if (tx.isActive()) {
115: tx.rollback();
116: }
117: pm.close();
118: }
119: }
120:
121: protected void tearDownDB() throws Exception {
122: PersistenceManager pm = persistenceManagerFactory
123: .getPersistenceManager();
124: Transaction tx = pm.currentTransaction();
125: try {
126: tx.begin();
127: book = (Book) pm.getObjectById(Book.class, book.getId());
128: pm.deletePersistent(book);
129: product = (Product) pm.getObjectById(Product.class, product
130: .getId());
131: pm.deletePersistent(product);
132: tx.commit();
133: } finally {
134: if (tx.isActive()) {
135: tx.rollback();
136: }
137: pm.close();
138: }
139: }
140:
141: }
|