"""
$RCSfile: TestXPathProcessor.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.3 $'[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 IXPathProcessor
from Products.ZopeXMLMethods.processors.ProcessorRegistry import ProcessorRegistry

class XPathProcessorTestCase(ZopeTestCase.ZopeTestCase):
    """

    XPath 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'''
        prod               = self.folder.manage_addProduct['ZopeXMLMethods']

        self.registry      = ProcessorRegistry

        # below is an example of how to test only a single processor...
        self.registry.prefer(IXPathProcessor, '4Suite 1.0beta', exclusive = 1)
        # ...for now, let's test them all

        if len(self.registry.names(IXPathProcessor)) == 0:
            raise Exception('Error: no supported XPath Processors found')

    def afterClear(self):
        '''Clean up after myself'''
        try: del self.prod
        except AttributeError: pass

    ################################################################
    # Test Cases
    ################################################################

    def test_01(self):
        "Test evaluation of a simple xpath expression"

        xmlFile         = open('testfiles/simple.xml', 'rb')
        xmlContents     = xmlFile.read()
        xpathExpression = '/document/b/text()'

        print
        for processor in self.registry.items(IXPathProcessor):
            print "Testing Processor:", processor.name,
            expr = processor.compile(xpathExpression)
            result = expr.evaluateString(xmlContents, "", 1)
            self.assertEquals(result.strip(), "Hello, world")
            print "...ok"

    def test_02(self):
        "Test for invalid XPath"

        xmlFile         = open('testfiles/simple.xml', 'rb')
        xmlContents     = xmlFile.read()
        xpathExpression = '?\\iAmInvalid\\'

        print
        for processor in self.registry.items(IXPathProcessor):
            print "Testing Processor:", processor.name,
            self.assertRaises(Exception, processor.compile,
                              xpathExpression)
            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(XPathProcessorTestCase))
        return suite
