01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.interceptor;
19:
20: import java.util.ArrayList;
21: import java.util.List;
22: import java.util.concurrent.Executor;
23:
24: import org.apache.cxf.endpoint.Endpoint;
25: import org.apache.cxf.message.Exchange;
26: import org.apache.cxf.message.ExchangeImpl;
27: import org.apache.cxf.message.MessageImpl;
28: import org.apache.cxf.service.ServiceImpl;
29: import org.apache.cxf.service.invoker.Invoker;
30: import org.apache.cxf.service.model.ServiceInfo;
31: import org.easymock.EasyMock;
32: import org.easymock.IMocksControl;
33: import org.junit.Assert;
34: import org.junit.Test;
35:
36: public class ServiceInvokerInterceptorTest extends Assert {
37:
38: @Test
39: public void testInterceptor() throws Exception {
40: ServiceInvokerInterceptor intc = new ServiceInvokerInterceptor();
41:
42: MessageImpl m = new MessageImpl();
43: Exchange exchange = new ExchangeImpl();
44: m.setExchange(exchange);
45: exchange.setInMessage(m);
46:
47: exchange.setOutMessage(new MessageImpl());
48:
49: TestInvoker i = new TestInvoker();
50: Endpoint endpoint = createEndpoint(i);
51: exchange.put(Endpoint.class, endpoint);
52: Object input = new Object();
53: List<Object> lst = new ArrayList<Object>();
54: lst.add(input);
55: m.setContent(List.class, lst);
56:
57: intc.handleMessage(m);
58:
59: assertTrue(i.invoked);
60:
61: List<?> list = exchange.getOutMessage().getContent(List.class);
62: assertEquals(input, list.get(0));
63: }
64:
65: Endpoint createEndpoint(Invoker i) throws Exception {
66: IMocksControl control = EasyMock.createNiceControl();
67: Endpoint endpoint = control.createMock(Endpoint.class);
68:
69: ServiceImpl service = new ServiceImpl((ServiceInfo) null);
70: service.setInvoker(i);
71: service.setExecutor(new SimpleExecutor());
72: EasyMock.expect(endpoint.getService()).andReturn(service)
73: .anyTimes();
74:
75: control.replay();
76:
77: return endpoint;
78: }
79:
80: static class TestInvoker implements Invoker {
81: boolean invoked;
82:
83: public Object invoke(Exchange exchange, Object o) {
84: invoked = true;
85: assertNotNull(exchange);
86: assertNotNull(o);
87: return o;
88: }
89: }
90:
91: static class SimpleExecutor implements Executor {
92:
93: public void execute(Runnable command) {
94: command.run();
95: }
96:
97: }
98: }
|