KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > primitives > adapters > TestByteListIteratorListIterator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.commons.collections.primitives.adapters;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.ListIterator JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.commons.collections.iterators.AbstractTestListIterator;
27 import org.apache.commons.collections.primitives.ArrayByteList;
28 import org.apache.commons.collections.primitives.ByteList;
29
30 /**
31  * @version $Revision: 480451 $ $Date: 2006-11-28 23:45:08 -0800 (Tue, 28 Nov 2006) $
32  * @author Rodney Waldhoff
33  */

34 public class TestByteListIteratorListIterator extends AbstractTestListIterator {
35
36     // conventional
37
// ------------------------------------------------------------------------
38

39     public TestByteListIteratorListIterator(String JavaDoc testName) {
40         super(testName);
41     }
42
43     public static Test suite() {
44         return new TestSuite(TestByteListIteratorListIterator.class);
45     }
46
47     // collections testing framework
48
// ------------------------------------------------------------------------
49

50     public ListIterator JavaDoc makeEmptyListIterator() {
51         return ByteListIteratorListIterator.wrap(makeEmptyByteList().listIterator());
52     }
53     
54     public ListIterator JavaDoc makeFullListIterator() {
55         return ByteListIteratorListIterator.wrap(makeFullByteList().listIterator());
56     }
57
58     protected ByteList makeEmptyByteList() {
59         return new ArrayByteList();
60     }
61     
62     protected ByteList makeFullByteList() {
63         ByteList list = makeEmptyByteList();
64         byte[] elts = getFullElements();
65         for(int i=0;i<elts.length;i++) {
66             list.add((byte)elts[i]);
67         }
68         return list;
69     }
70     
71     public byte[] getFullElements() {
72         return new byte[] { (byte)0, (byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8, (byte)9 };
73     }
74     
75     public Object JavaDoc addSetValue() {
76         return new Byte JavaDoc((byte)1);
77     }
78
79     // tests
80
// ------------------------------------------------------------------------
81

82     
83     public void testNextHasNextRemove() {
84         byte[] elements = getFullElements();
85         Iterator JavaDoc iter = makeFullIterator();
86         for(int i=0;i<elements.length;i++) {
87             assertTrue(iter.hasNext());
88             assertEquals(new Byte JavaDoc(elements[i]),iter.next());
89             if(supportsRemove()) {
90                 iter.remove();
91             }
92         }
93         assertTrue(! iter.hasNext() );
94     }
95
96     public void testEmptyIterator() {
97         assertTrue( ! makeEmptyIterator().hasNext() );
98         try {
99             makeEmptyIterator().next();
100             fail("Expected NoSuchElementException");
101         } catch(NoSuchElementException JavaDoc e) {
102             // expected
103
}
104         if(supportsRemove()) {
105             try {
106                 makeEmptyIterator().remove();
107                 fail("Expected IllegalStateException");
108             } catch(IllegalStateException JavaDoc e) {
109                 // expected
110
}
111         }
112     }
113
114     public void testRemoveBeforeNext() {
115         if(supportsRemove()) {
116             try {
117                 makeFullIterator().remove();
118                 fail("Expected IllegalStateException");
119             } catch(IllegalStateException JavaDoc e) {
120                 // expected
121
}
122         }
123     }
124
125     public void testRemoveAfterRemove() {
126         if(supportsRemove()) {
127             Iterator JavaDoc iter = makeFullIterator();
128             iter.next();
129             iter.remove();
130             try {
131                 iter.remove();
132                 fail("Expected IllegalStateException");
133             } catch(IllegalStateException JavaDoc e) {
134                 // expected
135
}
136         }
137     }
138
139 }
140
Popular Tags