1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package org.apache.commons.collections.buffer;
18 
19 import org.apache.commons.collections.AbstractTestObject;
20 import org.apache.commons.collections.BoundedCollection;
21 import org.apache.commons.collections.Buffer;
22 import org.apache.commons.collections.BufferOverflowException;
23 
24 import java.util.Iterator;
25 import java.util.Collections;
26 import java.util.Arrays;
27 
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30 
31 public class TestBoundedBuffer extends AbstractTestObject {
32 
TestBoundedBuffer(String testName)33     public TestBoundedBuffer(String testName) {
34         super(testName);
35     }
36 
suite()37     public static Test suite() {
38         return new TestSuite(TestBoundedBuffer.class);
39     }
40 
main(String args[])41     public static void main(String args[]) {
42         String[] testCaseName = { TestBoundedBuffer.class.getName() };
43         junit.textui.TestRunner.main(testCaseName);
44     }
45 
getCompatibilityVersion()46     public String getCompatibilityVersion() {
47         return "3.2";
48     }
49 
isEqualsCheckable()50     public boolean isEqualsCheckable() {
51         return false;
52     }
53 
makeObject()54     public Object makeObject() {
55         return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
56     }
57 
58     //-----------------------------------------------------------------------
testMaxSize()59     public void testMaxSize() {
60         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
61         BoundedCollection bc = (BoundedCollection) bounded;
62         assertEquals(2, bc.maxSize());
63         assertEquals(false, bc.isFull());
64         bounded.add("A");
65         assertEquals(false, bc.isFull());
66         bounded.add("B");
67         assertEquals(true, bc.isFull());
68         bounded.remove();
69         assertEquals(false, bc.isFull());
70         try {
71             BoundedBuffer.decorate(new UnboundedFifoBuffer(), 0);
72             fail();
73         } catch (IllegalArgumentException ex) {}
74         try {
75             BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1);
76             fail();
77         } catch (IllegalArgumentException ex) {}
78     }
79 
testAddToFullBufferNoTimeout()80     public void testAddToFullBufferNoTimeout() {
81         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
82         bounded.add( "Hello" );
83         try {
84             bounded.add("World");
85             fail();
86         } catch (BufferOverflowException e) {
87         }
88     }
89 
testAddAllToFullBufferNoTimeout()90     public void testAddAllToFullBufferNoTimeout() {
91         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
92         bounded.add( "Hello" );
93         try {
94             bounded.addAll(Collections.singleton("World"));
95             fail();
96         } catch (BufferOverflowException e) {
97         }
98     }
99 
testAddAllToEmptyBufferExceedMaxSizeNoTimeout()100     public void testAddAllToEmptyBufferExceedMaxSizeNoTimeout() {
101         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
102         try {
103             bounded.addAll(Collections.nCopies(2, "test"));
104             fail();
105         } catch (BufferOverflowException e) {
106         }
107     }
108 
testAddToFullBufferRemoveViaIterator()109     public void testAddToFullBufferRemoveViaIterator() {
110         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
111         bounded.add( "Hello" );
112         new DelayedIteratorRemove( bounded, 200 ).start();
113         bounded.add( "World" );
114         assertEquals( 1, bounded.size() );
115         assertEquals( "World", bounded.get() );
116 
117     }
118 
testAddAllToFullBufferRemoveViaIterator()119     public void testAddAllToFullBufferRemoveViaIterator() {
120         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
121         bounded.add( "Hello" );
122         bounded.add( "World" );
123         new DelayedIteratorRemove( bounded, 200, 2 ).start();
124         bounded.addAll( Arrays.asList( new String[] { "Foo", "Bar" } ) );
125         assertEquals( 2, bounded.size() );
126         assertEquals( "Foo", bounded.remove() );
127         assertEquals( "Bar", bounded.remove() );
128     }
129 
testAddToFullBufferWithTimeout()130     public void testAddToFullBufferWithTimeout() {
131         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
132         bounded.add( "Hello" );
133         new DelayedRemove( bounded, 200 ).start();
134         bounded.add( "World" );
135         assertEquals( 1, bounded.size() );
136         assertEquals( "World", bounded.get() );
137         try {
138             bounded.add( "!" );
139             fail();
140         }
141         catch( BufferOverflowException e ) {
142         }
143     }
144 
testAddAllToFullBufferWithTimeout()145     public void testAddAllToFullBufferWithTimeout() {
146         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
147         bounded.add( "Hello" );
148         bounded.add( "World" );
149         new DelayedRemove( bounded, 200, 2 ).start();
150 
151         bounded.addAll( Arrays.asList( new String[] { "Foo", "Bar" } ) );
152         assertEquals( 2, bounded.size() );
153         assertEquals( "Foo", bounded.get() );
154         try {
155             bounded.add( "!" );
156             fail();
157         }
158         catch( BufferOverflowException e ) {
159         }
160     }
161 
162     private class DelayedIteratorRemove extends Thread {
163 
164         private final Buffer buffer;
165 
166         private final long delay;
167 
168         private final int nToRemove;
169 
DelayedIteratorRemove(Buffer buffer, long delay, int nToRemove)170         public DelayedIteratorRemove(Buffer buffer, long delay, int nToRemove) {
171             this.buffer = buffer;
172             this.delay = delay;
173             this.nToRemove = nToRemove;
174         }
175 
DelayedIteratorRemove(Buffer buffer, long delay)176         public DelayedIteratorRemove(Buffer buffer, long delay) {
177             this(buffer, delay, 1);
178         }
179 
run()180         public void run() {
181             try {
182                 Thread.sleep(delay);
183                 Iterator iter = buffer.iterator();
184                 for (int i = 0; i < nToRemove; ++i) {
185                     iter.next();
186                     iter.remove();
187                 }
188 
189             } catch (InterruptedException e) {
190             }
191         }
192     }
193 
194     private class DelayedRemove extends Thread {
195 
196         private final Buffer buffer;
197 
198         private final long delay;
199 
200         private final int nToRemove;
201 
DelayedRemove(Buffer buffer, long delay, int nToRemove)202         public DelayedRemove(Buffer buffer, long delay, int nToRemove) {
203             this.buffer = buffer;
204             this.delay = delay;
205             this.nToRemove = nToRemove;
206         }
207 
DelayedRemove(Buffer buffer, long delay)208         public DelayedRemove(Buffer buffer, long delay) {
209             this(buffer, delay, 1);
210         }
211 
run()212         public void run() {
213             try {
214                 Thread.sleep(delay);
215                 for (int i = 0; i < nToRemove; ++i) {
216                     buffer.remove();
217                 }
218             } catch (InterruptedException e) {
219             }
220         }
221     }
222 }