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: */
019: package org.apache.axis2.jaxws.description;
020:
021: import java.lang.reflect.Proxy;
022: import java.net.URL;
023: import java.util.List;
024: import java.util.Map;
025:
026: import javax.wsdl.Port;
027: import javax.xml.namespace.QName;
028: import javax.xml.ws.Service;
029: import javax.xml.ws.WebServiceException;
030:
031: import junit.framework.TestCase;
032: import org.apache.axis2.jaxws.sample.addnumbers.AddNumbersPortType;
033: import org.apache.axis2.jaxws.spi.BindingProvider;
034: import org.apache.axis2.jaxws.spi.ServiceDelegate;
035: import org.apache.ws.axis2.tests.EchoPort;
036:
037: /**
038: *
039: */
040: public class PortSelectionTests extends TestCase {
041: private static String VALID_SERVICE_NAMESPACE = "http://org/test/addnumbers";
042: private static String VALID_SERVICE_LOCALPART_3 = "AddNumbersService3";
043:
044: public void testServiceDescPortSelectionMethods() {
045: URL wsdlURL = DescriptionTestUtils2
046: .getWSDLURL("WSDLMultiTests.wsdl");
047:
048: QName serviceQN = new QName(VALID_SERVICE_NAMESPACE,
049: VALID_SERVICE_LOCALPART_3);
050: Service service = Service.create(wsdlURL, serviceQN);
051: assertNotNull(service);
052: ServiceDelegate serviceDelegate = DescriptionTestUtils2
053: .getServiceDelegate(service);
054: assertNotNull(serviceDelegate);
055: ServiceDescription serviceDesc = serviceDelegate
056: .getServiceDescription();
057: assertNotNull(serviceDesc);
058:
059: ServiceDescriptionWSDL serviceDescWSDL = (ServiceDescriptionWSDL) serviceDesc;
060:
061: Map allPorts = serviceDescWSDL.getWSDLPorts();
062: assertEquals(4, allPorts.size());
063:
064: QName portTypeQName = new QName(VALID_SERVICE_NAMESPACE,
065: "AddNumbersPortType");
066: List<Port> portsUsingPortType = serviceDescWSDL
067: .getWSDLPortsUsingPortType(portTypeQName);
068: assertEquals(3, portsUsingPortType.size());
069:
070: QName otherPortTypeQName = new QName(VALID_SERVICE_NAMESPACE,
071: "AddNumbersPortTypeOtherPT");
072: List<Port> portsUsingOtherPortType = serviceDescWSDL
073: .getWSDLPortsUsingPortType(otherPortTypeQName);
074: assertEquals(1, portsUsingOtherPortType.size());
075:
076: List<Port> portsUsingSOAPAddress = serviceDescWSDL
077: .getWSDLPortsUsingSOAPAddress(portsUsingPortType);
078: assertEquals(2, portsUsingSOAPAddress.size());
079: }
080:
081: public void testPortSelection() {
082: // Test the Service.getPort call
083: URL wsdlURL = DescriptionTestUtils2
084: .getWSDLURL("WSDLMultiTests.wsdl");
085:
086: QName serviceQN = new QName(VALID_SERVICE_NAMESPACE,
087: VALID_SERVICE_LOCALPART_3);
088: Service service = Service.create(wsdlURL, serviceQN);
089: assertNotNull(service);
090: AddNumbersPortType selectPort = service
091: .getPort(AddNumbersPortType.class);
092: BindingProvider bindingProvider = (BindingProvider) Proxy
093: .getInvocationHandler(selectPort);
094: EndpointDescription endpointDesc = bindingProvider
095: .getEndpointDescription();
096: QName selectedPortQName = endpointDesc.getPortQName();
097: assertNotNull(selectedPortQName);
098: // We expect the first port in the WSDL which uses the PortType for the SEI AddNumbersPortType to be selected
099: // UNFORTUNATELY! WSDL4J Service.getPorts(), which returns a Map of ports under the service DOES NOT return
100: // them in the order defined in the WSDL. Therefore, we can't necessarily predict which one we'll get back.
101: // So, the following two lines may cause the test to fail.
102: // QName expectedQName = new QName(VALID_SERVICE_NAMESPACE, "AddNumbersPortS3P2");
103: // assertEquals(expectedQName, selectedPortQName);
104: // So, the best we can do is just test that ONE of the valid ports is the one that was selected.
105: boolean testSuccessful = false;
106: QName[] validPorts = new QName[3];
107: validPorts[0] = new QName(VALID_SERVICE_NAMESPACE,
108: "AddNumbersPortS3P2");
109: validPorts[1] = new QName(VALID_SERVICE_NAMESPACE,
110: "AddNumbersPortS3P3");
111: validPorts[2] = new QName(VALID_SERVICE_NAMESPACE,
112: "AddNumbersPortS3P4");
113: for (QName checkPort : validPorts) {
114: if (selectedPortQName.equals(checkPort)) {
115: testSuccessful = true;
116: break;
117: }
118: }
119: assertTrue(testSuccessful);
120: }
121:
122: public void testInvalidPortSelection() {
123: URL wsdlURL = DescriptionTestUtils2
124: .getWSDLURL("WSDLMultiTests.wsdl");
125:
126: QName serviceQN = new QName(VALID_SERVICE_NAMESPACE,
127: VALID_SERVICE_LOCALPART_3);
128: Service service = Service.create(wsdlURL, serviceQN);
129: assertNotNull(service);
130: // There should be no ports in the service that use this SEI!
131: try {
132: EchoPort selectPort = service.getPort(EchoPort.class);
133: fail("Shouldn't have found a port for the given SEI!");
134: } catch (WebServiceException ex) {
135: // Expected code path
136: } catch (Exception ex) {
137: fail("Unexpected exception " + ex.toString());
138: }
139:
140: }
141:
142: }
|