KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > net > PoolTest


1 package org.sapia.ubik.net;
2
3 import junit.framework.*;
4
5 import java.util.*;
6
7
8 /**
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class PoolTest extends TestCase {
17   /**
18    * Constructor for PoolTest.
19    * @param arg0
20    */

21   public PoolTest(String JavaDoc arg0) {
22     super(arg0);
23   }
24
25   public void testAcquire() throws Exception JavaDoc {
26     Pool p = new TestPool();
27
28     List acquired = new ArrayList();
29     Object JavaDoc 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 JavaDoc {
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 JavaDoc("object should not have been acquired");
56     } catch (NoObjectAvailableException e) {
57       // ok
58
}
59   }
60
61   public void testShrink() throws Exception JavaDoc {
62     Pool p = new TestPool();
63     List acquired = new ArrayList();
64     Object JavaDoc 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     /**
84      * Constructor for TestPool.
85      */

86     public TestPool() {
87       super();
88     }
89
90     /**
91      * Constructor for TestPool.
92      * @param maxSize
93      */

94     public TestPool(int maxSize) {
95       super(maxSize);
96     }
97
98     /**
99      * @see org.sapia.ubik.net.Pool#doNewObject()
100      */

101     protected Object JavaDoc doNewObject() throws Exception JavaDoc {
102       return "anObject" + getCreatedCount();
103     }
104   }
105 }
106
Popular Tags