TestErrorHandler.py :  » Language-Interface » Pyana » Pyana-0.9.2 » Test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Pyana 
Pyana » Pyana 0.9.2 » Test » TestErrorHandler.py
import StringIO
import os
import Pyana
import unittest
import sys

xsl = r'''
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>
    <xsl:template match="message">
        <xsl:value-of select="text()"/>
    </xsl:template>
</xsl:stylesheet>
'''   

xml = r'''<!DOCTYPE message SYSTEM "test.dtd">
<messages><message>bye bye!</message></messages>foo'''

class ErrorHandler:
    def __init__(self, expected):
        self.expected = expected
               
    def common(self, name, e):
        assert self.expected[0] == (name,) + e.args
        self.expected = self.expected[1:]
        
    def warning(self, e):
        self.common('warning', e)        
    def error(self, e):
        self.common('error', e)
    def fatalError(self, e):
        self.common('fatalError', e)
    def resetErrors(self):
        pass

class HandlerException(Exception):
    pass

class RaisingHandler(ErrorHandler):
    def __init__(self):
        pass
    def common(self, name, e):
        raise HandlerException, (name, e)

class ResetRaisingHandler(RaisingHandler):
    def common(self, name, e):
        pass
    def resetErrors(self):
        raise HandlerException, "resetErrors"
    
class ErrorHandlerTestCase(unittest.TestCase):
    def checkDefaultRemoval(self):
        save = Pyana.defaultErrorHandlerFactory

        del Pyana.defaultErrorHandlerFactory
        self.assertRaises(KeyError, Pyana.Transformer)
        Pyana.defaultErrorHandlerFactory = save
        
    def check(self):
        t = Pyana.Transformer()
        t.useValidation = 1
        errorHandler = ErrorHandler(
            [ # The following warning is generated because Xerces is attempting
              # to load a schema. Whether or not this is a bug is still up in
              # the air (the problem affects testXSLT too).
             ('error', "Unknown element 'messages'", 2, 11, None, 'Python String'),
             ('error', 'Root element different from DOCTYPE', 2, 11, None, 'Python String'),
             ('fatalError', 'Expected comment or processing instruction', 2, 49, None, 'Python String')]
            )
        t.setErrorHandler(errorHandler)
        assert t.transform2String(xml, xsl) == 'bye bye!'
        assert errorHandler.expected == []

        t.setErrorHandler(RaisingHandler())
        self.assertRaises(HandlerException, t.transform2String, xml, xsl)

        t.setErrorHandler(RaisingHandler())
        self.assertRaises(HandlerException, t.transform2String, xml, xsl)
        
def getTestSuites(type):
    return unittest.TestSuite([
            unittest.makeSuite(ErrorHandlerTestCase, type)
                ])
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.