001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.search;
019:
020: import org.apache.lucene.index.Term;
021: import org.apache.lucene.search.BooleanClause;
022: import org.apache.lucene.search.BooleanQuery;
023: import org.apache.lucene.search.Query;
024: import org.apache.lucene.search.TermQuery;
025: import org.apache.lucene.search.WildcardQuery;
026:
027: import de.finix.contelligent.CallData;
028: import de.finix.contelligent.Component;
029: import de.finix.contelligent.ComponentPath;
030: import de.finix.contelligent.Container;
031: import de.finix.contelligent.Session;
032: import de.finix.contelligent.core.BasicComponentManager;
033: import de.finix.contelligent.core.ContelligentImpl;
034: import de.finix.contelligent.core.TimeService;
035: import de.finix.contelligent.resource.TextResource;
036: import de.finix.contelligent.search.system.SystemIndex;
037: import de.finix.contelligent.test.ContelligentTestCase;
038:
039: public class SystemIndexTests extends ContelligentTestCase {
040:
041: final static long WF_ID = -2009L;
042:
043: final static String RESOURCE_KEY = "test:xx:yy";
044:
045: final static String RESOURCE_KEY2 = "test2:xx:yy";
046:
047: final static String TEXT_TYPE = "contelligent.content.Text";
048:
049: ComponentPath testFolderPath = TEST_WORK_DIR
050: .append("_test_folder_");
051:
052: ComponentPath testFolder2Path = testFolderPath
053: .append("_test_folder2_");
054:
055: ComponentPath testSubPath = testFolderPath.append("_test_test_");
056:
057: ComponentPath testSubSubPath = testFolder2Path
058: .append("_test_test_");
059:
060: Session session;
061:
062: CallData callData;
063:
064: BasicComponentManager root;
065:
066: public SystemIndexTests(String s) {
067: super (s);
068: }
069:
070: public void testSearch() throws Exception {
071: SystemIndex index = ((BasicComponentManager) ContelligentImpl
072: .getInstance().getRootComponentManager())
073: .getSystemIndex();
074: SearchResult hits = index.search("akiiliki");
075: assertEquals(4, hits.getLength());
076: }
077:
078: public void testSearchCustomQuery() throws Exception {
079: SystemIndex index = ((BasicComponentManager) ContelligentImpl
080: .getInstance().getRootComponentManager())
081: .getSystemIndex();
082: Query query = new WildcardQuery(new Term("path",
083: "/contelligent/website/*"));
084: SearchResult hits = index.search(query);
085: assertEquals(25, hits.getLength());
086: query = new TermQuery(new Term("path",
087: "/contelligent/test/pages/search"));
088: hits = index.search(query);
089: assertEquals(1, hits.getLength());
090: query = new WildcardQuery(new Term("path",
091: "/contelligent/test/error-pages/*"));
092: hits = index.search(query);
093: assertEquals(6, hits.getLength());
094: BooleanQuery bquery = new BooleanQuery();
095: // bquery.add(new BooleanClause(new TermQuery(new Term("type",
096: // "contelligent.content.Text")), true, false));
097: bquery.add(new BooleanClause(new WildcardQuery(new Term("path",
098: "/contelligent/test/error-pages/*")), true, false));
099: hits = index.search(bquery);
100: assertEquals(6, hits.getLength());
101: }
102:
103: public void testCreateComponentInEdit() throws Throwable {
104: BasicComponentManager edit = null;
105: ComponentPath testPath = TEST_WORK_DIR.append("_test_test_");
106: session = beginSession("admin", "system-users", "admin");
107: callData = createCallData(session);
108: root = (BasicComponentManager) getContelligent()
109: .getRootComponentManager();
110:
111: try {
112: beginTx(callData);
113: try {
114: getContelligent().getComponentManagerHierarchy()
115: .getPersistenceAdapter().getAllCMConfigs()
116: .size();
117:
118: edit = (BasicComponentManager) getContelligent()
119: .getComponentManagerHierarchy().spawnChild(
120: root, "testEdit", getSystemUser());
121: commitTx();
122: } catch (Exception e) {
123: rollbackTx();
124: throw e;
125: }
126: beginTx(callData);
127: try {
128: changeCM(callData, edit);
129:
130: edit.lockComponent(testPath, callData);
131: Container work = (Container) edit.getComponent(
132: TEST_WORK_DIR, callData);
133: Component comp = edit.createComponent(work, testPath
134: .getName(), TEXT_TYPE, null, null, callData);
135: comp.getComponentContext().setContentResource(
136: "",
137: new TextResource("skk skdi", TimeService
138: .getInstance().currentTimeMillis()));
139: edit.updateComponent(comp, callData);
140: assertTrue("Component not created", edit
141: .componentExists(testPath));
142: assertTrue("Component available in root", !root
143: .componentExists(testPath));
144: commitTx();
145: } catch (Throwable e) {
146: rollbackTx();
147: throw e;
148: }
149: // give the system index some time to update
150: Thread.sleep(2000);
151: // and now search
152: SearchResult hits = edit.getSystemIndex().search("skk");
153: assertEquals(1, hits.getLength());
154: hits = root.getSystemIndex().search("skk");
155: assertEquals(0, hits.getLength());
156: beginTx(callData);
157: try {
158: edit.commitServer(callData);
159: commitTx();
160: } catch (Exception e) {
161: rollbackTx();
162: throw e;
163: }
164: beginTx(callData);
165: try {
166: assertTrue("Component not created", root
167: .componentExists(testPath));
168: commitTx();
169: } catch (Throwable e) {
170: rollbackTx();
171: throw e;
172: }
173: // give the system index some time to update
174: Thread.sleep(2000);
175: hits = edit.getSystemIndex().search("skk");
176: assertEquals(1, hits.getLength());
177: hits = root.getSystemIndex().search("skk");
178: assertEquals(1, hits.getLength());
179: beginTx(callData);
180: try {
181: edit.deleteComponentTree(callData, testPath);
182: commitTx();
183: } catch (Throwable e) {
184: rollbackTx();
185: throw e;
186: }
187: // give the system index some time to update
188: Thread.sleep(2000);
189: // and now search
190: hits = edit.getSystemIndex().search("skk");
191: assertEquals(0, hits.getLength());
192: hits = root.getSystemIndex().search("skk");
193: assertEquals(1, hits.getLength());
194: } finally {
195: if (edit != null) {
196: beginTx(callData);
197: getContelligent().getComponentManagerHierarchy()
198: .destroyComponentManager(edit.getName(), true);
199: commitTx();
200: beginTx(callData);
201: try {
202: changeCM(callData, root);
203: root.lockComponent(TEST_ROOT_DIR, callData);
204: root.deleteComponentTree(callData, testPath);
205: root.unlockComponent(TEST_ROOT_DIR, callData);
206: commitTx();
207: } catch (Exception e) {
208: rollbackTx();
209: throw e;
210: }
211: }
212: endSession(session);
213: }
214: }
215:
216: // public void testIndex() throws IOException {
217: // LuceneIndexIterator iterator = new
218: // LuceneIndexIterator(IndexReader.open(ContelligentImpl.getInstance().getContelligentDir(Contelligent.DIR_INDEX
219: // + "system.Production")));
220: // int i = 0;
221: //
222: // while (iterator.hasNext()) {
223: // Document document = (Document)iterator.next();
224: // System.out.println(document.getField("path"));
225: // i++;
226: // }
227: // System.out.println(i);
228: // iterator.close();
229: // }
230: }
|