2

I am trying to validate generated WSDL to be correct. I have tried WS-i test tool downloaded from http://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?

wsxedc
  • 123
  • 3
  • 10
  • See http://stackoverflow.com/questions/152023/wsdl-validator and http://stackoverflow.com/questions/1208542/could-you-suggest-an-on-line-wsdl-validation-tool – skaffman May 26 '10 at 22:16

1 Answers1

0

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);
    }
}
Marco
  • 8,958
  • 1
  • 36
  • 56
  • If you have a maven project, you can either put MyService.wsdl in your /src/test/resources or otherwise use to make sure that it's copied out of your /src/main/resources or wherever. – Marco Jan 31 '11 at 14:19
  • Oo.. that test is for WSDL 2.0. See the answer below for WSDL 1.1 – Marco Feb 01 '11 at 09:14