001: /*
002: * Copyright 2005-2006 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 org.apache.commons.collections.AbstractTestObject;
019: import org.apache.commons.collections.BoundedCollection;
020: import org.apache.commons.collections.Buffer;
021: import org.apache.commons.collections.BufferOverflowException;
022:
023: import java.util.Iterator;
024: import java.util.Collections;
025: import java.util.Arrays;
026:
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: public class TestBoundedBuffer extends AbstractTestObject {
031:
032: public TestBoundedBuffer(String testName) {
033: super (testName);
034: }
035:
036: public static Test suite() {
037: return new TestSuite(TestBoundedBuffer.class);
038: }
039:
040: public static void main(String args[]) {
041: String[] testCaseName = { TestBoundedBuffer.class.getName() };
042: junit.textui.TestRunner.main(testCaseName);
043: }
044:
045: public String getCompatibilityVersion() {
046: return "3.2";
047: }
048:
049: public boolean isEqualsCheckable() {
050: return false;
051: }
052:
053: public Object makeObject() {
054: return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
055: }
056:
057: //-----------------------------------------------------------------------
058: public void testMaxSize() {
059: final Buffer bounded = BoundedBuffer.decorate(
060: new UnboundedFifoBuffer(), 2, 500);
061: BoundedCollection bc = (BoundedCollection) bounded;
062: assertEquals(2, bc.maxSize());
063: assertEquals(false, bc.isFull());
064: bounded.add("A");
065: assertEquals(false, bc.isFull());
066: bounded.add("B");
067: assertEquals(true, bc.isFull());
068: bounded.remove();
069: assertEquals(false, bc.isFull());
070: try {
071: BoundedBuffer.decorate(new UnboundedFifoBuffer(), 0);
072: fail();
073: } catch (IllegalArgumentException ex) {
074: }
075: try {
076: BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1);
077: fail();
078: } catch (IllegalArgumentException ex) {
079: }
080: }
081:
082: public void testAddToFullBufferNoTimeout() {
083: final Buffer bounded = BoundedBuffer.decorate(
084: new UnboundedFifoBuffer(), 1);
085: bounded.add("Hello");
086: try {
087: bounded.add("World");
088: fail();
089: } catch (BufferOverflowException e) {
090: }
091: }
092:
093: public void testAddAllToFullBufferNoTimeout() {
094: final Buffer bounded = BoundedBuffer.decorate(
095: new UnboundedFifoBuffer(), 1);
096: bounded.add("Hello");
097: try {
098: bounded.addAll(Collections.singleton("World"));
099: fail();
100: } catch (BufferOverflowException e) {
101: }
102: }
103:
104: public void testAddAllToEmptyBufferExceedMaxSizeNoTimeout() {
105: final Buffer bounded = BoundedBuffer.decorate(
106: new UnboundedFifoBuffer(), 1);
107: try {
108: bounded.addAll(Collections.nCopies(2, "test"));
109: fail();
110: } catch (BufferOverflowException e) {
111: }
112: }
113:
114: public void testAddToFullBufferRemoveViaIterator() {
115: final Buffer bounded = BoundedBuffer.decorate(
116: new UnboundedFifoBuffer(), 1, 500);
117: bounded.add("Hello");
118: new DelayedIteratorRemove(bounded, 200).start();
119: bounded.add("World");
120: assertEquals(1, bounded.size());
121: assertEquals("World", bounded.get());
122:
123: }
124:
125: public void testAddAllToFullBufferRemoveViaIterator() {
126: final Buffer bounded = BoundedBuffer.decorate(
127: new UnboundedFifoBuffer(), 2, 500);
128: bounded.add("Hello");
129: bounded.add("World");
130: new DelayedIteratorRemove(bounded, 200, 2).start();
131: bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
132: assertEquals(2, bounded.size());
133: assertEquals("Foo", bounded.remove());
134: assertEquals("Bar", bounded.remove());
135: }
136:
137: public void testAddToFullBufferWithTimeout() {
138: final Buffer bounded = BoundedBuffer.decorate(
139: new UnboundedFifoBuffer(), 1, 500);
140: bounded.add("Hello");
141: new DelayedRemove(bounded, 200).start();
142: bounded.add("World");
143: assertEquals(1, bounded.size());
144: assertEquals("World", bounded.get());
145: try {
146: bounded.add("!");
147: fail();
148: } catch (BufferOverflowException e) {
149: }
150: }
151:
152: public void testAddAllToFullBufferWithTimeout() {
153: final Buffer bounded = BoundedBuffer.decorate(
154: new UnboundedFifoBuffer(), 2, 500);
155: bounded.add("Hello");
156: bounded.add("World");
157: new DelayedRemove(bounded, 200, 2).start();
158:
159: bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
160: assertEquals(2, bounded.size());
161: assertEquals("Foo", bounded.get());
162: try {
163: bounded.add("!");
164: fail();
165: } catch (BufferOverflowException e) {
166: }
167: }
168:
169: private class DelayedIteratorRemove extends Thread {
170:
171: private final Buffer buffer;
172:
173: private final long delay;
174:
175: private final int nToRemove;
176:
177: public DelayedIteratorRemove(Buffer buffer, long delay,
178: int nToRemove) {
179: this .buffer = buffer;
180: this .delay = delay;
181: this .nToRemove = nToRemove;
182: }
183:
184: public DelayedIteratorRemove(Buffer buffer, long delay) {
185: this (buffer, delay, 1);
186: }
187:
188: public void run() {
189: try {
190: Thread.sleep(delay);
191: Iterator iter = buffer.iterator();
192: for (int i = 0; i < nToRemove; ++i) {
193: iter.next();
194: iter.remove();
195: }
196:
197: } catch (InterruptedException e) {
198: }
199: }
200: }
201:
202: private class DelayedRemove extends Thread {
203:
204: private final Buffer buffer;
205:
206: private final long delay;
207:
208: private final int nToRemove;
209:
210: public DelayedRemove(Buffer buffer, long delay, int nToRemove) {
211: this .buffer = buffer;
212: this .delay = delay;
213: this .nToRemove = nToRemove;
214: }
215:
216: public DelayedRemove(Buffer buffer, long delay) {
217: this (buffer, delay, 1);
218: }
219:
220: public void run() {
221: try {
222: Thread.sleep(delay);
223: for (int i = 0; i < nToRemove; ++i) {
224: buffer.remove();
225: }
226: } catch (InterruptedException e) {
227: }
228: }
229: }
230: }
|