Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package jboss.ws;
  2.  
  3. import static org.junit.Assert.assertEquals;
  4.  
  5. import java.io.File;
  6. import java.io.FileInputStream;
  7. import java.util.concurrent.Future;
  8.  
  9. import javax.xml.soap.MessageFactory;
  10. import javax.xml.soap.SOAPMessage;
  11. import javax.xml.ws.AsyncHandler;
  12. import javax.xml.ws.Dispatch;
  13. import javax.xml.ws.Response;
  14. import javax.xml.ws.Service;
  15.  
  16. import jboss.ws.hello.client.Hello;
  17. import jboss.ws.hello.client.HelloService;
  18. import jboss.ws.hello.client.Person;
  19.  
  20. import org.apache.cxf.endpoint.Client;
  21. import org.apache.cxf.frontend.ClientProxy;
  22. import org.apache.cxf.interceptor.LoggingInInterceptor;
  23. import org.apache.cxf.interceptor.LoggingOutInterceptor;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26.  
  27. public class HelloWorldTest {
  28.     private Client client = null;;
  29.     private Hello port = null;
  30.     private HelloService service = null;
  31.  
  32.     @Before
  33.     public void setUp() {
  34.         service = new HelloService();
  35.         port = service.getHelloPort();
  36.         client = ClientProxy.getClient(port);
  37.         client.getInInterceptors().add(new LoggingInInterceptor());
  38.         client.getOutInterceptors().add(new LoggingOutInterceptor());
  39.     }
  40.  
  41.     @Test
  42.     public void testHelloWSWithDispatchAPI() throws Exception {
  43.         FileInputStream input = new FileInputStream(new File(
  44.                 "src/test/resources/outbound.xml"));
  45.         MessageFactory factory = MessageFactory.newInstance();
  46.         SOAPMessage request = factory.createMessage(null, input);
  47.         Dispatch<SOAPMessage> dispatch = service
  48.                 .createDispatch(HelloService.HelloPort, SOAPMessage.class,
  49.                         Service.Mode.MESSAGE);
  50.        
  51.         // Synchronous, Message-Oriented.
  52.         SOAPMessage msg = dispatch.invoke(request);
  53.         String result = msg.getSOAPBody().getTextContent();
  54.         String expected = "Hello David . F by JBoss 6 WS with CXF implementation.";
  55.         assertEquals(expected, result);
  56.  
  57.         // Asynchronous, Polling, Message-Oriented
  58.         Response<SOAPMessage> response = dispatch.invokeAsync(request);
  59.         while (!response.isDone()) {
  60.             System.out
  61.                     .println("waiting for the response from hello web service......");
  62.             Thread.sleep(1000);
  63.         }
  64.         msg = response.get();
  65.         result = msg.getSOAPBody().getTextContent();
  66.         assertEquals(expected, result);
  67.  
  68.         // Asynchronous , Callback, Message-Oriented
  69.    
  70.         Future<?> future = dispatch.invokeAsync(request,
  71.                 new AsyncHandler<SOAPMessage>() {
  72.                     public void handleResponse(Response<SOAPMessage> response) {
  73.                         try {
  74.                             assertEquals(
  75.                                     "Hello David . F by JBoss 6 WS with CXF implementation.",
  76.                                     response.get().getSOAPBody().getTextContent());
  77.                         } catch (Exception e) {
  78.  
  79.                         }
  80.                     }
  81.                 });
  82.     }
  83.  
  84.     @Test
  85.     public void testSayHello() throws Exception {
  86.  
  87.         Person person = new Person();
  88.         person.setFirstName("David");
  89.         person.setLastName("F");
  90.         Object[] returnedObjects = client.invoke("sayHello",
  91.                 new Object[] { person });
  92.         String expected = "Hello David . F by JBoss 6 WS with CXF implementation.";
  93.         String result = (String) returnedObjects[0];
  94.         assertEquals(expected, result);
  95.     }
  96.  
  97. }