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.integration.avalon;
027:
028: import junit.framework.TestCase;
029: import org.apache.avalon.fortress.Container;
030: import org.apache.avalon.fortress.impl.handler.ComponentHandler;
031: import org.apache.avalon.framework.service.ServiceException;
032: import org.jicarilla.container.Resolver;
033: import org.jicarilla.container.adapters.SingletonAdapter;
034: import org.jicarilla.container.factories.Type1Factory;
035: import org.jicarilla.container.factories.Type25Factory;
036: import org.jicarilla.container.factories.Type2Factory;
037: import org.jicarilla.container.factories.Type35Factory;
038: import org.jicarilla.container.integration.avalon.JicarillaBasedFortressContainer;
039: import org.jicarilla.container.selectors.HintedClassSelector;
040: import org.jicarilla.container.tck.components.interfaces.Apu;
041: import org.jicarilla.container.tck.components.interfaces.Bart;
042: import org.jicarilla.container.tck.components.interfaces.Homer;
043: import org.jicarilla.container.tck.components.interfaces.Lisa;
044: import org.jicarilla.container.tck.components.interfaces.Maggie;
045: import org.jicarilla.container.tck.components.interfaces.Marge;
046: import org.jicarilla.container.tck.components.interfaces.Script;
047: import org.jicarilla.container.tck.components.type1.avalon.AvalonApu;
048: import org.jicarilla.container.tck.components.type1.avalon.AvalonBart;
049: import org.jicarilla.container.tck.components.type1.avalon.AvalonHomer;
050: import org.jicarilla.container.tck.components.type1.avalon.AvalonLisa;
051: import org.jicarilla.container.tck.components.type1.avalon.AvalonMaggie;
052: import org.jicarilla.container.tck.components.type1.avalon.AvalonMarge;
053: import org.jicarilla.container.tck.components.type3.rich.AllInTheFamilyScript;
054:
055: /**
056: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
057: * @version $Id: JicarillaBasedFortressContainerTestCase.java,v 1.2 2004/03/17 14:14:23 lsimons Exp $
058: */
059: public class JicarillaBasedFortressContainerTestCase extends TestCase {
060: Container container = getContainer();
061:
062: protected Container getContainer() {
063: JicarillaBasedFortressContainer jc = new JicarillaBasedFortressContainer();
064: Resolver r = jc.getResolver();
065: jc.registerAdapter(new HintedClassSelector(Apu.class),
066: new SingletonAdapter(new Type1Factory(r,
067: AvalonApu.class.getName())));
068: jc.registerAdapter(new HintedClassSelector(Marge.class),
069: new SingletonAdapter(new Type1Factory(r,
070: AvalonMarge.class.getName())));
071: jc.registerAdapter(new HintedClassSelector(Homer.class),
072: new SingletonAdapter(new Type25Factory(r,
073: AvalonHomer.class.getName())));
074: jc.registerAdapter(new HintedClassSelector(Bart.class),
075: new SingletonAdapter(new Type2Factory(r,
076: AvalonBart.class.getName())));
077: jc.registerAdapter(new HintedClassSelector(Lisa.class),
078: new SingletonAdapter(new Type1Factory(r,
079: AvalonLisa.class.getName())));
080: jc.registerAdapter(new HintedClassSelector(Maggie.class),
081: new SingletonAdapter(new Type2Factory(r,
082: AvalonMaggie.class.getName())));
083: jc.registerAdapter(new HintedClassSelector(Script.class),
084: new SingletonAdapter(new Type35Factory(r,
085: AllInTheFamilyScript.class.getName())));
086:
087: return jc;
088: }
089:
090: public void testBasicGet() throws Throwable {
091: ComponentHandler handler = (ComponentHandler) container.get(
092: Homer.class.getName(), null);
093:
094: Object instance = handler.get();
095: assertTrue(instance instanceof Homer);
096: assertTrue(Homer.class.isAssignableFrom(handler
097: .getComponentClass()));
098: handler.put(instance);
099: }
100:
101: public void testGetDoesNotAcceptNullKey() throws Throwable {
102: try {
103: container.get(null, null);
104: fail();
105: } catch (Throwable t) {
106: assertNotNull(t);
107: assertTrue(t instanceof ServiceException);
108: }
109: }
110:
111: public void testGetAllSimpsons() throws Throwable {
112: ComponentHandler handler = (ComponentHandler) container.get(
113: Homer.class.getName(), null);
114: Object instance = handler.get();
115: assertTrue(instance instanceof Homer);
116:
117: handler = (ComponentHandler) container.get(Apu.class.getName(),
118: null);
119: instance = handler.get();
120: assertTrue(instance instanceof Apu);
121:
122: handler = (ComponentHandler) container.get(Marge.class
123: .getName(), null);
124: instance = handler.get();
125: assertTrue(instance instanceof Marge);
126:
127: handler = (ComponentHandler) container.get(
128: Bart.class.getName(), null);
129: instance = handler.get();
130: assertTrue(instance instanceof Bart);
131:
132: handler = (ComponentHandler) container.get(
133: Lisa.class.getName(), null);
134: instance = handler.get();
135: assertTrue(instance instanceof Lisa);
136:
137: handler = (ComponentHandler) container.get(Maggie.class
138: .getName(), null);
139: instance = handler.get();
140: assertTrue(instance instanceof Maggie);
141:
142: handler = (ComponentHandler) container.get(Script.class
143: .getName(), null);
144: instance = handler.get();
145: assertTrue(instance instanceof Script);
146:
147: Script script = (Script) instance;
148: script.runEpisode();
149: }
150: }
|