Search in sources :

Example 1 with HelloInterface

use of org.apache.cxf.jaxws.service.HelloInterface in project cxf by apache.

the class CodeFirstTest method testClient.

@Test
public void testClient() throws Exception {
    Hello serviceImpl = new Hello();
    try (EndpointImpl ep = new EndpointImpl(getBus(), serviceImpl, (String) null)) {
        ep.publish("local://localhost:9090/hello");
        QName serviceName = new QName("http://service.jaxws.cxf.apache.org/", "HelloService");
        QName portName = new QName("http://service.jaxws.cxf.apache.org/", "HelloPort");
        // need to set the same bus with service , so use the ServiceImpl
        ServiceImpl service = new ServiceImpl(getBus(), (URL) null, serviceName, null);
        service.addPort(portName, "http://schemas.xmlsoap.org/soap/", "local://localhost:9090/hello");
        HelloInterface proxy = service.getPort(portName, HelloInterface.class, new LoggingFeature());
        Client client = ClientProxy.getClient(proxy);
        boolean found = false;
        for (Interceptor<? extends Message> i : client.getOutInterceptors()) {
            if (i instanceof LoggingOutInterceptor) {
                found = true;
            }
        }
        assertTrue(found);
        assertEquals("Get the wrong result", "hello", proxy.sayHi("hello"));
        String[] strInput = new String[2];
        strInput[0] = "Hello";
        strInput[1] = "Bonjour";
        String[] strings = proxy.getStringArray(strInput);
        assertEquals(strings.length, 2);
        assertEquals(strings[0], "HelloHello");
        assertEquals(strings[1], "BonjourBonjour");
        List<String> listInput = new ArrayList<>();
        listInput.add("Hello");
        listInput.add("Bonjour");
        List<String> list = proxy.getStringList(listInput);
        assertEquals(list.size(), 2);
        assertEquals(list.get(0), "HelloHello");
        assertEquals(list.get(1), "BonjourBonjour");
        // now the client side can't unmarshal the complex type without binding types annoutation
        List<String> result = proxy.getGreetings();
        assertEquals(2, result.size());
    }
}
Also used : QName(javax.xml.namespace.QName) ArrayServiceImpl(org.apache.cxf.jaxws.service.ArrayServiceImpl) FooServiceImpl(org.apache.cxf.jaxws.service.FooServiceImpl) ArrayList(java.util.ArrayList) Hello(org.apache.cxf.jaxws.service.Hello) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) LoggingFeature(org.apache.cxf.ext.logging.LoggingFeature) HelloInterface(org.apache.cxf.jaxws.service.HelloInterface) Client(org.apache.cxf.endpoint.Client) Test(org.junit.Test)

Example 2 with HelloInterface

use of org.apache.cxf.jaxws.service.HelloInterface in project cxf by apache.

the class CodeFirstTest method testException.

@Test
public void testException() throws Exception {
    Hello serviceImpl = new Hello();
    try (EndpointImpl ep = new EndpointImpl(getBus(), serviceImpl, (String) null)) {
        ep.publish("local://localhost:9090/hello");
        ep.getServer().getEndpoint().getInInterceptors().add(new LoggingInInterceptor());
        ep.getServer().getEndpoint().getOutInterceptors().add(new LoggingOutInterceptor());
        QName serviceName = new QName("http://service.jaxws.cxf.apache.org/", "HelloService");
        QName portName = new QName("http://service.jaxws.cxf.apache.org/", "HelloPort");
        // need to set the same bus with service , so use the ServiceImpl
        ServiceImpl service = new ServiceImpl(getBus(), (URL) null, serviceName, null);
        service.addPort(portName, "http://schemas.xmlsoap.org/soap/", "local://localhost:9090/hello");
        HelloInterface proxy = service.getPort(portName, HelloInterface.class);
        ClientProxy.getClient(proxy).getInFaultInterceptors().add(new LoggingInInterceptor());
        ClientProxy.getClient(proxy).getInInterceptors().add(new LoggingInInterceptor());
        try {
            proxy.addNumbers(1, -2);
            fail("should throw AddNumbersException");
        } catch (AddNumbersException e) {
            assertEquals(e.getInfo(), "Sum is less than 0.");
        }
        try {
            proxy.addNumbers(1, 99);
            fail("should throw AddNumbersSubException");
        } catch (AddNumbersSubException e) {
            assertEquals(e.getSubInfo(), "Sum is 100");
        } catch (AddNumbersException e) {
            fail("should throw AddNumbersSubException");
        }
        try (AutoCloseable c = (AutoCloseable) proxy) {
            assertEquals("Result = 2", proxy.addNumbers(1, 1));
        }
        try {
            proxy.addNumbers(1, 1);
            fail("Proxy should be closed");
        } catch (IllegalStateException t) {
        // this is expected as the client is closed.
        }
    }
}
Also used : AddNumbersException(org.apache.cxf.jaxws.service.AddNumbersException) AddNumbersSubException(org.apache.cxf.jaxws.service.AddNumbersSubException) Hello(org.apache.cxf.jaxws.service.Hello) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) QName(javax.xml.namespace.QName) ArrayServiceImpl(org.apache.cxf.jaxws.service.ArrayServiceImpl) FooServiceImpl(org.apache.cxf.jaxws.service.FooServiceImpl) HelloInterface(org.apache.cxf.jaxws.service.HelloInterface) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) Test(org.junit.Test)

Aggregations

QName (javax.xml.namespace.QName)2 LoggingOutInterceptor (org.apache.cxf.ext.logging.LoggingOutInterceptor)2 ArrayServiceImpl (org.apache.cxf.jaxws.service.ArrayServiceImpl)2 FooServiceImpl (org.apache.cxf.jaxws.service.FooServiceImpl)2 Hello (org.apache.cxf.jaxws.service.Hello)2 HelloInterface (org.apache.cxf.jaxws.service.HelloInterface)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 Client (org.apache.cxf.endpoint.Client)1 LoggingFeature (org.apache.cxf.ext.logging.LoggingFeature)1 LoggingInInterceptor (org.apache.cxf.ext.logging.LoggingInInterceptor)1 AddNumbersException (org.apache.cxf.jaxws.service.AddNumbersException)1 AddNumbersSubException (org.apache.cxf.jaxws.service.AddNumbersSubException)1