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.systest.factory_pattern;
019:
020: import java.lang.reflect.InvocationHandler;
021: import java.lang.reflect.Proxy;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.ws.BindingProvider;
025: import javax.xml.ws.Endpoint;
026: import javax.xml.ws.Service;
027:
028: import org.apache.cxf.binding.soap.SoapBindingFactory;
029: import org.apache.cxf.factory_pattern.IsEvenResponse;
030: import org.apache.cxf.factory_pattern.Number;
031: import org.apache.cxf.factory_pattern.NumberFactory;
032: import org.apache.cxf.factory_pattern.NumberFactoryService;
033: import org.apache.cxf.factory_pattern.NumberService;
034: import org.apache.cxf.jaxws.ServiceImpl;
035: import org.apache.cxf.jaxws.support.ServiceDelegateAccessor;
036: import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
037: import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
038: import org.apache.cxf.ws.addressing.EndpointReferenceType;
039: import org.apache.cxf.wsdl.EndpointReferenceUtils;
040: import org.junit.BeforeClass;
041: import org.junit.Test;
042:
043: public class ManualHttpMulitplexClientServerTest extends
044: AbstractBusClientServerTestBase {
045:
046: public static class Server extends AbstractBusTestServerBase {
047:
048: protected void run() {
049: Object implementor = new ManualNumberFactoryImpl();
050: Endpoint.publish(NumberFactoryImpl.FACTORY_ADDRESS,
051: implementor);
052: }
053:
054: public static void main(String[] args) {
055: try {
056: Server s = new Server();
057: s.start();
058: } catch (Exception ex) {
059: ex.printStackTrace();
060: System.exit(-1);
061: } finally {
062: System.out.println("done!");
063: }
064: }
065: }
066:
067: @BeforeClass
068: public static void startServers() throws Exception {
069: assertTrue("server did not launch correctly",
070: launchServer(Server.class));
071: }
072:
073: @Test
074: public void testWithManualMultiplexEprCreation() throws Exception {
075:
076: NumberFactoryService service = new NumberFactoryService();
077: NumberFactory nfact = service.getNumberFactoryPort();
078:
079: EndpointReferenceType epr = nfact.create("2");
080: assertNotNull("reference", epr);
081:
082: // use the epr info only
083: // no wsdl so default generated soap/http binding will be used
084: // address url must come from the calling context
085: QName serviceName = EndpointReferenceUtils.getServiceName(epr);
086: Service numService = Service.create(serviceName);
087:
088: String portString = EndpointReferenceUtils.getPortName(epr);
089: QName portName = new QName(serviceName.getNamespaceURI(),
090: portString);
091: numService.addPort(portName,
092: SoapBindingFactory.SOAP_11_BINDING, "http://foo");
093: Number num = (Number) numService
094: .getPort(portName, Number.class);
095:
096: setupContextWithEprAddress(epr, num);
097:
098: IsEvenResponse numResp = num.isEven();
099: assertTrue("2 is even", Boolean.TRUE.equals(numResp.isEven()));
100:
101: // try again with the address from another epr
102: epr = nfact.create("3");
103: setupContextWithEprAddress(epr, num);
104: numResp = num.isEven();
105: assertTrue("3 is not even", Boolean.FALSE.equals(numResp
106: .isEven()));
107:
108: // try again with the address from another epr
109: epr = nfact.create("6");
110: setupContextWithEprAddress(epr, num);
111: numResp = num.isEven();
112: assertTrue("6 is even", Boolean.TRUE.equals(numResp.isEven()));
113: }
114:
115: @Test
116: public void testWithGetPortExtensionHttp() throws Exception {
117:
118: NumberFactoryService service = new NumberFactoryService();
119: NumberFactory factory = service.getNumberFactoryPort();
120: EndpointReferenceType numberTwoRef = factory.create("20");
121: assertNotNull("reference", numberTwoRef);
122:
123: // use getPort with epr api on service
124: NumberService numService = new NumberService();
125: ServiceImpl serviceImpl = ServiceDelegateAccessor
126: .get(numService);
127:
128: Number num = (Number) serviceImpl.getPort(numberTwoRef,
129: Number.class);
130: assertTrue("20 is even", num.isEven().isEven());
131:
132: EndpointReferenceType numberTwentyThreeRef = factory
133: .create("23");
134: num = (Number) serviceImpl.getPort(numberTwentyThreeRef,
135: Number.class);
136: assertTrue("23 is not even", !num.isEven().isEven());
137: }
138:
139: private void setupContextWithEprAddress(EndpointReferenceType epr,
140: Number num) {
141:
142: String address = EndpointReferenceUtils.getAddress(epr);
143:
144: InvocationHandler handler = Proxy.getInvocationHandler(num);
145: BindingProvider bp = null;
146: if (handler instanceof BindingProvider) {
147: bp = (BindingProvider) handler;
148: bp.getRequestContext().put(
149: BindingProvider.ENDPOINT_ADDRESS_PROPERTY, address);
150: }
151: }
152: }
|