KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > util > ObjectPoolTest


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2003 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.test.notification.util;
22
23 import java.util.HashSet JavaDoc;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 import org.jacorb.notification.util.AbstractObjectPool;
30
31 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
32
33 /**
34  * @author Alphonse Bendt
35  */

36
37 public class ObjectPoolTest extends TestCase
38 {
39     public ObjectPoolTest(String JavaDoc name)
40     {
41         super(name);
42     }
43
44     public static Test suite()
45     {
46         TestSuite suite = new TestSuite(ObjectPoolTest.class);
47
48         return suite;
49     }
50
51     public void testGetReturnsUniqueObject() throws Exception JavaDoc
52     {
53         final SynchronizedInt counter = new SynchronizedInt(0);
54
55         int testSize = 10;
56
57         AbstractObjectPool pool = new AbstractObjectPool("Test", testSize, 1, 2, 20, 0)
58         {
59             public Object JavaDoc newInstance()
60             {
61                 return new Integer JavaDoc(counter.increment());
62             }
63         };
64
65         pool.configure(null);
66         
67         HashSet JavaDoc unique = new HashSet JavaDoc();
68
69         for (int x = 0; x < testSize * 2; ++x)
70         {
71             Integer JavaDoc i = (Integer JavaDoc) pool.lendObject();
72             assertFalse(unique.contains(i));
73             unique.add(i);
74         }
75     }
76
77     public void testMaximumElements() throws Exception JavaDoc
78     {
79         final SynchronizedInt counter = new SynchronizedInt(0);
80
81         AbstractObjectPool pool = new AbstractObjectPool("Test", 0, 0, 0, 0, 1)
82         {
83             public Object JavaDoc newInstance()
84             {
85                 return new Integer JavaDoc(counter.increment());
86             }
87         };
88         
89         pool.configure(null);
90
91         Object JavaDoc lended = pool.lendObject();
92
93         try
94         {
95             pool.lendObject();
96             fail();
97         } catch (RuntimeException JavaDoc e)
98         {
99             // expected
100
}
101
102         try
103         {
104             pool.returnObject(new Integer JavaDoc(10));
105             fail();
106         } catch (RuntimeException JavaDoc e)
107         {
108             // expected
109
}
110
111         try
112         {
113             pool.lendObject();
114             fail();
115         } catch (RuntimeException JavaDoc e)
116         {
117             // expected
118
}
119
120         pool.returnObject(lended);
121         
122         pool.lendObject();
123     }
124 }
125
Popular Tags