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.sample;
020:
021: import javax.xml.bind.JAXBContext;
022: import javax.xml.namespace.QName;
023: import javax.xml.ws.BindingProvider;
024: import javax.xml.ws.Dispatch;
025: import javax.xml.ws.Service;
026: import javax.xml.ws.Service.Mode;
027: import javax.xml.ws.soap.SOAPBinding;
028:
029: import junit.framework.TestCase;
030: import org.apache.axis2.jaxws.sample.addressbook.AddEntry;
031: import org.apache.axis2.jaxws.sample.addressbook.AddEntryResponse;
032: import org.apache.axis2.jaxws.sample.addressbook.AddressBook;
033: import org.apache.axis2.jaxws.sample.addressbook.AddressBookEntry;
034: import org.apache.axis2.jaxws.sample.addressbook.ObjectFactory;
035: import org.apache.axis2.jaxws.TestLogger;
036:
037: /**
038: * This tests the AddressBook same service that exists under
039: * org.apache.axis2.jaxws.sample.addressbook.*
040: */
041: public class AddressBookTests extends TestCase {
042:
043: private static final String NAMESPACE = "http://org/apache/axis2/jaxws/sample/addressbook";
044: private static final QName QNAME_SERVICE = new QName(NAMESPACE,
045: "AddressBookService");
046: private static final QName QNAME_PORT = new QName(NAMESPACE,
047: "AddressBook");
048: private static final String URL_ENDPOINT = "http://localhost:8080/axis2/services/AddressBookService";
049:
050: /**
051: * Test the endpoint by invoking it with a JAX-WS Dispatch.
052: */
053:
054: public void testAddressBookWithDispatch() throws Exception {
055: try {
056: TestLogger.logger
057: .debug("----------------------------------");
058: TestLogger.logger.debug("test: " + getName());
059:
060: JAXBContext jbc = JAXBContext
061: .newInstance("org.apache.axis2.jaxws.sample.addressbook");
062:
063: // Create the JAX-WS client needed to send the request
064: Service service = Service.create(QNAME_SERVICE);
065: service.addPort(QNAME_PORT, SOAPBinding.SOAP11HTTP_BINDING,
066: URL_ENDPOINT);
067: Dispatch<Object> dispatch = service.createDispatch(
068: QNAME_PORT, jbc, Mode.PAYLOAD);
069:
070: // Create the JAX-B object that will hold the data
071: ObjectFactory factory = new ObjectFactory();
072: AddEntry request = factory.createAddEntry();
073: AddressBookEntry content = factory.createAddressBookEntry();
074:
075: content.setFirstName("Ron");
076: content.setLastName("Testerson");
077: content.setPhone("512-459-2222");
078:
079: // Since this is a doc/lit wrapped WSDL, we need to set the
080: // data inside of a request wrapper element.
081: request.setEntry(content);
082:
083: AddEntryResponse response = (AddEntryResponse) dispatch
084: .invoke(request);
085:
086: // Validate the results
087: assertNotNull(response);
088: assertTrue(response.isStatus());
089: TestLogger.logger
090: .debug("[pass] - valid response received");
091: TestLogger.logger.debug("[response] - "
092: + response.isStatus());
093: } catch (Exception e) {
094: e.printStackTrace();
095: throw e;
096: }
097: }
098:
099: /**
100: * Test the "addEntry" operation. This sends a complex type and returns
101: * a simple type.
102: */
103: public void testAddEntry() throws Exception {
104: TestLogger.logger.debug("----------------------------------");
105: TestLogger.logger.debug("test: " + getName());
106:
107: // Create the JAX-WS client needed to send the request
108: Service service = Service.create(QNAME_SERVICE);
109: AddressBook ab = service.getPort(QNAME_PORT, AddressBook.class);
110: BindingProvider p1 = (BindingProvider) ab;
111: p1.getRequestContext()
112: .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
113: URL_ENDPOINT);
114:
115: ObjectFactory factory = new ObjectFactory();
116: AddressBookEntry content = factory.createAddressBookEntry();
117: content.setFirstName("Foo");
118: content.setLastName("Bar");
119: content.setPhone("512-459-2222");
120:
121: boolean added = ab.addEntry(content);
122:
123: // Validate the results
124: assertNotNull(added);
125: assertTrue(added);
126: }
127:
128: /**
129: * Test the "findEntryByName" operation. This sends a simple type and
130: * returns a complex type.
131: */
132: public void testFindEntryByName() throws Exception {
133: TestLogger.logger.debug("----------------------------------");
134: TestLogger.logger.debug("test: " + getName());
135:
136: // Create the JAX-WS client needed to send the request
137: Service service = Service.create(QNAME_SERVICE);
138: AddressBook ab = service.getPort(QNAME_PORT, AddressBook.class);
139: BindingProvider p1 = (BindingProvider) ab;
140: p1.getRequestContext()
141: .put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,
142: URL_ENDPOINT);
143:
144: String fname = "Joe";
145: String lname = "Test";
146: AddressBookEntry result = ab.findEntryByName(fname, lname);
147:
148: // Validate the results
149: assertNotNull(result);
150: assertNotNull(result.getFirstName());
151: assertNotNull(result.getLastName());
152: assertTrue(result.getFirstName().equals(fname));
153: assertTrue(result.getLastName().equals(lname));
154: }
155:
156: }
|