1 package org.apache.lucene.analysis;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.HashMap;
24 import java.util.Random;
26 import org.apache.lucene.util.LuceneTestCase;
29 * Analyzer for testing
31 * This analyzer is a replacement for Whitespace/Simple/KeywordAnalyzers
32 * for unit tests. If you are testing a custom component such as a queryparser
33 * or analyzer-wrapper that consumes analysis streams, its a great idea to test
34 * it with this analyzer instead. MockAnalyzer has the following behavior:
36 * <li>By default, the assertions in {@link MockTokenizer} are turned on for extra
37 * checks that the consumer is consuming properly. These checks can be disabled
38 * with {@link #setEnableChecks(boolean)}.
39 * <li>Payload data is randomly injected into the stream for more thorough testing
44 public final class MockAnalyzer extends Analyzer {
45 private final int pattern;
46 private final boolean lowerCase;
47 private final CharArraySet filter;
48 private final boolean enablePositionIncrements;
49 private int positionIncrementGap;
50 private final Random random;
51 private Map<String,Integer> previousMappings = new HashMap<String,Integer>();
52 private boolean enableChecks = true;
55 * Creates a new MockAnalyzer.
57 * @param random Random for payloads behavior
58 * @param pattern pattern constant describing how tokenization should happen
59 * @param lowerCase true if the tokenizer should lowercase terms
60 * @param filter CharArraySet describing how terms should be filtered (set of stopwords, etc)
61 * @param enablePositionIncrements true if position increments should reflect filtered terms.
63 public MockAnalyzer(Random random, int pattern, boolean lowerCase, CharArraySet filter, boolean enablePositionIncrements) {
65 this.pattern = pattern;
66 this.lowerCase = lowerCase;
68 this.enablePositionIncrements = enablePositionIncrements;
72 * Calls {@link #MockAnalyzer(Random, int, boolean, CharArraySet, boolean)
73 * MockAnalyzer(random, pattern, lowerCase, CharArraySet.EMPTY_STOPSET, false}).
75 public MockAnalyzer(Random random, int pattern, boolean lowerCase) {
76 this(random, pattern, lowerCase, CharArraySet.EMPTY_SET, false);
80 * Create a Whitespace-lowercasing analyzer with no stopwords removal.
82 * Calls {@link #MockAnalyzer(Random, int, boolean)
83 * MockAnalyzer(random, MockTokenizer.WHITESPACE, true)}.
85 public MockAnalyzer(Random random) {
86 this(random, MockTokenizer.WHITESPACE, true);
90 public TokenStream tokenStream(String fieldName, Reader reader) {
91 MockTokenizer tokenizer = new MockTokenizer(reader, pattern, lowerCase);
92 tokenizer.setEnableChecks(enableChecks);
93 StopFilter filt = new StopFilter(LuceneTestCase.TEST_VERSION_CURRENT, tokenizer, filter);
94 filt.setEnablePositionIncrements(enablePositionIncrements);
95 return maybePayload(filt, fieldName);
98 private class SavedStreams {
99 MockTokenizer tokenizer;
104 public TokenStream reusableTokenStream(String fieldName, Reader reader)
106 @SuppressWarnings("unchecked") Map<String,SavedStreams> map = (Map) getPreviousTokenStream();
108 map = new HashMap<String,SavedStreams>();
109 setPreviousTokenStream(map);
112 SavedStreams saved = map.get(fieldName);
114 saved = new SavedStreams();
115 saved.tokenizer = new MockTokenizer(reader, pattern, lowerCase);
116 saved.tokenizer.setEnableChecks(enableChecks);
117 StopFilter filt = new StopFilter(LuceneTestCase.TEST_VERSION_CURRENT, saved.tokenizer, filter);
118 filt.setEnablePositionIncrements(enablePositionIncrements);
120 saved.filter = maybePayload(saved.filter, fieldName);
121 map.put(fieldName, saved);
124 saved.tokenizer.reset(reader);
129 private synchronized TokenFilter maybePayload(TokenFilter stream, String fieldName) {
130 Integer val = previousMappings.get(fieldName);
132 val = -1; // no payloads
133 if (LuceneTestCase.rarely(random)) {
134 switch(random.nextInt(3)) {
135 case 0: val = -1; // no payloads
137 case 1: val = Integer.MAX_VALUE; // variable length payload
139 case 2: val = random.nextInt(12); // fixed length payload
143 previousMappings.put(fieldName, val); // save it so we are consistent for this field
148 else if (val == Integer.MAX_VALUE)
149 return new MockVariableLengthPayloadFilter(random, stream);
151 return new MockFixedLengthPayloadFilter(random, stream, val);
154 public void setPositionIncrementGap(int positionIncrementGap){
155 this.positionIncrementGap = positionIncrementGap;
159 public int getPositionIncrementGap(String fieldName){
160 return positionIncrementGap;
164 * Toggle consumer workflow checking: if your test consumes tokenstreams normally you
165 * should leave this enabled.
167 public void setEnableChecks(boolean enableChecks) {
168 this.enableChecks = enableChecks;