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 org.jicarilla.container.DefaultKeyRelayingContainer;
029: import org.jicarilla.container.KeyRelayingContainer;
030: import org.jicarilla.container.JicarillaException;
031: import org.jicarilla.container.KeyAwareAdapter;
032: import org.jicarilla.container.JicarillaIllegalAccessException;
033: import org.jicarilla.container.JicarillaInvocationTargetException;
034: import org.jicarilla.container.JicarillaInstantiationException;
035: import org.jicarilla.container.JicarillaClassNotFoundException;
036: import org.jicarilla.container.InitializationException;
037: import org.jicarilla.container.selectors.ClassSelector;
038: import org.jicarilla.lang.SelectorSwitch;
039: import org.jicarilla.lang.Selector;
040: import org.jicarilla.lang.FalseSelector;
041: import org.jicarilla.lang.TrueSelector;
042:
043: import junit.framework.TestCase;
044:
045: import java.lang.reflect.InvocationTargetException;
046:
047: /**
048: *
049: *
050: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
051: * @version $Id: DefaultKeyRelayingContainerTestCase.java,v 1.2 2004/03/23 13:37:53 lsimons Exp $
052: */
053: public class DefaultKeyRelayingContainerTestCase extends TestCase {
054: protected KeyRelayingContainer m_container;
055: protected MockAdapter m_adapter;
056: protected MockAdapter m_falseAdapter;
057: protected MockAdapter m_classAdapter;
058: protected MockExceptionAdapter m_exceptionAdapter;
059:
060: protected DefaultContainerTestCase.NoopComponentAdapter m_basic;
061:
062: protected Selector m_falseSelector;
063: protected Selector m_trueSelector;
064: protected Selector m_classSelector;
065:
066: public void setUp() throws Exception {
067: m_container = new DefaultKeyRelayingContainer();
068:
069: super .setUp();
070: m_adapter = new MockAdapter();
071: m_falseAdapter = new MockAdapter();
072: m_classAdapter = new MockAdapter();
073: m_exceptionAdapter = new MockExceptionAdapter(
074: new JicarillaException("some problem"));
075:
076: m_basic = new DefaultContainerTestCase.NoopComponentAdapter();
077:
078: m_falseSelector = new FalseSelector();
079: m_trueSelector = new TrueSelector();
080: m_classSelector = new ClassSelector(this .getClass());
081: }
082:
083: public void testConstructors() {
084: new DefaultKeyRelayingContainer();
085: new DefaultKeyRelayingContainer(
086: new DefaultContainerTestCase.MockResolver());
087: new DefaultKeyRelayingContainer(null);
088:
089: new DefaultKeyRelayingContainer(
090: new DefaultContainerTestCase.MockResolver(),
091: new SelectorSwitch());
092: new DefaultKeyRelayingContainer(null, new SelectorSwitch());
093: new DefaultKeyRelayingContainer(
094: new DefaultContainerTestCase.MockResolver(), null);
095: new DefaultKeyRelayingContainer(null, null);
096: }
097:
098: public void testRegisterSelectorBasedAdapter() {
099: m_container.registerAdapter(m_trueSelector, m_adapter);
100:
101: AssertionError ex = null;
102: try {
103: m_container.registerAdapter(m_trueSelector, null);
104: } catch (AssertionError ae) {
105: ex = ae;
106: }
107: assertNotNull(ex);
108:
109: ex = null;
110: try {
111: m_container.registerAdapter((Selector) null, m_adapter);
112: } catch (AssertionError ae) {
113: ex = ae;
114: }
115: assertNotNull(ex);
116: }
117:
118: public void testRegisterKeyBasedAdapter() {
119: m_container.registerAdapter((Object) m_trueSelector, m_adapter);
120: m_container.registerAdapter("blah", m_adapter);
121:
122: m_container.registerAdapter(this .getClass(), m_adapter);
123: m_container.registerAdapter(this .getClass().getName(),
124: m_adapter);
125:
126: m_container.registerAdapter(new Object(), m_adapter);
127: AssertionError ex = null;
128: try {
129: m_container.registerAdapter("blah", null);
130: } catch (AssertionError ae) {
131: ex = ae;
132: }
133: assertNotNull(ex);
134:
135: ex = null;
136: try {
137: m_container.registerAdapter((Object) null, m_adapter);
138: } catch (AssertionError ae) {
139: ex = ae;
140: }
141: assertNotNull(ex);
142: }
143:
144: public void testRegisterNonRelayingAdapter() throws Exception {
145: ((DefaultKeyRelayingContainer) m_container).registerAdapter(
146: m_trueSelector, m_basic);
147: assertEquals(m_basic.m_object, m_container.getResolver().get(
148: this ));
149: m_container.getResolver().releaseInstance(m_basic.m_object);
150: }
151:
152: public void testGet() throws IllegalAccessException,
153: InvocationTargetException, ClassNotFoundException,
154: InstantiationException {
155: // empty at first
156: assertNull(m_container.getResolver().get("blah"));
157:
158: // still 'empty'
159: m_container.registerAdapter(m_falseSelector, m_falseAdapter);
160: assertNull(m_container.getResolver().get("blah"));
161:
162: // no match possible
163: m_container.registerAdapter(m_classSelector, m_classAdapter);
164: assertNull(m_container.getResolver().get("blah"));
165:
166: // but this works
167: assertEquals(m_classAdapter.m_object, m_container.getResolver()
168: .get(this .getClass()));
169:
170: // now we will always get a component
171: m_container.registerAdapter(m_trueSelector, m_adapter);
172:
173: Object obj = m_container.getResolver().get("blah");
174: assertEquals(m_adapter.m_object, obj);
175:
176: // and the TestCase is still resolved by the classselector
177: assertEquals(m_classAdapter.m_object, m_container.getResolver()
178: .get(this .getClass()));
179:
180: // get the second component
181: AssertionError ex = null;
182: try {
183: m_container.getResolver().get(null);
184: } catch (AssertionError ae) {
185: ex = ae;
186: }
187: assertNotNull(ex);
188: }
189:
190: public void testGetThrowsExceptions()
191: throws IllegalAccessException, InvocationTargetException,
192: ClassNotFoundException, InstantiationException {
193: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
194: Throwable t = null;
195: try {
196: m_container.getResolver().get("blah");
197: } catch (Throwable th) {
198: t = th;
199: }
200: assertEquals(m_exceptionAdapter.t, t);
201:
202: m_container = new DefaultKeyRelayingContainer();
203: m_exceptionAdapter = new MockExceptionAdapter(
204: new JicarillaInvocationTargetException(new Exception()));
205: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
206: t = null;
207: try {
208: m_container.getResolver().get("blah");
209: } catch (Throwable th) {
210: t = th;
211: }
212: assertEquals(m_exceptionAdapter.t, t);
213:
214: m_container = new DefaultKeyRelayingContainer();
215: m_exceptionAdapter = new MockExceptionAdapter(
216: new JicarillaInstantiationException());
217: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
218: t = null;
219: try {
220: m_container.getResolver().get("blah");
221: } catch (Throwable th) {
222: t = th;
223: }
224: assertEquals(m_exceptionAdapter.t, t);
225:
226: m_container = new DefaultKeyRelayingContainer();
227: m_exceptionAdapter = new MockExceptionAdapter(
228: new JicarillaClassNotFoundException());
229: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
230: t = null;
231: try {
232: m_container.getResolver().get("blah");
233: } catch (Throwable th) {
234: t = th;
235: }
236: assertEquals(m_exceptionAdapter.t, t);
237:
238: // bad!
239: m_container = new DefaultKeyRelayingContainer();
240: m_exceptionAdapter = new MockExceptionAdapter(
241: new RuntimeException());
242: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
243: t = null;
244: try {
245: m_container.getResolver().get("blah");
246: } catch (Throwable th) {
247: t = th;
248: }
249: assertNotNull(t);
250:
251: /*
252: // bad!: will return true in contains(), then false on the next calls
253: m_container = new DefaultContainer();
254: m_container.addAdapter(
255: new SelectOnlyOnceSelector(),
256: new NoopComponentAdapter() );
257: t = null;
258: try
259: {
260: m_container.getResolver().get("blah");
261: }
262: catch( Throwable th )
263: {
264: t = th;
265: }
266: assertTrue( t instanceof RuntimeException ); // don't care much what kind of exception this is
267: */
268: }
269:
270: public void testRelease() throws Exception {
271: m_container.getResolver().releaseInstance(null);
272:
273: m_container.registerAdapter(m_trueSelector, m_adapter);
274: Object obj = m_container.getResolver().get("blah");
275: m_container.getResolver().releaseInstance(obj);
276:
277: m_container.getResolver().releaseInstance(new Object());
278: m_container.getResolver().releaseInstance(this );
279: }
280:
281: public static class MockAdapter implements KeyAwareAdapter {
282: public Object m_object;
283:
284: public MockAdapter() {
285: m_object = new Object();
286: }
287:
288: public Object getInstance(Object key)
289: throws JicarillaIllegalAccessException,
290: JicarillaInvocationTargetException,
291: JicarillaInstantiationException,
292: JicarillaClassNotFoundException,
293: InitializationException, JicarillaException {
294: return m_object;
295: }
296:
297: public void releaseInstance(Object instance) throws Exception {
298: }
299:
300: }
301:
302: public static class MockExceptionAdapter extends MockAdapter {
303: public RuntimeException t;
304:
305: public MockExceptionAdapter(RuntimeException ex) {
306: t = ex;
307: }
308:
309: public Object getInstance(Object key)
310: throws JicarillaIllegalAccessException,
311: JicarillaInvocationTargetException,
312: JicarillaInstantiationException,
313: JicarillaClassNotFoundException,
314: InitializationException, JicarillaException {
315: throw t;
316: }
317:
318: }
319:
320: }
|