1 package org.apache.lucene.facet.taxonomy.directory;
3 import java.util.Random;
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;
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
26 * http://www.apache.org/licenses/LICENSE-2.0
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.
35 public class TestDirectoryTaxonomyReader extends LuceneTestCase {
38 public void testCloseAfterIncRef() throws Exception {
39 Directory dir = newDirectory();
40 DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
41 ltw.addCategory(new CategoryPath("a"));
44 DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
48 // should not fail as we incRef() before close
56 public void testCloseTwice() throws Exception {
57 Directory dir = newDirectory();
58 DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
59 ltw.addCategory(new CategoryPath("a"));
62 DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
64 ltr.close(); // no exception should be thrown
70 * Test the boolean returned by TR.refresh
74 public void testReaderRefreshResult() throws Exception {
76 DirectoryTaxonomyWriter ltw = null;
77 DirectoryTaxonomyReader ltr = null;
81 ltw = new DirectoryTaxonomyWriter(dir);
83 ltw.addCategory(new CategoryPath("a"));
86 ltr = new DirectoryTaxonomyReader(dir);
87 assertFalse("Nothing has changed",ltr.refresh());
89 ltw.addCategory(new CategoryPath("b"));
92 assertTrue("changes were committed",ltr.refresh());
93 assertFalse("Nothing has changed",ltr.refresh());
95 IOUtils.close(ltw, ltr, dir);
100 public void testAlreadyClosed() throws Exception {
101 Directory dir = newDirectory();
102 DirectoryTaxonomyWriter ltw = new DirectoryTaxonomyWriter(dir);
103 ltw.addCategory(new CategoryPath("a"));
106 DirectoryTaxonomyReader ltr = new DirectoryTaxonomyReader(dir);
110 fail("An AlreadyClosedException should have been thrown here");
111 } catch (AlreadyClosedException ace) {
118 * recreating a taxonomy should work well with a freshly opened taxonomy reader
121 public void testFreshReadRecreatedTaxonomy() throws Exception {
122 doTestReadRecreatedTaxono(random, true);
126 * recreating a taxonomy should work well with a refreshed taxonomy reader
129 public void testRefreshReadRecreatedTaxonomy() throws Exception {
130 doTestReadRecreatedTaxono(random, false);
133 private void doTestReadRecreatedTaxono(Random random, boolean closeReader) throws Exception {
134 Directory dir = null;
135 TaxonomyWriter tw = null;
136 TaxonomyReader tr = null;
138 // prepare a few categories
140 CategoryPath[] cp = new CategoryPath[n];
141 for (int i=0; i<n; i++) {
142 cp[i] = new CategoryPath("a", Integer.toString(i));
146 dir = newDirectory();
148 tw = new DirectoryTaxonomyWriter(dir);
149 tw.addCategory(new CategoryPath("a"));
152 tr = new DirectoryTaxonomyReader(dir);
153 int baseNumcategories = tr.getSize();
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]));
164 tr = new DirectoryTaxonomyReader(dir);
168 fail("Expected InconsistentTaxonomyException");
169 } catch (InconsistentTaxonomyException e) {
171 tr = new DirectoryTaxonomyReader(dir);
174 assertEquals("Wrong #categories in taxonomy (i="+i+", k="+k+")", baseNumcategories + 1 + k, tr.getSize());
177 IOUtils.close(tr, tw, dir);