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.property.storeindex;
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.lucene.util.LuceneHelper;
027: import org.compass.core.test.AbstractTestCase;
028:
029: /**
030: * @author kimchy
031: */
032: public class StoreIndexTests extends AbstractTestCase {
033:
034: protected String[] getMappings() {
035: return new String[] { "property/storeindex/mapping.cpm.xml" };
036: }
037:
038: public void testInternalIdWithStoreNo() {
039: CompassSession session = openSession();
040: CompassTransaction tr = session.beginTransaction();
041:
042: Long id = new Long(1);
043: A a = new A();
044: a.setId(id);
045: a.setValue("first test string");
046:
047: session.save("a2", a);
048:
049: session.loadResource("a2", id);
050:
051: a = (A) session.load("a2", id);
052: assertEquals("first test string", a.getValue());
053:
054: tr.commit();
055: session.close();
056: }
057:
058: public void testStoreYesIndexTokenized() {
059: CompassSession session = openSession();
060: CompassTransaction tr = session.beginTransaction();
061:
062: Long id = new Long(1);
063: A a = new A();
064: a.setId(id);
065: a.setValue("first test string");
066:
067: session.save("a1", a);
068:
069: a = (A) session.load("a1", id);
070: assertEquals("first test string", a.getValue());
071: Resource r = session.loadResource("a1", id);
072: assertNotNull(r.getValue("mvalue1"));
073: assertEquals(true, r.getProperty("mvalue1").isStored());
074: assertEquals(true, r.getProperty("mvalue1").isIndexed());
075: assertEquals(true, r.getProperty("mvalue1").isTokenized());
076:
077: CompassHits hits = session.find("mvalue1:test");
078: assertEquals(1, hits.getLength());
079:
080: tr.commit();
081: session.close();
082: }
083:
084: public void testStoreNoIndexTokenized() {
085: CompassSession session = openSession();
086: CompassTransaction tr = session.beginTransaction();
087:
088: Long id = new Long(1);
089: A a = new A();
090: a.setId(id);
091: a.setValue("first test string");
092:
093: session.save("a3", a);
094:
095: a = (A) session.load("a3", id);
096: assertEquals("first test string", a.getValue());
097:
098: tr.commit();
099: session.close();
100:
101: session = openSession();
102: tr = session.beginTransaction();
103:
104: Resource r = session.loadResource("a3", id);
105: assertNull(r.getValue("mvalue1"));
106:
107: CompassHits hits = session.find("mvalue1:test");
108: assertEquals(1, hits.getLength());
109:
110: tr.commit();
111: session.close();
112: }
113:
114: public void testStoreYesIndexUnTokenized() {
115: CompassSession session = openSession();
116: CompassTransaction tr = session.beginTransaction();
117:
118: Long id = new Long(1);
119: A a = new A();
120: a.setId(id);
121: a.setValue("first test string");
122:
123: session.save("a4", a);
124:
125: a = (A) session.load("a4", id);
126: assertEquals("first test string", a.getValue());
127: Resource r = session.loadResource("a4", id);
128: assertNotNull(r.getValue("mvalue1"));
129: assertEquals(true, r.getProperty("mvalue1").isStored());
130: assertEquals(false, r.getProperty("mvalue1").isCompressed());
131: assertEquals(true, r.getProperty("mvalue1").isIndexed());
132: assertEquals(false, r.getProperty("mvalue1").isTokenized());
133:
134: CompassHits hits = session.find("mvalue1:test");
135: assertEquals(0, hits.getLength());
136:
137: tr.commit();
138: session.close();
139: }
140:
141: public void testStoreComressIndexTokenized() {
142: CompassSession session = openSession();
143: CompassTransaction tr = session.beginTransaction();
144:
145: Long id = new Long(1);
146: A a = new A();
147: a.setId(id);
148: a.setValue("first test string");
149:
150: session.save("a5", a);
151:
152: a = (A) session.load("a5", id);
153: assertEquals("first test string", a.getValue());
154: Resource r = session.loadResource("a5", id);
155: assertNotNull(r.getValue("mvalue1"));
156: assertEquals(true, r.getProperty("mvalue1").isStored());
157: assertEquals(true, r.getProperty("mvalue1").isCompressed());
158: assertEquals(true, r.getProperty("mvalue1").isIndexed());
159: assertEquals(true, r.getProperty("mvalue1").isTokenized());
160:
161: CompassHits hits = session.find("mvalue1:test");
162: assertEquals(1, hits.getLength());
163:
164: tr.commit();
165: session.close();
166: }
167:
168: public void testStoreCompressIndexNo() {
169: CompassSession session = openSession();
170: CompassTransaction tr = session.beginTransaction();
171:
172: Long id = new Long(1);
173: A a = new A();
174: a.setId(id);
175: a.setValue("first test string");
176:
177: session.save("a6", a);
178:
179: a = (A) session.load("a6", id);
180: assertEquals("first test string", a.getValue());
181: Resource r = session.loadResource("a6", id);
182: assertNotNull(r.getValue("mvalue1"));
183: assertEquals(true, r.getProperty("mvalue1").isStored());
184: assertEquals(false, r.getProperty("mvalue1").isCompressed());
185: assertEquals(false, r.getProperty("mvalue1").isIndexed());
186: assertEquals(false, r.getProperty("mvalue1").isTokenized());
187:
188: CompassHits hits = session.find("mavlue1:test");
189: assertEquals(0, hits.getLength());
190:
191: tr.commit();
192: session.close();
193: }
194:
195: public void testTermVectorNo() {
196: CompassSession session = openSession();
197: CompassTransaction tr = session.beginTransaction();
198:
199: Long id = new Long(1);
200: A a = new A();
201: a.setId(id);
202: a.setValue("first test string");
203:
204: session.save("a7", a);
205:
206: a = (A) session.load("a7", id);
207: assertEquals("first test string", a.getValue());
208: Resource r = session.loadResource("a7", id);
209: assertNotNull(r.getValue("mvalue1"));
210: assertEquals(true, r.getProperty("mvalue1").isStored());
211: assertEquals(true, r.getProperty("mvalue1").isIndexed());
212: assertEquals(false, r.getProperty("mvalue1")
213: .isTermVectorStored());
214:
215: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
216: session, r, "mvalue1");
217: assertNull(termInfoVector);
218:
219: TermFreqVector[] termInfoVectors = LuceneHelper
220: .getTermFreqVectors(session, r);
221: assertNull(termInfoVectors);
222:
223: tr.commit();
224: session.close();
225: }
226:
227: public void testTermVectorYes() {
228: CompassSession session = openSession();
229: CompassTransaction tr = session.beginTransaction();
230:
231: Long id = new Long(1);
232: A a = new A();
233: a.setId(id);
234: a.setValue("first test string");
235:
236: session.save("a8", a);
237:
238: a = (A) session.load("a8", id);
239: assertEquals("first test string", a.getValue());
240: Resource r = session.loadResource("a8", id);
241: assertNotNull(r.getValue("mvalue1"));
242: assertEquals(true, r.getProperty("mvalue1").isStored());
243: assertEquals(true, r.getProperty("mvalue1").isIndexed());
244: assertEquals(true, r.getProperty("mvalue1")
245: .isTermVectorStored());
246:
247: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
248: session, r, "mvalue1");
249: assertEquals("mvalue1", termInfoVector.getField());
250: String[] terms = termInfoVector.getTerms();
251: assertEquals(3, terms.length);
252: assertEquals("first", terms[0]);
253: assertEquals("string", terms[1]);
254: assertEquals("test", terms[2]);
255:
256: int[] freqs = termInfoVector.getTermFrequencies();
257: assertEquals(3, freqs.length);
258:
259: try {
260: ((TermPositionVector) termInfoVector).getTermPositions(0);
261: fail();
262: } catch (Exception e) {
263:
264: }
265:
266: try {
267: ((TermPositionVector) termInfoVector).getOffsets(0);
268: fail();
269: } catch (Exception e) {
270:
271: }
272:
273: TermFreqVector[] termInfoVectors = LuceneHelper
274: .getTermFreqVectors(session, r);
275: assertEquals(1, termInfoVectors.length);
276:
277: tr.commit();
278: session.close();
279: }
280:
281: public void testTermVectorYesWithPositions() {
282: CompassSession session = openSession();
283: CompassTransaction tr = session.beginTransaction();
284:
285: Long id = new Long(1);
286: A a = new A();
287: a.setId(id);
288: a.setValue("first test string");
289:
290: session.save("a9", a);
291:
292: a = (A) session.load("a9", id);
293: assertEquals("first test string", a.getValue());
294: Resource r = session.loadResource("a9", id);
295: assertNotNull(r.getValue("mvalue1"));
296: assertEquals(true, r.getProperty("mvalue1").isStored());
297: assertEquals(true, r.getProperty("mvalue1").isIndexed());
298: assertEquals(true, r.getProperty("mvalue1")
299: .isTermVectorStored());
300:
301: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
302: session, r, "mvalue1");
303: assertEquals("mvalue1", termInfoVector.getField());
304: String[] terms = termInfoVector.getTerms();
305: assertEquals(3, terms.length);
306: assertEquals("first", terms[0]);
307: assertEquals("string", terms[1]);
308: assertEquals("test", terms[2]);
309:
310: int[] freqs = termInfoVector.getTermFrequencies();
311: assertEquals(3, freqs.length);
312:
313: int[] positions = ((TermPositionVector) termInfoVector)
314: .getTermPositions(0);
315: assertNotNull(positions);
316: assertEquals(1, positions.length);
317:
318: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
319: .getOffsets(0);
320: assertNull(offsets);
321:
322: TermFreqVector[] termInfoVectors = LuceneHelper
323: .getTermFreqVectors(session, r);
324: assertEquals(1, termInfoVectors.length);
325:
326: tr.commit();
327: session.close();
328: }
329:
330: public void testTermVectorYesWithOffsets() {
331: CompassSession session = openSession();
332: CompassTransaction tr = session.beginTransaction();
333:
334: Long id = new Long(1);
335: A a = new A();
336: a.setId(id);
337: a.setValue("first test string");
338:
339: session.save("a10", a);
340:
341: a = (A) session.load("a10", id);
342: assertEquals("first test string", a.getValue());
343: Resource r = session.loadResource("a10", id);
344: assertNotNull(r.getValue("mvalue1"));
345: assertEquals(true, r.getProperty("mvalue1").isStored());
346: assertEquals(true, r.getProperty("mvalue1").isIndexed());
347: assertEquals(true, r.getProperty("mvalue1")
348: .isTermVectorStored());
349:
350: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
351: session, r, "mvalue1");
352: assertEquals("mvalue1", termInfoVector.getField());
353: String[] terms = termInfoVector.getTerms();
354: assertEquals(3, terms.length);
355: assertEquals("first", terms[0]);
356: assertEquals("string", terms[1]);
357: assertEquals("test", terms[2]);
358:
359: int[] freqs = termInfoVector.getTermFrequencies();
360: assertEquals(3, freqs.length);
361:
362: int[] positions = ((TermPositionVector) termInfoVector)
363: .getTermPositions(0);
364: assertNull(positions);
365:
366: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
367: .getOffsets(0);
368: assertNotNull(offsets);
369: assertEquals(1, offsets.length);
370:
371: TermFreqVector[] termInfoVectors = LuceneHelper
372: .getTermFreqVectors(session, r);
373: assertEquals(1, termInfoVectors.length);
374:
375: tr.commit();
376: session.close();
377: }
378:
379: public void testTermVectorYesWithPostionsAndOffsets() {
380: CompassSession session = openSession();
381: CompassTransaction tr = session.beginTransaction();
382:
383: Long id = new Long(1);
384: A a = new A();
385: a.setId(id);
386: a.setValue("first test string");
387:
388: session.save("a11", a);
389:
390: verifyTermVectorYesWithPostionsAndOffsets(session);
391:
392: tr.commit();
393: session.close();
394:
395: // now test with non transactional data
396: session = openSession();
397: tr = session.beginTransaction();
398: verifyTermVectorYesWithPostionsAndOffsets(session);
399: tr.commit();
400: session.close();
401: }
402:
403: private void verifyTermVectorYesWithPostionsAndOffsets(
404: CompassSession session) {
405: Long id = new Long(1);
406: A a = (A) session.load("a11", id);
407: assertEquals("first test string", a.getValue());
408: Resource r = session.loadResource("a11", id);
409: assertNotNull(r.getValue("mvalue1"));
410: assertEquals(true, r.getProperty("mvalue1").isStored());
411: assertEquals(true, r.getProperty("mvalue1").isIndexed());
412: assertEquals(true, r.getProperty("mvalue1")
413: .isTermVectorStored());
414:
415: TermFreqVector termInfoVector = LuceneHelper.getTermFreqVector(
416: session, r, "mvalue1");
417: assertEquals("mvalue1", termInfoVector.getField());
418: String[] terms = termInfoVector.getTerms();
419: assertEquals(3, terms.length);
420: assertEquals("first", terms[0]);
421: assertEquals("string", terms[1]);
422: assertEquals("test", terms[2]);
423:
424: int[] freqs = termInfoVector.getTermFrequencies();
425: assertEquals(3, freqs.length);
426:
427: int[] positions = ((TermPositionVector) termInfoVector)
428: .getTermPositions(0);
429: assertNotNull(positions);
430: assertEquals(1, positions.length);
431:
432: TermVectorOffsetInfo[] offsets = ((TermPositionVector) termInfoVector)
433: .getOffsets(0);
434: assertNotNull(offsets);
435: assertEquals(1, offsets.length);
436:
437: TermFreqVector[] termInfoVectors = LuceneHelper
438: .getTermFreqVectors(session, r);
439: assertEquals(1, termInfoVectors.length);
440: }
441:
442: public void testStoreNoIndexNotSpecifiedTokenized() {
443: CompassSession session = openSession();
444: CompassTransaction tr = session.beginTransaction();
445:
446: CompassHits hits = session.find("mvalue1:test");
447: assertEquals(0, hits.getLength());
448:
449: Long id = new Long(1);
450: A a = new A();
451: a.setId(id);
452: a.setValue("first test string");
453:
454: session.save("a12", a);
455:
456: a = (A) session.load("a12", id);
457: assertEquals("first test string", a.getValue());
458:
459: tr.commit();
460: session.close();
461:
462: session = openSession();
463: tr = session.beginTransaction();
464:
465: Resource r = session.loadResource("a12", id);
466: assertNull(r.getValue("mvalue1"));
467:
468: hits = session.find("mvalue1:test");
469: assertEquals(1, hits.getLength());
470:
471: tr.commit();
472: session.close();
473: }
474:
475: public void testStoreNoManagedIdFalse() {
476: CompassSession session = openSession();
477: CompassTransaction tr = session.beginTransaction();
478:
479: CompassHits hits = session.find("mvalue1:test");
480: assertEquals(0, hits.getLength());
481:
482: Long id = new Long(1);
483: A a = new A();
484: a.setId(id);
485: a.setValue("first test string");
486:
487: session.save("a13", a);
488:
489: a = (A) session.load("a13", id);
490: assertNull(a.getValue());
491:
492: tr.commit();
493: session.close();
494: }
495: }
|