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.impl;
018:
019: import java.util.Properties;
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:
031: /**
032: * @author kimchy
033: */
034: public class SingleCompassGpsIndexTests extends TestCase {
035:
036: private Compass compass;
037:
038: private SingleCompassGps compassGps;
039:
040: private MockIndexGpsDevice device;
041:
042: public void testSimpleIndex() {
043: CompassConfiguration conf = new CompassConfiguration();
044: conf.setSetting(CompassEnvironment.CONNECTION,
045: "target/test-index");
046: conf.addClass(MockIndexGpsDeviceObject.class);
047: compass = conf.buildCompass();
048:
049: device = new MockIndexGpsDevice();
050: device.setName("test");
051: compassGps = new SingleCompassGps(compass);
052: compassGps.addGpsDevice(device);
053: compassGps.start();
054:
055: compass.getSearchEngineIndexManager().deleteIndex();
056: compass.getSearchEngineIndexManager().createIndex();
057:
058: assertNoObjects();
059:
060: device.add(new Long(1), "testvalue");
061: compassGps.index();
062:
063: assertExists(new Long(1));
064:
065: compassGps.stop();
066: compass.close();
067: }
068:
069: public void testWithPropertiesForSingleComopassGps() {
070: CompassConfiguration conf = new CompassConfiguration();
071: conf.setSetting(CompassEnvironment.CONNECTION,
072: "target/test-index");
073: conf.addClass(MockIndexGpsDeviceObject.class);
074: compass = conf.buildCompass();
075: compass.getSearchEngineIndexManager().deleteIndex();
076: compass.getSearchEngineIndexManager().createIndex();
077:
078: device = new MockIndexGpsDevice();
079: device.setName("test");
080: compassGps = new SingleCompassGps(compass);
081: compassGps.addGpsDevice(device);
082: Properties props = new Properties();
083: props.setProperty(
084: LuceneEnvironment.SearchEngineIndex.MAX_BUFFERED_DOCS,
085: "100");
086: compassGps.setIndexSettings(props);
087: compassGps.start();
088:
089: assertNoObjects();
090:
091: device.add(new Long(1), "testvalue");
092: compassGps.index();
093:
094: assertExists(new Long(1));
095:
096: compassGps.stop();
097: compass.close();
098: }
099:
100: private void assertExists(Long id) {
101: CompassSession session = compass.openSession();
102: CompassTransaction tr = session.beginTransaction();
103: session.load(MockIndexGpsDeviceObject.class, id);
104: tr.commit();
105: session.close();
106: }
107:
108: private void assertNoObjects() {
109: CompassSession session = compass.openSession();
110: CompassTransaction tr = session.beginTransaction();
111: assertEquals(0, session.queryBuilder().matchAll().hits()
112: .length());
113: tr.commit();
114: session.close();
115: }
116: }
|