1 package org.sapia.ubik.net; 2 3 import junit.framework.*; 4 5 import java.util.*; 6 7 8 16 public class PoolTest extends TestCase { 17 21 public PoolTest(String arg0) { 22 super(arg0); 23 } 24 25 public void testAcquire() throws Exception { 26 Pool p = new TestPool(); 27 28 List acquired = new ArrayList(); 29 Object o; 30 31 for (int i = 0; i < 10; i++) { 32 super.assertEquals("anObject" + i, o = p.acquire()); 33 acquired.add(o); 34 } 35 36 for (int i = 0; i < acquired.size(); i++) { 37 p.release(acquired.get(i)); 38 } 39 40 for (int i = 0; i < 10; i++) { 41 super.assertEquals("anObject" + i, o = p.acquire()); 42 acquired.add(o); 43 } 44 } 45 46 public void testAcquireTimeout() throws Exception { 47 Pool p = new TestPool(5); 48 49 for (int i = 0; i < 5; i++) { 50 p.acquire(); 51 } 52 53 try { 54 p.acquire(1000); 55 throw new Exception ("object should not have been acquired"); 56 } catch (NoObjectAvailableException e) { 57 } 59 } 60 61 public void testShrink() throws Exception { 62 Pool p = new TestPool(); 63 List acquired = new ArrayList(); 64 Object o; 65 66 for (int i = 0; i < 10; i++) { 67 o = p.acquire(); 68 acquired.add(o); 69 } 70 71 for (int i = 0; i < acquired.size(); i++) { 72 p.release(acquired.get(i)); 73 } 74 75 super.assertEquals(10, p.getCreatedCount()); 76 super.assertEquals(10, p.size()); 77 p.shrinkTo(0); 78 super.assertEquals(0, p.getCreatedCount()); 79 super.assertEquals(0, p.size()); 80 } 81 82 static class TestPool extends Pool { 83 86 public TestPool() { 87 super(); 88 } 89 90 94 public TestPool(int maxSize) { 95 super(maxSize); 96 } 97 98 101 protected Object doNewObject() throws Exception { 102 return "anObject" + getCreatedCount(); 103 } 104 } 105 } 106 | Popular Tags |