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;
027:
028: import junit.framework.TestCase;
029: import org.jicarilla.container.Adapter;
030: import org.jicarilla.container.Container;
031: import org.jicarilla.container.DefaultContainer;
032: import org.jicarilla.container.SynchronizationUtil;
033: import org.jicarilla.container.adapters.SingletonAdapter;
034: import org.jicarilla.container.test.adapters.AbstracAdapterTestCase;
035: import org.jicarilla.lang.CascadingRuntimeException;
036: import org.jicarilla.lang.TrueSelector;
037:
038: /**
039: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
040: * @version $Id: SynchronizationUtilTestCase.java,v 1.8 2004/03/23 13:37:53 lsimons Exp $
041: */
042: public class SynchronizationUtilTestCase extends TestCase {
043: public void testSynchronizedAdapter() throws Exception {
044: //TrueSelector selector = new TrueSelector();
045: AbstracAdapterTestCase.LoggingFactory factory = new AbstracAdapterTestCase.LoggingFactory();
046:
047: Adapter adapter = new SingletonAdapter(factory);
048:
049: adapter = SynchronizationUtil.synchronizedAdapter(adapter);
050:
051: Object instance = adapter.getInstance();
052:
053: assertSame(instance, adapter.getInstance());
054: assertSame(instance, adapter.getInstance());
055: assertSame(instance, adapter.getInstance());
056:
057: adapter.releaseInstance(instance);
058:
059: //assertEquals( selector, adapter.getSelector() );
060: }
061:
062: public static class Holder {
063: public Object obj = null;
064: }
065:
066: public void testSynchronizedContainer() throws Exception {
067: final Container container = SynchronizationUtil
068: .synchronizedContainer(new DefaultContainer());
069:
070: final Holder exceptionHolder = new Holder();
071:
072: int size = 5;
073: Thread[] threads = new Thread[size];
074: for (int i = 0; i < size; i++)
075: threads[i] = new Thread(new Runnable() {
076: public void run() {
077: try {
078: Thread.sleep((int) (Math.random() * 10));
079: } catch (InterruptedException e) {
080: Thread.currentThread().interrupt();
081: }
082:
083: container
084: .registerAdapter(
085: new TrueSelector(),
086: new DefaultContainerTestCase.NoopComponentAdapter());
087: container
088: .registerAdapter(
089: new TrueSelector(),
090: new DefaultContainerTestCase.NoopComponentAdapter());
091: container
092: .registerAdapter(
093: this ,
094: new DefaultContainerTestCase.NoopComponentAdapter());
095: assertTrue(container.getResolver().contains("blah"));
096:
097: Adapter adapter = new DefaultContainerTestCase.NoopComponentAdapter();
098: try {
099:
100: assertNotNull(container.getResolver().get(
101: "blah"));
102: Object[] stuff = container.getResolver()
103: .getAll("blah");
104: assertTrue(stuff.length >= 2);
105:
106: container.getResolver().releaseInstance(
107: container.getResolver().get("blah"));
108: container.getResolver().releaseInstance(null);
109:
110: Throwable t = null;
111: try {
112: container.registerAdapter(null, adapter);
113: } catch (AssertionError th) {
114: t = th;
115: }
116: assertNotNull(t);
117:
118: t = null;
119: try {
120: container.registerAdapter(
121: new TrueSelector(), null);
122: } catch (AssertionError th) {
123: t = th;
124: }
125: assertNotNull(t);
126:
127: t = null;
128: try {
129: container.getResolver().get(null);
130: } catch (AssertionError th) {
131: t = th;
132: }
133: assertNotNull(t);
134:
135: t = null;
136: try {
137: container.getResolver().getAll(null);
138: } catch (AssertionError th) {
139: t = th;
140: }
141: assertNotNull(t);
142:
143: assertFalse(container.getResolver().contains(
144: null));
145: } catch (Exception e) {
146: exceptionHolder.obj = e;
147: throw new CascadingRuntimeException(e
148: .getMessage(), e);
149: }
150: }
151: });
152:
153: for (int i = 0; i < size; i++)
154: threads[i].start();
155: for (int i = 0; i < size; i++)
156: threads[i].join();
157:
158: assertNull(exceptionHolder.obj);
159: }
160: }
|