Source Code Cross Referenced for TestPoolUtils.java in  » Database-JDBC-Connection-Pool » Apache-commons-pool-1.3 » org » apache » commons » pool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » Apache commons pool 1.3 » org.apache.commons.pool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2006 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.pool;
018:
019:        import junit.framework.TestCase;
020:        import junit.framework.AssertionFailedError;
021:        import junit.framework.Test;
022:        import junit.framework.TestSuite;
023:
024:        import java.lang.reflect.Proxy;
025:        import java.lang.reflect.InvocationHandler;
026:        import java.lang.reflect.Method;
027:        import java.util.List;
028:        import java.util.ArrayList;
029:        import java.util.Set;
030:        import java.util.HashSet;
031:        import java.util.TimerTask;
032:        import java.util.Collection;
033:        import java.util.Map;
034:        import java.util.Iterator;
035:        import java.util.Arrays;
036:
037:        /**
038:         * Unit tests for {@link PoolUtils}.
039:         *
040:         * @author Sandy McArthur
041:         * @version $Revision: 384433 $ $Date: 2006-03-09 00:19:21 -0500 (Thu, 09 Mar 2006) $
042:         */
043:        public class TestPoolUtils extends TestCase {
044:
045:            /** Period between checks for minIdle tests. Increase this if you happen to get too many false failures. */
046:            private static final int CHECK_PERIOD = 300;
047:
048:            /** Times to let the minIdle check run. */
049:            private static final int CHECK_COUNT = 4;
050:
051:            /** Sleep time to let the minIdle tests run CHECK_COUNT times. */
052:            private static final int CHECK_SLEEP_PERIOD = CHECK_PERIOD
053:                    * (CHECK_COUNT - 1) + CHECK_PERIOD / 2;
054:
055:            public static Test suite() {
056:                return new TestSuite(TestPoolUtils.class);
057:            }
058:
059:            public void testJavaBeanInstantiation() {
060:                new PoolUtils();
061:            }
062:
063:            public void testAdaptKeyedPoolableObjectFactory() throws Exception {
064:                try {
065:                    PoolUtils.adapt((KeyedPoolableObjectFactory) null);
066:                    fail("PoolUtils.adapt(KeyedPoolableObjectFactory) must not allow null factory.");
067:                } catch (IllegalArgumentException iae) {
068:                    // expected
069:                }
070:            }
071:
072:            public void testAdaptKeyedPoolableObjectFactoryKey()
073:                    throws Exception {
074:                try {
075:                    PoolUtils.adapt((KeyedPoolableObjectFactory) null,
076:                            new Object());
077:                    fail("PoolUtils.adapt(KeyedPoolableObjectFactory, key) must not allow null factory.");
078:                } catch (IllegalArgumentException iae) {
079:                    // expected
080:                }
081:                try {
082:                    PoolUtils.adapt((KeyedPoolableObjectFactory) createProxy(
083:                            KeyedPoolableObjectFactory.class, null), null);
084:                    fail("PoolUtils.adapt(KeyedPoolableObjectFactory, key) must not allow null key.");
085:                } catch (IllegalArgumentException iae) {
086:                    // expected
087:                }
088:
089:                final List calledMethods = new ArrayList();
090:                final KeyedPoolableObjectFactory kpof = (KeyedPoolableObjectFactory) createProxy(
091:                        KeyedPoolableObjectFactory.class, calledMethods);
092:
093:                final PoolableObjectFactory pof = PoolUtils.adapt(kpof);
094:                final List expectedMethods = invokeEveryMethod(pof);
095:                assertEquals(expectedMethods, calledMethods);
096:            }
097:
098:            public void testAdaptPoolableObjectFactory() throws Exception {
099:                try {
100:                    PoolUtils.adapt((PoolableObjectFactory) null);
101:                    fail("PoolUtils.adapt(PoolableObjectFactory) must not allow null factory.");
102:                } catch (IllegalArgumentException iae) {
103:                    // expected
104:                }
105:
106:                final List calledMethods = new ArrayList();
107:                final PoolableObjectFactory pof = (PoolableObjectFactory) createProxy(
108:                        PoolableObjectFactory.class, calledMethods);
109:
110:                final KeyedPoolableObjectFactory kpof = PoolUtils.adapt(pof);
111:                final List expectedMethods = invokeEveryMethod(kpof);
112:                assertEquals(expectedMethods, calledMethods);
113:            }
114:
115:            public void testAdaptKeyedObjectPool() throws Exception {
116:                try {
117:                    PoolUtils.adapt((KeyedObjectPool) null);
118:                    fail("PoolUtils.adapt(KeyedObjectPool) must not allow a null pool.");
119:                } catch (IllegalArgumentException iae) {
120:                    // expected
121:                }
122:            }
123:
124:            public void testAdaptKeyedObjectPoolKey() throws Exception {
125:                try {
126:                    PoolUtils.adapt((KeyedObjectPool) null, new Object());
127:                    fail("PoolUtils.adapt(KeyedObjectPool, key) must not allow a null pool.");
128:                } catch (IllegalArgumentException iae) {
129:                    // expected
130:                }
131:                try {
132:                    PoolUtils.adapt((KeyedObjectPool) createProxy(
133:                            KeyedObjectPool.class, null), null);
134:                    fail("PoolUtils.adapt(KeyedObjectPool, key) must not allow a null key.");
135:                } catch (IllegalArgumentException iae) {
136:                    // expected
137:                }
138:
139:                final List calledMethods = new ArrayList();
140:                final KeyedObjectPool kop = (KeyedObjectPool) createProxy(
141:                        KeyedObjectPool.class, calledMethods);
142:
143:                final ObjectPool op = PoolUtils.adapt(kop, new Object());
144:                final List expectedMethods = invokeEveryMethod(op);
145:                assertEquals(expectedMethods, calledMethods);
146:            }
147:
148:            public void testAdaptObjectPool() throws Exception {
149:                try {
150:                    PoolUtils.adapt((ObjectPool) null);
151:                    fail("PoolUtils.adapt(ObjectPool) must not allow a null pool.");
152:                } catch (IllegalArgumentException iae) {
153:                    // expected
154:                }
155:
156:                final List calledMethods = new ArrayList();
157:                final ObjectPool op = (ObjectPool) createProxy(
158:                        ObjectPool.class, calledMethods);
159:
160:                final KeyedObjectPool kop = PoolUtils.adapt(op);
161:                final List expectedMethods = invokeEveryMethod(kop);
162:                assertEquals(expectedMethods, calledMethods);
163:            }
164:
165:            public void testCheckedPoolObjectPool() throws Exception {
166:                try {
167:                    PoolUtils.checkedPool((ObjectPool) null, Object.class);
168:                    fail("PoolUtils.checkedPool(ObjectPool, Class) must not allow a null pool.");
169:                } catch (IllegalArgumentException iae) {
170:                    // expected
171:                }
172:                try {
173:                    PoolUtils.checkedPool((ObjectPool) createProxy(
174:                            ObjectPool.class, null), null);
175:                    fail("PoolUtils.checkedPool(ObjectPool, Class) must not allow a null type.");
176:                } catch (IllegalArgumentException iae) {
177:                    // expected
178:                }
179:
180:                final List calledMethods = new ArrayList();
181:                ObjectPool op = (ObjectPool) createProxy(ObjectPool.class,
182:                        calledMethods);
183:
184:                ObjectPool cop = PoolUtils.checkedPool(op, Object.class);
185:                final List expectedMethods = invokeEveryMethod(cop);
186:                assertEquals(expectedMethods, calledMethods);
187:
188:                op = new BaseObjectPool() {
189:                    public Object borrowObject() throws Exception {
190:                        return new Integer(0);
191:                    }
192:
193:                    public void returnObject(Object obj) {
194:                    }
195:
196:                    public void invalidateObject(Object obj) {
197:                    }
198:                };
199:                cop = PoolUtils.checkedPool(op, String.class);
200:
201:                try {
202:                    cop.borrowObject();
203:                    fail("borrowObject should have failed as Integer !instanceof String.");
204:                } catch (ClassCastException cce) {
205:                    // expected
206:                }
207:                try {
208:                    cop.returnObject(new Integer(1));
209:                    fail("returnObject should have failed as Integer !instanceof String.");
210:                } catch (ClassCastException cce) {
211:                    // expected
212:                }
213:                try {
214:                    cop.invalidateObject(new Integer(2));
215:                    fail("invalidateObject should have failed as Integer !instanceof String.");
216:                } catch (ClassCastException cce) {
217:                    // expected
218:                }
219:            }
220:
221:            public void testCheckedPoolKeyedObjectPool() throws Exception {
222:                try {
223:                    PoolUtils.checkedPool((KeyedObjectPool) null, Object.class);
224:                    fail("PoolUtils.checkedPool(KeyedObjectPool, Class) must not allow a null pool.");
225:                } catch (IllegalArgumentException iae) {
226:                    // expected
227:                }
228:                try {
229:                    PoolUtils.checkedPool((KeyedObjectPool) createProxy(
230:                            KeyedObjectPool.class, null), null);
231:                    fail("PoolUtils.checkedPool(KeyedObjectPool, Class) must not allow a null type.");
232:                } catch (IllegalArgumentException iae) {
233:                    // expected
234:                }
235:
236:                final List calledMethods = new ArrayList();
237:                KeyedObjectPool op = (KeyedObjectPool) createProxy(
238:                        KeyedObjectPool.class, calledMethods);
239:
240:                KeyedObjectPool cop = PoolUtils.checkedPool(op, Object.class);
241:                final List expectedMethods = invokeEveryMethod(cop);
242:                assertEquals(expectedMethods, calledMethods);
243:
244:                op = new BaseKeyedObjectPool() {
245:                    public Object borrowObject(Object key) {
246:                        return new Integer(0);
247:                    }
248:
249:                    public void returnObject(Object key, Object obj) {
250:                    }
251:
252:                    public void invalidateObject(Object key, Object obj) {
253:                    }
254:                };
255:                cop = PoolUtils.checkedPool(op, String.class);
256:
257:                try {
258:                    cop.borrowObject(null);
259:                    fail("borrowObject should have failed as Integer !instanceof String.");
260:                } catch (ClassCastException cce) {
261:                    // expected
262:                }
263:                try {
264:                    cop.returnObject(null, new Integer(1));
265:                    fail("returnObject should have failed as Integer !instanceof String.");
266:                } catch (ClassCastException cce) {
267:                    // expected
268:                }
269:                try {
270:                    cop.invalidateObject(null, new Integer(2));
271:                    fail("invalidateObject should have failed as Integer !instanceof String.");
272:                } catch (ClassCastException cce) {
273:                    // expected
274:                }
275:            }
276:
277:            public void testCheckMinIdleObjectPool() throws Exception {
278:                try {
279:                    PoolUtils.checkMinIdle(null, 1, 1);
280:                    fail("PoolUtils.checkMinIdle(ObjectPool,,) must not allow null pool.");
281:                } catch (IllegalArgumentException iae) {
282:                    // expected
283:                }
284:                try {
285:                    final ObjectPool pool = (ObjectPool) createProxy(
286:                            ObjectPool.class, null);
287:                    PoolUtils.checkMinIdle(pool, -1, 1);
288:                    fail("PoolUtils.checkMinIdle(ObjectPool,,) must not accept negative min idle values.");
289:                } catch (IllegalArgumentException iae) {
290:                    // expected
291:                }
292:
293:                // Because this isn't determinist and you can get false failures, try more than once.
294:                AssertionFailedError afe = null;
295:                int triesLeft = 3;
296:                do {
297:                    afe = null;
298:                    try {
299:                        final List calledMethods = new ArrayList();
300:                        final ObjectPool pool = (ObjectPool) createProxy(
301:                                ObjectPool.class, calledMethods);
302:                        final TimerTask task = PoolUtils.checkMinIdle(pool, 1,
303:                                CHECK_PERIOD); // checks minIdle immediately
304:
305:                        Thread.sleep(CHECK_SLEEP_PERIOD); // will check CHECK_COUNT more times.
306:                        task.cancel();
307:                        task.toString();
308:
309:                        final List expectedMethods = new ArrayList();
310:                        for (int i = 0; i < CHECK_COUNT; i++) {
311:                            expectedMethods.add("getNumIdle");
312:                            expectedMethods.add("addObject");
313:                        }
314:                        expectedMethods.add("toString");
315:                        assertEquals(expectedMethods, calledMethods); // may fail because of the thread scheduler
316:                    } catch (AssertionFailedError e) {
317:                        afe = e;
318:                    }
319:                } while (--triesLeft > 0 && afe != null);
320:                if (afe != null) {
321:                    throw afe;
322:                }
323:            }
324:
325:            public void testCheckMinIdleKeyedObjectPool() throws Exception {
326:                try {
327:                    PoolUtils.checkMinIdle(null, new Object(), 1, 1);
328:                    fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not allow null pool.");
329:                } catch (IllegalArgumentException iae) {
330:                    // expected
331:                }
332:                try {
333:                    final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
334:                            KeyedObjectPool.class, null);
335:                    PoolUtils.checkMinIdle(pool, (Object) null, 1, 1);
336:                    fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not accept null keys.");
337:                } catch (IllegalArgumentException iae) {
338:                    // expected
339:                }
340:                try {
341:                    final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
342:                            KeyedObjectPool.class, null);
343:                    PoolUtils.checkMinIdle(pool, new Object(), -1, 1);
344:                    fail("PoolUtils.checkMinIdle(KeyedObjectPool,Object,int,long) must not accept negative min idle values.");
345:                } catch (IllegalArgumentException iae) {
346:                    // expected
347:                }
348:
349:                // Because this isn't determinist and you can get false failures, try more than once.
350:                AssertionFailedError afe = null;
351:                int triesLeft = 3;
352:                do {
353:                    afe = null;
354:                    try {
355:                        final List calledMethods = new ArrayList();
356:                        final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
357:                                KeyedObjectPool.class, calledMethods);
358:                        final Object key = new Object();
359:                        final TimerTask task = PoolUtils.checkMinIdle(pool,
360:                                key, 1, CHECK_PERIOD); // checks minIdle immediately
361:
362:                        Thread.sleep(CHECK_SLEEP_PERIOD); // will check CHECK_COUNT more times.
363:                        task.cancel();
364:                        task.toString();
365:
366:                        final List expectedMethods = new ArrayList();
367:                        for (int i = 0; i < CHECK_COUNT; i++) {
368:                            expectedMethods.add("getNumIdle");
369:                            expectedMethods.add("addObject");
370:                        }
371:                        expectedMethods.add("toString");
372:                        assertEquals(expectedMethods, calledMethods); // may fail because of the thread scheduler
373:                    } catch (AssertionFailedError e) {
374:                        afe = e;
375:                    }
376:                } while (--triesLeft > 0 && afe != null);
377:                if (afe != null) {
378:                    throw afe;
379:                }
380:            }
381:
382:            public void testCheckMinIdleKeyedObjectPoolKeys() throws Exception {
383:                try {
384:                    final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
385:                            KeyedObjectPool.class, null);
386:                    PoolUtils.checkMinIdle(pool, null, 1, 1);
387:                    fail("PoolUtils.checkMinIdle(KeyedObjectPool,Collection,int,long) must not accept null keys.");
388:                } catch (IllegalArgumentException iae) {
389:                    // expected
390:                }
391:
392:                // Because this isn't determinist and you can get false failures, try more than once.
393:                AssertionFailedError afe = null;
394:                int triesLeft = 3;
395:                do {
396:                    afe = null;
397:                    try {
398:                        final List calledMethods = new ArrayList();
399:                        final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
400:                                KeyedObjectPool.class, calledMethods);
401:                        final Collection keys = new ArrayList(2);
402:                        keys.add("one");
403:                        keys.add("two");
404:                        final Map tasks = PoolUtils.checkMinIdle(pool, keys, 1,
405:                                CHECK_PERIOD); // checks minIdle immediately
406:
407:                        Thread.sleep(CHECK_SLEEP_PERIOD); // will check CHECK_COUNT more times.
408:                        final Iterator iter = tasks.values().iterator();
409:                        while (iter.hasNext()) {
410:                            final TimerTask task = (TimerTask) iter.next();
411:                            task.cancel();
412:                        }
413:
414:                        final List expectedMethods = new ArrayList();
415:                        for (int i = 0; i < CHECK_COUNT * keys.size(); i++) {
416:                            expectedMethods.add("getNumIdle");
417:                            expectedMethods.add("addObject");
418:                        }
419:                        assertEquals(expectedMethods, calledMethods); // may fail because of the thread scheduler
420:                    } catch (AssertionFailedError e) {
421:                        afe = e;
422:                    }
423:                } while (--triesLeft > 0 && afe != null);
424:                if (afe != null) {
425:                    throw afe;
426:                }
427:            }
428:
429:            public void testPrefillObjectPool() throws Exception {
430:                try {
431:                    PoolUtils.prefill(null, 1);
432:                    fail("PoolUtils.prefill(ObjectPool,int) must not allow null pool.");
433:                } catch (IllegalArgumentException iae) {
434:                    // expected
435:                }
436:
437:                final List calledMethods = new ArrayList();
438:                final ObjectPool pool = (ObjectPool) createProxy(
439:                        ObjectPool.class, calledMethods);
440:
441:                PoolUtils.prefill(pool, 0);
442:                final List expectedMethods = new ArrayList();
443:                assertEquals(expectedMethods, calledMethods);
444:
445:                calledMethods.clear();
446:                PoolUtils.prefill(pool, 3);
447:                for (int i = 0; i < 3; i++) {
448:                    expectedMethods.add("addObject");
449:                }
450:                assertEquals(expectedMethods, calledMethods);
451:            }
452:
453:            public void testPrefillKeyedObjectPool() throws Exception {
454:                try {
455:                    PoolUtils.prefill(null, new Object(), 1);
456:                    fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null pool.");
457:                } catch (IllegalArgumentException iae) {
458:                    // expected
459:                }
460:                try {
461:                    final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
462:                            KeyedObjectPool.class, null);
463:                    PoolUtils.prefill(pool, (Object) null, 1);
464:                    fail("PoolUtils.prefill(KeyedObjectPool,Object,int) must not accept null key.");
465:                } catch (IllegalArgumentException iae) {
466:                    // expected
467:                }
468:
469:                final List calledMethods = new ArrayList();
470:                final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
471:                        KeyedObjectPool.class, calledMethods);
472:
473:                PoolUtils.prefill(pool, new Object(), 0);
474:                final List expectedMethods = new ArrayList();
475:                assertEquals(expectedMethods, calledMethods);
476:
477:                calledMethods.clear();
478:                PoolUtils.prefill(pool, new Object(), 3);
479:                for (int i = 0; i < 3; i++) {
480:                    expectedMethods.add("addObject");
481:                }
482:                assertEquals(expectedMethods, calledMethods);
483:            }
484:
485:            public void testPrefillKeyedObjectPoolCollection() throws Exception {
486:                try {
487:                    final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
488:                            KeyedObjectPool.class, null);
489:                    PoolUtils.prefill(pool, null, 1);
490:                    fail("PoolUtils.prefill(KeyedObjectPool,Collection,int) must not accept null keys.");
491:                } catch (IllegalArgumentException iae) {
492:                    // expected
493:                }
494:
495:                final List calledMethods = new ArrayList();
496:                final KeyedObjectPool pool = (KeyedObjectPool) createProxy(
497:                        KeyedObjectPool.class, calledMethods);
498:
499:                final Set keys = new HashSet();
500:                PoolUtils.prefill(pool, keys, 0);
501:                final List expectedMethods = new ArrayList();
502:                assertEquals(expectedMethods, calledMethods);
503:
504:                calledMethods.clear();
505:                keys.add(new Integer(1));
506:                keys.add("two");
507:                keys.add(new Double(3.1415926));
508:                PoolUtils.prefill(pool, keys, 3);
509:                for (int i = 0; i < keys.size() * 3; i++) {
510:                    expectedMethods.add("addObject");
511:                }
512:                assertEquals(expectedMethods, calledMethods);
513:            }
514:
515:            public void testSynchronizedPoolObjectPool() throws Exception {
516:                try {
517:                    PoolUtils.synchronizedPool((ObjectPool) null);
518:                    fail("PoolUtils.synchronizedPool(ObjectPool) must not allow a null pool.");
519:                } catch (IllegalArgumentException iae) {
520:                    // expected
521:                }
522:
523:                final List calledMethods = new ArrayList();
524:                final ObjectPool op = (ObjectPool) createProxy(
525:                        ObjectPool.class, calledMethods);
526:
527:                final ObjectPool sop = PoolUtils.synchronizedPool(op);
528:                final List expectedMethods = invokeEveryMethod(sop);
529:                assertEquals(expectedMethods, calledMethods);
530:
531:                // TODO: Anyone feel motivated to construct a test that verifies proper synchronization?
532:            }
533:
534:            public void testSynchronizedPoolKeyedObjectPool() throws Exception {
535:                try {
536:                    PoolUtils.synchronizedPool((KeyedObjectPool) null);
537:                    fail("PoolUtils.synchronizedPool(KeyedObjectPool) must not allow a null pool.");
538:                } catch (IllegalArgumentException iae) {
539:                    // expected
540:                }
541:
542:                final List calledMethods = new ArrayList();
543:                final KeyedObjectPool kop = (KeyedObjectPool) createProxy(
544:                        KeyedObjectPool.class, calledMethods);
545:
546:                final KeyedObjectPool skop = PoolUtils.synchronizedPool(kop);
547:                final List expectedMethods = invokeEveryMethod(skop);
548:                assertEquals(expectedMethods, calledMethods);
549:
550:                // TODO: Anyone feel motivated to construct a test that verifies proper synchronization?
551:            }
552:
553:            public void testSynchronizedPoolableFactoryPoolableObjectFactory()
554:                    throws Exception {
555:                try {
556:                    PoolUtils
557:                            .synchronizedPoolableFactory((PoolableObjectFactory) null);
558:                    fail("PoolUtils.synchronizedPoolableFactory(PoolableObjectFactory) must not allow a null factory.");
559:                } catch (IllegalArgumentException iae) {
560:                    // expected
561:                }
562:
563:                final List calledMethods = new ArrayList();
564:                final PoolableObjectFactory pof = (PoolableObjectFactory) createProxy(
565:                        PoolableObjectFactory.class, calledMethods);
566:
567:                final PoolableObjectFactory spof = PoolUtils
568:                        .synchronizedPoolableFactory(pof);
569:                final List expectedMethods = invokeEveryMethod(spof);
570:                assertEquals(expectedMethods, calledMethods);
571:
572:                // TODO: Anyone feel motivated to construct a test that verifies proper synchronization?
573:            }
574:
575:            public void testSynchronizedPoolableFactoryKeyedPoolableObjectFactory()
576:                    throws Exception {
577:                try {
578:                    PoolUtils
579:                            .synchronizedPoolableFactory((KeyedPoolableObjectFactory) null);
580:                    fail("PoolUtils.synchronizedPoolableFactory(KeyedPoolableObjectFactory) must not allow a null factory.");
581:                } catch (IllegalArgumentException iae) {
582:                    // expected
583:                }
584:
585:                final List calledMethods = new ArrayList();
586:                final KeyedPoolableObjectFactory kpof = (KeyedPoolableObjectFactory) createProxy(
587:                        KeyedPoolableObjectFactory.class, calledMethods);
588:
589:                final KeyedPoolableObjectFactory skpof = PoolUtils
590:                        .synchronizedPoolableFactory(kpof);
591:                final List expectedMethods = invokeEveryMethod(skpof);
592:                assertEquals(expectedMethods, calledMethods);
593:
594:                // TODO: Anyone feel motivated to construct a test that verifies proper synchronization?
595:            }
596:
597:            private static List invokeEveryMethod(ObjectPool op)
598:                    throws Exception {
599:                op.addObject();
600:                op.borrowObject();
601:                op.clear();
602:                op.close();
603:                op.getNumActive();
604:                op.getNumIdle();
605:                op.invalidateObject(new Object());
606:                op.returnObject(new Object());
607:                op.setFactory((PoolableObjectFactory) createProxy(
608:                        PoolableObjectFactory.class, null));
609:                op.toString();
610:
611:                final List expectedMethods = Arrays.asList(new String[] {
612:                        "addObject", "borrowObject", "clear", "close",
613:                        "getNumActive", "getNumIdle", "invalidateObject",
614:                        "returnObject", "setFactory", "toString" });
615:                return expectedMethods;
616:            }
617:
618:            private static List invokeEveryMethod(KeyedObjectPool kop)
619:                    throws Exception {
620:                kop.addObject(null);
621:                kop.borrowObject(null);
622:                kop.clear();
623:                kop.clear(null);
624:                kop.close();
625:                kop.getNumActive();
626:                kop.getNumActive(null);
627:                kop.getNumIdle();
628:                kop.getNumIdle(null);
629:                kop.invalidateObject(null, new Object());
630:                kop.returnObject(null, new Object());
631:                kop.setFactory((KeyedPoolableObjectFactory) createProxy(
632:                        KeyedPoolableObjectFactory.class, null));
633:                kop.toString();
634:
635:                final List expectedMethods = Arrays.asList(new String[] {
636:                        "addObject", "borrowObject", "clear", "clear", "close",
637:                        "getNumActive", "getNumActive", "getNumIdle",
638:                        "getNumIdle", "invalidateObject", "returnObject",
639:                        "setFactory", "toString" });
640:                return expectedMethods;
641:            }
642:
643:            private static List invokeEveryMethod(PoolableObjectFactory pof)
644:                    throws Exception {
645:                pof.activateObject(null);
646:                pof.destroyObject(null);
647:                pof.makeObject();
648:                pof.passivateObject(null);
649:                pof.validateObject(null);
650:                pof.toString();
651:
652:                final List expectedMethods = Arrays.asList(new String[] {
653:                        "activateObject", "destroyObject", "makeObject",
654:                        "passivateObject", "validateObject", "toString", });
655:                return expectedMethods;
656:            }
657:
658:            private static List invokeEveryMethod(
659:                    KeyedPoolableObjectFactory kpof) throws Exception {
660:                kpof.activateObject(null, null);
661:                kpof.destroyObject(null, null);
662:                kpof.makeObject(null);
663:                kpof.passivateObject(null, null);
664:                kpof.validateObject(null, null);
665:                kpof.toString();
666:
667:                final List expectedMethods = Arrays.asList(new String[] {
668:                        "activateObject", "destroyObject", "makeObject",
669:                        "passivateObject", "validateObject", "toString", });
670:                return expectedMethods;
671:            }
672:
673:            private static Object createProxy(final Class clazz,
674:                    final List logger) {
675:                return Proxy.newProxyInstance(clazz.getClassLoader(),
676:                        new Class[] { clazz }, new MethodCallLogger(logger));
677:            }
678:
679:            private static class MethodCallLogger implements  InvocationHandler {
680:                private final List calledMethods;
681:
682:                MethodCallLogger(final List calledMethods) {
683:                    this .calledMethods = calledMethods;
684:                }
685:
686:                public Object invoke(final Object proxy, final Method method,
687:                        final Object[] args) throws Throwable {
688:                    calledMethods.add(method.getName());
689:                    if (boolean.class.equals(method.getReturnType())) {
690:                        return Boolean.FALSE;
691:                    } else if (int.class.equals(method.getReturnType())) {
692:                        return new Integer(0);
693:                    } else if (long.class.equals(method.getReturnType())) {
694:                        return new Long(0);
695:                    } else if (Object.class.equals(method.getReturnType())) {
696:                        return new Object();
697:                    } else {
698:                        return null;
699:                    }
700:                }
701:            }
702:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.