KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > TestCircularFifoBuffer


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.buffer;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.Test;
24
25 import org.apache.commons.collections.Buffer;
26 import org.apache.commons.collections.BufferUnderflowException;
27 import org.apache.commons.collections.BulkTest;
28 import org.apache.commons.collections.collection.AbstractTestCollection;
29
30 /**
31  * Test cases for CircularFifoBuffer.
32  *
33  * @version $Revision: 1.4 $ $Date: 2004/06/02 23:12:45 $
34  *
35  * @author Stephen Colebourne
36  */

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

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

70     public boolean isNullSupported() {
71         return false;
72     }
73
74     /**
75      * Overridden because UnboundedFifoBuffer isn't fail fast.
76      * @return false
77      */

78     public boolean isFailFastSupported() {
79         return false;
80     }
81
82     //-----------------------------------------------------------------------
83
/**
84      * Returns an empty ArrayList.
85      *
86      * @return an empty ArrayList
87      */

88     public Collection JavaDoc makeConfirmedCollection() {
89         return new ArrayList JavaDoc();
90     }
91
92     /**
93      * Returns a full ArrayList.
94      *
95      * @return a full ArrayList
96      */

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

108     public Collection JavaDoc makeCollection() {
109         return new CircularFifoBuffer(100);
110     }
111
112     //-----------------------------------------------------------------------
113
/**
114      * Tests that the removal operation actually removes the first element.
115      */

116     public void testCircularFifoBufferCircular() {
117         List JavaDoc list = new ArrayList JavaDoc();
118         list.add("A");
119         list.add("B");
120         list.add("C");
121         Buffer buf = new CircularFifoBuffer(list);
122         
123         assertEquals(true, buf.contains("A"));
124         assertEquals(true, buf.contains("B"));
125         assertEquals(true, buf.contains("C"));
126         
127         buf.add("D");
128         
129         assertEquals(false, buf.contains("A"));
130         assertEquals(true, buf.contains("B"));
131         assertEquals(true, buf.contains("C"));
132         assertEquals(true, buf.contains("D"));
133         
134         assertEquals("B", buf.get());
135         assertEquals("B", buf.remove());
136         assertEquals("C", buf.remove());
137         assertEquals("D", buf.remove());
138     }
139
140     /**
141      * Tests that the removal operation actually removes the first element.
142      */

143     public void testCircularFifoBufferRemove() {
144         resetFull();
145         int size = confirmed.size();
146         for (int i = 0; i < size; i++) {
147             Object JavaDoc o1 = ((CircularFifoBuffer) collection).remove();
148             Object JavaDoc o2 = ((ArrayList JavaDoc) confirmed).remove(0);
149             assertEquals("Removed objects should be equal", o1, o2);
150             verify();
151         }
152
153         try {
154             ((CircularFifoBuffer) collection).remove();
155             fail("Empty buffer should raise Underflow.");
156         } catch (BufferUnderflowException e) {
157             // expected
158
}
159     }
160
161     /**
162      * Tests that the constructor correctly throws an exception.
163      */

164     public void testConstructorException1() {
165         try {
166             new CircularFifoBuffer(0);
167         } catch (IllegalArgumentException JavaDoc ex) {
168             return;
169         }
170         fail();
171     }
172
173     /**
174      * Tests that the constructor correctly throws an exception.
175      */

176     public void testConstructorException2() {
177         try {
178             new CircularFifoBuffer(-20);
179         } catch (IllegalArgumentException JavaDoc ex) {
180             return;
181         }
182         fail();
183     }
184     
185     /**
186      * Tests that the constructor correctly throws an exception.
187      */

188     public void testConstructorException3() {
189         try {
190             new CircularFifoBuffer(null);
191         } catch (NullPointerException JavaDoc ex) {
192             return;
193         }
194         fail();
195     }
196
197     public String JavaDoc getCompatibilityVersion() {
198         return "3.1";
199     }
200
201 // public void testCreate() throws Exception {
202
// resetEmpty();
203
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/CircularFifoBuffer.emptyCollection.version3.1.obj");
204
// resetFull();
205
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/CircularFifoBuffer.fullCollection.version3.1.obj");
206
// }
207

208 }
209
Popular Tags