pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / backwards / src / test-framework / org / apache / lucene / analysis / MockAnalyzer.java
1 package org.apache.lucene.analysis;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.io.Reader;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Random;
25
26 import org.apache.lucene.util.LuceneTestCase;
27
28 /**
29  * Analyzer for testing
30  * <p>
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:
35  * <ul>
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
40  *       of payloads.
41  * </ul>
42  * @see MockTokenizer
43  */
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;
53
54   /**
55    * Creates a new MockAnalyzer.
56    * 
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.
62    */
63   public MockAnalyzer(Random random, int pattern, boolean lowerCase, CharArraySet filter, boolean enablePositionIncrements) {
64     this.random = random;
65     this.pattern = pattern;
66     this.lowerCase = lowerCase;
67     this.filter = filter;
68     this.enablePositionIncrements = enablePositionIncrements;
69   }
70
71   /**
72    * Calls {@link #MockAnalyzer(Random, int, boolean, CharArraySet, boolean) 
73    * MockAnalyzer(random, pattern, lowerCase, CharArraySet.EMPTY_STOPSET, false}).
74    */
75   public MockAnalyzer(Random random, int pattern, boolean lowerCase) {
76     this(random, pattern, lowerCase, CharArraySet.EMPTY_SET, false);
77   }
78
79   /** 
80    * Create a Whitespace-lowercasing analyzer with no stopwords removal.
81    * <p>
82    * Calls {@link #MockAnalyzer(Random, int, boolean) 
83    * MockAnalyzer(random, MockTokenizer.WHITESPACE, true)}.
84    */
85   public MockAnalyzer(Random random) {
86     this(random, MockTokenizer.WHITESPACE, true);
87   }
88
89   @Override
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);
96   }
97
98   private class SavedStreams {
99     MockTokenizer tokenizer;
100     TokenFilter filter;
101   }
102
103   @Override
104   public TokenStream reusableTokenStream(String fieldName, Reader reader)
105       throws IOException {
106     @SuppressWarnings("unchecked") Map<String,SavedStreams> map = (Map) getPreviousTokenStream();
107     if (map == null) {
108       map = new HashMap<String,SavedStreams>();
109       setPreviousTokenStream(map);
110     }
111     
112     SavedStreams saved = map.get(fieldName);
113     if (saved == null) {
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);
119       saved.filter = filt;
120       saved.filter = maybePayload(saved.filter, fieldName);
121       map.put(fieldName, saved);
122       return saved.filter;
123     } else {
124       saved.tokenizer.reset(reader);
125       return saved.filter;
126     }
127   }
128   
129   private synchronized TokenFilter maybePayload(TokenFilter stream, String fieldName) {
130     Integer val = previousMappings.get(fieldName);
131     if (val == null) {
132       val = -1; // no payloads
133       if (LuceneTestCase.rarely(random)) {
134         switch(random.nextInt(3)) {
135           case 0: val = -1; // no payloads
136                   break;
137           case 1: val = Integer.MAX_VALUE; // variable length payload
138                   break;
139           case 2: val = random.nextInt(12); // fixed length payload
140                   break;
141         }
142       }
143       previousMappings.put(fieldName, val); // save it so we are consistent for this field
144     }
145     
146     if (val == -1)
147       return stream;
148     else if (val == Integer.MAX_VALUE)
149       return new MockVariableLengthPayloadFilter(random, stream);
150     else
151       return new MockFixedLengthPayloadFilter(random, stream, val);
152   }
153   
154   public void setPositionIncrementGap(int positionIncrementGap){
155     this.positionIncrementGap = positionIncrementGap;
156   }
157   
158   @Override
159   public int getPositionIncrementGap(String fieldName){
160     return positionIncrementGap;
161   }
162   
163   /** 
164    * Toggle consumer workflow checking: if your test consumes tokenstreams normally you
165    * should leave this enabled.
166    */
167   public void setEnableChecks(boolean enableChecks) {
168     this.enableChecks = enableChecks;
169   }
170 }