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