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.spring.test.transaction;
018:
019: import javax.transaction.UserTransaction;
020:
021: import junit.framework.TestCase;
022: import org.compass.core.Compass;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.config.CompassConfiguration;
026: import org.compass.core.config.CompassEnvironment;
027: import org.compass.core.lucene.LuceneEnvironment;
028: import org.compass.gps.device.MockIndexGpsDevice;
029: import org.compass.gps.device.MockIndexGpsDeviceObject;
030: import org.compass.gps.impl.SingleCompassGps;
031: import org.compass.spring.device.SpringSyncTransactionGpsDeviceWrapper;
032: import org.compass.spring.transaction.SpringSyncTransactionFactory;
033: import org.springframework.transaction.jta.JotmFactoryBean;
034: import org.springframework.transaction.jta.JtaTransactionManager;
035:
036: /**
037: * @author kimchy
038: */
039: public class SpringSyncGpsIndexTests extends TestCase {
040:
041: private Compass compass;
042:
043: private SingleCompassGps compassGps;
044:
045: private MockIndexGpsDevice device;
046:
047: private JotmFactoryBean jotmFactoryBean;
048:
049: private JtaTransactionManager transactionManager;
050: private SpringSyncTransactionGpsDeviceWrapper gpsDevice;
051:
052: protected void setUp() throws Exception {
053:
054: jotmFactoryBean = new JotmFactoryBean();
055:
056: transactionManager = new JtaTransactionManager();
057: transactionManager
058: .setUserTransaction((UserTransaction) jotmFactoryBean
059: .getObject());
060: transactionManager.afterPropertiesSet();
061: }
062:
063: protected void tearDown() throws Exception {
064: jotmFactoryBean.destroy();
065: }
066:
067: public void testSimpleIndex() throws Exception {
068: SpringSyncTransactionFactory
069: .setTransactionManager(transactionManager);
070:
071: CompassConfiguration conf = new CompassConfiguration();
072: conf.setSetting(CompassEnvironment.CONNECTION,
073: "target/test-index");
074: conf.setSetting(CompassEnvironment.Transaction.FACTORY,
075: SpringSyncTransactionFactory.class.getName());
076: conf.setSetting(LuceneEnvironment.Optimizer.SCHEDULE, "false");
077: conf
078: .setSetting(
079: LuceneEnvironment.SearchEngineIndex.INDEX_MANAGER_SCHEDULE_INTERVAL,
080: "-1");
081: conf.addClass(MockIndexGpsDeviceObject.class);
082: compass = conf.buildCompass();
083:
084: device = new MockIndexGpsDevice();
085: device.setName("test");
086: compassGps = new SingleCompassGps(compass);
087: gpsDevice = new SpringSyncTransactionGpsDeviceWrapper(device);
088: gpsDevice.afterPropertiesSet();
089: compassGps.addGpsDevice(gpsDevice);
090: compassGps.start();
091:
092: compass.getSearchEngineIndexManager().deleteIndex();
093: compass.getSearchEngineIndexManager().createIndex();
094:
095: assertNoObjects();
096:
097: device.add(new Long(1), "testvalue");
098: compassGps.index();
099:
100: assertExists(new Long(1));
101:
102: compassGps.stop();
103: compass.close();
104: }
105:
106: private void assertExists(Long id) {
107: CompassSession session = compass.openSession();
108: CompassTransaction tr = session.beginTransaction();
109: session.load(MockIndexGpsDeviceObject.class, id);
110: tr.commit();
111: session.close();
112: }
113:
114: private void assertNoObjects() {
115: CompassSession session = compass.openSession();
116: CompassTransaction tr = session.beginTransaction();
117: assertEquals(0, session.queryBuilder().matchAll().hits()
118: .length());
119: tr.commit();
120: session.close();
121: }
122: }
|