Add a test for new FilterIterator

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1374835 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-08-19 20:06:55 +00:00
parent 24b60f3a45
commit 22d7239af9
2 changed files with 146 additions and 0 deletions

View File

@ -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;

View File

@ -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
}
}
}