org.pentaho.test.platform.util.XmlHelperTest.java Source code

Java tutorial

Introduction

Here is the source code for org.pentaho.test.platform.util.XmlHelperTest.java

Source

/*
 * This program is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software 
 * Foundation.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this 
 * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html 
 * or from the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * Copyright 2007 - 2008 Pentaho Corporation.  All rights reserved.
 */
package org.pentaho.test.platform.util;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.pentaho.platform.util.JarEntityResolver;
import org.pentaho.platform.util.messages.LocaleHelper;
import org.pentaho.platform.util.xml.XmlHelper;
import org.pentaho.platform.util.xml.dom4j.XmlDom4JHelper;
import org.pentaho.platform.util.xml.w3c.XmlW3CHelper;

@SuppressWarnings({ "all" })
public class XmlHelperTest extends TestCase {

    public void testGetEncoding() {
        // these should succeed, and cause the specified (windows-1252) encoding to be returned
        String[] winXmls = { "<?xml version=\"1.0\" encoding=\"windows-1252\"?><root></root>", //$NON-NLS-1$
                "<?xml encoding=\"windows-1252\" version=\"1.0\"?><root></root>", //$NON-NLS-1$
                "<?xml encoding=\"windows-1252\" version='1.0'?><root></root>", //$NON-NLS-1$
                "<?xml encoding='windows-1252' version=\"1.0\"?><root></root>", //$NON-NLS-1$
                "<?xml encoding='windows-1252' version='1.0'?><root></root>" }; //$NON-NLS-1$

        // these should fail, and cause the default system encoding to be returned
        String[] defaultXmls = { "<?xml encoding='UTF-8' version='1.0'?><root></root>", //$NON-NLS-1$ 
                "<?xml encoding='UTF-8' version='1.0'?><root></root>", //$NON-NLS-1$
                "<?xml encoding='UTF-8' version=\"1.0\"?><root></root>", //$NON-NLS-1$
                "<?xml encoding='UTF-8' version='1.0'?><root>encoding=bad</root>" }; //$NON-NLS-1$ 

        for (String element : winXmls) {
            String enc = XmlHelper.getEncoding(element);
            System.out.println("xml: " + element + " enc: " + enc); //$NON-NLS-1$ //$NON-NLS-2$
            Assert.assertTrue(enc.equals("windows-1252")); //$NON-NLS-1$
        }

        for (String element : defaultXmls) {
            String enc = XmlHelper.getEncoding(element);
            System.out.println("LocaleHelper.getSystemEncoding() =   " + LocaleHelper.getSystemEncoding());
            System.out.println("xml encoding: " + element + " enc: " + enc); //$NON-NLS-1$ //$NON-NLS-2$
            Assert.assertTrue(enc.equals(LocaleHelper.getSystemEncoding()));
        }
    }

    /**
     * Load an XML file into a dom4j.Document, convert that document to a string, load that string
     * into a w3c.Document, and turn it back into a string.
     * 
     * @throws FileNotFoundException
     * @throws TransformerConfigurationException
     * @throws TransformerException
     */
    public void testGetXMLFromDocument()
            throws FileNotFoundException, TransformerConfigurationException, TransformerException {
        try {
            Class resourceClass = this.getClass();
            InputStream in = resourceClass.getResourceAsStream("/test/xml/JFreeQuadrantForRegion.xml");
            org.dom4j.Document doc = XmlDom4JHelper.getDocFromStream(in);

            org.w3c.dom.Document w3cDoc = XmlW3CHelper.getDomFromString(doc.asXML());

            StringBuffer strBuff = XmlDom4JHelper.docToString(w3cDoc);
            System.out.println(strBuff.toString());
            Assert.assertTrue(!StringUtils.isEmpty(strBuff.toString()));
        } catch (Throwable e) {
            System.out.println("Exception thrown " + e.getMessage()); //$NON-NLS-1$
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testEncodeDecode() {
        String emptyString = null;
        Assert.assertNull("decode(null) Should return null", XmlHelper.decode(emptyString)); //$NON-NLS-1$
        Assert.assertNull("encode(null) Should return null", XmlHelper.encode(emptyString)); //$NON-NLS-1$

        String[] emptyArray = null;
        try {
            XmlHelper.decode(emptyArray);
        } catch (Throwable t) {
            Assert.fail("decode with a null array should not throw exception: " + t.getMessage()); //$NON-NLS-1$
        }
        try {
            XmlHelper.encode(emptyArray);
        } catch (Throwable t) {
            Assert.fail("encode with a null array should not throw exception: " + t.getMessage()); //$NON-NLS-1$
        }

        String encodedXml = " ABC 123 abc &amp;amp;=&amp; &amp;lt;=&lt; &amp;gt;=&gt; &amp;apos;=&apos; &amp;quot;=&quot; ABC123abc &amp;quot = &quot; &amp;apos; = &apos; &amp;gt; = &gt; &amp;lt; = &lt; &amp;amp; = &amp; ABC123abc "; //$NON-NLS-1$
        String decodedXml = " ABC 123 abc &amp;=& &lt;=< &gt;=> &apos;=' &quot;=\" ABC123abc &quot = \" &apos; = ' &gt; = > &lt; = < &amp; = & ABC123abc "; //$NON-NLS-1$
        Assert.assertEquals("Error in decode", decodedXml, XmlHelper.decode(encodedXml)); //$NON-NLS-1$
        Assert.assertEquals("Error in encode", encodedXml, XmlHelper.encode(decodedXml)); //$NON-NLS-1$
        Assert.assertEquals("Error encoding after decoding", encodedXml, //$NON-NLS-1$
                XmlHelper.encode(XmlHelper.decode(encodedXml)));
        Assert.assertEquals("Error decoding after encoding", decodedXml, //$NON-NLS-1$
                XmlHelper.decode(XmlHelper.encode(decodedXml)));
    }

    public void testXForm() throws TransformerException {
        try {
            InputStream inStrm = XmlHelperTest.class.getResourceAsStream("/test/xml/XmlHelperTest1.xml"); //$NON-NLS-1$
            String xslName = "CustomReportParametersForPortlet.xsl"; //$NON-NLS-1$
            String xslPath = "system/custom/xsl"; //$NON-NLS-1$

            StringBuffer b = XmlHelper.transformXml(xslName, xslPath, inStrm, null, new JarEntityResolver());
            Assert.assertTrue(!StringUtils.isEmpty(b.toString()));
        } catch (Throwable e) {
            System.out.println("Exception thrown " + e.getMessage()); //$NON-NLS-1$
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testFailureGetDocFromString() {
        try {
            Document doc = XmlDom4JHelper.getDocFromString("1231231231231", null); //$NON-NLS-1$
            Assert.assertTrue("Unexpected XML parsing success", doc == null);
        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), true); //$NON-NLS-1$
        }
    }

    public void testSuccessGetDocFromString() {
        try {
            Document doc = XmlDom4JHelper.getDocFromString(
                    "<root><contents><value>One</value><value>Two</value></contents></root>", null); //$NON-NLS-1$
            Assert.assertTrue("Unexpected XML parsing failure", doc != null);
            System.out.println("Document as String" + doc.getStringValue()); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testGetDocFromString() {
        try {
            Class resourceClass = this.getClass();
            InputStream in = resourceClass.getResourceAsStream("/test/xml/index.xml");
            Document doc = XmlDom4JHelper.getDocFromStream(in);
            System.out.println("Document as String" + doc.getStringValue()); //$NON-NLS-1$      
        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testFailureTransformXML() {
        try {
            String parameterXsl = "DefaultParameterForm.xsl"; //$NON-NLS-1$
            String xslPath = "system/custom/xsl";

            Map parameters = new HashMap();
            String actionUrl = "http://localhost:8080/pentaho/ViewAction?";//$NON-NLS-1$
            String baseUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            String displayUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            parameters.put("baseUrl", baseUrl); //$NON-NLS-1$ 
            parameters.put("actionUrl", actionUrl); //$NON-NLS-1$ 
            parameters.put("displayUrl", displayUrl); //$NON-NLS-1$ 

            //Document document = XmlHelper.getDocFromString("<?xml encoding=\"windows-1252\" version=\"1.0\"?><root></root>"); //$NON-NLS-1$
            Document document = XmlDom4JHelper
                    .getDocFromString("<?xml version=\"1.0\" encoding=\"windows-1252\"?><root></root>", null); //$NON-NLS-1$
            Assert.assertEquals(document.getRootElement().getName(), "root");//$NON-NLS-1$

            StringBuffer content = XmlHelper.transformXml(parameterXsl, xslPath, document.asXML(), parameters,
                    new JarEntityResolver());
            System.out.println("Transformed XML" + content); //$NON-NLS-1$

        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testFailureTransformXML2() {
        try {
            String parameterXsl = "DefaultParameterForm.xsl"; //$NON-NLS-1$
            String xslPath = "system/custom/xsl";

            Map parameters = new HashMap();
            String actionUrl = "http://localhost:8080/pentaho/ViewAction?";//$NON-NLS-1$
            String baseUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            String displayUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            parameters.put("baseUrl", baseUrl); //$NON-NLS-1$ 
            parameters.put("actionUrl", actionUrl); //$NON-NLS-1$ 
            parameters.put("displayUrl", displayUrl); //$NON-NLS-1$ 

            String xmlString = "<?xml version=\"1.0\" encoding=\"" + "UTF-8" //$NON-NLS-1$//$NON-NLS-2$
                    + "\" ?><filters xmlns:xf=\"http://www.w3.org/2002/xforms\">" + //$NON-NLS-1$  
                    "Header" + "<id><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "]]></id><description><![CDATA[" + //$NON-NLS-1$
                    "MyDescription" + "]]></description><icon><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "GetIcon" + "]]></icon><help><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "GetHelp" + "]]></help>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "<action><![CDATA[" + actionUrl + "]]></action>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "<display><![CDATA[" + displayUrl + "]]></display>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "Body" + "</filters>"; //$NON-NLS-1$ //$NON-NLS-2$

            Document document = XmlDom4JHelper.getDocFromString(xmlString, null);

            StringBuffer content = XmlHelper.transformXml(parameterXsl, xslPath, document.asXML(), parameters,
                    new JarEntityResolver());
            System.out.println("Transformed XML" + content); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public void testFailureTransformXML3() {
        try {
            String parameterXsl = "DefaultParameterForm.xsl"; //$NON-NLS-1$
            String xslPath = "system/custom/xsl";

            Map parameters = new HashMap();
            String actionUrl = "http://localhost:8080/pentaho/ViewAction?";//$NON-NLS-1$
            String baseUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            String displayUrl = "http://localhost:8080/pentaho";//$NON-NLS-1$
            parameters.put("baseUrl", baseUrl); //$NON-NLS-1$ 
            parameters.put("actionUrl", actionUrl); //$NON-NLS-1$ 
            parameters.put("displayUrl", displayUrl); //$NON-NLS-1$ 

            Document document = XmlDom4JHelper.getDocFromString("<?xml version=\"1.0\" encoding=\"" + "UTF-8" //$NON-NLS-1$//$NON-NLS-2$
                    + "\" ?><filters xmlns:xf=\"http://www.w3.org/2002/xforms\">" + //$NON-NLS-1$  
                    "Header" + "<id><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "]]></id><description><![CDATA[" + //$NON-NLS-1$
                    "MyDescription" + "]]></description><icon><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "GetIcon" + "]]></icon><help><![CDATA[" + //$NON-NLS-1$ //$NON-NLS-2$
                    "GetHelp" + "]]></help>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "<action><![CDATA[" + actionUrl + "]]></action>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "<display><![CDATA[" + displayUrl + "]]></display>" + //$NON-NLS-1$ //$NON-NLS-2$
                    "Body" + "</filters>", null); //$NON-NLS-1$ //$NON-NLS-2$

            StringBuffer content = XmlHelper.transformXml(parameterXsl, xslPath,
                    new ByteArrayInputStream(document.asXML().getBytes()), parameters, new JarEntityResolver());
            System.out.println("Transformed XML" + content); //$NON-NLS-1$
        } catch (Exception e) {
            e.printStackTrace();
            Assert.assertTrue("Exception thrown " + e.getMessage(), false); //$NON-NLS-1$
        }
    }

    public static void main(final String[] args) {
    }

}