KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > 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.buffer;
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.BufferUnderflowException;
25 import org.apache.commons.collections.BulkTest;
26 import org.apache.commons.collections.collection.AbstractTestCollection;
27
28 /**
29  * Test cases for BoundedFifoBuffer.
30  *
31  * @version $Revision: 1.4 $ $Date: 2004/06/02 23:12:45 $
32  *
33  * @author Paul Jack
34  */

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

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

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

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

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

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

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

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

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

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

159     public void testConstructorException3() {
160         try {
161             new BoundedFifoBuffer(null);
162         } catch (NullPointerException JavaDoc ex) {
163             return;
164         }
165         fail();
166     }
167
168     public String JavaDoc getCompatibilityVersion() {
169         return "3.1";
170     }
171
172 // public void testCreate() throws Exception {
173
// resetEmpty();
174
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/BoundedFifoBuffer.emptyCollection.version3.1.obj");
175
// resetFull();
176
// writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/BoundedFifoBuffer.fullCollection.version3.1.obj");
177
// }
178

179 }
180
Popular Tags