org.jboss.as.jbossws.jbpapp10625.DuplicateServicesGeneratedWsdlIT.java Source code

Java tutorial

Introduction

Here is the source code for org.jboss.as.jbossws.jbpapp10625.DuplicateServicesGeneratedWsdlIT.java

Source

package org.jboss.as.jbossws.jbpapp10625;

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import static org.junit.Assert.*;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import org.apache.commons.io.IOUtils;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.tests.test1.webservices.HelloWorld;
import org.jboss.tests.test1.webservices.HelloWorldInterface;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
@RunAsClient
public class DuplicateServicesGeneratedWsdlIT {
    private static final String name = "DuplicateServices";

    private static final String URL1 = "http://" + getServerBindAddress() + ":" + getServerBindPort() + "/" + name
            + "/" + "HelloWorld";

    private static final String URL2 = "http://" + getServerBindAddress() + ":" + getServerBindPort() + "/" + name
            + "/" + "V2/HelloWorld";

    static String getServerBindAddress() {
        String serverBindAddress = System.getProperty("jboss.bind.address");
        return serverBindAddress == null || serverBindAddress.isEmpty() ? "localhost" : serverBindAddress;
    }

    static String getServerBindPort() {
        String serverBindport = System.getProperty("jboss.bind.port");
        return serverBindport == null || serverBindport.isEmpty() ? "8080" : serverBindport;
    }

    @Deployment
    static WebArchive createDeployment() throws Exception {

        WebArchive archive = ShrinkWrap.create(WebArchive.class, name + ".war").addClass(HelloWorld.class)
                .addClass(HelloWorldInterface.class).addClass(org.jboss.tests.test1.v2.webservices.HelloWorld.class)
                .addClass(org.jboss.tests.test1.v2.webservices.HelloWorldInterface.class)
                .addAsWebInfResource(new File("src/test/contexts/test1/webapp/WEB-INF/web.xml"));
        ;
        archive.as(ZipExporter.class).exportTo(new File("target", archive.getName()), true);
        return archive;
    }

    @Test
    public void testWsdlSchemaIsAvailable() throws Exception {
        System.err.println("Test " + getClass().getName());
        assertWsdlIsAvailable(createWsdlUrl(URL1));
        assertWsdlIsAvailable(createWsdlUrl(URL2));
    }

    private void assertWsdlIsAvailable(URL wsdlUrl) throws UnsupportedEncodingException, IOException {
        String wsdl = readFromUrlToString(wsdlUrl, "utf-8");
        System.err.println(wsdl);
    }

    @Test
    public void testWebServiceIsFunctional() throws Exception {
        HelloWorldInterface proxy1 = createServiceProxy(URL1, HelloWorldInterface.class,
                new QName("http://org.jboss.examples/HelloWorld", "HelloWorld"));
        assertEquals("Hello " + name, proxy1.sayHello(name));
        org.jboss.tests.test1.v2.webservices.HelloWorldInterface proxy2 = createServiceProxy(URL2,
                org.jboss.tests.test1.v2.webservices.HelloWorldInterface.class,
                new QName("http://org.jboss.examples.v2/HelloWorld", "HelloWorld"));
        assertEquals("Hello V2 " + name, proxy2.sayHello(name));

    }

    protected <T> T createServiceProxy(String serviceURL, Class<T> serviceProxyClass, QName serviceName)
            throws MalformedURLException {
        Service service = Service.create(createWsdlUrl(serviceURL), serviceName);
        return service.getPort(serviceProxyClass);
    }

    private URL createWsdlUrl(String serviceURL) throws MalformedURLException {
        URL wsdlURL = new URL(serviceURL + "?wsdl");
        return wsdlURL;
    }

    public static String readFromUrlToString(final URL url, String encoding)
            throws UnsupportedEncodingException, IOException {
        InputStream stream = null;
        InputStreamReader inputStream = null;
        URLConnection connection = null;
        try {
            connection = url.openConnection();
            stream = connection.getInputStream();
            inputStream = new InputStreamReader(stream, encoding);
            return IOUtils.toString(inputStream);
        } catch (ConnectException e) {
            throw new IllegalStateException("Can not read from " + url.toString() + " " + e.getMessage(), e);
        } catch (IOException e) {
            throw new IllegalStateException("Error reading from " + url.toString() + " " + e.getMessage(), e);
        } finally {
            IOUtils.close(connection); //has to be closed first, otherwise the connection is pooled and reused !!!!
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(stream);
        }
    }

}