KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > TestBoundedFifoBuffer


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
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 package org.apache.commons.collections;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import junit.framework.Test;
23
24 import org.apache.commons.collections.collection.AbstractTestCollection;
25
26 /**
27  * Test cases for BoundedFifoBuffer.
28  *
29  * @version $Revision: 1.13 $ $Date: 2004/02/18 01:20:35 $
30  *
31  * @author Paul Jack
32  */

33 public class TestBoundedFifoBuffer extends AbstractTestCollection {
34
35     public TestBoundedFifoBuffer(String JavaDoc n) {
36         super(n);
37     }
38
39     public static Test suite() {
40         return BulkTest.makeSuite(TestBoundedFifoBuffer.class);
41     }
42
43     //-----------------------------------------------------------------------
44
/**
45      * Runs through the regular verifications, but also verifies that
46      * the buffer contains the same elements in the same sequence as the
47      * list.
48      */

49     public void verify() {
50         super.verify();
51         Iterator JavaDoc iterator1 = collection.iterator();
52         Iterator JavaDoc iterator2 = confirmed.iterator();
53         while (iterator2.hasNext()) {
54             assertTrue(iterator1.hasNext());
55             Object JavaDoc o1 = iterator1.next();
56             Object JavaDoc o2 = iterator2.next();
57             assertEquals(o1, o2);
58         }
59     }
60
61     //-----------------------------------------------------------------------
62
/**
63      * Overridden because UnboundedFifoBuffer doesn't allow null elements.
64      * @return false
65      */

66     public boolean isNullSupported() {
67         return false;
68     }
69
70     /**
71      * Overridden because UnboundedFifoBuffer isn't fail fast.
72      * @return false
73      */

74     public boolean isFailFastSupported() {
75         return false;
76     }
77
78     //-----------------------------------------------------------------------
79
/**
80      * Returns an empty ArrayList.
81      *
82      * @return an empty ArrayList
83      */

84     public Collection JavaDoc makeConfirmedCollection() {
85         return new ArrayList JavaDoc();
86     }
87
88     /**
89      * Returns a full ArrayList.
90      *
91      * @return a full ArrayList
92      */

93     public Collection JavaDoc makeConfirmedFullCollection() {
94         Collection JavaDoc c = makeConfirmedCollection();
95         c.addAll(java.util.Arrays.asList(getFullElements()));
96         return c;
97     }
98
99     /**
100      * Returns an empty BoundedFifoBuffer that won't overflow.
101      *
102      * @return an empty BoundedFifoBuffer
103      */

104     public Collection JavaDoc makeCollection() {
105         return new BoundedFifoBuffer(100);
106     }
107
108     //-----------------------------------------------------------------------
109
/**
110      * Tests that the removal operation actually removes the first element.
111      */

112     public void testBoundedFifoBufferRemove() {
113         resetFull();
114         int size = confirmed.size();
115         for (int i = 0; i < size; i++) {
116             Object JavaDoc o1 = ((BoundedFifoBuffer)collection).remove();
117             Object JavaDoc o2 = ((ArrayList JavaDoc)confirmed).remove(0);
118             assertEquals("Removed objects should be equal", o1, o2);
119             verify();
120         }
121
122         try {
123             ((BoundedFifoBuffer)collection).remove();
124             fail("Empty buffer should raise Underflow.");
125         } catch (BufferUnderflowException e) {
126             // expected
127
}
128     }
129
130     /**
131      * Tests that the constructor correctly throws an exception.
132      */

133     public void testConstructorException1() {
134         try {
135             new BoundedFifoBuffer(0);
136         } catch (IllegalArgumentException JavaDoc ex) {
137             return;
138         }
139         fail();
140     }
141     
142     /**
143      * Tests that the constructor correctly throws an exception.
144      */

145     public void testConstructorException2() {
146         try {
147             new BoundedFifoBuffer(-20);
148         } catch (IllegalArgumentException JavaDoc ex) {
149             return;
150         }
151         fail();
152     }
153
154     /**
155      * Tests that the constructor correctly throws an exception.
156      */

157     public void testConstructorException3() {
158         try {
159             new BoundedFifoBuffer(null);
160         } catch (NullPointerException JavaDoc ex) {
161             return;
162         }
163         fail();
164     }
165 }
166
Popular Tags