"""
$RCSfile: TestXSLTProcessor.py,v $
Unit tests for ZopeXMLMethods product

Author: <a href="mailto:cstrong@arielpartners.com">Craeg Strong</a>
Release: 1.0
"""

__cvstag__  = '$Name:  $'[6:-2]
__date__    = '$Date: 2005/09/06 02:08:59 $'[6:-2]
__version__ = '$Revision: 1.6 $'[10:-2]

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# Load fixture
from Testing import ZopeTestCase

ZopeTestCase.installProduct('ZopeXMLMethods')

# ZopeXMLMethods
from Products.ZopeXMLMethods.processors.interfaces import IXSLTProcessor
from Products.ZopeXMLMethods.processors.ProcessorRegistry import ProcessorRegistry

class XSLTProcessorTestCase(ZopeTestCase.ZopeTestCase):
    """

    XSLT Processor tests.  This is particularly useful if switching
    from one library to another, for example upgrading 4Suite or
    switching to Pyana or Saxon.

    """

    ################################################################
    # Fixture
    ################################################################

    def afterSetUp(self):
        '''Add object to default fixture'''
        self.registry      = ProcessorRegistry

        # below is an example of how to test only a single processor...
        self.registry.prefer(IXSLTProcessor, '4Suite 1.0beta', exclusive = 1)
        # ...for now, let's test them all
        
        if len(self.registry.names(IXSLTProcessor)) == 0:
            raise Exception('Error: no supported XSLT Processors found')

    ################################################################
    # Test Cases
    ################################################################

    def test_01(self):
        "Test simple XSLT transformation"

        xmlFile      = open('testfiles/simple.xml', 'rb')
        xmlContents  = xmlFile.read()
        xsltFile     = open('testfiles/simple.xsl','rb')
        xsltContents = xsltFile.read()

        print
        for processor in self.registry.items(IXSLTProcessor):
            print "Testing processor:", processor.name,
            result = processor.transform(xmlContents, "", xsltContents,
                                         "", None, {}, None)
            self.assertEquals(result, "Hello, world")
            print "...ok"

    def test_02(self):
        "Non well-formed XML source should raise an appropriate exception"

        xmlFile      = open('testfiles/notwellformed.xml', 'rb')
        xmlContents  = xmlFile.read()
        xsltFile     = open('testfiles/simple.xsl','rb')
        xsltContents = xsltFile.read()

        print
        for processor in self.registry.items(IXSLTProcessor):
            print "Testing processor:", processor.name,
            self.assertRaises(Exception, processor.transform,
                              xmlContents, "", xsltContents,
                              "", None, {}, None)
            print "...ok"            

    def test_03(self):
        "Non well-formed XSLT should raise an appropriate exception"

        xmlFile      = open('testfiles/simple.xml', 'rb')
        xmlContents  = xmlFile.read()
        xsltFile     = open('testfiles/notwellformed.xsl','rb')
        xsltContents = xsltFile.read()

        print
        for processor in self.registry.items(IXSLTProcessor):
            print "Testing processor:", processor.name,
            self.assertRaises(Exception, processor.transform,
                              xmlContents, "", xsltContents,
                              "", None, {}, None)
            print "...ok"                        

    def test_04(self):
        "Test that the processor handles parameters correctly"

        xmlFile      = open('testfiles/param.xml', 'rb')
        xmlContents  = xmlFile.read()
        xsltFile     = open('testfiles/param.xsl','rb')
        xsltContents = xsltFile.read()

        print
        for processor in self.registry.items(IXSLTProcessor):
            print "Testing processor:", processor.name,
            params = {'who': 'world', 'message':'goodbye'}
            result = processor.transform(xmlContents, "", xsltContents,
                                         "", None, params, None)

            self.assertEquals(result, "Hello, world and goodbye")
            print "...ok"

    ################################################################
    # Test Runner Setup
    ################################################################

if __name__ == '__main__':
    framework(descriptions=1, verbosity=2)
else:
    # While framework.py provides its own test_suite() 
    # method the testrunner utility does not.
    import unittest
    def test_suite():
        suite = unittest.TestSuite()
        suite.addTest(unittest.makeSuite(XSLTProcessorTestCase))
        return suite
