add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / backwards / src / test / org / apache / lucene / index / TestSameTokenSamePosition.java
1 package org.apache.lucene.index;
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
23 import org.apache.lucene.analysis.Analyzer;
24 import org.apache.lucene.analysis.TokenStream;
25 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
26 import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;
27 import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;
28 import org.apache.lucene.document.Document;
29 import org.apache.lucene.document.Field;
30 import org.apache.lucene.store.Directory;
31 import org.apache.lucene.util.LuceneTestCase;
32
33 public class TestSameTokenSamePosition extends LuceneTestCase {
34
35   /**
36    * Attempt to reproduce an assertion error that happens
37    * only with the trunk version around April 2011.
38    * @param args
39    */
40   public void test() throws Exception {
41     Directory dir = newDirectory();
42     RandomIndexWriter riw = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new BugReproAnalyzer()));
43     Document doc = new Document();
44     doc.add(new Field("eng", "Six drunken" /*This shouldn't matter. */, 
45                       Field.Store.YES, Field.Index.ANALYZED));
46     riw.addDocument(doc);
47     riw.close();
48     dir.close();
49   }
50 }
51
52 final class BugReproAnalyzer extends Analyzer{
53   @Override
54   public TokenStream tokenStream(String arg0, Reader arg1) {
55     return new BugReproAnalyzerTokenizer();
56   }
57 }
58
59 final class BugReproAnalyzerTokenizer extends TokenStream {
60   private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
61   private final OffsetAttribute offsetAtt = addAttribute(OffsetAttribute.class);
62   private final PositionIncrementAttribute posIncAtt = addAttribute(PositionIncrementAttribute.class);
63   int tokenCount = 4;
64   int nextTokenIndex = 0;
65   String terms[] = new String[]{"six", "six", "drunken", "drunken"};
66   int starts[] = new int[]{0, 0, 4, 4};
67   int ends[] = new int[]{3, 3, 11, 11};
68   int incs[] = new int[]{1, 0, 1, 0};
69
70   @Override
71   public boolean incrementToken() throws IOException {
72     if (nextTokenIndex < tokenCount) {
73       termAtt.setEmpty().append(terms[nextTokenIndex]);
74       offsetAtt.setOffset(starts[nextTokenIndex], ends[nextTokenIndex]);
75       posIncAtt.setPositionIncrement(incs[nextTokenIndex]);
76       nextTokenIndex++;
77       return true;                      
78     } else {
79       return false;
80     }
81   }
82 }