001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.xml.interceptor;
019:
020: import java.io.InputStream;
021: import java.io.OutputStream;
022: import java.lang.reflect.Method;
023: import java.util.SortedSet;
024: import java.util.TreeSet;
025:
026: import javax.xml.bind.JAXBContext;
027: import javax.xml.namespace.QName;
028: import javax.xml.stream.XMLStreamReader;
029: import javax.xml.stream.XMLStreamWriter;
030:
031: import org.apache.cxf.Bus;
032: import org.apache.cxf.binding.Binding;
033: import org.apache.cxf.binding.BindingFactoryManager;
034: import org.apache.cxf.binding.xml.XMLBindingFactory;
035: import org.apache.cxf.endpoint.Endpoint;
036: import org.apache.cxf.endpoint.EndpointImpl;
037: import org.apache.cxf.jaxb.JAXBDataBinding;
038: import org.apache.cxf.message.Exchange;
039: import org.apache.cxf.message.ExchangeImpl;
040: import org.apache.cxf.message.Message;
041: import org.apache.cxf.message.MessageImpl;
042: import org.apache.cxf.phase.Phase;
043: import org.apache.cxf.phase.PhaseInterceptorChain;
044: import org.apache.cxf.service.model.EndpointInfo;
045: import org.apache.cxf.service.model.ServiceInfo;
046: import org.apache.cxf.staxutils.StaxUtils;
047: import org.apache.cxf.transport.DestinationFactoryManager;
048: import org.apache.cxf.wsdl.WSDLManager;
049: import org.apache.cxf.wsdl11.WSDLManagerImpl;
050: import org.apache.cxf.wsdl11.WSDLServiceFactory;
051: import org.easymock.classextension.EasyMock;
052: import org.easymock.classextension.IMocksControl;
053: import org.junit.After;
054: import org.junit.Assert;
055: import org.junit.Before;
056:
057: public class TestBase extends Assert {
058:
059: protected PhaseInterceptorChain chain;
060:
061: protected Message xmlMessage;
062:
063: protected Bus bus;
064:
065: protected IMocksControl control;
066:
067: protected ServiceInfo serviceInfo;
068:
069: @Before
070: public void setUp() throws Exception {
071: SortedSet<Phase> phases = new TreeSet<Phase>();
072: Phase phase1 = new Phase("phase1", 1);
073: Phase phase2 = new Phase("phase2", 2);
074: Phase phase3 = new Phase("phase3", 3);
075: phases.add(phase1);
076: phases.add(phase2);
077: phases.add(phase3);
078: chain = new PhaseInterceptorChain(phases);
079:
080: Exchange exchange = new ExchangeImpl();
081: MessageImpl messageImpl = new MessageImpl();
082: messageImpl.setInterceptorChain(chain);
083: messageImpl.setExchange(exchange);
084: xmlMessage = messageImpl;
085: }
086:
087: @After
088: public void tearDown() throws Exception {
089: }
090:
091: public InputStream getTestStream(Class<?> clz, String file) {
092: return clz.getResourceAsStream(file);
093: }
094:
095: public XMLStreamReader getXMLStreamReader(InputStream is) {
096: return StaxUtils.createXMLStreamReader(is);
097: }
098:
099: public XMLStreamWriter getXMLStreamWriter(OutputStream os) {
100: return StaxUtils.createXMLStreamWriter(os);
101: }
102:
103: public Method getTestMethod(Class<?> sei, String methodName) {
104: Method[] iMethods = sei.getMethods();
105: for (Method m : iMethods) {
106: if (methodName.equals(m.getName())) {
107: return m;
108: }
109: }
110: return null;
111: }
112:
113: protected void common(String wsdl, QName portName,
114: Class... jaxbClasses) throws Exception {
115: control = EasyMock.createNiceControl();
116:
117: bus = control.createMock(Bus.class);
118: EasyMock.expect(bus.getExtension(WSDLManager.class))
119: .andStubReturn(new WSDLManagerImpl());
120:
121: BindingFactoryManager bindingFactoryManager = control
122: .createMock(BindingFactoryManager.class);
123: EasyMock.expect(bus.getExtension(BindingFactoryManager.class))
124: .andStubReturn(bindingFactoryManager);
125: DestinationFactoryManager dfm = control
126: .createMock(DestinationFactoryManager.class);
127: EasyMock.expect(
128: bus.getExtension(DestinationFactoryManager.class))
129: .andStubReturn(dfm);
130:
131: control.replay();
132:
133: assertNotNull(bus.getExtension(WSDLManager.class));
134:
135: WSDLServiceFactory factory = new WSDLServiceFactory(bus,
136: getClass().getResource(wsdl), new QName(portName
137: .getNamespaceURI(), "XMLService"));
138:
139: org.apache.cxf.service.Service service = factory.create();
140:
141: EndpointInfo epi = service.getEndpointInfo(portName);
142: serviceInfo = epi.getService();
143: assertNotNull(epi);
144: Binding xmlBinding = new XMLBindingFactory().createBinding(epi
145: .getBinding());
146:
147: control.reset();
148: JAXBDataBinding db = new JAXBDataBinding();
149: db.initialize(service);
150: db.setContext(JAXBContext.newInstance(jaxbClasses));
151: service.setDataBinding(db);
152:
153: Endpoint endpoint = control.createMock(EndpointImpl.class);
154: EasyMock.expect(endpoint.getEndpointInfo()).andStubReturn(epi);
155: EasyMock.expect(endpoint.getBinding())
156: .andStubReturn(xmlBinding);
157: EasyMock.expect(endpoint.getService()).andStubReturn(service);
158:
159: control.replay();
160:
161: xmlMessage.getExchange().put(Endpoint.class, endpoint);
162: xmlMessage.getExchange().put(
163: org.apache.cxf.service.Service.class, service);
164:
165: }
166: }
|