1 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 ; 24 import java.util.Collections ; 25 import java.util.Arrays ; 26 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 public class TestBoundedBuffer extends AbstractTestObject { 31 32 public TestBoundedBuffer(String 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 args[]) { 41 String [] testCaseName = { TestBoundedBuffer.class.getName() }; 42 junit.textui.TestRunner.main(testCaseName); 43 } 44 45 public String getCompatibilityVersion() { 46 return "3.2"; 47 } 48 49 public boolean isEqualsCheckable() { 50 return false; 51 } 52 53 public Object makeObject() { 54 return BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1); 55 } 56 57 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 ex) {} 73 try { 74 BoundedBuffer.decorate(new UnboundedFifoBuffer(), -1); 75 fail(); 76 } catch (IllegalArgumentException 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 [] { "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 [] { "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 { 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 iter = buffer.iterator(); 183 for (int i = 0; i < nToRemove; ++i) { 184 iter.next(); 185 iter.remove(); 186 } 187 188 } catch (InterruptedException e) { 189 } 190 } 191 } 192 193 private class DelayedRemove extends Thread { 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 e) { 218 } 219 } 220 } 221 } | Popular Tags |