Java
105 строк · 2.8 Кб
1/*
2* Copyright (C) 2009 The Guava Authors
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17package com.google.common.collect.testing;
18
19import com.google.common.annotations.GwtCompatible;
20import java.util.Collections;
21import java.util.Iterator;
22import java.util.NoSuchElementException;
23import junit.framework.TestCase;
24
25/**
26* Unit test for {@link MinimalIterable}.
27*
28* @author Kevin Bourrillion
29*/
30@GwtCompatible
31public class MinimalIterableTest extends TestCase {
32
33public void testOf_empty() {
34Iterable<String> iterable = MinimalIterable.<String>of();
35Iterator<String> iterator = iterable.iterator();
36assertFalse(iterator.hasNext());
37try {
38iterator.next();
39fail();
40} catch (NoSuchElementException expected) {
41}
42try {
43iterable.iterator();
44fail();
45} catch (IllegalStateException expected) {
46}
47}
48
49public void testOf_one() {
50Iterable<String> iterable = MinimalIterable.of("a");
51Iterator<String> iterator = iterable.iterator();
52assertTrue(iterator.hasNext());
53assertEquals("a", iterator.next());
54assertFalse(iterator.hasNext());
55try {
56iterator.next();
57fail();
58} catch (NoSuchElementException expected) {
59}
60try {
61iterable.iterator();
62fail();
63} catch (IllegalStateException expected) {
64}
65}
66
67public void testFrom_empty() {
68Iterable<String> iterable = MinimalIterable.from(Collections.<String>emptySet());
69Iterator<String> iterator = iterable.iterator();
70assertFalse(iterator.hasNext());
71try {
72iterator.next();
73fail();
74} catch (NoSuchElementException expected) {
75}
76try {
77iterable.iterator();
78fail();
79} catch (IllegalStateException expected) {
80}
81}
82
83public void testFrom_one() {
84Iterable<String> iterable = MinimalIterable.from(Collections.singleton("a"));
85Iterator<String> iterator = iterable.iterator();
86assertTrue(iterator.hasNext());
87assertEquals("a", iterator.next());
88try {
89iterator.remove();
90fail();
91} catch (UnsupportedOperationException expected) {
92}
93assertFalse(iterator.hasNext());
94try {
95iterator.next();
96fail();
97} catch (NoSuchElementException expected) {
98}
99try {
100iterable.iterator();
101fail();
102} catch (IllegalStateException expected) {
103}
104}
105}
106