Doorgaan naar hoofdcontent

XSLT Unit Testing

I like test driven development and automatic testing, so also for my XSLT code.
I wanted to do this also within Oracle SOA Suite. Unfortunately this comes not out-of-the-box from the SOA Suite, so i searched the Net.
Eventually i came to XMLUnit 1.2, whcih is an extension of JUnit (version 3.8.2).
This framework contains some extension functions especially for XML, like transformations.

Configuration
First you have to configure XMLUnit to use the Oracle XSLT classes:
System.setProperty("javax.xml.parsers.DocumentBuilderFactory", "oracle.xml.jaxp.JXDocumentBuilderFactory");
System.setProperty("javax.xml.parsers.SAXParserFactory", "oracle.xml.jaxp.JXSAXParserFactory");
System.setProperty("javax.xml.transform.TransformerFactory", "oracle.xml.jaxp.JXSAXTransformerFactory");

Implementing a test case
Extend from XMLTestCase:
public class MyTestCase extends XMLTestCase

Configure namespaces (this can be done within the setup() method:
    protected void setUp() {
        HashMap m = new HashMap();
        m.put("msg", "http://www.example.nl/Message/v1.0");
        m.put("org", "http://www.example.nl/Organisation/v1.0");
        ctx = new SimpleNamespaceContext(m);
        XMLUnit.setXpathNamespaceContext(ctx);

Do a transformation on test data:
    public static Document transform(String input, String xsl) throws Exception{
        System.out.println("Input: " + input);
        System.out.println("Xsl: " + xsl);
        File inputStream = new File(input);
        File myStyleSheet = new File(xsl);
        Transform myTransformation = new Transform(
            new StreamSource(inputStream), new StreamSource(myStyleSheet));
        String result = myTransformation.getResultString();
        Document doc = myTransformation.getResultDocument();
        
        System.out.println(result);
        return doc;
    }

Test the outcome with asserts:
assertXpathEvaluatesTo("000001","/msg:Message/org:Employment/org:ID", doc);
"/msg:CizMessage/msg:Body/org:Employment/org:StartDate", doc);

Put into JUnit testsuite (Example AllTests class):
    public static TestSuite suite( ) {
        TestSuite suite= new TestSuite("All JUnit Tests");
        suite.addTestSuite(unittest.MyTestCase.class);
        return suite;
    }
    TestRunner.run(tests.suite());

Be sure to include the jars (JDeveloper):
%ORACLE_SOA%\integration\esb\lib\bpm-services.jar
%ORACLE_SOA%\integration\esb\lib\commons-logging.jar
%ORACLE_SOA%\integration\esb\lib\orabpel.jar
%ORACLE_SOA%\integration\esb\lib\xmlparserv2.jar
%ORACLE_SOA%\integration\esb\lib\xsu12.jar
%JUNIT%\junit.jar
%XMLUnit%\lib\xmlunit-1.2.jar

You can also run the tests within Ant (I used JDeveloper 10.1.3.4 included Ant)
Build.xml file:

Waarbij ${testsuite.class}=AllTests

Reacties

Populaire posts van deze blog

Microservices mindmap

"The tree" - See also   my photo page When you are fairly new within the Microservices land, there are a lot of terms fired at you. So also for my own understanding i have made a mindmap. I think it has a good status now, so that i can share it with you. As always feedback is very welcome ! You can download the mindmap here .

OSB 10gR3 and SWA and MTOM

This blog is about using soap with attachments and the use of MTOM within the OSB (10gR3). A service is created that accepts a soap with attachment (DocumentService) and translates it to a service that accepts a binary element. MTOM is used for performance reasons for the second. Some notes: * For the use of attachments you need RPC-style document instead of the usual document-style. This due to the fact that the document-style limits a message to a single . * A service can not have both SWA and MTOM within OSB. First a WSDL is setup for the DocumentService: The $attachments variable holds the attachments and the body holds the attachment data. Also other data is stored within the attachment element (see h...

Cloud to Cloud Application Integration

A lot of applications have integration possibilities, so do cloud applications. The question I got from a customer is whether to have a point-to-point integration with Cloud applications or to go through their ESB solution. This blog describes some considerations. Context The customer has a HRM application in which job vacancies are managed. Furthermore that system also handles the full applicant process flow. They also have another cloud application that handles the job vacancies. This application posts the jobs to social sites and other channels to promote the vacancies. Furthermore this application has some intelligence for job seekers to advice some new vacancies based on previous visits or profiles. The job vacancies need to be sent to the Vacancies application and applicant information needs to be sent to the HRM application, when a job seeker actually applies for a job. Furthermore status information about the job application is als...