KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > buffer > TestBoundedBuffer


1 /*
2  * Copyright 2005-2006 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.buffer;
17
18 import org.apache.commons.collections.AbstractTestObject;
19 import org.apache.commons.collections.BoundedCollection;
20 import org.apache.commons.collections.Buffer;
21 import org.apache.commons.collections.BufferOverflowException;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Arrays JavaDoc;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 public class TestBoundedBuffer extends AbstractTestObject {
31
32     public TestBoundedBuffer(String JavaDoc testName) {
33         super(testName);
34     }
35
36     public static Test suite() {
37         return new TestSuite(TestBoundedBuffer.class);
38     }
39
40     public static void main(String JavaDoc args[]) {
41         String JavaDoc[] testCaseName = { TestBoundedBuffer.class.getName() };
42         junit.textui.TestRunner.main(testCaseName);
43     }
44
45     public String JavaDoc getCompatibilityVersion() {
46         return "3.2";
47     }
48
49     public boolean isEqualsCheckable() {
50         return false;
51     }
52
53     public Object JavaDoc makeObject() {
54         return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
55     }
56
57     //-----------------------------------------------------------------------
58
public void testMaxSize() {
59         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
60         BoundedCollection bc = (BoundedCollection) bounded;
61         assertEquals(2, bc.maxSize());
62         assertEquals(false, bc.isFull());
63         bounded.add("A");
64         assertEquals(false, bc.isFull());
65         bounded.add("B");
66         assertEquals(true, bc.isFull());
67         bounded.remove();
68         assertEquals(false, bc.isFull());
69         try {
70             BoundedBuffer.decorate(new UnboundedFifoBuffer(), 0);
71             fail();
72         } catch (IllegalArgumentException JavaDoc ex) {}
73         try {
74             BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1);
75             fail();
76         } catch (IllegalArgumentException JavaDoc ex) {}
77     }
78
79     public void testAddToFullBufferNoTimeout() {
80         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
81         bounded.add( "Hello" );
82         try {
83             bounded.add("World");
84             fail();
85         } catch (BufferOverflowException e) {
86         }
87     }
88
89     public void testAddAllToFullBufferNoTimeout() {
90         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
91         bounded.add( "Hello" );
92         try {
93             bounded.addAll(Collections.singleton("World"));
94             fail();
95         } catch (BufferOverflowException e) {
96         }
97     }
98
99     public void testAddAllToEmptyBufferExceedMaxSizeNoTimeout() {
100         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1);
101         try {
102             bounded.addAll(Collections.nCopies(2, "test"));
103             fail();
104         } catch (BufferOverflowException e) {
105         }
106     }
107
108     public void testAddToFullBufferRemoveViaIterator() {
109         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
110         bounded.add( "Hello" );
111         new DelayedIteratorRemove( bounded, 200 ).start();
112         bounded.add( "World" );
113         assertEquals( 1, bounded.size() );
114         assertEquals( "World", bounded.get() );
115
116     }
117
118     public void testAddAllToFullBufferRemoveViaIterator() {
119         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
120         bounded.add( "Hello" );
121         bounded.add( "World" );
122         new DelayedIteratorRemove( bounded, 200, 2 ).start();
123         bounded.addAll( Arrays.asList( new String JavaDoc[] { "Foo", "Bar" } ) );
124         assertEquals( 2, bounded.size() );
125         assertEquals( "Foo", bounded.remove() );
126         assertEquals( "Bar", bounded.remove() );
127     }
128
129     public void testAddToFullBufferWithTimeout() {
130         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
131         bounded.add( "Hello" );
132         new DelayedRemove( bounded, 200 ).start();
133         bounded.add( "World" );
134         assertEquals( 1, bounded.size() );
135         assertEquals( "World", bounded.get() );
136         try {
137             bounded.add( "!" );
138             fail();
139         }
140         catch( BufferOverflowException e ) {
141         }
142     }
143
144     public void testAddAllToFullBufferWithTimeout() {
145         final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
146         bounded.add( "Hello" );
147         bounded.add( "World" );
148         new DelayedRemove( bounded, 200, 2 ).start();
149
150         bounded.addAll( Arrays.asList( new String JavaDoc[] { "Foo", "Bar" } ) );
151         assertEquals( 2, bounded.size() );
152         assertEquals( "Foo", bounded.get() );
153         try {
154             bounded.add( "!" );
155             fail();
156         }
157         catch( BufferOverflowException e ) {
158         }
159     }
160
161     private class DelayedIteratorRemove extends Thread JavaDoc {
162
163         private final Buffer buffer;
164
165         private final long delay;
166
167         private final int nToRemove;
168
169         public DelayedIteratorRemove(Buffer buffer, long delay, int nToRemove) {
170             this.buffer = buffer;
171             this.delay = delay;
172             this.nToRemove = nToRemove;
173         }
174
175         public DelayedIteratorRemove(Buffer buffer, long delay) {
176             this(buffer, delay, 1);
177         }
178
179         public void run() {
180             try {
181                 Thread.sleep(delay);
182                 Iterator JavaDoc iter = buffer.iterator();
183                 for (int i = 0; i < nToRemove; ++i) {
184                     iter.next();
185                     iter.remove();
186                 }
187
188             } catch (InterruptedException JavaDoc e) {
189             }
190         }
191     }
192
193     private class DelayedRemove extends Thread JavaDoc {
194
195         private final Buffer buffer;
196
197         private final long delay;
198
199         private final int nToRemove;
200
201         public DelayedRemove(Buffer buffer, long delay, int nToRemove) {
202             this.buffer = buffer;
203             this.delay = delay;
204             this.nToRemove = nToRemove;
205         }
206
207         public DelayedRemove(Buffer buffer, long delay) {
208             this(buffer, delay, 1);
209         }
210
211         public void run() {
212             try {
213                 Thread.sleep(delay);
214                 for (int i = 0; i < nToRemove; ++i) {
215                     buffer.remove();
216                 }
217             } catch (InterruptedException JavaDoc e) {
218             }
219         }
220     }
221 }
Popular Tags