1 package org.apache.lucene.facet.taxonomy.lucene;
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.Directory;
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;
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
24 * http://www.apache.org/licenses/LICENSE-2.0
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.
33 public class TestLuceneTaxonomyWriter extends LuceneTestCase {
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 {
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; }
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"));
60 IndexReader r = IndexReader.open(dir);
61 assertEquals("No categories should have been committed to the underlying directory", 1, r.numDocs());
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);
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")));