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;
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.collection.AbstractTestCollection;
025:
026: /**
027: * Test cases for BoundedFifoBuffer.
028: *
029: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
030: *
031: * @author Paul Jack
032: */
033: public class TestBoundedFifoBuffer extends AbstractTestCollection {
034:
035: public TestBoundedFifoBuffer(String n) {
036: super (n);
037: }
038:
039: public static Test suite() {
040: return BulkTest.makeSuite(TestBoundedFifoBuffer.class);
041: }
042:
043: //-----------------------------------------------------------------------
044: /**
045: * Runs through the regular verifications, but also verifies that
046: * the buffer contains the same elements in the same sequence as the
047: * list.
048: */
049: public void verify() {
050: super .verify();
051: Iterator iterator1 = collection.iterator();
052: Iterator iterator2 = confirmed.iterator();
053: while (iterator2.hasNext()) {
054: assertTrue(iterator1.hasNext());
055: Object o1 = iterator1.next();
056: Object o2 = iterator2.next();
057: assertEquals(o1, o2);
058: }
059: }
060:
061: //-----------------------------------------------------------------------
062: /**
063: * Overridden because UnboundedFifoBuffer doesn't allow null elements.
064: * @return false
065: */
066: public boolean isNullSupported() {
067: return false;
068: }
069:
070: /**
071: * Overridden because UnboundedFifoBuffer isn't fail fast.
072: * @return false
073: */
074: public boolean isFailFastSupported() {
075: return false;
076: }
077:
078: //-----------------------------------------------------------------------
079: /**
080: * Returns an empty ArrayList.
081: *
082: * @return an empty ArrayList
083: */
084: public Collection makeConfirmedCollection() {
085: return new ArrayList();
086: }
087:
088: /**
089: * Returns a full ArrayList.
090: *
091: * @return a full ArrayList
092: */
093: public Collection makeConfirmedFullCollection() {
094: Collection c = makeConfirmedCollection();
095: c.addAll(java.util.Arrays.asList(getFullElements()));
096: return c;
097: }
098:
099: /**
100: * Returns an empty BoundedFifoBuffer that won't overflow.
101: *
102: * @return an empty BoundedFifoBuffer
103: */
104: public Collection 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 o1 = ((BoundedFifoBuffer) collection).remove();
117: Object o2 = ((ArrayList) 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 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 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 ex) {
161: return;
162: }
163: fail();
164: }
165: }
|