~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to lucene/contrib/facet/src/test/org/apache/lucene/facet/enhancements/TwoEnhancementsTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.lucene.facet.enhancements;
2
 
 
3
 
import java.util.ArrayList;
4
 
import java.util.Arrays;
5
 
import java.util.List;
6
 
 
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;
15
 
 
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.directory.DirectoryTaxonomyWriter;
25
 
 
26
 
/**
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
33
 
 *
34
 
 *     http://www.apache.org/licenses/LICENSE-2.0
35
 
 *
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.
41
 
 */
42
 
 
43
 
public class TwoEnhancementsTest extends LuceneTestCase {
44
 
 
45
 
  @Test
46
 
  public void testTwoEmptyAndNonEmptyByteArrays() throws Exception {
47
 
    Directory indexDir = newDirectory();
48
 
    Directory taxoDir = newDirectory();
49
 
 
50
 
    EnhancementsIndexingParams indexingParams = 
51
 
      new DefaultEnhancementsIndexingParams(
52
 
          new CategoryEnhancementDummy1(),
53
 
          new CategoryEnhancementDummy3());
54
 
 
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"));
58
 
 
59
 
    RandomIndexWriter indexWriter = new RandomIndexWriter(random, indexDir, newIndexWriterConfig(
60
 
        TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
61
 
    TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir);
62
 
 
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));
68
 
 
69
 
    IndexReader indexReader = indexWriter.getReader();
70
 
    indexWriter.close();
71
 
 
72
 
    Term term = DrillDown.term(indexingParams, new CategoryPath("a","b"));
73
 
    EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
74
 
        indexingParams.getCategoryEnhancements(), indexReader, term);
75
 
 
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));
84
 
    indexReader.close();
85
 
    indexDir.close();
86
 
    taxo.close();
87
 
    taxoDir.close();
88
 
  }
89
 
 
90
 
  @Test
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();
95
 
 
96
 
    EnhancementsIndexingParams indexingParams = 
97
 
      new DefaultEnhancementsIndexingParams(
98
 
          new CategoryEnhancementDummy2(),
99
 
          new CategoryEnhancementDummy3());
100
 
 
101
 
    List<CategoryPath> categoryPaths = new ArrayList<CategoryPath>();
102
 
    categoryPaths.add(new CategoryPath("a", "b"));
103
 
 
104
 
    RandomIndexWriter indexWriter = new RandomIndexWriter(random, indexDir, newIndexWriterConfig(
105
 
        TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.WHITESPACE, false)));
106
 
    TaxonomyWriter taxo = new DirectoryTaxonomyWriter(taxoDir);
107
 
 
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));
113
 
 
114
 
    IndexReader indexReader = indexWriter.getReader();
115
 
    indexWriter.close();
116
 
 
117
 
    Term term = DrillDown.term(indexingParams, new CategoryPath("a","b"));
118
 
    EnhancementsPayloadIterator iterator = new EnhancementsPayloadIterator(
119
 
        indexingParams.getCategoryEnhancements(), indexReader, term);
120
 
 
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));
131
 
    indexReader.close();
132
 
    taxo.close();
133
 
    indexDir.close();
134
 
    taxoDir.close();
135
 
  }
136
 
}