1 package org.apache.lucene.index;
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;
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;
33 public class TestSameTokenSamePosition extends LuceneTestCase {
36 * Attempt to reproduce an assertion error that happens
37 * only with the trunk version around April 2011.
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));
52 final class BugReproAnalyzer extends Analyzer{
54 public TokenStream tokenStream(String arg0, Reader arg1) {
55 return new BugReproAnalyzerTokenizer();
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);
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};
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]);