001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.container.test.adapters;
027:
028: import junit.framework.TestCase;
029: import org.apache.commons.pool.ObjectPool;
030: import org.apache.commons.pool.impl.SoftReferenceObjectPool;
031: import org.jicarilla.container.adapters.PoolingAdapter;
032: import org.jicarilla.lang.RecyclingObjectFactory;
033:
034: /**
035: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
036: * @version $Id: PoolingAdapterTestCase.java,v 1.2 2004/03/23 13:37:53 lsimons Exp $
037: */
038: public class PoolingAdapterTestCase extends TestCase {
039: public static class Factory extends RecyclingObjectFactory {
040: public Object o = new Object();
041: public boolean objectMade = false;
042: public boolean objectPassivated = false;
043: public boolean myObjectPassivated = false;
044:
045: public Object makeObject() throws Exception {
046: objectMade = true;
047: return o;
048: }
049:
050: public void passivateObject(Object obj) {
051: objectPassivated = true;
052: if (o.equals(obj))
053: myObjectPassivated = true;
054: }
055: }
056:
057: public void testEverything() throws Exception {
058: //TrueSelector selector = new TrueSelector();
059: Factory factory = new Factory();
060:
061: ObjectPool pool = new SoftReferenceObjectPool(factory);
062:
063: PoolingAdapter pca = new PoolingAdapter(pool);
064:
065: Object instance = pca.getInstance();
066: assertEquals(factory.o, instance);
067: assertTrue(factory.objectMade);
068:
069: pca.releaseInstance(null);
070: assertTrue(factory.objectPassivated);
071: factory.objectPassivated = false;
072: pca.releaseInstance(new Object());
073: assertTrue(factory.objectPassivated);
074:
075: pca.releaseInstance(instance);
076: assertTrue(factory.myObjectPassivated);
077:
078: //assertEquals( selector, pca.getSelector() );
079: }
080:
081: public void testExceptions() throws Exception {
082: //TrueSelector selector = new TrueSelector();
083: Factory factory = new Factory();
084:
085: ObjectPool pool = new SoftReferenceObjectPool(factory);
086:
087: Throwable t = null;
088: try {
089: new PoolingAdapter(null);
090: } catch (AssertionError th) {
091: t = th;
092: }
093: assertNotNull(t);
094:
095: pool = new SoftReferenceObjectPool(factory) {
096: public Object borrowObject() throws Exception {
097: throw new Exception();
098: }
099: };
100: t = null;
101: PoolingAdapter adapter = new PoolingAdapter(pool);
102: try {
103: adapter.getInstance();
104: } catch (Throwable th) {
105: t = th;
106: }
107: assertNotNull(t);
108: }
109: }
|