Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

Add a test for new FilterIterator

Uwe Schindler22d7239af9b2
diff --git a/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java b/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
index f332ef02e80..b20f6c62ca5 100644
--- a/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
+++ b/lucene/core/src/java/org/apache/lucene/util/WeakIdentityMap.java
@@ -107,6 +107,8 @@ public final class WeakIdentityMap<K,V> {
public Iterator<K> keyIterator() {
reap();
final Iterator<IdentityWeakReference> iterator = backingStore.keySet().iterator();
+ // IMPORTANT: Don't use oal.util.FilterIterator here:
+ // We need *strong* reference to current key after setNext()!!!
return new Iterator<K>() {
// holds strong reference to next element in backing iterator:
private Object next = null;
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestFilterIterator.java b/lucene/core/src/test/org/apache/lucene/util/TestFilterIterator.java
new file mode 100644
index 00000000000..3f9f66c5297
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/util/TestFilterIterator.java
@@ -0,0 +1,144 @@
+/*
+ * 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.
+ */
+
+package org.apache.lucene.util;
+
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.NoSuchElementException;
+
+public class TestFilterIterator extends LuceneTestCase {
+
+ private static final Set<String> set = new TreeSet<String>(Arrays.asList("a", "b", "c"));
+
+ private static void assertNoMore(Iterator<?> it) {
+ assertFalse(it.hasNext());
+ try {
+ it.next();
+ fail("Should throw NoSuchElementException");
+ } catch (NoSuchElementException nsee) {
+ // pass
+ }
+ assertFalse(it.hasNext());
+ }
+
+ public void testEmpty() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return false;
+ }
+ };
+ assertNoMore(it);
+ }
+
+ public void testA1() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return "a".equals(s);
+ }
+ };
+ assertTrue(it.hasNext());
+ assertEquals("a", it.next());
+ assertNoMore(it);
+ }
+
+ public void testA2() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return "a".equals(s);
+ }
+ };
+ // this time without check: assertTrue(it.hasNext());
+ assertEquals("a", it.next());
+ assertNoMore(it);
+ }
+
+ public void testB1() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return "b".equals(s);
+ }
+ };
+ assertTrue(it.hasNext());
+ assertEquals("b", it.next());
+ assertNoMore(it);
+ }
+
+ public void testB2() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return "b".equals(s);
+ }
+ };
+ // this time without check: assertTrue(it.hasNext());
+ assertEquals("b", it.next());
+ assertNoMore(it);
+ }
+
+ public void testAll1() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return true;
+ }
+ };
+ assertTrue(it.hasNext());
+ assertEquals("a", it.next());
+ assertTrue(it.hasNext());
+ assertEquals("b", it.next());
+ assertTrue(it.hasNext());
+ assertEquals("c", it.next());
+ assertNoMore(it);
+ }
+
+ public void testAll2() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return true;
+ }
+ };
+ assertEquals("a", it.next());
+ assertEquals("b", it.next());
+ assertEquals("c", it.next());
+ assertNoMore(it);
+ }
+
+ public void testUnmodifiable() {
+ final Iterator<String> it = new FilterIterator<String>(set.iterator()) {
+ @Override
+ protected boolean predicateFunction(String s) {
+ return true;
+ }
+ };
+ assertEquals("a", it.next());
+ try {
+ it.remove();
+ fail("Should throw UnsupportedOperationException");
+ } catch (UnsupportedOperationException oue) {
+ // pass
+ }
+ }
+
+}