@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* 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.springframework.util;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -5,29 +21,36 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* Test case for {@link CompositeIterator}.
|
||||
*
|
||||
* @author Erwin Vervaet
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class CompositeIteratorTests extends TestCase {
|
||||
public class CompositeIteratorTests {
|
||||
|
||||
@Test
|
||||
public void testNoIterators() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
CompositeIterator<String> it = new CompositeIterator<String>();
|
||||
assertFalse(it.hasNext());
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
}
|
||||
catch (NoSuchElementException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleIterator() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
|
||||
CompositeIterator<String> it = new CompositeIterator<String>();
|
||||
it.add(Arrays.asList("0", "1").iterator());
|
||||
for (int i = 0; i < 2; i++) {
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals(String.valueOf(i), it.next());
|
||||
@@ -36,16 +59,18 @@ public class CompositeIteratorTests extends TestCase {
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
}
|
||||
catch (NoSuchElementException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleIterators() {
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
it.add(Arrays.asList(new String[] { "0", "1" }).iterator());
|
||||
it.add(Arrays.asList(new String[] { "2" }).iterator());
|
||||
it.add(Arrays.asList(new String[] { "3", "4" }).iterator());
|
||||
CompositeIterator<String> it = new CompositeIterator<String>();
|
||||
it.add(Arrays.asList("0", "1").iterator());
|
||||
it.add(Arrays.asList("2").iterator());
|
||||
it.add(Arrays.asList("3", "4").iterator());
|
||||
for (int i = 0; i < 5; i++) {
|
||||
assertTrue(it.hasNext());
|
||||
assertEquals(String.valueOf(i), it.next());
|
||||
@@ -54,43 +79,49 @@ public class CompositeIteratorTests extends TestCase {
|
||||
try {
|
||||
it.next();
|
||||
fail();
|
||||
} catch (NoSuchElementException e) {
|
||||
}
|
||||
catch (NoSuchElementException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInUse() {
|
||||
List list = Arrays.asList(new String[] { "0", "1" });
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
List<String> list = Arrays.asList("0", "1");
|
||||
CompositeIterator<String> it = new CompositeIterator<String>();
|
||||
it.add(list.iterator());
|
||||
it.hasNext();
|
||||
try {
|
||||
it.add(list.iterator());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
it = new CompositeIterator();
|
||||
it = new CompositeIterator<String>();
|
||||
it.add(list.iterator());
|
||||
it.next();
|
||||
try {
|
||||
it.add(list.iterator());
|
||||
fail();
|
||||
} catch (IllegalStateException e) {
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDuplicateIterators() {
|
||||
List list = Arrays.asList(new String[] { "0", "1" });
|
||||
Iterator iterator = list.iterator();
|
||||
CompositeIterator it = new CompositeIterator();
|
||||
List<String> list = Arrays.asList("0", "1");
|
||||
Iterator<String> iterator = list.iterator();
|
||||
CompositeIterator<String> it = new CompositeIterator<String>();
|
||||
it.add(iterator);
|
||||
it.add(list.iterator());
|
||||
try {
|
||||
it.add(iterator);
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user