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.indexmanager;
018:
019: import org.compass.core.CompassSession;
020: import org.compass.core.CompassTransaction;
021: import org.compass.core.config.CompassSettings;
022: import org.compass.core.engine.SearchEngineIndexManager;
023: import org.compass.core.lucene.LuceneEnvironment;
024: import org.compass.core.test.AbstractTestCase;
025:
026: /**
027: */
028: public class SearchEngineIndexManagerTests extends AbstractTestCase {
029:
030: protected String[] getMappings() {
031: return new String[] { "indexmanager/indexmanager.cpm.xml" };
032: }
033:
034: protected void addSettings(CompassSettings settings) {
035: super .addSettings(settings);
036: settings
037: .setFloatSetting(
038: LuceneEnvironment.SearchEngineIndex.INDEX_MANAGER_SCHEDULE_INTERVAL,
039: 0.2f);
040: }
041:
042: public void testCleanIndex() {
043: SearchEngineIndexManager indexManager = getCompass()
044: .getSearchEngineIndexManager();
045:
046: CompassSession session = openSession();
047: CompassTransaction tr = session.beginTransaction();
048: A a = new A();
049: a.setId(1l);
050: a.setValue("test");
051: session.save("a1", a);
052: tr.commit();
053: session.close();
054:
055: indexManager.cleanIndex();
056:
057: session = openSession();
058: tr = session.beginTransaction();
059: Object o = session.get("a1", 1);
060: assertNull(o);
061: tr.commit();
062: session.close();
063: }
064:
065: public void testIsCached() throws Exception {
066: SearchEngineIndexManager indexManager = getCompass()
067: .getSearchEngineIndexManager();
068: assertFalse(indexManager.isCached());
069: assertFalse(indexManager.isCached("a1"));
070: assertFalse(indexManager.isCached("a2"));
071:
072: acquireCache("a1");
073: assertTrue(indexManager.isCached());
074: assertTrue(indexManager.isCached("a1"));
075: assertFalse(indexManager.isCached("a2"));
076:
077: acquireCache("a2");
078: assertTrue(indexManager.isCached());
079: assertTrue(indexManager.isCached("a1"));
080: assertTrue(indexManager.isCached("a2"));
081: }
082:
083: public void testLocalCacheInvalidationBySubIndex() throws Exception {
084: SearchEngineIndexManager indexManager = getCompass()
085: .getSearchEngineIndexManager();
086: acquireCache("a1");
087: assertTrue(indexManager.isCached());
088: assertTrue(indexManager.isCached("a1"));
089: assertFalse(indexManager.isCached("a2"));
090:
091: indexManager.clearCache("a2");
092: assertTrue(indexManager.isCached());
093: assertTrue(indexManager.isCached("a1"));
094: assertFalse(indexManager.isCached("a2"));
095:
096: indexManager.clearCache("a1");
097: assertFalse(indexManager.isCached());
098: assertFalse(indexManager.isCached("a1"));
099: assertFalse(indexManager.isCached("a2"));
100: }
101:
102: public void testLocalCacheInvalidation() throws Exception {
103: SearchEngineIndexManager indexManager = getCompass()
104: .getSearchEngineIndexManager();
105: acquireCache("a1");
106: acquireCache("a2");
107: assertTrue(indexManager.isCached());
108: assertTrue(indexManager.isCached("a1"));
109: assertTrue(indexManager.isCached("a2"));
110: indexManager.clearCache();
111: assertFalse(indexManager.isCached());
112: assertFalse(indexManager.isCached("a1"));
113: assertFalse(indexManager.isCached("a2"));
114: }
115:
116: public void testGlobalCacheInvalidation() throws Exception {
117: SearchEngineIndexManager indexManager = getCompass()
118: .getSearchEngineIndexManager();
119: acquireCache("a1");
120: acquireCache("a2");
121: assertTrue(indexManager.isCached());
122: assertTrue(indexManager.isCached("a1"));
123: assertTrue(indexManager.isCached("a2"));
124: Thread.sleep(3000);
125: indexManager.notifyAllToClearCache();
126: for (int i = 0; i < 20; i++) {
127: Thread.sleep(500);
128: if (!indexManager.isCached()) {
129: break;
130: }
131: }
132: assertFalse(indexManager.isCached());
133: assertFalse(indexManager.isCached("a1"));
134: assertFalse(indexManager.isCached("a2"));
135: }
136:
137: private void acquireCache(String alias) {
138: CompassSession session = openSession();
139: CompassTransaction tr = session.beginTransaction();
140: session.get(alias, new Long(1));
141: tr.commit();
142: session.close();
143: }
144:
145: }
|