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 
17 package com.google.common.collect.testing;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import java.util.Collections;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
23 import junit.framework.TestCase;
24 
25 /**
26  * Unit test for {@link MinimalIterable}.
27  *
28  * @author Kevin Bourrillion
29  */
30 @GwtCompatible
31 public class MinimalIterableTest extends TestCase {
32 
testOf_empty()33   public void testOf_empty() {
34     Iterable<String> iterable = MinimalIterable.<String>of();
35     Iterator<String> iterator = iterable.iterator();
36     assertFalse(iterator.hasNext());
37     try {
38       iterator.next();
39       fail();
40     } catch (NoSuchElementException expected) {
41     }
42     try {
43       iterable.iterator();
44       fail();
45     } catch (IllegalStateException expected) {
46     }
47   }
48 
testOf_one()49   public void testOf_one() {
50     Iterable<String> iterable = MinimalIterable.of("a");
51     Iterator<String> iterator = iterable.iterator();
52     assertTrue(iterator.hasNext());
53     assertEquals("a", iterator.next());
54     assertFalse(iterator.hasNext());
55     try {
56       iterator.next();
57       fail();
58     } catch (NoSuchElementException expected) {
59     }
60     try {
61       iterable.iterator();
62       fail();
63     } catch (IllegalStateException expected) {
64     }
65   }
66 
testFrom_empty()67   public void testFrom_empty() {
68     Iterable<String> iterable = MinimalIterable.from(Collections.<String>emptySet());
69     Iterator<String> iterator = iterable.iterator();
70     assertFalse(iterator.hasNext());
71     try {
72       iterator.next();
73       fail();
74     } catch (NoSuchElementException expected) {
75     }
76     try {
77       iterable.iterator();
78       fail();
79     } catch (IllegalStateException expected) {
80     }
81   }
82 
testFrom_one()83   public void testFrom_one() {
84     Iterable<String> iterable = MinimalIterable.from(Collections.singleton("a"));
85     Iterator<String> iterator = iterable.iterator();
86     assertTrue(iterator.hasNext());
87     assertEquals("a", iterator.next());
88     try {
89       iterator.remove();
90       fail();
91     } catch (UnsupportedOperationException expected) {
92     }
93     assertFalse(iterator.hasNext());
94     try {
95       iterator.next();
96       fail();
97     } catch (NoSuchElementException expected) {
98     }
99     try {
100       iterable.iterator();
101       fail();
102     } catch (IllegalStateException expected) {
103     }
104   }
105 }
106