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.tck.test.util;
027:
028: import junit.framework.TestCase;
029: import org.jicarilla.container.tck.util.DefaultProxyManager;
030:
031: import java.lang.reflect.InvocationHandler;
032: import java.lang.reflect.Method;
033:
034: /**
035: *
036: *
037: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
038: * @version $Id: DefaultProxyManagerTestCase.java,v 1.2 2004/01/14 20:58:50 lsimons Exp $
039: */
040: public class DefaultProxyManagerTestCase extends TestCase {
041: public DefaultProxyManager pm = new DefaultProxyManager();
042:
043: public void testConstructor() {
044: new DefaultProxyManager();
045: }
046:
047: public void testGetPassthroughProxy() {
048: MockImpl mock = new MockImpl();
049:
050: Object proxy = pm.getProxy(mock, new Class[] { Mock.class },
051: this .getClass().getClassLoader());
052:
053: assertTrue(proxy instanceof Mock);
054: assertFalse(proxy instanceof MockImpl);
055:
056: Mock proxyMock = (Mock) proxy;
057: assertFalse(proxyMock.getStuffDone());
058:
059: proxyMock.doStuff();
060: assertTrue(mock.getStuffDone());
061: assertTrue(proxyMock.getStuffDone());
062:
063: try {
064: pm.getProxy(null, new Class[] { Mock.class }, this
065: .getClass().getClassLoader());
066:
067: fail("Expected an exception!");
068: } catch (AssertionError th) {
069: }
070: }
071:
072: public void testGetCustomHandlerProxy() {
073: MockInvocationHandler handler = new MockInvocationHandler();
074:
075: Object proxy = pm.getProxy(new Class[] { Mock.class }, this
076: .getClass().getClassLoader(), handler);
077:
078: assertTrue(proxy instanceof Mock);
079: assertFalse(proxy instanceof MockImpl);
080: assertFalse(proxy instanceof InvocationHandler);
081:
082: Mock proxyMock = (Mock) proxy;
083: assertFalse(proxyMock.getStuffDone());
084:
085: proxyMock.doStuff();
086: assertTrue(proxyMock.getStuffDone());
087: }
088:
089: public static interface Mock {
090: public void doStuff();
091:
092: public boolean getStuffDone();
093: }
094:
095: public static class MockImpl implements Mock {
096: public boolean m_stuffDone = false;
097:
098: public void doStuff() {
099: m_stuffDone = true;
100: }
101:
102: public boolean getStuffDone() {
103: return m_stuffDone;
104: }
105: }
106:
107: public static class MockInvocationHandler implements
108: InvocationHandler {
109: public boolean invoked = false;
110:
111: public Object invoke(Object proxy, Method method, Object[] args)
112: throws Throwable {
113: if (method.getName() == "doStuff")
114: invoked = true;
115: if (method.getName() == "getStuffDone")
116: return new Boolean(invoked);
117:
118: return null;
119: }
120:
121: }
122: }
|