URL wsdlUrl = ClientMetadataTest.getWsdlURL(multiPortWsdl);
QName portQN1 = new QName(namespaceURI, multiPortWsdl_portLocalPart1);
Service service1 = Service.create(wsdlUrl, serviceQName);
ServiceDelegate serviceDelegate1 = DescriptionTestUtils2.getServiceDelegate(service1);
ServiceDescription svcDesc1 = serviceDelegate1.getServiceDescription();
ClientMetadataPortSEI port1 = service1.getPort(ClientMetadataPortSEI.class);
assertNotNull(port1);
// Get the endpoint address to verify which port we got. Note that the WSDL is setup
// so that the endpoint address ends with the name of the port for testing.
BindingProvider bindingProvider1 = (BindingProvider) port1;
Map<String, Object> requestContext1 = bindingProvider1.getRequestContext();
String endpointAddress1 = (String) requestContext1.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
assertNotNull(endpointAddress1);
// FIXME: We should get the first port in the WSDL, but that isn't working
// Depending on the JDK in use, the 2nd or 3rd port is returned
// assertTrue(endpointAddress.endsWith(multiPortWsdl_portLocalPart1));
// Set a prefered port and create the service
QName portQN2 = new QName(namespaceURI, multiPortWsdl_portLocalPart2);
DescriptionBuilderComposite sparseComposite2 = new DescriptionBuilderComposite();
sparseComposite2.setPreferredPort(portQN2);
ServiceDelegate.setServiceMetadata(sparseComposite2);
Service service2 = Service.create(wsdlUrl, serviceQName);
ServiceDelegate serviceDelegate2 = DescriptionTestUtils2.getServiceDelegate(service2);
ServiceDescription svcDesc2 = serviceDelegate2.getServiceDescription();
assertNotSame(service1, service2);
assertNotSame(serviceDelegate1, serviceDelegate2);
assertSame(svcDesc1, svcDesc2);
ClientMetadataPortSEI port2 = service2.getPort(ClientMetadataPortSEI.class);
BindingProvider bindingProvider2 = (BindingProvider) port2;
Map<String, Object> requestContext2 = bindingProvider2.getRequestContext();
String endpointAddress2 = (String) requestContext2.get(BindingProvider.ENDPOINT_ADDRESS_PROPERTY);
assertNotNull(endpointAddress2);
assertTrue(endpointAddress2.endsWith(multiPortWsdl_portLocalPart2));
// Create a third service without a composite and make sure the previous composite
// setting of preferred port doesn't affect this one.
Service service3 = Service.create(wsdlUrl, serviceQName);
ServiceDelegate serviceDelegate3 = DescriptionTestUtils2.getServiceDelegate(service3);
ServiceDescription svcDesc3 = serviceDelegate3.getServiceDescription();
assertNotSame(service2, service3);
assertNotSame(serviceDelegate1, serviceDelegate3);
assertNotSame(serviceDelegate2, serviceDelegate3);
assertSame(svcDesc1, svcDesc3);