📄 testiteratorutils.java
字号:
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* Licensed 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.commons.collections;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
import junit.framework.Test;
import org.apache.commons.collections.iterators.EmptyIterator;
import org.apache.commons.collections.iterators.EmptyListIterator;
import org.apache.commons.collections.iterators.EmptyMapIterator;
import org.apache.commons.collections.iterators.EmptyOrderedIterator;
import org.apache.commons.collections.iterators.EmptyOrderedMapIterator;
/**
* Tests for IteratorUtils.
*
* @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
*
* @author Unknown
*/
public class TestIteratorUtils extends BulkTest {
public TestIteratorUtils(String name) {
super(name);
}
public static void main(String args[]) {
junit.textui.TestRunner.run(suite());
}
public static Test suite() {
return BulkTest.makeSuite(TestIteratorUtils.class);
}
public void testToList() {
List list = new ArrayList();
list.add(new Integer(1));
list.add("Two");
list.add(null);
List result = IteratorUtils.toList(list.iterator());
assertEquals(list, result);
}
public void testToArray() {
List list = new ArrayList();
list.add(new Integer(1));
list.add("Two");
list.add(null);
Object[] result = IteratorUtils.toArray(list.iterator());
assertEquals(list, Arrays.asList(result));
}
public void testToArray2() {
List list = new ArrayList();
list.add("One");
list.add("Two");
list.add(null);
String[] result = (String[]) IteratorUtils.toArray(list.iterator(), String.class);
assertEquals(list, Arrays.asList(result));
}
public void testArrayIterator() {
Object[] objArray = {"a", "b", "c"};
ResettableIterator iterator = IteratorUtils.arrayIterator(objArray);
assertTrue(iterator.next().equals("a"));
assertTrue(iterator.next().equals("b"));
iterator.reset();
assertTrue(iterator.next().equals("a"));
try {
iterator = IteratorUtils.arrayIterator(new Integer(0));
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayIterator(null);
fail("Expecting NullPointerException");
} catch (NullPointerException ex) {
// expected
}
iterator = IteratorUtils.arrayIterator(objArray, 1);
assertTrue(iterator.next().equals("b"));
try {
iterator = IteratorUtils.arrayIterator(objArray, -1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayIterator(objArray, 3);
assertTrue(!iterator.hasNext());
iterator.reset();
try {
iterator = IteratorUtils.arrayIterator(objArray, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayIterator(objArray, 2, 3);
assertTrue(iterator.next().equals("c"));
try {
iterator = IteratorUtils.arrayIterator(objArray, 2, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayIterator(objArray, -1, 1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayIterator(objArray, 2, 1);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
int[] intArray = {0, 1, 2};
iterator = IteratorUtils.arrayIterator(intArray);
assertTrue(iterator.next().equals(new Integer(0)));
assertTrue(iterator.next().equals(new Integer(1)));
iterator.reset();
assertTrue(iterator.next().equals(new Integer(0)));
iterator = IteratorUtils.arrayIterator(intArray, 1);
assertTrue(iterator.next().equals(new Integer(1)));
try {
iterator = IteratorUtils.arrayIterator(intArray, -1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayIterator(intArray, 3);
assertTrue(!iterator.hasNext());
iterator.reset();
try {
iterator = IteratorUtils.arrayIterator(intArray, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayIterator(intArray, 2, 3);
assertTrue(iterator.next().equals(new Integer(2)));
try {
iterator = IteratorUtils.arrayIterator(intArray, 2, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayIterator(intArray, -1, 1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayIterator(intArray, 2, 1);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testArrayListIterator() {
Object[] objArray = {"a", "b", "c", "d"};
ResettableListIterator iterator = IteratorUtils.arrayListIterator(objArray);
assertTrue(!iterator.hasPrevious());
assertTrue(iterator.previousIndex() == -1);
assertTrue(iterator.nextIndex() == 0);
assertTrue(iterator.next().equals("a"));
assertTrue(iterator.previous().equals("a"));
assertTrue(iterator.next().equals("a"));
assertTrue(iterator.previousIndex() == 0);
assertTrue(iterator.nextIndex() == 1);
assertTrue(iterator.next().equals("b"));
assertTrue(iterator.next().equals("c"));
assertTrue(iterator.next().equals("d"));
assertTrue(iterator.nextIndex() == 4); // size of list
assertTrue(iterator.previousIndex() == 3);
try {
iterator = IteratorUtils.arrayListIterator(new Integer(0));
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(null);
fail("Expecting NullPointerException");
} catch (NullPointerException ex) {
// expected
}
iterator = IteratorUtils.arrayListIterator(objArray, 1);
assertTrue(iterator.previousIndex() == -1);
assertTrue(!iterator.hasPrevious());
assertTrue(iterator.nextIndex() == 0);
assertTrue(iterator.next().equals("b"));
assertTrue(iterator.previousIndex() == 0);
try {
iterator = IteratorUtils.arrayListIterator(objArray, -1);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayListIterator(objArray, 3);
assertTrue(iterator.hasNext());
try {
Object x = iterator.previous();
fail("Expecting NoSuchElementException.");
} catch (NoSuchElementException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(objArray, 5);
fail("Expecting IndexOutOfBoundsException.");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayListIterator(objArray, 2, 3);
assertTrue(iterator.next().equals("c"));
try {
iterator = IteratorUtils.arrayListIterator(objArray, 2, 5);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(objArray, -1, 1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(objArray, 2, 1);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
int[] intArray = {0, 1, 2};
iterator = IteratorUtils.arrayListIterator(intArray);
assertTrue(iterator.previousIndex() == -1);
assertTrue(!iterator.hasPrevious());
assertTrue(iterator.nextIndex() == 0);
assertTrue(iterator.next().equals(new Integer(0)));
assertTrue(iterator.previousIndex() == 0);
assertTrue(iterator.nextIndex() == 1);
assertTrue(iterator.next().equals(new Integer(1)));
assertTrue(iterator.previousIndex() == 1);
assertTrue(iterator.nextIndex() == 2);
assertTrue(iterator.previous().equals(new Integer(1)));
assertTrue(iterator.next().equals(new Integer(1)));
iterator = IteratorUtils.arrayListIterator(intArray, 1);
assertTrue(iterator.previousIndex() == -1);
assertTrue(!iterator.hasPrevious());
assertTrue(iterator.nextIndex() == 0);
assertTrue(iterator.next().equals(new Integer(1)));
assertTrue(iterator.previous().equals(new Integer(1)));
assertTrue(iterator.next().equals(new Integer(1)));
assertTrue(iterator.previousIndex() == 0);
assertTrue(iterator.nextIndex() == 1);
assertTrue(iterator.next().equals(new Integer(2)));
assertTrue(iterator.previousIndex() == 1);
assertTrue(iterator.nextIndex() == 2);
assertTrue(iterator.previous().equals(new Integer(2)));
assertTrue(iterator.previousIndex() == 0);
assertTrue(iterator.nextIndex() == 1);
try {
iterator = IteratorUtils.arrayListIterator(intArray, -1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayListIterator(intArray, 3);
assertTrue(!iterator.hasNext());
try {
iterator = IteratorUtils.arrayListIterator(intArray, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
iterator = IteratorUtils.arrayListIterator(intArray, 2, 3);
assertTrue(!iterator.hasPrevious());
assertTrue(iterator.previousIndex() == -1);
assertTrue(iterator.next().equals(new Integer(2)));
assertTrue(iterator.hasPrevious());
assertTrue(!iterator.hasNext());
try {
iterator = IteratorUtils.arrayListIterator(intArray, 2, 4);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(intArray, -1, 1);
fail("Expecting IndexOutOfBoundsException");
} catch (IndexOutOfBoundsException ex) {
// expected
}
try {
iterator = IteratorUtils.arrayListIterator(intArray, 2, 1);
fail("Expecting IllegalArgumentException");
} catch (IllegalArgumentException ex) {
// expected
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -