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.service.factory;
019:
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: import javax.wsdl.extensions.soap.SOAPAddress;
025: import javax.xml.bind.JAXBException;
026: import javax.xml.namespace.QName;
027:
028: import org.apache.cxf.binding.soap.model.SoapBindingInfo;
029: import org.apache.cxf.binding.soap.model.SoapOperationInfo;
030: import org.apache.cxf.endpoint.Endpoint;
031: import org.apache.cxf.endpoint.Server;
032: import org.apache.cxf.frontend.ServerFactoryBean;
033: import org.apache.cxf.service.Service;
034: import org.apache.cxf.service.model.BindingInfo;
035: import org.apache.cxf.service.model.BindingOperationInfo;
036: import org.apache.cxf.service.model.EndpointInfo;
037: import org.apache.cxf.service.model.InterfaceInfo;
038: import org.apache.cxf.service.model.MessagePartInfo;
039: import org.apache.cxf.service.model.OperationInfo;
040: import org.apache.cxf.service.model.ServiceInfo;
041: import org.junit.Test;
042:
043: public class ReflectionServiceFactoryTest extends
044: AbstractSimpleFrontendTest {
045: private ReflectionServiceFactoryBean serviceFactory;
046:
047: @Test
048: public void testUnwrappedBuild() throws Exception {
049: Service service = createService(false);
050:
051: ServiceInfo si = service.getServiceInfos().get(0);
052: InterfaceInfo intf = si.getInterface();
053:
054: assertEquals(4, intf.getOperations().size());
055:
056: String ns = si.getName().getNamespaceURI();
057: OperationInfo sayHelloOp = intf.getOperation(new QName(ns,
058: "sayHello"));
059: assertNotNull(sayHelloOp);
060:
061: assertEquals("sayHello", sayHelloOp.getInput().getName()
062: .getLocalPart());
063:
064: List<MessagePartInfo> messageParts = sayHelloOp.getInput()
065: .getMessageParts();
066: assertEquals(0, messageParts.size());
067:
068: // test output
069: messageParts = sayHelloOp.getOutput().getMessageParts();
070: assertEquals(1, messageParts.size());
071: assertEquals("sayHelloResponse", sayHelloOp.getOutput()
072: .getName().getLocalPart());
073:
074: MessagePartInfo mpi = messageParts.get(0);
075: assertEquals("return", mpi.getName().getLocalPart());
076: assertEquals(String.class, mpi.getTypeClass());
077:
078: OperationInfo op = si.getInterface().getOperation(
079: new QName(ns, "echoWithExchange"));
080: assertEquals(1, op.getInput().getMessageParts().size());
081: }
082:
083: @Test
084: public void testWrappedBuild() throws Exception {
085: Service service = createService(true);
086:
087: ServiceInfo si = service.getServiceInfos().get(0);
088: InterfaceInfo intf = si.getInterface();
089:
090: assertEquals(4, intf.getOperations().size());
091:
092: String ns = si.getName().getNamespaceURI();
093: OperationInfo sayHelloOp = intf.getOperation(new QName(ns,
094: "sayHello"));
095: assertNotNull(sayHelloOp);
096:
097: assertEquals("sayHello", sayHelloOp.getInput().getName()
098: .getLocalPart());
099:
100: List<MessagePartInfo> messageParts = sayHelloOp.getInput()
101: .getMessageParts();
102: assertEquals(1, messageParts.size());
103: assertNotNull(messageParts.get(0).getXmlSchema());
104:
105: // test unwrapping
106: assertTrue(sayHelloOp.isUnwrappedCapable());
107:
108: OperationInfo unwrappedOp = sayHelloOp.getUnwrappedOperation();
109: assertEquals("sayHello", unwrappedOp.getInput().getName()
110: .getLocalPart());
111:
112: messageParts = unwrappedOp.getInput().getMessageParts();
113: assertEquals(0, messageParts.size());
114:
115: // test output
116: messageParts = sayHelloOp.getOutput().getMessageParts();
117: assertEquals(1, messageParts.size());
118: assertEquals("sayHelloResponse", sayHelloOp.getOutput()
119: .getName().getLocalPart());
120:
121: messageParts = unwrappedOp.getOutput().getMessageParts();
122: assertEquals("sayHelloResponse", unwrappedOp.getOutput()
123: .getName().getLocalPart());
124: assertEquals(1, messageParts.size());
125: MessagePartInfo mpi = messageParts.get(0);
126: assertEquals("return", mpi.getName().getLocalPart());
127: assertEquals(String.class, mpi.getTypeClass());
128: }
129:
130: private Service createService(boolean wrapped) throws JAXBException {
131: serviceFactory = new ReflectionServiceFactoryBean();
132: serviceFactory.setBus(getBus());
133: serviceFactory.setServiceClass(HelloService.class);
134: serviceFactory.setWrapped(wrapped);
135:
136: Map<String, Object> props = new HashMap<String, Object>();
137: props.put("test", "test");
138: serviceFactory.setProperties(props);
139:
140: return serviceFactory.create();
141: }
142:
143: @Test
144: public void testServerFactoryBean() throws Exception {
145: Service service = createService(true);
146: assertEquals("test", service.get("test"));
147:
148: ServerFactoryBean svrBean = new ServerFactoryBean();
149: svrBean.setAddress("http://localhost/Hello");
150: svrBean.setServiceFactory(serviceFactory);
151: svrBean.setBus(getBus());
152:
153: Map<String, Object> props = new HashMap<String, Object>();
154: props.put("test", "test");
155: serviceFactory.setProperties(props);
156: svrBean.setProperties(props);
157:
158: Server server = svrBean.create();
159: assertNotNull(server);
160: Map<QName, Endpoint> eps = service.getEndpoints();
161: assertEquals(1, eps.size());
162:
163: Endpoint ep = eps.values().iterator().next();
164: EndpointInfo endpointInfo = ep.getEndpointInfo();
165:
166: assertEquals("test", ep.get("test"));
167:
168: SOAPAddress soapAddress = endpointInfo
169: .getExtensor(SOAPAddress.class);
170: assertNotNull(soapAddress);
171:
172: BindingInfo b = endpointInfo.getService().getBindings()
173: .iterator().next();
174:
175: assertTrue(b instanceof SoapBindingInfo);
176:
177: SoapBindingInfo sb = (SoapBindingInfo) b;
178: assertEquals("HelloServiceSoapBinding", b.getName()
179: .getLocalPart());
180: assertEquals("document", sb.getStyle());
181:
182: assertEquals(4, b.getOperations().size());
183:
184: BindingOperationInfo bop = b.getOperations().iterator().next();
185: SoapOperationInfo sop = bop
186: .getExtensor(SoapOperationInfo.class);
187: assertNotNull(sop);
188: assertEquals("", sop.getAction());
189: assertEquals("document", sop.getStyle());
190: }
191: }
|