From 4027f793701fe147bc7ff95555c06655a4d6d467 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Wed, 20 Mar 2013 18:13:43 +0000 Subject: [PATCH] emulate jvm bug git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1458987 13f79535-47bb-0310-9956-ffa450edef68 --- .../postingshighlight/WholeBreakIterator.java | 6 ++ .../TestWholeBreakIterator.java | 82 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestWholeBreakIterator.java diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java index 54eecd426dd..5250f6956fe 100644 --- a/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java +++ b/lucene/highlighter/src/java/org/apache/lucene/search/postingshighlight/WholeBreakIterator.java @@ -42,6 +42,9 @@ final class WholeBreakIterator extends BreakIterator { if (pos < 0 || pos > len) { throw new IllegalArgumentException("offset out of bounds"); } else if (pos == len) { + // this conflicts with the javadocs, but matches actual behavior (Oracle has a bug in something) + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=9000909 + current = len; return DONE; } else { return last(); @@ -86,6 +89,9 @@ final class WholeBreakIterator extends BreakIterator { if (pos < 0 || pos > len) { throw new IllegalArgumentException("offset out of bounds"); } else if (pos == 0) { + // this conflicts with the javadocs, but matches actual behavior (Oracle has a bug in something) + // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=9000909 + current = 0; return DONE; } else { return first(); diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestWholeBreakIterator.java b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestWholeBreakIterator.java new file mode 100644 index 00000000000..d5964a62935 --- /dev/null +++ b/lucene/highlighter/src/test/org/apache/lucene/search/postingshighlight/TestWholeBreakIterator.java @@ -0,0 +1,82 @@ +package org.apache.lucene.search.postingshighlight; + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.text.BreakIterator; +import java.util.Locale; + +import org.apache.lucene.util.LuceneTestCase; + +public class TestWholeBreakIterator extends LuceneTestCase { + + /** For single sentences, we know WholeBreakIterator should break the same as a sentence iterator */ + public void testSingleSentences() throws Exception { + BreakIterator expected = BreakIterator.getSentenceInstance(Locale.ROOT); + BreakIterator actual = new WholeBreakIterator(); + assertSameBreaks("a", expected, actual); + assertSameBreaks("ab", expected, actual); + assertSameBreaks("abc", expected, actual); + assertSameBreaks("", expected, actual); + } + + /** Asserts that two breakiterators break the text the same way */ + // TODO: change this to use offsets with non-zero start/end + public void assertSameBreaks(String text, BreakIterator expected, BreakIterator actual) { + expected.setText(text); + actual.setText(text); + + assertEquals(expected.current(), actual.current()); + + // next() + int v = expected.current(); + while (v != BreakIterator.DONE) { + assertEquals(v = expected.next(), actual.next()); + assertEquals(expected.current(), actual.current()); + } + + // first() + assertEquals(expected.first(), actual.first()); + assertEquals(expected.current(), actual.current()); + // last() + assertEquals(expected.last(), actual.last()); + assertEquals(expected.current(), actual.current()); + + // previous() + v = expected.current(); + while (v != BreakIterator.DONE) { + assertEquals(v = expected.previous(), actual.previous()); + assertEquals(expected.current(), actual.current()); + } + + // following() + for (int i = 0; i <= text.length(); i++) { + expected.first(); + actual.first(); + assertEquals(expected.following(i), actual.following(i)); + assertEquals(expected.current(), actual.current()); + } + + // preceding() + for (int i = 0; i <= text.length(); i++) { + expected.last(); + actual.last(); + assertEquals(expected.preceding(i), actual.preceding(i)); + assertEquals(expected.current(), actual.current()); + } + } +}