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.builder;
027:
028: import junit.framework.TestCase;
029: import org.easymock.MockControl;
030: import org.jicarilla.container.KeyAwareAdapter;
031: import org.jicarilla.container.KeyRelayingContainer;
032: import org.jicarilla.container.Resolver;
033: import org.jicarilla.container.adapters.ResolverBasedKeyAwareAdapter;
034: import org.jicarilla.container.builder.BuilderHelper;
035: import org.jicarilla.container.builder.ContainerAwareBuilderHelper;
036: import org.jicarilla.container.builder.DefaultBuilder;
037: import org.jicarilla.container.builder.DefaultHelper;
038: import org.jicarilla.container.builder.EntryHelper;
039: import org.jicarilla.lang.Selector;
040: import org.jicarilla.lang.TrueSelector;
041:
042: /**
043: *
044: *
045: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
046: * @version $Id: EntryHelperTestCase.java,v 1.3 2004/03/23 15:59:54 lsimons Exp $
047: */
048: public class EntryHelperTestCase extends TestCase {
049: MockControl resolverControl;
050: Resolver resolver;
051: MockControl containerControl;
052: KeyRelayingContainer container;
053:
054: public void setUp() throws Exception {
055: super .setUp();
056:
057: resolverControl = MockControl.createControl(Resolver.class);
058: resolver = (Resolver) resolverControl.getMock();
059: containerControl = MockControl
060: .createControl(KeyRelayingContainer.class);
061: container = (KeyRelayingContainer) containerControl.getMock();
062: }
063:
064: public void testGetAdapterUsingBuilderHelper() throws Exception {
065: BuilderHelper h = new DefaultHelper();
066:
067: resolver.get(this );
068: resolverControl.setReturnValue(h);
069: resolverControl.replay();
070: containerControl.replay();
071:
072: EntryHelper helper = new EntryHelper(resolver, container);
073:
074: DefaultBuilder.Entry entry = new DefaultBuilder.Entry(
075: new Boolean(true), this );
076:
077: KeyAwareAdapter kaa = helper.getAdapter(entry);
078: assertNotNull(kaa);
079: assertEquals(this , kaa.getInstance("blah"));
080:
081: resolverControl.verify();
082: containerControl.verify();
083: }
084:
085: public void testGetAdapterUsingContainerAwareBuilderHelper()
086: throws Exception {
087: MockControl hControl = MockControl
088: .createControl(ContainerAwareBuilderHelper.class);
089: ContainerAwareBuilderHelper h = (ContainerAwareBuilderHelper) hControl
090: .getMock();
091:
092: h.getAdapter(this , resolver);
093: hControl.setReturnValue(new ResolverBasedKeyAwareAdapter(
094: resolver));
095: hControl.replay();
096:
097: resolver.get(this );
098: resolverControl.setReturnValue(h);
099: resolver.get("blah");
100: resolverControl.setReturnValue("booh!");
101: resolverControl.replay();
102: container.getResolver();
103: containerControl.setReturnValue(resolver);
104: containerControl.replay();
105:
106: EntryHelper helper = new EntryHelper(resolver, container);
107:
108: DefaultBuilder.Entry entry = new DefaultBuilder.Entry(
109: new Boolean(true), this );
110:
111: KeyAwareAdapter kaa = helper.getAdapter(entry);
112: assertNotNull(kaa);
113: assertEquals("booh!", kaa.getInstance("blah"));
114:
115: resolverControl.verify();
116: containerControl.verify();
117: hControl.verify();
118: }
119:
120: public void testGetAdapterDoesNotAcceptNullArgument()
121: throws Exception {
122: EntryHelper helper = new EntryHelper(resolver, container);
123: try {
124: helper.getAdapter(null);
125: fail("Expected an exception!");
126: } catch (AssertionError e) {
127: }
128: }
129:
130: public void testGetAdapterThrowsClassCastException()
131: throws Exception {
132: EntryHelper helper = new EntryHelper(resolver, container);
133: try {
134: helper.getAdapter(this );
135: fail("Expected an exception!");
136: } catch (ClassCastException e) {
137: }
138: }
139:
140: public void testGetSelector() throws Exception {
141: // TODO
142: EntryHelper helper = new EntryHelper(resolver, container);
143:
144: TrueSelector selector = new TrueSelector();
145:
146: DefaultBuilder.Entry entry = new DefaultBuilder.Entry(selector,
147: this );
148:
149: Selector s = helper.getSelector(entry);
150: assertEquals(selector, s);
151:
152: entry = new DefaultBuilder.Entry(this .getClass(), this );
153: s = helper.getSelector(entry);
154: assertTrue(s.select(this .getClass()));
155: assertTrue(s.select(this .getClass().getName()));
156: assertFalse(s.select(null));
157: assertFalse(s.select(resolver));
158: assertFalse(s.select(helper));
159:
160: entry = new DefaultBuilder.Entry("blah", this );
161: s = helper.getSelector(entry);
162: assertTrue(s.select("blah"));
163: assertFalse(s.select(null));
164: assertFalse(s.select(this ));
165:
166: entry = new DefaultBuilder.Entry(resolver, this );
167: s = helper.getSelector(entry);
168: resolver.contains("blah");
169: resolverControl.setReturnValue(true);
170: resolverControl.replay();
171: assertTrue(s.select("blah"));
172: resolverControl.verify();
173:
174: entry = new DefaultBuilder.Entry(this , this );
175: s = helper.getSelector(entry);
176: assertTrue(s.select(this ));
177: assertFalse(s.select(null));
178: assertFalse(s.select(this .getClass()));
179: }
180:
181: public void testGetSelectorDoesNotAcceptNullArgument()
182: throws Exception {
183: EntryHelper helper = new EntryHelper(resolver, container);
184: try {
185: helper.getSelector(null);
186: fail("Expected an exception!");
187: } catch (AssertionError e) {
188: }
189: }
190:
191: public void testGetSelectorThrowsClassCastException()
192: throws Exception {
193: EntryHelper helper = new EntryHelper(resolver, container);
194: try {
195: helper.getSelector(this );
196: fail("Expected an exception!");
197: } catch (ClassCastException e) {
198: }
199: }
200: }
|