1 package org.apache.lucene.facet.taxonomy.directory;
3 import java.util.HashMap;
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;
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;
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
25 * http://www.apache.org/licenses/LICENSE-2.0
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.
34 public class TestDirectoryTaxonomyWriter extends LuceneTestCase {
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 {
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; }
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"));
61 IndexReader r = IndexReader.open(dir);
62 assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
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);
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")));
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"));
98 dtw.addCategory(new CategoryPath("a"));
99 fail("should not have succeeded to add a category following rollback.");
100 } catch (AlreadyClosedException e) {
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);
113 dtw.addCategory(new CategoryPath("a"));
114 fail("should not have succeeded to add a category following close.");
115 } catch (AlreadyClosedException e) {