pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / test / org / apache / lucene / facet / taxonomy / directory / TestDirectoryTaxonomyReader.java
1 package org.apache.lucene.facet.taxonomy.directory;
2
3 import java.util.Random;
4
5 import org.apache.lucene.facet.taxonomy.CategoryPath;
6 import org.apache.lucene.facet.taxonomy.InconsistentTaxonomyException;
7 import org.apache.lucene.facet.taxonomy.TaxonomyReader;
8 import org.apache.lucene.facet.taxonomy.TaxonomyWriter;
9 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
10 import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
11 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
12 import org.apache.lucene.store.AlreadyClosedException;
13 import org.apache.lucene.store.Directory;
14 import org.apache.lucene.util.IOUtils;
15 import org.apache.lucene.util.LuceneTestCase;
16 import org.junit.Test;
17
18 /**
19  * Licensed to the Apache Software Foundation (ASF) under one or more
20  * contributor license agreements.  See the NOTICE file distributed with
21  * this work for additional information regarding copyright ownership.
22  * The ASF licenses this file to You under the Apache License, Version 2.0
23  * (the "License"); you may not use this file except in compliance with
24  * the License.  You may obtain a copy of the License at
25  *
26  *     http://www.apache.org/licenses/LICENSE-2.0
27  *
28  * Unless required by applicable law or agreed to in writing, software
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  */
34
35 public class TestDirectoryTaxonomyReader extends LuceneTestCase {
36
37   @Test
38   public void testCloseAfterIncRef() throws Exception {
39     Directory dir = newDirectory();
40     DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
41     ltw.addCategory(new CategoryPath("a"));
42     ltw.close();
43     
44     DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
45     ltr.incRef();
46     ltr.close();
47     
48     // should not fail as we incRef() before close
49     ltr.getSize();
50     ltr.decRef();
51     
52     dir.close();
53   }
54   
55   @Test
56   public void testCloseTwice() throws Exception {
57     Directory dir = newDirectory();
58     DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
59     ltw.addCategory(new CategoryPath("a"));
60     ltw.close();
61     
62     DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
63     ltr.close();
64     ltr.close(); // no exception should be thrown
65     
66     dir.close();
67   }
68   
69   /**
70    * Test the boolean returned by TR.refresh
71    * @throws Exception
72    */
73   @Test
74   public void testReaderRefreshResult() throws Exception {
75     Directory dir = null;
76     DirectoryTaxonomyWriter ltw = null;
77     DirectoryTaxonomyReader ltr = null;
78     
79     try {
80       dir = newDirectory();
81       ltw = new DirectoryTaxonomyWriter(dir);
82       
83       ltw.addCategory(new CategoryPath("a"));
84       ltw.commit();
85       
86       ltr = new DirectoryTaxonomyReader(dir);
87       assertFalse("Nothing has changed",ltr.refresh());
88       
89       ltw.addCategory(new CategoryPath("b"));
90       ltw.commit();
91       
92       assertTrue("changes were committed",ltr.refresh());
93       assertFalse("Nothing has changed",ltr.refresh());
94     } finally {
95       IOUtils.close(ltw, ltr, dir);
96     }
97   }
98   
99   @Test
100   public void testAlreadyClosed() throws Exception {
101     Directory dir = newDirectory();
102     DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
103     ltw.addCategory(new CategoryPath("a"));
104     ltw.close();
105     
106     DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
107     ltr.close();
108     try {
109       ltr.getSize();
110       fail("An AlreadyClosedException should have been thrown here");
111     } catch (AlreadyClosedException ace) {
112       // good!
113     }
114     dir.close();
115   }
116   
117   /**
118    * recreating a taxonomy should work well with a freshly opened taxonomy reader 
119    */
120   @Test
121   public void testFreshReadRecreatedTaxonomy() throws Exception {
122     doTestReadRecreatedTaxono(random, true);
123   }
124   
125   /**
126    * recreating a taxonomy should work well with a refreshed taxonomy reader 
127    */
128   @Test
129   public void testRefreshReadRecreatedTaxonomy() throws Exception {
130     doTestReadRecreatedTaxono(random, false);
131   }
132   
133   private void doTestReadRecreatedTaxono(Random random, boolean closeReader) throws Exception {
134     Directory dir = null;
135     TaxonomyWriter tw = null;
136     TaxonomyReader tr = null;
137     
138     // prepare a few categories
139     int  n = 10;
140     CategoryPath[] cp = new CategoryPath[n];
141     for (int i=0; i<n; i++) {
142       cp[i] = new CategoryPath("a", Integer.toString(i));
143     }
144     
145     try {
146       dir = newDirectory();
147       
148       tw = new DirectoryTaxonomyWriter(dir);
149       tw.addCategory(new CategoryPath("a"));
150       tw.close();
151       
152       tr = new DirectoryTaxonomyReader(dir);
153       int baseNumcategories = tr.getSize();
154       
155       for (int i=0; i<n; i++) {
156         int k = random.nextInt(n);
157         tw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE);
158         for (int j=0; j<=k; j++) {
159           tw.addCategory(new CategoryPath(cp[j]));
160         }
161         tw.close();
162         if (closeReader) {
163           tr.close();
164           tr = new DirectoryTaxonomyReader(dir);
165         } else {
166           try {
167             tr.refresh();
168             fail("Expected InconsistentTaxonomyException");
169           } catch (InconsistentTaxonomyException e) {
170             tr.close();
171             tr = new DirectoryTaxonomyReader(dir);
172           }
173         }
174         assertEquals("Wrong #categories in taxonomy (i="+i+", k="+k+")", baseNumcategories + 1 + k, tr.getSize());
175       }
176     } finally {
177       IOUtils.close(tr, tw, dir);
178     }
179   }
180   
181 }