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.core.test.cache;
018:
019: import org.compass.core.Compass;
020: import org.compass.core.CompassHits;
021: import org.compass.core.CompassSession;
022: import org.compass.core.CompassTransaction;
023: import org.compass.core.config.CompassEnvironment;
024: import org.compass.core.config.CompassSettings;
025: import org.compass.core.lucene.LuceneEnvironment;
026: import org.compass.core.test.AbstractTestCase;
027:
028: /**
029: *
030: * @author kimchy
031: *
032: */
033: public class CacheTests extends AbstractTestCase {
034:
035: protected String[] getMappings() {
036: return new String[] { "cache/cache.cpm.xml" };
037: }
038:
039: protected void addSettings(CompassSettings settings) {
040: super .addSettings(settings);
041: settings
042: .setLongSetting(
043: LuceneEnvironment.SearchEngineIndex.CACHE_INTERVAL_INVALIDATION,
044: 100);
045: }
046:
047: public void testSimpleCacheInvalidation() throws Exception {
048:
049: if (getCompass().getSettings().getSetting(
050: CompassEnvironment.CONNECTION).startsWith("ram://")) {
051: // since we open two compass instances, this test won't work with ram based index
052: return;
053: }
054:
055: Long id = new Long(1);
056:
057: Compass compass2 = buildCompass();
058:
059: CompassSession session = openSession();
060: CompassTransaction tr = session.beginTransaction();
061: A a = new A();
062: a.setId(id);
063: a.setValue("value1");
064: session.save("a1", a);
065: a.setValue("value2");
066: session.save("a2", a);
067:
068: tr.commit();
069: session.close();
070:
071: // this should be visible to the new compass instance
072: // since no caching has been done on this instance yet...
073: session = compass2.openSession();
074: tr = session.beginTransaction();
075:
076: a = (A) session.load("a1", id);
077: assertEquals("value1", a.getValue());
078: a = (A) session.load("a2", id);
079: assertEquals("value2", a.getValue());
080:
081: tr.commit();
082:
083: // not update the instances for the first compass instance
084: session = openSession();
085: tr = session.beginTransaction();
086: a = new A();
087: a.setId(id);
088: a.setValue("newvalue1");
089: session.save("a1", a);
090: a.setValue("newvalue2");
091: session.save("a2", a);
092:
093: tr.commit();
094: session.close();
095:
096: Thread.sleep(1000);
097:
098: // now check that the cache was invalidated for BOTH a1 and a2
099: session = compass2.openSession();
100: tr = session.beginTransaction();
101:
102: CompassHits hits = session.find("newvalue1 OR newvalue2");
103: assertEquals(2, hits.length());
104:
105: tr.commit();
106: session.close();
107:
108: compass2.close();
109: }
110: }
|