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.Arrays;
019: import java.util.Collection;
020:
021: import junit.framework.Test;
022:
023: import org.apache.commons.collections.BoundedCollection;
024: import org.apache.commons.collections.BufferOverflowException;
025: import org.apache.commons.collections.BulkTest;
026:
027: /**
028: * Runs tests against a full BoundedFifoBuffer, since many of the algorithms
029: * differ depending on whether the fifo is full or not.
030: *
031: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
032: *
033: * @author Unknown
034: */
035: public class TestBoundedFifoBuffer2 extends TestBoundedFifoBuffer {
036:
037: public TestBoundedFifoBuffer2(String n) {
038: super (n);
039: }
040:
041: public static Test suite() {
042: return BulkTest.makeSuite(TestBoundedFifoBuffer2.class);
043: }
044:
045: /**
046: * Returns a BoundedFifoBuffer that's filled to capacity.
047: * Any attempt to add to the returned buffer will result in a
048: * BufferOverflowException.
049: *
050: * @return a full BoundedFifoBuffer
051: */
052: public Collection makeFullCollection() {
053: return new BoundedFifoBuffer(Arrays.asList(getFullElements()));
054: }
055:
056: /**
057: * Overridden to skip the add tests. All of them would fail with a
058: * BufferOverflowException.
059: *
060: * @return false
061: */
062: public boolean isAddSupported() {
063: return false;
064: }
065:
066: /**
067: * Overridden because the add operations raise BufferOverflowException
068: * instead of UnsupportedOperationException.
069: */
070: public void testUnsupportedAdd() {
071: }
072:
073: /**
074: * Tests to make sure the add operations raise BufferOverflowException.
075: */
076: public void testBufferOverflow() {
077: resetFull();
078: try {
079: collection.add(getOtherElements()[0]);
080: fail("add should raise BufferOverflow.");
081: } catch (BufferOverflowException e) {
082: // expected
083: }
084: verify();
085:
086: try {
087: collection.addAll(Arrays.asList(getOtherElements()));
088: fail("addAll should raise BufferOverflow.");
089: } catch (BufferOverflowException e) {
090: // expected
091: }
092: verify();
093: }
094:
095: /**
096: * Tests is full
097: */
098: public void testIsFull() {
099: resetFull();
100: assertEquals(true, ((BoundedCollection) collection).isFull());
101: ((BoundedFifoBuffer) collection).remove();
102: assertEquals(false, ((BoundedCollection) collection).isFull());
103: ((BoundedFifoBuffer) collection).add("jj");
104: assertEquals(true, ((BoundedCollection) collection).isFull());
105: }
106:
107: /**
108: * Tests max size
109: */
110: public void testMaxSize() {
111: resetFull();
112: assertEquals(getFullElements().length,
113: ((BoundedCollection) collection).maxSize());
114: ((BoundedFifoBuffer) collection).remove();
115: assertEquals(getFullElements().length,
116: ((BoundedCollection) collection).maxSize());
117: ((BoundedFifoBuffer) collection).add("jj");
118: assertEquals(getFullElements().length,
119: ((BoundedCollection) collection).maxSize());
120: }
121:
122: }
|