pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / enhancements / association / CustomAssociationPropertyTest.java
1 package org.apache.lucene.facet.enhancements.association;
2
3 import org.apache.lucene.analysis.MockAnalyzer;
4 import org.apache.lucene.analysis.MockTokenizer;
5 import org.apache.lucene.document.Document;
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.RandomIndexWriter;
8 import org.apache.lucene.store.Directory;
9 import org.junit.Test;
10
11 import org.apache.lucene.util.LuceneTestCase;
12 import org.apache.lucene.facet.enhancements.EnhancementsDocumentBuilder;
13 import org.apache.lucene.facet.enhancements.params.DefaultEnhancementsIndexingParams;
14 import org.apache.lucene.facet.enhancements.params.EnhancementsIndexingParams;
15 import org.apache.lucene.facet.index.CategoryContainer;
16 import org.apache.lucene.facet.index.attributes.CategoryAttributeImpl;
17 import org.apache.lucene.facet.index.attributes.CategoryProperty;
18 import org.apache.lucene.facet.taxonomy.CategoryPath;
19 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
20 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
21
22 /**
23  * Licensed to the Apache Software Foundation (ASF) under one or more
24  * contributor license agreements.  See the NOTICE file distributed with
25  * this work for additional information regarding copyright ownership.
26  * The ASF licenses this file to You under the Apache License, Version 2.0
27  * (the "License"); you may not use this file except in compliance with
28  * the License.  You may obtain a copy of the License at
29  *
30  *     http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS,
34  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  */
38
39 public class CustomAssociationPropertyTest extends LuceneTestCase {
40
41   @Test
42   public void testCustomProperty() throws Exception {
43     class CustomProperty extends AssociationIntProperty {
44       public CustomProperty(int value) {
45         super(value);
46       }
47       @Override
48       public void merge(CategoryProperty other) {
49         throw new UnsupportedOperationException();
50       }
51     }
52
53     final int NUM_CATEGORIES = 10;
54     EnhancementsIndexingParams iParams = new DefaultEnhancementsIndexingParams(
55         new AssociationEnhancement());
56
57     Directory iDir = newDirectory();
58     Directory tDir = newDirectory();
59     
60     RandomIndexWriter w = new RandomIndexWriter(random, iDir, 
61         newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random, MockTokenizer.KEYWORD, false)));
62     DirectoryTaxonomyWriter taxoW = new DirectoryTaxonomyWriter(tDir);
63     
64     CategoryContainer cc = new CategoryContainer();
65     EnhancementsDocumentBuilder builder = new EnhancementsDocumentBuilder(taxoW, iParams);
66     for (int i = 1; i <= NUM_CATEGORIES; i++) {
67       CategoryAttributeImpl ca = new CategoryAttributeImpl(new CategoryPath(Integer.toString(i)));
68       ca.addProperty(new CustomProperty(i));
69       
70       cc.addCategory(ca);
71     }
72     builder.setCategories(cc);
73     w.addDocument(builder.build(new Document()));
74     taxoW.close();
75     IndexReader reader = w.getReader();
76     w.close();
77     
78     DirectoryTaxonomyReader taxo = new DirectoryTaxonomyReader(tDir);
79     String field = iParams.getCategoryListParams(new CategoryPath("0")).getTerm().field();
80     AssociationsPayloadIterator api = new AssociationsPayloadIterator(reader, field);
81
82     api.setNextDoc(0);
83
84     boolean flag = false;
85     for (int i = 1; i <= NUM_CATEGORIES; i++) {
86       int ordinal = taxo.getOrdinal(new CategoryPath(Integer.toString(i)));
87       flag = true;
88       long association = api.getAssociation(ordinal);
89       assertTrue("Association expected for ordinal "+ordinal+" but none was found",
90           association <= Integer.MAX_VALUE);
91       
92       assertEquals("Wrong association value for category '"+ i+"'", i, (int)association);
93     }
94     
95     assertTrue("No categories found for doc #0", flag);
96     
97     reader.close();
98     taxo.close();
99     iDir.close();
100     tDir.close();
101   }
102 }