summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/sca/core/src/test/java/org/apache/tuscany/core/wire/InvocationErrorTestCase.java
blob: 1d04e1875743a4957acef0d7fa17faa26d3b4c9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.core.wire;

import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.tuscany.core.wire.impl.InvokerInterceptor;
import org.apache.tuscany.core.wire.jdk.JDKInvocationHandler;
import org.apache.tuscany.core.wire.mock.MockHandler;
import org.apache.tuscany.core.wire.mock.MockStaticInvoker;
import org.apache.tuscany.core.wire.mock.MockSyncInterceptor;
import org.apache.tuscany.core.message.impl.MessageFactoryImpl;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;

/**
 * Tests handling of exceptions thrown during an wire
 * 
 * @version $Rev: 377006 $ $Date: 2006-02-11 09:41:59 -0800 (Sat, 11 Feb 2006) $
 */
public class InvocationErrorTestCase extends TestCase {

    private Method checkedMethod;
    private Method runtimeMethod;

    public InvocationErrorTestCase() {
        super();
    }

    public InvocationErrorTestCase(String arg0) {
        super(arg0);
    }

    public void setUp() throws Exception {
        checkedMethod = TestBean.class.getDeclaredMethod("checkedException", (Class[]) null);
        runtimeMethod = TestBean.class.getDeclaredMethod("runtimeException", (Class[]) null);
        Assert.assertNotNull(checkedMethod);
        Assert.assertNotNull(runtimeMethod);
    }

    public void testCheckedException() throws Exception {
        Map<Method, InvocationConfiguration> config = new MethodHashMap();
        config.put(checkedMethod, getConfiguration(checkedMethod));
        InvocationHandler handler = new JDKInvocationHandler(new MessageFactoryImpl(), config);
        try {
            TestBean proxy = (TestBean) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                    new Class[]{TestBean.class}, handler);
            proxy.checkedException();
        } catch (TestException e) {
            return;
        }
        Assert.fail(TestException.class.getName() + " should have been thrown");
    }

    public void testRuntimeException() throws Exception {
        Map<Method, InvocationConfiguration> config = new MethodHashMap<InvocationConfiguration>();
        config.put(runtimeMethod, getConfiguration(runtimeMethod));
        InvocationHandler handler = new JDKInvocationHandler(new MessageFactoryImpl(), config);
        try {
            TestBean proxy = (TestBean) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                    new Class[]{TestBean.class}, handler);
            proxy.runtimeException();
        } catch (TestRuntimeException e) {
            return;
        }
        Assert.fail(TestException.class.getName() + " should have been thrown");
    }

    private InvocationConfiguration getConfiguration(Method m) {
        MockStaticInvoker invoker = new MockStaticInvoker(m, new TestBeanImpl());
        SourceInvocationConfiguration invocationConfiguration=new SourceInvocationConfiguration(m);
        invocationConfiguration.addInterceptor(new MockSyncInterceptor());
        invocationConfiguration.addRequestHandler(new MockHandler());
        invocationConfiguration.setTargetInvoker(invoker);
        invocationConfiguration.setTargetInterceptor(new InvokerInterceptor());
        invocationConfiguration.build();
        return invocationConfiguration;
    }

    public interface TestBean {

        public void checkedException() throws TestException;

        public void runtimeException() throws TestRuntimeException;

    }

    public class TestBeanImpl implements TestBean {

        public void checkedException() throws TestException {
            throw new TestException();
        }

        public void runtimeException() throws TestRuntimeException {
            throw new TestRuntimeException();
        }
    }

    public class TestException extends Exception {
    }

    public class TestRuntimeException extends RuntimeException {
    }

}