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.jicarilla.container.Adapter;
030: import org.jicarilla.container.Factory;
031: import org.jicarilla.container.JicarillaClassNotFoundException;
032: import org.jicarilla.container.JicarillaException;
033: import org.jicarilla.container.JicarillaIllegalAccessException;
034: import org.jicarilla.container.JicarillaInstantiationException;
035: import org.jicarilla.container.JicarillaInvocationTargetException;
036: import org.jicarilla.container.SynchronizationUtil;
037: import org.jicarilla.container.adapters.ThreadLocalAdapter;
038: import org.jicarilla.lang.CascadingRuntimeException;
039:
040: import java.lang.reflect.InvocationTargetException;
041: import java.util.ArrayList;
042: import java.util.Collections;
043: import java.util.List;
044:
045: /**
046: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
047: * @version $Id: ThreadLocalAdapterTestCase.java,v 1.2 2004/03/23 13:37:53 lsimons Exp $
048: */
049: public class ThreadLocalAdapterTestCase extends TestCase {
050: public static class FactoryImpl implements Factory {
051: public int newInstanceCalled = 0;
052:
053: public Object newInstance() {
054: increment();
055: return new Object();
056: }
057:
058: public synchronized void increment() {
059: newInstanceCalled++;
060: }
061:
062: public void releaseInstance(Object o) throws Exception {
063: }
064: }
065:
066: public static class ExceptionFactory implements Factory {
067: protected Throwable t;
068:
069: public ExceptionFactory(Throwable exception) {
070: t = exception;
071: }
072:
073: public Object newInstance() {
074: if (t instanceof RuntimeException)
075: throw (RuntimeException) t;
076:
077: throw new JicarillaException("blah", t);
078: }
079:
080: public void releaseInstance(Object o) throws Exception {
081: }
082: }
083:
084: public static class Holder {
085: public Object obj = null;
086: }
087:
088: public static class Runner implements Runnable {
089: private Adapter m_adapter;
090: private List m_components;
091: private Holder m_counter;
092: private Holder m_exception;
093:
094: public Runner(Adapter adapter, List components, Holder counter,
095: Holder exception) {
096: m_adapter = adapter;
097: m_components = components;
098: m_counter = counter;
099: m_exception = exception;
100: }
101:
102: public void run() {
103: try {
104: Object instance = m_adapter.getInstance();
105: m_components.add(instance);
106: m_counter.obj = new Integer(((Integer) m_counter.obj)
107: .intValue() + 1);
108: } catch (Exception e) {
109: m_exception.obj = e;
110: throw new CascadingRuntimeException(e.getMessage(), e);
111: }
112: }
113: }
114:
115: public void testSingleThreaded() throws Exception {
116: //TrueSelector selector = new TrueSelector();
117: AbstracAdapterTestCase.LoggingFactory factory = new AbstracAdapterTestCase.LoggingFactory();
118:
119: ThreadLocalAdapter adapter = new ThreadLocalAdapter(factory);
120:
121: Object instance = adapter.getInstance();
122: factory.newInstanceCalled = false;
123:
124: assertSame(instance, adapter.getInstance());
125: assertSame(instance, adapter.getInstance());
126: assertSame(instance, adapter.getInstance());
127:
128: assertFalse(factory.newInstanceCalled);
129: }
130:
131: public void testMultiThreaded() throws Exception {
132: //TrueSelector selector = new TrueSelector();
133: FactoryImpl factory = new FactoryImpl();
134:
135: final Adapter adapter = SynchronizationUtil
136: .synchronizedAdapter(new ThreadLocalAdapter(factory));
137:
138: final List components = Collections
139: .synchronizedList(new ArrayList());
140: final Holder exceptionHolder = new Holder();
141: final Holder countHolder = new Holder();
142: countHolder.obj = new Integer(0);
143:
144: int size = 5;
145: Thread[] threads = new Thread[size];
146: for (int i = 0; i < size; i++)
147: threads[i] = new Thread(new Runner(adapter, components,
148: countHolder, exceptionHolder));
149:
150: for (int i = 0; i < size; i++)
151: threads[i].start();
152:
153: for (int i = 0; i < size; i++)
154: threads[i].join();
155:
156: assertNull(exceptionHolder.obj);
157: assertEquals(size, components.size());
158: assertEquals(size, ((Integer) countHolder.obj).intValue());
159:
160: assertEquals(size, factory.newInstanceCalled);
161:
162: for (int i = 0; i < components.size() - 1; i++) {
163: Object o = components.get(i);
164: Object o2 = components.get(i + 1);
165: assertNotSame(o, o2);
166: }
167: }
168:
169: public void testExceptions() throws InvocationTargetException,
170: ClassNotFoundException, InstantiationException,
171: IllegalAccessException {
172: //TrueSelector selector = new TrueSelector();
173: ExceptionFactory factory = new ExceptionFactory(
174: new JicarillaIllegalAccessException());
175: ThreadLocalAdapter adapter = new ThreadLocalAdapter(factory);
176: Throwable t = null;
177: try {
178: adapter.getInstance();
179: } catch (JicarillaIllegalAccessException th) {
180: t = th;
181: }
182: assertNotNull(t);
183:
184: factory = new ExceptionFactory(
185: new JicarillaInvocationTargetException(new Exception()));
186: adapter = new ThreadLocalAdapter(factory);
187: t = null;
188: try {
189: adapter.getInstance();
190: } catch (JicarillaInvocationTargetException th) {
191: t = th;
192: }
193: assertNotNull(t);
194:
195: factory = new ExceptionFactory(
196: new JicarillaClassNotFoundException());
197: adapter = new ThreadLocalAdapter(factory);
198: t = null;
199: try {
200: adapter.getInstance();
201: } catch (JicarillaClassNotFoundException th) {
202: t = th;
203: }
204: assertNotNull(t);
205:
206: factory = new ExceptionFactory(
207: new JicarillaInstantiationException());
208: adapter = new ThreadLocalAdapter(factory);
209: t = null;
210: try {
211: adapter.getInstance();
212: } catch (JicarillaInstantiationException th) {
213: t = th;
214: }
215: assertNotNull(t);
216: }
217: }
|