001: package com.mockrunner.test.web;
002:
003: import java.io.Serializable;
004: import java.lang.reflect.Method;
005:
006: import com.mockrunner.struts.DynamicMockProxyGenerator;
007:
008: import junit.framework.TestCase;
009:
010: public class DynamicMockProxyGeneratorTest extends TestCase {
011: private Delegator delegator;
012:
013: protected void setUp() throws Exception {
014: super .setUp();
015: delegator = new Delegator();
016: }
017:
018: protected void tearDown() throws Exception {
019: super .tearDown();
020: delegator = null;
021: }
022:
023: public void testProperInstance() {
024: DynamicMockProxyGenerator generator = new DynamicMockProxyGenerator(
025: DynamicMockProxyGeneratorTest.class, delegator,
026: Delegator.class.getDeclaredMethods(), new Method[0],
027: Serializable.class);
028: Object proxy = generator.createProxy();
029: assertTrue(proxy instanceof Serializable);
030: assertTrue(proxy instanceof DynamicMockProxyGeneratorTest);
031: }
032:
033: public void testDelegationAndDuplication() {
034: DynamicMockProxyGenerator generator = new DynamicMockProxyGenerator(
035: DynamicMockProxyGeneratorTest.class, delegator,
036: Delegator.class.getDeclaredMethods(), Super.class
037: .getDeclaredMethods());
038: DynamicMockProxyGeneratorTest proxy = (DynamicMockProxyGeneratorTest) generator
039: .createProxy();
040: proxy.method1("method1");
041: assertEquals(3, proxy.method2("method2", (short) 5));
042: assertEquals("test", proxy.method3());
043: assertTrue(delegator.wasMethod1Called());
044: assertTrue(delegator.wasMethod2Called());
045: assertEquals("method1", delegator.getMethod1Param());
046: assertEquals("method2", delegator.getMethod2Param1());
047: assertEquals(5, delegator.getMethod2Param2());
048: assertEquals("DynamicMockProxyGeneratorTest", proxy
049: .super Method(5));
050: assertTrue(delegator.wasSuperMethodCalled());
051: assertEquals(5, delegator.getSuperMethodParam());
052: }
053:
054: public void method1(String name) {
055:
056: }
057:
058: public int method2(String name, short value) {
059: return 0;
060: }
061:
062: public String method3() {
063: return "test";
064: }
065:
066: public String super Method(int value) {
067: return "DynamicMockProxyGeneratorTest";
068: }
069:
070: public static class Super {
071: private boolean wasSuperMethodCalled = false;
072: private int super MethodParam = 0;
073:
074: public String super Method(int value) {
075: wasSuperMethodCalled = true;
076: super MethodParam = value;
077: return "Super";
078: }
079:
080: public boolean wasSuperMethodCalled() {
081: return wasSuperMethodCalled;
082: }
083:
084: public int getSuperMethodParam() {
085: return super MethodParam;
086: }
087: }
088:
089: public static class Delegator extends Super {
090: private boolean wasMethod1Called = false;
091: private boolean wasMethod2Called = false;
092: private String method1Param = null;
093: private String method2Param1 = null;
094: private short method2Param2 = 0;
095:
096: public void method1(String name) {
097: method1Param = name;
098: wasMethod1Called = true;
099: }
100:
101: public int method2(String name, short value) {
102: method2Param1 = name;
103: method2Param2 = value;
104: wasMethod2Called = true;
105: return 3;
106: }
107:
108: public String getMethod1Param() {
109: return method1Param;
110: }
111:
112: public String getMethod2Param1() {
113: return method2Param1;
114: }
115:
116: public short getMethod2Param2() {
117: return method2Param2;
118: }
119:
120: public boolean wasMethod1Called() {
121: return wasMethod1Called;
122: }
123:
124: public boolean wasMethod2Called() {
125: return wasMethod2Called;
126: }
127: }
128: }
|