~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/search/TestCategoryListCache.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.search;
2
 
 
3
 
import java.io.IOException;
4
 
import java.util.List;
5
 
import java.util.Map;
6
 
 
7
 
import org.apache.lucene.search.MatchAllDocsQuery;
8
 
 
9
 
import org.junit.After;
10
 
import org.junit.Before;
11
 
import org.junit.Test;
12
 
 
13
 
import org.apache.lucene.facet.FacetTestBase;
14
 
import org.apache.lucene.facet.index.params.CategoryListParams;
15
 
import org.apache.lucene.facet.index.params.FacetIndexingParams;
16
 
import org.apache.lucene.facet.search.cache.CategoryListCache;
17
 
import org.apache.lucene.facet.search.cache.CategoryListData;
18
 
import org.apache.lucene.facet.search.params.CountFacetRequest;
19
 
import org.apache.lucene.facet.search.params.FacetSearchParams;
20
 
import org.apache.lucene.facet.search.results.FacetResult;
21
 
import org.apache.lucene.facet.taxonomy.CategoryPath;
22
 
 
23
 
/**
24
 
 * Licensed to the Apache Software Foundation (ASF) under one or more
25
 
 * contributor license agreements.  See the NOTICE file distributed with
26
 
 * this work for additional information regarding copyright ownership.
27
 
 * The ASF licenses this file to You under the Apache License, Version 2.0
28
 
 * (the "License"); you may not use this file except in compliance with
29
 
 * the License.  You may obtain a copy of the License at
30
 
 *
31
 
 *     http://www.apache.org/licenses/LICENSE-2.0
32
 
 *
33
 
 * Unless required by applicable law or agreed to in writing, software
34
 
 * distributed under the License is distributed on an "AS IS" BASIS,
35
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36
 
 * See the License for the specific language governing permissions and
37
 
 * limitations under the License.
38
 
 */
39
 
 
40
 
public class TestCategoryListCache extends FacetTestBase {
41
 
 
42
 
  public TestCategoryListCache() {
43
 
    super();
44
 
  }
45
 
  
46
 
  @Before
47
 
  @Override
48
 
  public void setUp() throws Exception {
49
 
    super.setUp();
50
 
    initIndex();
51
 
  }
52
 
  
53
 
  @After
54
 
  @Override
55
 
  public void tearDown() throws Exception {
56
 
    closeAll();
57
 
    super.tearDown();
58
 
  }
59
 
  
60
 
  @Test
61
 
  public void testNoClCache() throws Exception {
62
 
    doTest(false,false);
63
 
  }
64
 
 
65
 
  @Test
66
 
  public void testCorrectClCache() throws Exception {
67
 
    doTest(true,false);
68
 
  }
69
 
  
70
 
  @Test
71
 
  public void testWrongClCache() throws Exception {
72
 
    doTest(true,true);
73
 
  }
74
 
  
75
 
  private void doTest(boolean withCache, boolean plantWrongData) throws IOException, Exception {
76
 
    Map<CategoryPath,Integer> truth = facetCountsTruth();
77
 
    CategoryPath cp = (CategoryPath) truth.keySet().toArray()[0]; // any category path will do for this test 
78
 
    CountFacetRequest frq = new CountFacetRequest(cp, 10);
79
 
    FacetSearchParams sParams = getFacetedSearchParams();
80
 
    sParams.addFacetRequest(frq);
81
 
    if (withCache) {
82
 
      //let's use a cached cl data
83
 
      FacetIndexingParams iparams = sParams.getFacetIndexingParams();
84
 
      CategoryListParams clp = new CategoryListParams(); // default term ok as only single list
85
 
      CategoryListCache clCache = new CategoryListCache();
86
 
      clCache.loadAndRegister(clp, indexReader, taxoReader, iparams);
87
 
      if (plantWrongData) {
88
 
        // let's mess up the cached data and then expect a wrong result...
89
 
        messCachedData(clCache, clp);
90
 
      }
91
 
      sParams.setClCache(clCache);
92
 
    }
93
 
    FacetsCollector fc = new FacetsCollector(sParams, indexReader, taxoReader);
94
 
    searcher.search(new MatchAllDocsQuery(), fc);
95
 
    List<FacetResult> res = fc.getFacetResults();
96
 
    try {
97
 
      assertCountsAndCardinality(truth, res);
98
 
      assertFalse("Correct results not expected when wrong data was cached", plantWrongData);
99
 
    } catch (Throwable e) {
100
 
      assertTrue("Wrong results not expected unless wrong data was cached", withCache);
101
 
      assertTrue("Wrong results not expected unless wrong data was cached", plantWrongData);
102
 
    }
103
 
  }
104
 
 
105
 
  /** Mess the cached data for this {@link CategoryListParams} */
106
 
  private void messCachedData(CategoryListCache clCache, CategoryListParams clp) {
107
 
    final CategoryListData cld = clCache.get(clp);
108
 
    CategoryListData badCld = new CategoryListData() {
109
 
      @Override
110
 
      public CategoryListIterator iterator(int partition)  throws IOException {
111
 
        final CategoryListIterator it = cld.iterator(partition);
112
 
        return new CategoryListIterator() {              
113
 
          public boolean skipTo(int docId) throws IOException {
114
 
            return it.skipTo(docId);
115
 
          }
116
 
          public long nextCategory() throws IOException {
117
 
            long res = it.nextCategory();
118
 
            if (res>Integer.MAX_VALUE) {
119
 
              return res;
120
 
            }
121
 
            return res>1 ? res-1 : res+1;
122
 
          }
123
 
          public boolean init() throws IOException {
124
 
            return it.init();
125
 
          }
126
 
        };
127
 
      }
128
 
    };
129
 
    clCache.register(clp, badCld);
130
 
  }
131
 
  
132
 
}