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.DefaultInvocationRecorder;
030: import org.jicarilla.container.tck.util.InvocationRecorderWrapper;
031: import org.jicarilla.container.tck.util.MemberInvocation;
032:
033: import java.lang.reflect.Method;
034: import java.util.List;
035:
036: /**
037: *
038: *
039: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
040: * @version $Id: InvocationLoggerWrapperTestCase.java,v 1.3 2004/01/18 19:32:05 lsimons Exp $
041: */
042: public class InvocationLoggerWrapperTestCase extends TestCase {
043: public void testConstructor() {
044: Mock target = new MockImpl();
045: DefaultInvocationRecorder logger = new DefaultInvocationRecorder();
046:
047: new InvocationRecorderWrapper(target, logger);
048:
049: try {
050: new InvocationRecorderWrapper(null, logger);
051:
052: fail("Expected an exception!");
053: } catch (AssertionError th) {
054: }
055: try {
056: new InvocationRecorderWrapper(target, null);
057:
058: fail("Expected an exception!");
059: } catch (AssertionError th) {
060: }
061: }
062:
063: public void testInvocations() throws Throwable {
064: Mock target = new MockImpl();
065: DefaultInvocationRecorder logger = new DefaultInvocationRecorder();
066:
067: InvocationRecorderWrapper wrapper = new InvocationRecorderWrapper(
068: target, logger);
069:
070: Method m = Mock.class.getMethod("doStuff", new Class[0]);
071:
072: Object result = wrapper.invoke(target, m, new Object[0]);
073: assertTrue(result == null);
074:
075: List invocations = logger.getInvocations();
076: assertEquals(1, invocations.size());
077:
078: MemberInvocation inv = (MemberInvocation) invocations.get(0);
079: assertNotNull(inv);
080:
081: assertEquals(m, inv.getMember());
082: assertEquals(target, inv.getTarget());
083: assertEquals(target, inv.getProxy());
084: assertNull(inv.getThrowable());
085: assertNull(inv.getResult());
086:
087: assertEquals(0, inv.getArgs().length);
088:
089: m = Mock.class.getMethod("getStuffDone", new Class[0]);
090: result = wrapper.invoke(target, m, new Object[0]);
091: assertTrue(result instanceof Boolean);
092: boolean isTrue = ((Boolean) result).booleanValue();
093: assertTrue(isTrue);
094:
095: m = Mock.class.getMethod("throwException",
096: new Class[] { String.class });
097:
098: result = wrapper.invoke(target, m, new Object[] { "blah" });
099: assertNull(result);
100: assertEquals(3, invocations.size());
101:
102: inv = (MemberInvocation) invocations.get(2);
103: Throwable t = inv.getThrowable();
104: assertTrue(t instanceof RuntimeException);
105: RuntimeException re = (RuntimeException) t;
106: assertEquals("blah", re.getMessage());
107: }
108:
109: public static interface Mock {
110: public void doStuff();
111:
112: public boolean getStuffDone();
113:
114: public void throwException(String message)
115: throws RuntimeException;
116: }
117:
118: public static class MockImpl implements Mock {
119: public boolean m_stuffDone = false;
120:
121: public void doStuff() {
122: m_stuffDone = true;
123: }
124:
125: public boolean getStuffDone() {
126: return m_stuffDone;
127: }
128:
129: public void throwException(String message)
130: throws RuntimeException {
131: throw new RuntimeException(message);
132: }
133: }
134:
135: }
|