"""
$RCSfile: TestDTDValidateMethod.py,v $
Unit tests for ZopeXMLMethods product

Author: Craeg Strong <cstrong@arielpartners.com>

Release: 1.1
"""

__cvstag__  = '$Name:  $'[6:-2]
__date__    = '$Date: 2005/09/06 02:08:59 $'[6:-2]
__version__ = '$Revision: 1.2 $'[10:-2]

import os, sys
if __name__ == '__main__':
    execfile(os.path.join(sys.path[0], 'framework.py'))

# Load fixture
from Testing import ZopeTestCase

from transaction import manager

# ZopeXMLMethods
from Products.ZopeXMLMethods import DTDValidateMethod

ZopeTestCase.installProduct('ZopeXMLMethods')
ZopeTestCase.installProduct('PageTemplates')

from Testing.ZopeTestCase import folder_name, standard_permissions
host, port = ZopeTestCase.utils.startZServer(3)
base = 'http://%s:%d/%s' %(host, port, folder_name)

class DTDValidateMethodTestCase(ZopeTestCase.ZopeTestCase):
    """
    Unit tests for ZopeXMLMethods Zope Product.  These tests use the
    ZopeTestCase enhanced testing product.
    """

    ################################################################
    # Fixture
    ################################################################

    def beforeSetUp(self):
        manager.begin()

    def afterSetUp(self):
        '''Add object to default fixture'''
        pass

    def beforeClose(self):
        # Commit the cleared app
        manager.commit()

    def afterClear(self):
        '''Clean up after myself'''
        pass
    
    ################################################################
    # Test Cases
    ################################################################

    def test_01(self):
        "Ensure that DTDValidateMethod cannot be created with a blank ID"
        self.assertRaises(Exception, DTDValidateMethod.addInstance,
                          self.folder,id='')

    def test_02(self):
        "Ensure that DTDValidateMethod can be created with minimal correct params"

        validator = DTDValidateMethod.addInstance(self.folder,id='aValidator')
        
        self.failUnless(validator, "DTDValidateMethod successfully created should not be None")
        self.assertEquals(validator.id, 'aValidator')

    def test_03(self):
        """Test validation of a valid XML document"""

        filepath        = 'testfiles/valid.xml'
        xmlFile         = open(filepath, 'rb')
        self.folder.manage_addDTMLMethod('aDocument', '', xmlFile)
        
        filepath        = 'testfiles/document.dtd'
        dtdFile         = open(filepath, 'rb')
        self.folder.manage_addDTMLMethod('document.dtd', '', dtdFile)
        
        DTDValidateMethod.addInstance(self.folder,id='aValidator')
        #
        # We must commit the transaction so the ZServer (running in a
        # different thread) can see our new Zope objects when it
        # tries to resolve the relative URL defined in self.folder.properties
        #
        manager.commit()
        
        result = self.folder.aDocument.aValidator.isValid(self.app.REQUEST)
        self.assertEquals(result, 1)
        result = self.folder.aDocument.aValidator.validationResultsString(self.app.REQUEST)
        self.failIf(result.find("is valid") == -1)
        validator = self.folder.aDocument.aValidator
        result = validator(validator, self.app.REQUEST)
        self.failIf(result.find("is valid") == -1)

    ################################################################
    # Test Runner Setup
    ################################################################

if __name__ == '__main__':
    from Products.ZopeXMLMethods.processors.ProcessorRegistry import ProcessorRegistry
    from Products.ZopeXMLMethods.processors.interfaces import IDTDValidator
    
    # Specify which processor to use
    #ProcessorRegistry.CandidateProcessors = ( 'FourSuiteProcessor' )
        
    # report which processor we are using
    print "Using processor:", ProcessorRegistry.defaultName(IDTDValidator)

    framework(descriptions=1, verbosity=2) # 0=quiet 1=default 2=verbose
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(DTDValidateMethodTestCase))
        return suite
