Back to index

plone3  3.1.7
common.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2004 Zope Corporation and Contributors. All Rights Reserved.
00004 #
00005 # This software is subject to the provisions of the Zope Public License,
00006 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
00007 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00008 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00009 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00010 # FOR A PARTICULAR PURPOSE.
00011 #
00012 ##############################################################################
00013 """ GenericSetup product:  unit test utilities.
00014 
00015 $Id: common.py 73617 2007-03-25 20:10:32Z shh $
00016 """
00017 
00018 import os
00019 import shutil
00020 from tarfile import TarFile
00021 
00022 from AccessControl.SecurityManagement import newSecurityManager
00023 from AccessControl.User import UnrestrictedUser
00024 from Testing.ZopeTestCase import ZopeTestCase
00025 from zope.interface import implements
00026 
00027 from Products.GenericSetup.interfaces import IExportContext
00028 from Products.GenericSetup.interfaces import IImportContext
00029 from Products.GenericSetup.testing import DummyLogger
00030 
00031 
00032 class DOMComparator:
00033 
00034     def _compareDOM( self, found_text, expected_text, debug=False ):
00035 
00036         found_lines = [ x.strip() for x in found_text.splitlines() ]
00037         found_text = '\n'.join( filter( None, found_lines ) )
00038 
00039         expected_lines = [ x.strip() for x in expected_text.splitlines() ]
00040         expected_text = '\n'.join( filter( None, expected_lines ) )
00041 
00042         from xml.dom.minidom import parseString
00043         found = parseString( found_text )
00044         expected = parseString( expected_text )
00045         fxml = found.toxml()
00046         exml = expected.toxml()
00047 
00048         if fxml != exml:
00049 
00050             if debug:
00051                 zipped = zip( fxml, exml )
00052                 diff = [ ( i, zipped[i][0], zipped[i][1] )
00053                         for i in range( len( zipped ) )
00054                         if zipped[i][0] != zipped[i][1]
00055                     ]
00056                 import pdb; pdb.set_trace()
00057 
00058             print 'Found:'
00059             print fxml
00060             print
00061             print 'Expected:'
00062             print exml
00063             print
00064 
00065         self.assertEqual( found.toxml(), expected.toxml() )
00066 
00067 
00068 class BaseRegistryTests(ZopeTestCase, DOMComparator):
00069 
00070     def afterSetUp(self):
00071         self.root = self.app
00072         newSecurityManager(None, UnrestrictedUser('god', '', ['Manager'], ''))
00073 
00074     def _makeOne( self, *args, **kw ):
00075         # Derived classes must implement _getTargetClass
00076         return self._getTargetClass()( *args, **kw )
00077 
00078 
00079 def _clearTestDirectory( root_path ):
00080 
00081     if os.path.exists( root_path ):
00082         shutil.rmtree( root_path )
00083 
00084 def _makeTestFile( filename, root_path, contents ):
00085 
00086     path, filename = os.path.split( filename )
00087 
00088     subdir = os.path.join( root_path, path )
00089 
00090     if not os.path.exists( subdir ):
00091         os.makedirs( subdir )
00092 
00093     fqpath = os.path.join( subdir, filename )
00094 
00095     file = open( fqpath, 'wb' )
00096     file.write( contents )
00097     file.close()
00098     return fqpath
00099 
00100 
00101 class FilesystemTestBase(ZopeTestCase):
00102 
00103     def _makeOne(self, *args, **kw):
00104         return self._getTargetClass()(*args, **kw)
00105 
00106     def beforeTearDown(self):
00107         _clearTestDirectory(self._PROFILE_PATH)
00108 
00109     def _makeFile(self, filename, contents):
00110         return _makeTestFile(filename, self._PROFILE_PATH, contents)
00111 
00112 
00113 class TarballTester( DOMComparator ):
00114 
00115     def _verifyTarballContents( self, fileish, toc_list, when=None ):
00116 
00117         fileish.seek( 0L )
00118         tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
00119         items = tarfile.getnames()
00120         items.sort()
00121         toc_list.sort()
00122 
00123         self.assertEqual( len( items ), len( toc_list ) )
00124         for i in range( len( items ) ):
00125             self.assertEqual( items[ i ], toc_list[ i ] )
00126 
00127         if when is not None:
00128             for tarinfo in tarfile:
00129                 self.failIf( tarinfo.mtime < when )
00130 
00131     def _verifyTarballEntry( self, fileish, entry_name, data ):
00132 
00133         fileish.seek( 0L )
00134         tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
00135         extract = tarfile.extractfile( entry_name )
00136         found = extract.read()
00137         self.assertEqual( found, data )
00138 
00139     def _verifyTarballEntryXML( self, fileish, entry_name, data ):
00140 
00141         fileish.seek( 0L )
00142         tarfile = TarFile.open( 'foo.tar.gz', fileobj=fileish, mode='r:gz' )
00143         extract = tarfile.extractfile( entry_name )
00144         found = extract.read()
00145         self._compareDOM( found, data )
00146 
00147 
00148 class DummyExportContext:
00149 
00150     implements(IExportContext)
00151 
00152     def __init__( self, site, tool=None ):
00153         self._site = site
00154         self._tool = tool
00155         self._wrote = []
00156         self._notes = []
00157 
00158     def getSite( self ):
00159         return self._site
00160 
00161     def getSetupTool( self ):
00162         return self._tool
00163 
00164     def getLogger(self, name):
00165         return DummyLogger(name, self._notes)
00166 
00167     def writeDataFile( self, filename, text, content_type, subdir=None ):
00168         if subdir is not None:
00169             filename = '%s/%s' % ( subdir, filename )
00170         self._wrote.append( ( filename, text, content_type ) )
00171 
00172 
00173 class DummyImportContext:
00174 
00175     implements(IImportContext)
00176 
00177     def __init__( self, site, purge=True, encoding=None, tool=None ):
00178         self._site = site
00179         self._tool = tool
00180         self._purge = purge
00181         self._encoding = encoding
00182         self._files = {}
00183         self._notes = []
00184 
00185     def getSite( self ):
00186         return self._site
00187 
00188     def getSetupTool( self ):
00189         return self._tool
00190 
00191     def getEncoding( self ):
00192         return self._encoding
00193 
00194     def getLogger(self, name):
00195         return DummyLogger(name, self._notes)
00196 
00197     def readDataFile( self, filename, subdir=None ):
00198 
00199         if subdir is not None:
00200             filename = '/'.join( (subdir, filename) )
00201 
00202         return self._files.get( filename )
00203 
00204     def shouldPurge( self ):
00205 
00206         return self._purge
00207 
00208 
00209 def dummy_handler( context ):
00210 
00211     pass
00212 
00213 
00214 # BBB: PAS tests use this
00215 class SecurityRequestTest(ZopeTestCase):
00216 
00217     def setUp(self):
00218         ZopeTestCase.setUp(self)
00219         self.root = self.app
00220