1 package org.apache.lucene.facet.enhancements;
3 import java.util.ArrayList;
4 import java.util.Arrays;
7 import org.apache.lucene.analysis.MockAnalyzer;
8 import org.apache.lucene.analysis.MockTokenizer;
9 import org.apache.lucene.document.Document;
10 import org.apache.lucene.index.IndexReader;
11 import org.apache.lucene.index.RandomIndexWriter;
12 import org.apache.lucene.index.Term;
13 import org.apache.lucene.store.Directory;
14 import org.junit.Test;
16 import org.apache.lucene.util.LuceneTestCase;
17 import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
18 import org.apache.lucene.facet.enhancements.EnhancementsPayloadIterator;
19 import org.apache.lucene.facet.enhancements.params.DefaultEnhancementsIndexingParams;
20 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
21 import org.apache.lucene.facet.search.DrillDown;
22 import org.apache.lucene.facet.taxonomy.CategoryPath;
23 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
24 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
27 * Licensed to the Apache Software Foundation (ASF) under one or more
28 * contributor license agreements. See the NOTICE file distributed with
29 * this work for additional information regarding copyright ownership.
30 * The ASF licenses this file to You under the Apache License, Version 2.0
31 * (the "License"); you may not use this file except in compliance with
32 * the License. You may obtain a copy of the License at
34 * http://www.apache.org/licenses/LICENSE-2.0
36 * Unless required by applicable law or agreed to in writing, software
37 * distributed under the License is distributed on an "AS IS" BASIS,
38 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
39 * See the License for the specific language governing permissions and
40 * limitations under the License.
43 public class TwoEnhancementsTest extends LuceneTestCase {
46 public void testTwoEmptyAndNonEmptyByteArrays() throws Exception {
47 Directory indexDir = newDirectory();
48 Directory taxoDir = newDirectory();
50 EnhancementsIndexingParams indexingParams =
51 new DefaultEnhancementsIndexingParams(
52 new CategoryEnhancementDummy1(),
53 new CategoryEnhancementDummy3());
55 // add document with a category containing data for both enhancements
56 List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>();
57 categoryPaths.add(new CategoryPath("a", "b"));
59 RandomIndexWriter indexWriter = new RandomIndexWriter(random, indexDir, newIndexWriterConfig(
60 TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
61 TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir);
63 // a category document builder will add the categories to a document
64 // once build() is called
65 Document doc = new Document();
66 indexWriter.addDocument(new EnhancementsDocumentBuilder(taxo,
67 indexingParams).setCategoryPaths(categoryPaths).build(doc));
69 IndexReader indexReader = indexWriter.getReader();
72 Term term = DrillDown.term(indexingParams, new CategoryPath("a","b"));
73 EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
74 indexingParams.getCategoryEnhancements(), indexReader, term);
76 assertTrue("EnhancementsPayloadIterator failure", iterator.init());
77 assertTrue("Missing document 0", iterator.setdoc(0));
78 assertNull("Unexpected data for CategoryEnhancementDummy2", iterator
79 .getCategoryData(new CategoryEnhancementDummy1()));
80 byte[] dummy3 = (byte[]) iterator
81 .getCategoryData(new CategoryEnhancementDummy3());
82 assertTrue("Bad array returned for CategoryEnhancementDummy3", Arrays
83 .equals(dummy3, CategoryEnhancementDummy3.CATEGORY_TOKEN_BYTES));
91 public void testTwoNonEmptyByteArrays() throws Exception {
92 // add document with a category containing data for both enhancements
93 Directory indexDir = newDirectory();
94 Directory taxoDir = newDirectory();
96 EnhancementsIndexingParams indexingParams =
97 new DefaultEnhancementsIndexingParams(
98 new CategoryEnhancementDummy2(),
99 new CategoryEnhancementDummy3());
101 List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>();
102 categoryPaths.add(new CategoryPath("a", "b"));
104 RandomIndexWriter indexWriter = new RandomIndexWriter(random, indexDir, newIndexWriterConfig(
105 TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
106 TaxonomyWriter taxo = new LuceneTaxonomyWriter(taxoDir);
108 // a category document builder will add the categories to a document
109 // once build() is called
110 Document doc = new Document();
111 indexWriter.addDocument(new EnhancementsDocumentBuilder(taxo,
112 indexingParams).setCategoryPaths(categoryPaths).build(doc));
114 IndexReader indexReader = indexWriter.getReader();
117 Term term = DrillDown.term(indexingParams, new CategoryPath("a","b"));
118 EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
119 indexingParams.getCategoryEnhancements(), indexReader, term);
121 assertTrue("EnhancementsPayloadIterator failure", iterator.init());
122 assertTrue("Missing document 0", iterator.setdoc(0));
123 byte[] dummy2 = (byte[]) iterator
124 .getCategoryData(new CategoryEnhancementDummy2());
125 assertTrue("Bad array returned for CategoryEnhancementDummy2", Arrays
126 .equals(dummy2, CategoryEnhancementDummy2.CATEGORY_TOKEN_BYTES));
127 byte[] dummy3 = (byte[]) iterator
128 .getCategoryData(new CategoryEnhancementDummy3());
129 assertTrue("Bad array returned for CategoryEnhancementDummy3", Arrays
130 .equals(dummy3, CategoryEnhancementDummy3.CATEGORY_TOKEN_BYTES));