add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / taxonomy / lucene / TestLuceneTaxonomyWriter.java
1 package org.apache.lucene.facet.taxonomy.lucene;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import org.apache.lucene.index.IndexReader;
7 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
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.taxonomy.CategoryPath;
13 import org.apache.lucene.facet.taxonomy.lucene.LuceneTaxonomyWriter;
14 import org.apache.lucene.facet.taxonomy.writercache.TaxonomyWriterCache;
15
16 /**
17  * Licensed to the Apache Software Foundation (ASF) under one or more
18  * contributor license agreements.  See the NOTICE file distributed with
19  * this work for additional information regarding copyright ownership.
20  * The ASF licenses this file to You under the Apache License, Version 2.0
21  * (the "License"); you may not use this file except in compliance with
22  * the License.  You may obtain a copy of the License at
23  *
24  *     http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  */
32
33 public class TestLuceneTaxonomyWriter extends LuceneTestCase {
34
35   // A No-Op TaxonomyWriterCache which always discards all given categories, and
36   // always returns true in put(), to indicate some cache entries were cleared.
37   private static class NoOpCache implements TaxonomyWriterCache {
38
39     NoOpCache() { }
40     
41     public void close() {}
42     public int get(CategoryPath categoryPath) { return -1; }
43     public int get(CategoryPath categoryPath, int length) { return get(categoryPath); }
44     public boolean put(CategoryPath categoryPath, int ordinal) { return true; }
45     public boolean put(CategoryPath categoryPath, int prefixLen, int ordinal) { return true; }
46     public boolean hasRoom(int numberOfEntries) { return false; }
47     
48   }
49   
50   @Test
51   public void testCommit() throws Exception {
52     // Verifies that nothing is committed to the underlying Directory, if
53     // commit() wasn't called.
54     Directory dir = newDirectory();
55     LuceneTaxonomyWriter ltw = new LuceneTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, new NoOpCache());
56     assertFalse(IndexReader.indexExists(dir));
57     ltw.commit(); // first commit, so that an index will be created
58     ltw.addCategory(new CategoryPath("a"));
59     
60     IndexReader r = IndexReader.open(dir);
61     assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
62     r.close();
63     ltw.close();
64     dir.close();
65   }
66   
67   @Test
68   public void testCommitUserData() throws Exception {
69     // Verifies that committed data is retrievable
70     Directory dir = newDirectory();
71     LuceneTaxonomyWriter ltw = new LuceneTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, new NoOpCache());
72     assertFalse(IndexReader.indexExists(dir));
73     ltw.commit(); // first commit, so that an index will be created
74     ltw.addCategory(new CategoryPath("a"));
75     ltw.addCategory(new CategoryPath("b"));
76     Map <String, String> userCommitData = new HashMap<String, String>();
77     userCommitData.put("testing", "1 2 3");
78     ltw.commit(userCommitData);
79     ltw.close();
80     IndexReader r = IndexReader.open(dir);
81     assertEquals("2 categories plus root should have been committed to the underlying directory", 3, r.numDocs());
82     Map <String, String> readUserCommitData = r.getCommitUserData();
83     assertTrue("wrong value extracted from commit data", 
84         "1 2 3".equals(readUserCommitData.get("testing")));
85     r.close();
86     dir.close();
87   }
88   
89 }