有没有简单的方法来执行WSDL WS-I合规性的Junit测试

时间:2010-05-26 22:00:27

标签: wsdl

我正在尝试验证生成的WSDL是否正确。我已经尝试了从http://www.ws-i.org/下载的WS-i测试工具,但它的测试工具要求所有输入都通过一个配置xml,输出又是一个输出xml文件。还有其他更简单的验证WSDL的方法吗?

1 个答案:

答案 0 :(得分:0)

Woden库/ jar提供了足够的功能来实现这一目标。如果您的wsdl无效,则最后一个语句reader.readWSDL(...)将抛出异常。

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);        
    }
}

如果您需要WSDL 1.1的单元测试,请参阅以下内容:

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);
    }
}
相关问题