开发者

Is there any easy way to perform Junit test for WSDL WS-I compliance

开发者 https://www.devze.com 2023-01-01 08:30 出处:网络
I am trying to validate generated WSDL to be correct.I have tried WS-i test tool downloaded from h开发者_开发知识库ttp://www.ws-i.org/ but it\'s test tool require all input to go through a config xml

I am trying to validate generated WSDL to be correct. I have tried WS-i test tool downloaded from h开发者_开发知识库ttp://www.ws-i.org/ but it's test tool require all input to go through a config xml and the output is again an output xml file. Is there other easier way of validating a WSDL?


The Woden library/jar provides adequate functionality to be able to do this. If your wsdl isn't valid, the last statement, reader.readWSDL(...), will throw an exception.

import static junit.framework.Assert.fail;

import java.net.URISyntaxException;

import org.apache.woden.WSDLException;
import org.apache.woden.WSDLFactory;
import org.apache.woden.WSDLReader;
import org.apache.woden.wsdl20.Description;
import org.junit.Test;


public class WSDLValidationTest {
    String wsdlFileName =   "/MyService.wsdl";

    @Test
    public void validateWSDL2() throws WSDLException {

        String wsdlUri = null;
        try { 
            wsdlUri = this.getClass().getResource(wsdlFileName).toURI().toString();
        }
        catch( URISyntaxException urise) { 
            urise.printStackTrace();
            fail( "Unable to retrieve wsdl: " + urise.getMessage());
        }

        WSDLFactory factory = WSDLFactory.newInstance("org.apache.woden.internal.OMWSDLFactory");
        WSDLReader reader = factory.newWSDLReader();
        reader.setFeature(WSDLReader.FEATURE_VALIDATION, true);
        reader.readWSDL(wsdlUri);        
    }
}

And should you need a unit test for WSDL 1.1, see the following:

import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.fail;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.factory.WSDLFactory;
import javax.wsdl.xml.WSDLReader;
import javax.xml.stream.XMLStreamException;

import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;

import com.sun.xml.ws.api.model.wsdl.WSDLModel;
import com.sun.xml.ws.api.server.SDDocumentSource;
import com.sun.xml.ws.api.wsdl.parser.WSDLParserExtension;
import com.sun.xml.ws.api.wsdl.parser.XMLEntityResolver;


public class WSDLValidationTest {
    String wsdlFileName =   "/MyService.wsdl";
    String wsdlUri = null;
    URL wsdlUrl = null;

    @Before
    public void before() 
    {
        try { 
            wsdlUrl = this.getClass().getResource(wsdlFileName);
            wsdlUri = wsdlUrl.toURI().toString();
        }
        catch( URISyntaxException urise) { 
            urise.printStackTrace();
            fail( "Unable to retrieve wsdl: " + urise.getMessage());
        }
    }

    @Test
    public void parseAndValidateWSDL1_1WithWSDL4J() throws WSDLException
    {

        WSDLReader wsdlReader = null;
        try { 
            WSDLFactory factory = WSDLFactory.newInstance();
            wsdlReader = factory.newWSDLReader();
        }
        catch( WSDLException wsdle) { 
            wsdle.printStackTrace();
            fail( "Unable to instantiate wsdl reader: " + wsdle.getMessage());
        }

        // Read WSDL service interface document
        Definition def = wsdlReader.readWSDL(null, wsdlUri);
        assertNotNull(def);
    }

    @Test
    public void parseAndValidateWSDL1_1WithJaxWS() throws IOException, XMLStreamException, SAXException 
    {
        final SDDocumentSource doc = SDDocumentSource.create(wsdlUrl);
        final XMLEntityResolver.Parser parser =  new XMLEntityResolver.Parser(doc);
        WSDLModel model = WSDLModel.WSDLParser.parse( parser, null, false, new WSDLParserExtension[] {} );
        assertNotNull(model);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消