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.all;
018:
019: import org.apache.lucene.index.TermFreqVector;
020: import org.apache.lucene.index.TermPositionVector;
021: import org.apache.lucene.index.TermVectorOffsetInfo;
022: import org.compass.core.CompassHits;
023: import org.compass.core.CompassSession;
024: import org.compass.core.CompassTransaction;
025: import org.compass.core.Resource;
026: import org.compass.core.config.CompassEnvironment;
027: import org.compass.core.config.CompassSettings;
028: import org.compass.core.lucene.util.LuceneHelper;
029: import org.compass.core.test.AbstractTestCase;
030:
031: /**
032: * @author kimchy
033: */
034: public class AllTests extends AbstractTestCase {
035:
036: protected String[] getMappings() {
037: return new String[] { "all/All.cpm.xml" };
038: }
039:
040: protected void addSettings(CompassSettings settings) {
041: settings.setBooleanSetting(
042: CompassEnvironment.All.EXCLUDE_ALIAS, false);
043: }
044:
045: public void testAll() {
046: CompassSession session = openSession();
047: CompassTransaction tr = session.beginTransaction();
048:
049: Long id = (long) 1;
050: A a = new A();
051: a.setId(id);
052: a.setValue1("test1");
053: a.setValue2("test2");
054: session.save("a1", a);
055:
056: CompassHits result = session.find("test1");
057: assertEquals(1, result.getLength());
058: a = (A) result.data(0);
059: assertEquals("test1", a.getValue1());
060: assertEquals("test2", a.getValue2());
061:
062: // verify that we can find the alias in the all property as well
063: result = session.find("a1");
064: assertEquals(1, result.getLength());
065:
066: result = session.find("test2");
067: assertEquals(1, result.getLength());
068: a = (A) result.data(0);
069: assertEquals("test1", a.getValue1());
070: assertEquals("test2", a.getValue2());
071:
072: result = session.find(CompassEnvironment.All.DEFAULT_NAME
073: + ":test2");
074: assertEquals(1, result.getLength());
075: a = (A) result.data(0);
076: assertEquals("test1", a.getValue1());
077: assertEquals("test2", a.getValue2());
078:
079: tr.commit();
080: }
081:
082: public void testAllWithTermVectorOnProperties() {
083: CompassSession session = openSession();
084: CompassTransaction tr = session.beginTransaction();
085:
086: Long id = (long) 1;
087: A a = new A();
088: a.setId(id);
089: a.setValue1("test1");
090: a.setValue2("test2");
091: session.save("a10", a);
092:
093: CompassHits result = session.find("test1");
094: assertEquals(1, result.getLength());
095: a = (A) result.data(0);
096: assertEquals("test1", a.getValue1());
097: assertEquals("test2", a.getValue2());
098:
099: // verify that we can find the alias in the all property as well
100: result = session.find("a10");
101: assertEquals(1, result.getLength());
102:
103: result = session.find("test2");
104: assertEquals(1, result.getLength());
105: a = (A) result.data(0);
106: assertEquals("test1", a.getValue1());
107: assertEquals("test2", a.getValue2());
108:
109: result = session.find(CompassEnvironment.All.DEFAULT_NAME
110: + ":test2");
111: assertEquals(1, result.getLength());
112: a = (A) result.data(0);
113: assertEquals("test1", a.getValue1());
114: assertEquals("test2", a.getValue2());
115:
116: tr.commit();
117: }
118:
119: public void testAllWithDifferentAllMetaData() {
120: CompassSession session = openSession();
121: CompassTransaction tr = session.beginTransaction();
122:
123: Long id = new Long(1);
124: A a = new A();
125: a.setId(id);
126: a.setValue1("test1");
127: a.setValue2("test2");
128: session.save("a2", a);
129:
130: CompassHits result = session.find("everything:test1");
131: assertEquals(1, result.getLength());
132: a = (A) result.data(0);
133: assertEquals("test1", a.getValue1());
134: assertEquals("test2", a.getValue2());
135:
136: result = session.find("test2");
137: assertEquals(0, result.getLength());
138:
139: result = session.find("everything:test2");
140: assertEquals(1, result.getLength());
141: a = (A) result.data(0);
142: assertEquals("test1", a.getValue1());
143: assertEquals("test2", a.getValue2());
144:
145: result = session.find(CompassEnvironment.All.DEFAULT_NAME
146: + ":test2");
147: assertEquals(0, result.getLength());
148:
149: tr.commit();
150: }
151:
152: public void testAllNoAll() {
153: CompassSession session = openSession();
154: CompassTransaction tr = session.beginTransaction();
155:
156: Long id = new Long(1);
157: A a = new A();
158: a.setId(id);
159: a.setValue1("test1");
160: a.setValue2("test2");
161: session.save("a3", a);
162:
163: CompassHits result = session.find("test2");
164: assertEquals(0, result.getLength());
165:
166: result = session.find(CompassEnvironment.All.DEFAULT_NAME
167: + ":test2");
168: assertEquals(0, result.getLength());
169:
170: tr.commit();
171: }
172:
173: public void testExcludeAll() {
174: CompassSession session = openSession();
175: CompassTransaction tr = session.beginTransaction();
176:
177: Long id = new Long(1);
178: A a = new A();
179: a.setId(id);
180: a.setValue1("test1111");
181: a.setValue2("test2222");
182: session.save("a4", a);
183:
184: CompassHits result = session.find("test1111");
185: assertEquals(0, result.getLength());
186:
187: result = session.find("test2222");
188: assertEquals(1, result.getLength());
189:
190: result = session.find("cValue11");
191: assertEquals(0, result.getLength());
192:
193: result = session.find("cValue21");
194: assertEquals(1, result.getLength());
195:
196: tr.commit();
197: session.close();
198: }
199:
200: public void testAllWithTermVector() {
201: CompassSession session = openSession();
202: CompassTransaction tr = session.beginTransaction();
203:
204: Long id = new Long(1);
205: A a = new A();
206: a.setId(id);
207: a.setValue1("test1");
208: a.setValue2("test2");
209: session.save("a5", a);
210:
211: Resource r = session.loadResource("a5", id);
212: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
213: session, r, CompassEnvironment.All.DEFAULT_NAME);
214: assertNotNull(termInfoVector);
215: try {
216: int[] positions = ((TermPositionVector) termInfoVector)
217: .getTermPositions(0);
218: fail();
219: } catch (Exception e) {
220:
221: }
222:
223: try {
224: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
225: .getOffsets(0);
226: fail();
227: } catch (Exception e) {
228:
229: }
230:
231: tr.commit();
232: session.close();
233: }
234:
235: public void testAllWithTermVectorPositions() {
236: CompassSession session = openSession();
237: CompassTransaction tr = session.beginTransaction();
238:
239: Long id = new Long(1);
240: A a = new A();
241: a.setId(id);
242: a.setValue1("test1");
243: a.setValue2("test2");
244: session.save("a6", a);
245:
246: Resource r = session.loadResource("a6", id);
247: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
248: session, r, CompassEnvironment.All.DEFAULT_NAME);
249: assertNotNull(termInfoVector);
250: int[] positions = ((TermPositionVector) termInfoVector)
251: .getTermPositions(0);
252: assertNotNull(positions);
253:
254: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
255: .getOffsets(0);
256: assertNull(offsets);
257:
258: tr.commit();
259: session.close();
260: }
261:
262: public void testAllWithTermVectorOffsets() {
263: CompassSession session = openSession();
264: CompassTransaction tr = session.beginTransaction();
265:
266: Long id = new Long(1);
267: A a = new A();
268: a.setId(id);
269: a.setValue1("test1");
270: a.setValue2("test2");
271: session.save("a7", a);
272:
273: Resource r = session.loadResource("a7", id);
274: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
275: session, r, CompassEnvironment.All.DEFAULT_NAME);
276: assertNotNull(termInfoVector);
277: int[] positions = ((TermPositionVector) termInfoVector)
278: .getTermPositions(0);
279: assertNull(positions);
280:
281: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
282: .getOffsets(0);
283: assertNotNull(offsets);
284:
285: tr.commit();
286: session.close();
287: }
288:
289: public void testAllWithTermVectorPositionsAndOffsets() {
290: CompassSession session = openSession();
291: CompassTransaction tr = session.beginTransaction();
292:
293: Long id = new Long(1);
294: A a = new A();
295: a.setId(id);
296: a.setValue1("test1");
297: a.setValue2("test2");
298: session.save("a8", a);
299:
300: Resource r = session.loadResource("a8", id);
301: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
302: session, r, CompassEnvironment.All.DEFAULT_NAME);
303: assertNotNull(termInfoVector);
304: int[] positions = ((TermPositionVector) termInfoVector)
305: .getTermPositions(0);
306: assertNotNull(positions);
307:
308: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
309: .getOffsets(0);
310: assertNotNull(offsets);
311:
312: tr.commit();
313: session.close();
314: }
315:
316: public void testExcludeAliasFromAll() {
317: CompassSession session = openSession();
318: CompassTransaction tr = session.beginTransaction();
319:
320: Long id = new Long(1);
321: A a = new A();
322: a.setId(id);
323: a.setValue1("test1");
324: a.setValue2("test2");
325: session.save("a9", a);
326:
327: CompassHits hits = session.find("a9");
328: assertEquals(0, hits.length());
329:
330: tr.commit();
331: session.close();
332: }
333: }
|