2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package org.apache.lucene.search.suggest;
21 import org.apache.lucene.search.suggest.Lookup;
22 import org.apache.lucene.search.suggest.fst.FSTLookup;
23 import org.apache.lucene.search.suggest.jaspell.JaspellLookup;
24 import org.apache.lucene.search.suggest.tst.TSTLookup;
25 import org.apache.lucene.util.LuceneTestCase;
27 public class PersistenceTest extends LuceneTestCase {
28 public final String[] keys = new String[] {
45 public void testTSTPersistence() throws Exception {
46 runTest(TSTLookup.class, true);
49 public void testJaspellPersistence() throws Exception {
50 runTest(JaspellLookup.class, true);
53 public void testFSTPersistence() throws Exception {
54 runTest(FSTLookup.class, false);
57 private void runTest(Class<? extends Lookup> lookupClass,
58 boolean supportsExactWeights) throws Exception {
60 // Add all input keys.
61 Lookup lookup = lookupClass.newInstance();
62 TermFreq[] keys = new TermFreq[this.keys.length];
63 for (int i = 0; i < keys.length; i++)
64 keys[i] = new TermFreq(this.keys[i], (float) i);
65 lookup.build(new TermFreqArrayIterator(keys));
67 // Store the suggester.
68 File storeDir = TEMP_DIR;
69 lookup.store(storeDir);
71 // Re-read it from disk.
72 lookup = lookupClass.newInstance();
73 lookup.load(storeDir);
76 float previous = Float.NEGATIVE_INFINITY;
77 for (TermFreq k : keys) {
78 Float val = (Float) lookup.get(k.term);
79 assertNotNull(k.term, val);
81 if (supportsExactWeights) {
82 assertEquals(k.term, Float.valueOf(k.v), val);
84 assertTrue(val + ">=" + previous, val >= previous);
85 previous = val.floatValue();