001: /*
002: * Copyright 2001-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.buffer;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.List;
026:
027: import junit.framework.Test;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.collections.Buffer;
031: import org.apache.commons.collections.BufferUnderflowException;
032: import org.apache.commons.collections.BulkTest;
033: import org.apache.commons.collections.collection.AbstractTestCollection;
034:
035: /**
036: * Test cases for CircularFifoBuffer.
037: *
038: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
039: *
040: * @author Stephen Colebourne
041: */
042: public class TestCircularFifoBuffer extends AbstractTestCollection {
043:
044: public TestCircularFifoBuffer(String n) {
045: super (n);
046: }
047:
048: public static Test suite() {
049: return BulkTest.makeSuite(TestCircularFifoBuffer.class);
050: }
051:
052: public static void main(String args[]) {
053: TestRunner.run(TestCircularFifoBuffer.class);
054: }
055:
056: //-----------------------------------------------------------------------
057: /**
058: * Runs through the regular verifications, but also verifies that
059: * the buffer contains the same elements in the same sequence as the
060: * list.
061: */
062: public void verify() {
063: super .verify();
064: Iterator iterator1 = collection.iterator();
065: Iterator iterator2 = confirmed.iterator();
066: while (iterator2.hasNext()) {
067: assertTrue(iterator1.hasNext());
068: Object o1 = iterator1.next();
069: Object o2 = iterator2.next();
070: assertEquals(o1, o2);
071: }
072: }
073:
074: //-----------------------------------------------------------------------
075: /**
076: * Overridden because UnboundedFifoBuffer doesn't allow null elements.
077: * @return false
078: */
079: public boolean isNullSupported() {
080: return false;
081: }
082:
083: /**
084: * Overridden because UnboundedFifoBuffer isn't fail fast.
085: * @return false
086: */
087: public boolean isFailFastSupported() {
088: return false;
089: }
090:
091: //-----------------------------------------------------------------------
092: /**
093: * Returns an empty ArrayList.
094: *
095: * @return an empty ArrayList
096: */
097: public Collection makeConfirmedCollection() {
098: return new ArrayList();
099: }
100:
101: /**
102: * Returns a full ArrayList.
103: *
104: * @return a full ArrayList
105: */
106: public Collection makeConfirmedFullCollection() {
107: Collection c = makeConfirmedCollection();
108: c.addAll(java.util.Arrays.asList(getFullElements()));
109: return c;
110: }
111:
112: /**
113: * Returns an empty BoundedFifoBuffer that won't overflow.
114: *
115: * @return an empty BoundedFifoBuffer
116: */
117: public Collection makeCollection() {
118: return new CircularFifoBuffer(100);
119: }
120:
121: //-----------------------------------------------------------------------
122: /**
123: * Tests that the removal operation actually removes the first element.
124: */
125: public void testCircularFifoBufferCircular() {
126: List list = new ArrayList();
127: list.add("A");
128: list.add("B");
129: list.add("C");
130: Buffer buf = new CircularFifoBuffer(list);
131:
132: assertEquals(true, buf.contains("A"));
133: assertEquals(true, buf.contains("B"));
134: assertEquals(true, buf.contains("C"));
135:
136: buf.add("D");
137:
138: assertEquals(false, buf.contains("A"));
139: assertEquals(true, buf.contains("B"));
140: assertEquals(true, buf.contains("C"));
141: assertEquals(true, buf.contains("D"));
142:
143: assertEquals("B", buf.get());
144: assertEquals("B", buf.remove());
145: assertEquals("C", buf.remove());
146: assertEquals("D", buf.remove());
147: }
148:
149: /**
150: * Tests that the removal operation actually removes the first element.
151: */
152: public void testCircularFifoBufferRemove() {
153: resetFull();
154: int size = confirmed.size();
155: for (int i = 0; i < size; i++) {
156: Object o1 = ((CircularFifoBuffer) collection).remove();
157: Object o2 = ((ArrayList) confirmed).remove(0);
158: assertEquals("Removed objects should be equal", o1, o2);
159: verify();
160: }
161:
162: try {
163: ((CircularFifoBuffer) collection).remove();
164: fail("Empty buffer should raise Underflow.");
165: } catch (BufferUnderflowException e) {
166: // expected
167: }
168: }
169:
170: /**
171: * Tests that the constructor correctly throws an exception.
172: */
173: public void testConstructorException1() {
174: try {
175: new CircularFifoBuffer(0);
176: } catch (IllegalArgumentException ex) {
177: return;
178: }
179: fail();
180: }
181:
182: /**
183: * Tests that the constructor correctly throws an exception.
184: */
185: public void testConstructorException2() {
186: try {
187: new CircularFifoBuffer(-20);
188: } catch (IllegalArgumentException ex) {
189: return;
190: }
191: fail();
192: }
193:
194: /**
195: * Tests that the constructor correctly throws an exception.
196: */
197: public void testConstructorException3() {
198: try {
199: new CircularFifoBuffer(null);
200: } catch (NullPointerException ex) {
201: return;
202: }
203: fail();
204: }
205:
206: public void testRemoveError1() throws Exception {
207: // based on bug 33071
208: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
209: fifo.add("1");
210: fifo.add("2");
211: fifo.add("3");
212: fifo.add("4");
213: fifo.add("5");
214:
215: assertEquals("[1, 2, 3, 4, 5]", fifo.toString());
216:
217: fifo.remove("3");
218: assertEquals("[1, 2, 4, 5]", fifo.toString());
219:
220: fifo.remove("4");
221: assertEquals("[1, 2, 5]", fifo.toString());
222: }
223:
224: public void testRemoveError2() throws Exception {
225: // based on bug 33071
226: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
227: fifo.add("1");
228: fifo.add("2");
229: fifo.add("3");
230: fifo.add("4");
231: fifo.add("5");
232: fifo.add("6");
233:
234: assertEquals(5, fifo.size());
235: assertEquals("[2, 3, 4, 5, 6]", fifo.toString());
236:
237: fifo.remove("3");
238: assertEquals("[2, 4, 5, 6]", fifo.toString());
239:
240: fifo.remove("4");
241: assertEquals("[2, 5, 6]", fifo.toString());
242: }
243:
244: public void testRemoveError3() throws Exception {
245: // based on bug 33071
246: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
247: fifo.add("1");
248: fifo.add("2");
249: fifo.add("3");
250: fifo.add("4");
251: fifo.add("5");
252:
253: assertEquals("[1, 2, 3, 4, 5]", fifo.toString());
254:
255: fifo.remove("3");
256: assertEquals("[1, 2, 4, 5]", fifo.toString());
257:
258: fifo.add("6");
259: fifo.add("7");
260: assertEquals("[2, 4, 5, 6, 7]", fifo.toString());
261:
262: fifo.remove("4");
263: assertEquals("[2, 5, 6, 7]", fifo.toString());
264: }
265:
266: public void testRemoveError4() throws Exception {
267: // based on bug 33071
268: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
269: fifo.add("1");
270: fifo.add("2");
271: fifo.add("3");
272: fifo.add("4");
273: fifo.add("5"); // end=0
274: fifo.add("6"); // end=1
275: fifo.add("7"); // end=2
276:
277: assertEquals("[3, 4, 5, 6, 7]", fifo.toString());
278:
279: fifo.remove("4"); // remove element in middle of array, after start
280: assertEquals("[3, 5, 6, 7]", fifo.toString());
281: }
282:
283: public void testRemoveError5() throws Exception {
284: // based on bug 33071
285: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
286: fifo.add("1");
287: fifo.add("2");
288: fifo.add("3");
289: fifo.add("4");
290: fifo.add("5"); // end=0
291: fifo.add("6"); // end=1
292: fifo.add("7"); // end=2
293:
294: assertEquals("[3, 4, 5, 6, 7]", fifo.toString());
295:
296: fifo.remove("5"); // remove element at last pos in array
297: assertEquals("[3, 4, 6, 7]", fifo.toString());
298: }
299:
300: public void testRemoveError6() throws Exception {
301: // based on bug 33071
302: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
303: fifo.add("1");
304: fifo.add("2");
305: fifo.add("3");
306: fifo.add("4");
307: fifo.add("5"); // end=0
308: fifo.add("6"); // end=1
309: fifo.add("7"); // end=2
310:
311: assertEquals("[3, 4, 5, 6, 7]", fifo.toString());
312:
313: fifo.remove("6"); // remove element at position zero in array
314: assertEquals("[3, 4, 5, 7]", fifo.toString());
315: }
316:
317: public void testRemoveError7() throws Exception {
318: // based on bug 33071
319: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
320: fifo.add("1");
321: fifo.add("2");
322: fifo.add("3");
323: fifo.add("4");
324: fifo.add("5"); // end=0
325: fifo.add("6"); // end=1
326: fifo.add("7"); // end=2
327:
328: assertEquals("[3, 4, 5, 6, 7]", fifo.toString());
329:
330: fifo.remove("7"); // remove element at position one in array
331: assertEquals("[3, 4, 5, 6]", fifo.toString());
332: }
333:
334: public void testRemoveError8() throws Exception {
335: // based on bug 33071
336: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
337: fifo.add("1");
338: fifo.add("2");
339: fifo.add("3");
340: fifo.add("4");
341: fifo.add("5"); // end=0
342: fifo.add("6"); // end=1
343: fifo.add("7"); // end=2
344: fifo.add("8"); // end=3
345:
346: assertEquals("[4, 5, 6, 7, 8]", fifo.toString());
347:
348: fifo.remove("7"); // remove element at position one in array, need to shift 8
349: assertEquals("[4, 5, 6, 8]", fifo.toString());
350: }
351:
352: public void testRemoveError9() throws Exception {
353: // based on bug 33071
354: CircularFifoBuffer fifo = new CircularFifoBuffer(5);
355: fifo.add("1");
356: fifo.add("2");
357: fifo.add("3");
358: fifo.add("4");
359: fifo.add("5"); // end=0
360: fifo.add("6"); // end=1
361: fifo.add("7"); // end=2
362: fifo.add("8"); // end=3
363:
364: assertEquals("[4, 5, 6, 7, 8]", fifo.toString());
365:
366: fifo.remove("8"); // remove element at position two in array
367: assertEquals("[4, 5, 6, 7]", fifo.toString());
368: }
369:
370: //-----------------------------------------------------------------------
371: public void testRepeatedSerialization() throws Exception {
372: // bug 31433
373: CircularFifoBuffer b = new CircularFifoBuffer(2);
374: b.add("a");
375: assertEquals(1, b.size());
376: assertEquals(true, b.contains("a"));
377:
378: ByteArrayOutputStream bos = new ByteArrayOutputStream();
379: new ObjectOutputStream(bos).writeObject(b);
380:
381: CircularFifoBuffer b2 = (CircularFifoBuffer) new ObjectInputStream(
382: new ByteArrayInputStream(bos.toByteArray()))
383: .readObject();
384:
385: assertEquals(1, b2.size());
386: assertEquals(true, b2.contains("a"));
387: b2.add("b");
388: assertEquals(2, b2.size());
389: assertEquals(true, b2.contains("a"));
390: assertEquals(true, b2.contains("b"));
391:
392: bos = new ByteArrayOutputStream();
393: new ObjectOutputStream(bos).writeObject(b2);
394:
395: CircularFifoBuffer b3 = (CircularFifoBuffer) new ObjectInputStream(
396: new ByteArrayInputStream(bos.toByteArray()))
397: .readObject();
398:
399: assertEquals(2, b3.size());
400: assertEquals(true, b3.contains("a"));
401: assertEquals(true, b3.contains("b"));
402: b3.add("c");
403: assertEquals(2, b3.size());
404: assertEquals(true, b3.contains("b"));
405: assertEquals(true, b3.contains("c"));
406: }
407:
408: public String getCompatibilityVersion() {
409: return "3.1";
410: }
411:
412: // public void testCreate() throws Exception {
413: // resetEmpty();
414: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/CircularFifoBuffer.emptyCollection.version3.1.obj");
415: // resetFull();
416: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/CircularFifoBuffer.fullCollection.version3.1.obj");
417: // }
418:
419: }
|