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.InitializationException;
033: import org.jicarilla.container.JicarillaClassNotFoundException;
034: import org.jicarilla.container.JicarillaException;
035: import org.jicarilla.container.JicarillaIllegalAccessException;
036: import org.jicarilla.container.JicarillaInstantiationException;
037: import org.jicarilla.container.JicarillaInvocationTargetException;
038: import org.jicarilla.container.ResolutionException;
039: import org.jicarilla.container.Resolver;
040: import org.jicarilla.container.selectors.ClassSelector;
041: import org.jicarilla.lang.FalseSelector;
042: import org.jicarilla.lang.Selector;
043: import org.jicarilla.lang.SelectorSwitch;
044: import org.jicarilla.lang.TrueSelector;
045:
046: import java.lang.reflect.InvocationTargetException;
047:
048: /**
049: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
050: * @version $Id: DefaultContainerTestCase.java,v 1.9 2004/03/23 13:37:52 lsimons Exp $
051: */
052: public class DefaultContainerTestCase extends TestCase {
053: protected Container m_container;
054: protected NoopComponentAdapter m_adapter;
055: protected NoopComponentAdapter m_falseAdapter;
056: protected NoopComponentAdapter m_classAdapter;
057: protected ExceptionThrowingAdapter m_exceptionAdapter;
058:
059: protected Selector m_falseSelector;
060: protected Selector m_trueSelector;
061: protected Selector m_classSelector;
062:
063: public void setUp() {
064: m_container = new DefaultContainer();
065: m_adapter = new NoopComponentAdapter();
066: m_falseAdapter = new NoopComponentAdapter();
067: m_classAdapter = new NoopComponentAdapter();
068: m_exceptionAdapter = new ExceptionThrowingAdapter(
069: new JicarillaException("some problem"));
070:
071: m_falseSelector = new FalseSelector();
072: m_trueSelector = new TrueSelector();
073: m_classSelector = new ClassSelector(
074: DefaultContainerTestCase.class);
075: }
076:
077: public void testConstructor() {
078: new DefaultContainer();
079: new DefaultContainer(new MockResolver());
080: new DefaultContainer(null);
081:
082: new DefaultContainer(new MockResolver(), new SelectorSwitch());
083: new DefaultContainer(null, new SelectorSwitch());
084: new DefaultContainer(new MockResolver(), null);
085: new DefaultContainer(null, null);
086: }
087:
088: public void testContains() {
089: assertFalse(m_container.getResolver().contains("blah"));
090: assertFalse(m_container.getResolver().contains(null));
091:
092: // still 'empty'
093: m_container.registerAdapter(m_falseSelector, m_falseAdapter);
094: assertFalse(m_container.getResolver().contains("blah"));
095: assertFalse(m_container.getResolver().contains(null));
096:
097: // gotcha!
098: m_container.registerAdapter(m_trueSelector, m_adapter);
099: assertTrue(m_container.getResolver().contains("blah"));
100: assertFalse(m_container.getResolver().contains(null));
101: }
102:
103: public void testRegisterSelectorBasedAdapter() {
104: m_container.registerAdapter(m_trueSelector, m_adapter);
105:
106: AssertionError ex = null;
107: try {
108: m_container.registerAdapter(m_trueSelector, null);
109: } catch (AssertionError ae) {
110: ex = ae;
111: }
112: assertNotNull(ex);
113:
114: ex = null;
115: try {
116: m_container.registerAdapter((Selector) null, m_adapter);
117: } catch (AssertionError ae) {
118: ex = ae;
119: }
120: assertNotNull(ex);
121: }
122:
123: public void testRegisterKeyBasedAdapter() {
124: m_container.registerAdapter((Object) m_trueSelector, m_adapter);
125: m_container.registerAdapter("blah", m_adapter);
126:
127: m_container.registerAdapter(this .getClass(), m_adapter);
128: m_container.registerAdapter(this .getClass().getName(),
129: m_adapter);
130:
131: m_container.registerAdapter(new Object(), m_adapter);
132: AssertionError ex = null;
133: try {
134: m_container.registerAdapter("blah", null);
135: } catch (AssertionError ae) {
136: ex = ae;
137: }
138: assertNotNull(ex);
139:
140: ex = null;
141: try {
142: m_container.registerAdapter((Object) null, m_adapter);
143: } catch (AssertionError ae) {
144: ex = ae;
145: }
146: assertNotNull(ex);
147: }
148:
149: public void testGet() throws IllegalAccessException,
150: InvocationTargetException, ClassNotFoundException,
151: InstantiationException {
152: // empty at first
153: assertNull(m_container.getResolver().get("blah"));
154:
155: // still 'empty'
156: m_container.registerAdapter(m_falseSelector, m_falseAdapter);
157: assertNull(m_container.getResolver().get("blah"));
158:
159: // no match possible
160: m_container.registerAdapter(m_classSelector, m_classAdapter);
161: assertNull(m_container.getResolver().get("blah"));
162:
163: // but this works
164: assertEquals(m_classAdapter.m_object, m_container.getResolver()
165: .get(this .getClass()));
166:
167: // now we will always get a component
168: m_container.registerAdapter(m_trueSelector, m_adapter);
169:
170: Object obj = m_container.getResolver().get("blah");
171: assertEquals(m_adapter.m_object, obj);
172:
173: // and the TestCase is still resolved by the classselector
174: assertEquals(m_classAdapter.m_object, m_container.getResolver()
175: .get(this .getClass()));
176:
177: // get the second component
178: AssertionError ex = null;
179: try {
180: m_container.getResolver().get(null);
181: } catch (AssertionError ae) {
182: ex = ae;
183: }
184: assertNotNull(ex);
185: }
186:
187: public void testGetThrowsExceptions()
188: throws IllegalAccessException, InvocationTargetException,
189: ClassNotFoundException, InstantiationException {
190: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
191: Throwable t = null;
192: try {
193: m_container.getResolver().get("blah");
194: } catch (Throwable th) {
195: t = th;
196: }
197: assertEquals(m_exceptionAdapter.t, t);
198:
199: m_container = new DefaultContainer();
200: m_exceptionAdapter = new ExceptionThrowingAdapter(
201: new JicarillaInvocationTargetException(new Exception()));
202: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
203: t = null;
204: try {
205: m_container.getResolver().get("blah");
206: } catch (Throwable th) {
207: t = th;
208: }
209: assertEquals(m_exceptionAdapter.t, t);
210:
211: m_container = new DefaultContainer();
212: m_exceptionAdapter = new ExceptionThrowingAdapter(
213: new JicarillaInstantiationException());
214: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
215: t = null;
216: try {
217: m_container.getResolver().get("blah");
218: } catch (Throwable th) {
219: t = th;
220: }
221: assertEquals(m_exceptionAdapter.t, t);
222:
223: m_container = new DefaultContainer();
224: m_exceptionAdapter = new ExceptionThrowingAdapter(
225: new JicarillaClassNotFoundException());
226: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
227: t = null;
228: try {
229: m_container.getResolver().get("blah");
230: } catch (Throwable th) {
231: t = th;
232: }
233: assertEquals(m_exceptionAdapter.t, t);
234:
235: // bad!
236: m_container = new DefaultContainer();
237: m_exceptionAdapter = new ExceptionThrowingAdapter(
238: new RuntimeException());
239: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
240: t = null;
241: try {
242: m_container.getResolver().get("blah");
243: } catch (Throwable th) {
244: t = th;
245: }
246: assertNotNull(t);
247:
248: /*
249: // bad!: will return true in contains(), then false on the next calls
250: m_container = new DefaultContainer();
251: m_container.addAdapter(
252: new SelectOnlyOnceSelector(),
253: new NoopComponentAdapter() );
254: t = null;
255: try
256: {
257: m_container.getResolver().get("blah");
258: }
259: catch( Throwable th )
260: {
261: t = th;
262: }
263: assertTrue( t instanceof RuntimeException ); // don't care much what kind of exception this is
264: */
265: }
266:
267: public void testGetAll() throws Exception {
268: Throwable t = null;
269: try {
270: m_container.getResolver().getAll(null);
271: } catch (AssertionError th) {
272: t = th;
273: }
274: assertNotNull(t);
275:
276: assertEquals(0, m_container.getResolver().getAll("blah").length);
277:
278: m_container.registerAdapter(m_trueSelector, m_adapter);
279:
280: assertEquals(1, m_container.getResolver().getAll("blah").length);
281: assertEquals(m_adapter.m_object, m_container.getResolver()
282: .getAll("blah")[0]);
283:
284: m_container.registerAdapter(m_trueSelector, m_adapter);
285: assertEquals(2, m_container.getResolver().getAll("blah").length);
286:
287: m_container.registerAdapter(m_falseSelector,
288: new NoopComponentAdapter());
289: assertEquals(2, m_container.getResolver().getAll("blah").length);
290:
291: m_container.registerAdapter(m_trueSelector,
292: new NoopComponentAdapter());
293: assertEquals(3,
294: m_container.getResolver().getAll("Yahoo!").length);
295: }
296:
297: public void testGetAllThrowsExceptions()
298: throws IllegalAccessException, InvocationTargetException,
299: ClassNotFoundException, InstantiationException {
300: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
301: Throwable t = null;
302: try {
303: m_container.getResolver().getAll("blah");
304: } catch (Throwable th) {
305: t = th;
306: }
307: assertEquals(m_exceptionAdapter.t, t);
308:
309: m_container = new DefaultContainer();
310: m_exceptionAdapter = new ExceptionThrowingAdapter(
311: new JicarillaInvocationTargetException(new Exception()));
312: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
313: t = null;
314: try {
315: m_container.getResolver().getAll("blah");
316: } catch (Throwable th) {
317: t = th;
318: }
319: assertEquals(m_exceptionAdapter.t, t);
320:
321: m_container = new DefaultContainer();
322: m_exceptionAdapter = new ExceptionThrowingAdapter(
323: new JicarillaInstantiationException());
324: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
325: t = null;
326: try {
327: m_container.getResolver().getAll("blah");
328: } catch (Throwable th) {
329: t = th;
330: }
331: assertEquals(m_exceptionAdapter.t, t);
332:
333: m_container = new DefaultContainer();
334: m_exceptionAdapter = new ExceptionThrowingAdapter(
335: new JicarillaClassNotFoundException());
336: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
337: t = null;
338: try {
339: m_container.getResolver().getAll("blah");
340: } catch (Throwable th) {
341: t = th;
342: }
343: assertEquals(m_exceptionAdapter.t, t);
344:
345: // bad!
346: m_container = new DefaultContainer();
347: m_exceptionAdapter = new ExceptionThrowingAdapter(
348: new RuntimeException());
349: m_container.registerAdapter(m_trueSelector, m_exceptionAdapter);
350: t = null;
351: try {
352: m_container.getResolver().getAll("blah");
353: } catch (Throwable th) {
354: t = th;
355: }
356: assertNotNull(t);
357:
358: // bad!: will return true in contains(), then false on the next calls
359: m_container = new DefaultContainer();
360: m_container.registerAdapter(new SelectOnlyOnceSelector(),
361: new NoopComponentAdapter());
362: Object[] instances = m_container.getResolver().getAll("blah");
363: assertEquals(0, instances.length);
364: }
365:
366: public void testRelease() throws Exception {
367: m_container.getResolver().releaseInstance(null);
368:
369: m_container.registerAdapter(m_trueSelector, m_adapter);
370: Object obj = m_container.getResolver().get("blah");
371: m_container.getResolver().releaseInstance(obj);
372:
373: m_container.getResolver().releaseInstance(new Object());
374: m_container.getResolver().releaseInstance(this );
375: }
376:
377: public static class NoopComponentAdapter implements Adapter {
378: public Object m_object;
379:
380: public NoopComponentAdapter() {
381: m_object = new Object();
382: }
383:
384: public Object getInstance() {
385: return m_object;
386: }
387:
388: public void releaseInstance(Object component) throws Exception {
389: }
390: }
391:
392: public static class ExceptionThrowingAdapter extends
393: NoopComponentAdapter {
394: public Throwable t;
395:
396: public ExceptionThrowingAdapter(Throwable throwable) {
397: t = throwable;
398: }
399:
400: public Object getInstance() {
401: if (t instanceof RuntimeException)
402: throw (RuntimeException) t;
403:
404: throw new JicarillaException("blah", t);
405: }
406: }
407:
408: public static class SelectOnlyOnceSelector implements Selector {
409: public boolean didSelect = false;
410:
411: public boolean select(Object object) {
412: if (!didSelect) {
413: didSelect = true;
414: return true;
415: }
416: return false;
417: }
418: }
419:
420: public static class MockResolver implements Resolver {
421: public Object get(Object key) throws ResolutionException,
422: JicarillaIllegalAccessException,
423: JicarillaInvocationTargetException,
424: JicarillaInstantiationException,
425: JicarillaClassNotFoundException,
426: InitializationException, JicarillaException {
427: return null;
428: }
429:
430: public Object[] getAll(Object key)
431: throws JicarillaIllegalAccessException,
432: JicarillaInvocationTargetException,
433: JicarillaInstantiationException,
434: JicarillaClassNotFoundException,
435: InitializationException, JicarillaException {
436: return new Object[0];
437: }
438:
439: public boolean contains(Object key) {
440: return false;
441: }
442:
443: public void releaseInstance(Object instance) throws Exception {
444: }
445:
446: }
447:
448: /*public static class Exposer extends DefaultContainer
449: {
450: protected void setInstanceToAdapterMap( final Map instanceToAdapterMap )
451: {
452: m_instanceToAdapterMap = instanceToAdapterMap;
453: }
454: protected void setSwitch( final Switch switcher )
455: {
456: if( switcher != null )
457: m_switch = switcher;
458: else
459: m_switch = new SelectorSwitch();
460: }
461: protected void setResolver( final Resolver resolver )
462: {
463: if( resolver != null )
464: m_resolver = resolver;
465: else
466: m_resolver = new DefaultResolver( new DefaultResolverCallback() );
467: }
468: }*/
469: }
|