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.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Iterator;
021:
022: import junit.framework.Test;
023:
024: import org.apache.commons.collections.BufferUnderflowException;
025: import org.apache.commons.collections.BulkTest;
026: import org.apache.commons.collections.collection.AbstractTestCollection;
027:
028: /**
029: * Test cases for BoundedFifoBuffer.
030: *
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Paul Jack
034: */
035: public class TestBoundedFifoBuffer extends AbstractTestCollection {
036:
037: public TestBoundedFifoBuffer(String n) {
038: super (n);
039: }
040:
041: public static Test suite() {
042: return BulkTest.makeSuite(TestBoundedFifoBuffer.class);
043: }
044:
045: //-----------------------------------------------------------------------
046: /**
047: * Runs through the regular verifications, but also verifies that
048: * the buffer contains the same elements in the same sequence as the
049: * list.
050: */
051: public void verify() {
052: super .verify();
053: Iterator iterator1 = collection.iterator();
054: Iterator iterator2 = confirmed.iterator();
055: while (iterator2.hasNext()) {
056: assertTrue(iterator1.hasNext());
057: Object o1 = iterator1.next();
058: Object o2 = iterator2.next();
059: assertEquals(o1, o2);
060: }
061: }
062:
063: //-----------------------------------------------------------------------
064: /**
065: * Overridden because UnboundedFifoBuffer doesn't allow null elements.
066: * @return false
067: */
068: public boolean isNullSupported() {
069: return false;
070: }
071:
072: /**
073: * Overridden because UnboundedFifoBuffer isn't fail fast.
074: * @return false
075: */
076: public boolean isFailFastSupported() {
077: return false;
078: }
079:
080: //-----------------------------------------------------------------------
081: /**
082: * Returns an empty ArrayList.
083: *
084: * @return an empty ArrayList
085: */
086: public Collection makeConfirmedCollection() {
087: return new ArrayList();
088: }
089:
090: /**
091: * Returns a full ArrayList.
092: *
093: * @return a full ArrayList
094: */
095: public Collection makeConfirmedFullCollection() {
096: Collection c = makeConfirmedCollection();
097: c.addAll(java.util.Arrays.asList(getFullElements()));
098: return c;
099: }
100:
101: /**
102: * Returns an empty BoundedFifoBuffer that won't overflow.
103: *
104: * @return an empty BoundedFifoBuffer
105: */
106: public Collection 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 o1 = ((BoundedFifoBuffer) collection).remove();
119: Object o2 = ((ArrayList) 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 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 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 ex) {
163: return;
164: }
165: fail();
166: }
167:
168: public String getCompatibilityVersion() {
169: return "3.1";
170: }
171:
172: // BZ 33071 -- gets start=end=1 before removal of interior element
173: public void testShift() {
174: BoundedFifoBuffer fifo = new BoundedFifoBuffer(3);
175: fifo.add("a");
176: fifo.add("b");
177: fifo.add("c");
178: fifo.remove();
179: fifo.add("e");
180: fifo.remove("c");
181: }
182:
183: // public void testCreate() throws Exception {
184: // resetEmpty();
185: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/BoundedFifoBuffer.emptyCollection.version3.1.obj");
186: // resetFull();
187: // writeExternalFormToDisk((java.io.Serializable) collection, "D:/dev/collections/data/test/BoundedFifoBuffer.fullCollection.version3.1.obj");
188: // }
189:
190: }
|