Back to index

plone3  3.1.7
test_context.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 """ Unit tests for import / export contexts.
00014 
00015 $Id: test_context.py 71241 2006-11-21 13:15:05Z yuppie $
00016 """
00017 
00018 import unittest
00019 from Testing.ZopeTestCase import ZopeTestCase
00020 
00021 import logging
00022 import os
00023 import time
00024 from StringIO import StringIO
00025 from tarfile import TarFile
00026 from tarfile import TarInfo
00027 
00028 from DateTime.DateTime import DateTime
00029 from OFS.Folder import Folder
00030 from OFS.Image import File
00031 
00032 from common import FilesystemTestBase
00033 from common import TarballTester
00034 from conformance import ConformsToISetupContext
00035 from conformance import ConformsToIImportContext
00036 from conformance import ConformsToIExportContext
00037 
00038 
00039 class DummySite( Folder ):
00040 
00041     pass
00042 
00043 class DummyTool( Folder ):
00044 
00045     pass
00046 
00047 
00048 class DirectoryImportContextTests( FilesystemTestBase
00049                                  , ConformsToISetupContext
00050                                  , ConformsToIImportContext
00051                                  ):
00052 
00053     _PROFILE_PATH = '/tmp/ICTTexts'
00054 
00055     def _getTargetClass( self ):
00056 
00057         from Products.GenericSetup.context import DirectoryImportContext
00058         return DirectoryImportContext
00059 
00060     def test_getLogger( self ):
00061 
00062         site = DummySite('site').__of__(self.app)
00063         ctx = self._makeOne( site, self._PROFILE_PATH )
00064         self.assertEqual( len( ctx.listNotes() ), 0 )
00065 
00066         logger = ctx.getLogger('foo')
00067         logger.info('bar')
00068 
00069         self.assertEqual( len( ctx.listNotes() ), 1 )
00070         level, component, message = ctx.listNotes()[0]
00071         self.assertEqual( level, logging.INFO )
00072         self.assertEqual( component, 'foo' )
00073         self.assertEqual( message, 'bar' )
00074 
00075         ctx.clearNotes()
00076         self.assertEqual( len( ctx.listNotes() ), 0 )
00077 
00078     def test_readDataFile_nonesuch( self ):
00079 
00080         FILENAME = 'nonesuch.txt'
00081 
00082         site = DummySite('site').__of__(self.app)
00083         ctx = self._makeOne( site, self._PROFILE_PATH )
00084 
00085         self.assertEqual( ctx.readDataFile( FILENAME ), None )
00086 
00087     def test_readDataFile_simple( self ):
00088 
00089         from string import printable
00090 
00091         FILENAME = 'simple.txt'
00092         self._makeFile( FILENAME, printable )
00093 
00094         site = DummySite('site').__of__(self.app)
00095         ctx = self._makeOne( site, self._PROFILE_PATH )
00096 
00097         self.assertEqual( ctx.readDataFile( FILENAME ), printable )
00098 
00099     def test_readDataFile_subdir( self ):
00100 
00101         from string import printable
00102 
00103         FILENAME = 'subdir/nested.txt'
00104         self._makeFile( FILENAME, printable )
00105 
00106         site = DummySite('site').__of__(self.app)
00107         ctx = self._makeOne( site, self._PROFILE_PATH )
00108 
00109         self.assertEqual( ctx.readDataFile( FILENAME ), printable )
00110 
00111     def test_getLastModified_nonesuch( self ):
00112 
00113         FILENAME = 'nonesuch.txt'
00114 
00115         site = DummySite('site').__of__(self.app)
00116         ctx = self._makeOne( site, self._PROFILE_PATH )
00117 
00118         self.assertEqual( ctx.getLastModified( FILENAME ), None )
00119 
00120     def test_getLastModified_simple( self ):
00121 
00122         from string import printable
00123 
00124         FILENAME = 'simple.txt'
00125         fqpath = self._makeFile( FILENAME, printable )
00126         timestamp = os.path.getmtime( fqpath )
00127 
00128         site = DummySite('site').__of__(self.app)
00129         ctx = self._makeOne( site, self._PROFILE_PATH )
00130 
00131         lm = ctx.getLastModified( FILENAME )
00132         self.failUnless( isinstance( lm, DateTime ) )
00133         self.assertEqual( lm, timestamp )
00134 
00135     def test_getLastModified_subdir( self ):
00136 
00137         from string import printable
00138 
00139         SUBDIR = 'subdir'
00140         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00141         fqpath = self._makeFile( FILENAME, printable )
00142         timestamp = os.path.getmtime( fqpath )
00143 
00144         site = DummySite('site').__of__(self.app)
00145         ctx = self._makeOne( site, self._PROFILE_PATH )
00146 
00147         lm = ctx.getLastModified( FILENAME )
00148         self.failUnless( isinstance( lm, DateTime ) )
00149         self.assertEqual( lm, timestamp )
00150 
00151     def test_getLastModified_directory( self ):
00152 
00153         from string import printable
00154 
00155         SUBDIR = 'subdir'
00156         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00157         fqpath = self._makeFile( FILENAME, printable )
00158         path, file = os.path.split( fqpath )
00159         timestamp = os.path.getmtime( path )
00160 
00161         site = DummySite('site').__of__(self.app)
00162         ctx = self._makeOne( site, self._PROFILE_PATH )
00163 
00164         lm = ctx.getLastModified( SUBDIR )
00165         self.failUnless( isinstance( lm, DateTime ) )
00166         self.assertEqual( lm, timestamp )
00167 
00168     def test_isDirectory_nonesuch( self ):
00169 
00170         FILENAME = 'nonesuch.txt'
00171 
00172         site = DummySite('site').__of__(self.app)
00173         ctx = self._makeOne( site, self._PROFILE_PATH )
00174 
00175         self.assertEqual( ctx.isDirectory( FILENAME ), None )
00176 
00177     def test_isDirectory_simple( self ):
00178 
00179         from string import printable
00180 
00181         FILENAME = 'simple.txt'
00182         fqpath = self._makeFile( FILENAME, printable )
00183 
00184         site = DummySite('site').__of__(self.app)
00185         ctx = self._makeOne( site, self._PROFILE_PATH )
00186 
00187         self.assertEqual( ctx.isDirectory( FILENAME ), False )
00188 
00189     def test_isDirectory_nested( self ):
00190 
00191         from string import printable
00192 
00193         SUBDIR = 'subdir'
00194         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00195         fqpath = self._makeFile( FILENAME, printable )
00196 
00197         site = DummySite('site').__of__(self.app)
00198         ctx = self._makeOne( site, self._PROFILE_PATH )
00199 
00200         self.assertEqual( ctx.isDirectory( FILENAME ), False )
00201 
00202     def test_isDirectory_directory( self ):
00203 
00204         from string import printable
00205 
00206         SUBDIR = 'subdir'
00207         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00208         fqpath = self._makeFile( FILENAME, printable )
00209 
00210         site = DummySite('site').__of__(self.app)
00211         ctx = self._makeOne( site, self._PROFILE_PATH )
00212 
00213         self.assertEqual( ctx.isDirectory( SUBDIR ), True )
00214 
00215     def test_listDirectory_nonesuch( self ):
00216 
00217         FILENAME = 'nonesuch.txt'
00218 
00219         site = DummySite('site').__of__(self.app)
00220         ctx = self._makeOne( site, self._PROFILE_PATH )
00221 
00222         self.assertEqual( ctx.listDirectory( FILENAME ), None )
00223 
00224     def test_listDirectory_root( self ):
00225 
00226         from string import printable
00227 
00228         site = DummySite('site').__of__(self.app)
00229         ctx = self._makeOne( site, self._PROFILE_PATH )
00230 
00231         FILENAME = 'simple.txt'
00232         self._makeFile( FILENAME, printable )
00233 
00234         self.assertEqual( len( ctx.listDirectory( None ) ), 1 )
00235         self.failUnless( FILENAME in ctx.listDirectory( None ) )
00236 
00237     def test_listDirectory_simple( self ):
00238 
00239         from string import printable
00240 
00241         FILENAME = 'simple.txt'
00242         self._makeFile( FILENAME, printable )
00243 
00244         site = DummySite('site').__of__(self.app)
00245         ctx = self._makeOne( site, self._PROFILE_PATH )
00246 
00247         self.assertEqual( ctx.listDirectory( FILENAME ), None )
00248 
00249     def test_listDirectory_nested( self ):
00250 
00251         from string import printable
00252 
00253         SUBDIR = 'subdir'
00254         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00255         self._makeFile( FILENAME, printable )
00256 
00257         site = DummySite('site').__of__(self.app)
00258         ctx = self._makeOne( site, self._PROFILE_PATH )
00259 
00260         self.assertEqual( ctx.listDirectory( FILENAME ), None )
00261 
00262     def test_listDirectory_single( self ):
00263 
00264         from string import printable
00265 
00266         SUBDIR = 'subdir'
00267         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00268         self._makeFile( FILENAME, printable )
00269 
00270         site = DummySite('site').__of__(self.app)
00271         ctx = self._makeOne( site, self._PROFILE_PATH )
00272 
00273         names = ctx.listDirectory( SUBDIR )
00274         self.assertEqual( len( names ), 1 )
00275         self.failUnless( 'nested.txt' in names )
00276 
00277     def test_listDirectory_multiple( self ):
00278 
00279         from string import printable
00280         SUBDIR = 'subdir'
00281         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00282         self._makeFile( FILENAME, printable )
00283         self._makeFile( os.path.join( SUBDIR, 'another.txt' ), 'ABC' )
00284 
00285         site = DummySite('site').__of__(self.app)
00286         ctx = self._makeOne( site, self._PROFILE_PATH )
00287 
00288         names = ctx.listDirectory( SUBDIR )
00289         self.assertEqual( len( names ), 2 )
00290         self.failUnless( 'nested.txt' in names )
00291         self.failUnless( 'another.txt' in names )
00292 
00293     def test_listDirectory_skip_implicit( self ):
00294 
00295         from string import printable
00296         SUBDIR = 'subdir'
00297         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00298         self._makeFile( FILENAME, printable )
00299         self._makeFile( os.path.join( SUBDIR, 'another.txt' ), 'ABC' )
00300         self._makeFile( os.path.join( SUBDIR, 'another.txt~' ), '123' )
00301         self._makeFile( os.path.join( SUBDIR, 'CVS/skip.txt' ), 'DEF' )
00302         self._makeFile( os.path.join( SUBDIR, '.svn/skip.txt' ), 'GHI' )
00303 
00304         site = DummySite('site').__of__(self.app)
00305         ctx = self._makeOne( site, self._PROFILE_PATH )
00306 
00307         names = ctx.listDirectory( SUBDIR )
00308         self.assertEqual( len( names ), 2 )
00309         self.failUnless( 'nested.txt' in names )
00310         self.failUnless( 'another.txt' in names )
00311         self.failIf( 'another.txt~' in names )
00312         self.failIf( 'CVS' in names )
00313         self.failIf( '.svn' in names )
00314 
00315     def test_listDirectory_skip_explicit( self ):
00316 
00317         from string import printable
00318         SUBDIR = 'subdir'
00319         FILENAME = os.path.join( SUBDIR, 'nested.txt' )
00320         self._makeFile( FILENAME, printable )
00321         self._makeFile( os.path.join( SUBDIR, 'another.txt' ), 'ABC' )
00322         self._makeFile( os.path.join( SUBDIR, 'another.bak' ), '123' )
00323         self._makeFile( os.path.join( SUBDIR, 'CVS/skip.txt' ), 'DEF' )
00324         self._makeFile( os.path.join( SUBDIR, '.svn/skip.txt' ), 'GHI' )
00325 
00326         site = DummySite('site').__of__(self.app)
00327         ctx = self._makeOne( site, self._PROFILE_PATH )
00328 
00329         names = ctx.listDirectory(SUBDIR, skip=('nested.txt',),
00330                                   skip_suffixes=('.bak',))
00331         self.assertEqual( len( names ), 3 )
00332         self.failIf( 'nested.txt' in names )
00333         self.failIf( 'nested.bak' in names )
00334         self.failUnless( 'another.txt' in names )
00335         self.failUnless( 'CVS' in names )
00336         self.failUnless( '.svn' in names )
00337 
00338 
00339 class DirectoryExportContextTests( FilesystemTestBase
00340                                  , ConformsToISetupContext
00341                                  , ConformsToIExportContext
00342                                  ):
00343 
00344     _PROFILE_PATH = '/tmp/ECTTexts'
00345 
00346     def _getTargetClass( self ):
00347 
00348         from Products.GenericSetup.context import DirectoryExportContext
00349         return DirectoryExportContext
00350 
00351     def test_getLogger( self ):
00352 
00353         site = DummySite('site').__of__(self.app)
00354         ctx = self._makeOne( site, self._PROFILE_PATH )
00355         self.assertEqual( len( ctx.listNotes() ), 0 )
00356 
00357         logger = ctx.getLogger('foo')
00358         logger.info('bar')
00359 
00360         self.assertEqual( len( ctx.listNotes() ), 1 )
00361         level, component, message = ctx.listNotes()[0]
00362         self.assertEqual( level, logging.INFO )
00363         self.assertEqual( component, 'foo' )
00364         self.assertEqual( message, 'bar' )
00365 
00366         ctx.clearNotes()
00367         self.assertEqual( len( ctx.listNotes() ), 0 )
00368 
00369     def test_writeDataFile_simple( self ):
00370 
00371         from string import printable, digits
00372         FILENAME = 'simple.txt'
00373         fqname = self._makeFile( FILENAME, printable )
00374 
00375         site = DummySite('site').__of__(self.app)
00376         ctx = self._makeOne( site, self._PROFILE_PATH )
00377 
00378         ctx.writeDataFile( FILENAME, digits, 'text/plain' )
00379 
00380         self.assertEqual( open( fqname, 'rb' ).read(), digits )
00381 
00382     def test_writeDataFile_new_subdir( self ):
00383 
00384         from string import printable, digits
00385         SUBDIR = 'subdir'
00386         FILENAME = 'nested.txt'
00387         fqname = os.path.join( self._PROFILE_PATH, SUBDIR, FILENAME )
00388 
00389         site = DummySite('site').__of__(self.app)
00390         ctx = self._makeOne( site, self._PROFILE_PATH )
00391 
00392         ctx.writeDataFile( FILENAME, digits, 'text/plain', SUBDIR )
00393 
00394         self.assertEqual( open( fqname, 'rb' ).read(), digits )
00395 
00396     def test_writeDataFile_overwrite( self ):
00397 
00398         from string import printable, digits
00399         SUBDIR = 'subdir'
00400         FILENAME = 'nested.txt'
00401         fqname = self._makeFile( os.path.join( SUBDIR, FILENAME )
00402                                , printable )
00403 
00404         site = DummySite('site').__of__(self.app)
00405         ctx = self._makeOne( site, self._PROFILE_PATH )
00406 
00407         ctx.writeDataFile( FILENAME, digits, 'text/plain', SUBDIR )
00408 
00409         self.assertEqual( open( fqname, 'rb' ).read(), digits )
00410 
00411     def test_writeDataFile_existing_subdir( self ):
00412 
00413         from string import printable, digits
00414         SUBDIR = 'subdir'
00415         FILENAME = 'nested.txt'
00416         self._makeFile( os.path.join( SUBDIR, 'another.txt' ), printable )
00417         fqname = os.path.join( self._PROFILE_PATH, SUBDIR, FILENAME )
00418 
00419         site = DummySite('site').__of__(self.app)
00420         ctx = self._makeOne( site, self._PROFILE_PATH )
00421 
00422         ctx.writeDataFile( FILENAME, digits, 'text/plain', SUBDIR )
00423 
00424         self.assertEqual( open( fqname, 'rb' ).read(), digits )
00425 
00426 
00427 class TarballImportContextTests(ZopeTestCase, ConformsToISetupContext,
00428                                 ConformsToIImportContext):
00429 
00430     def _getTargetClass( self ):
00431 
00432         from Products.GenericSetup.context import TarballImportContext
00433         return TarballImportContext
00434 
00435     def _makeOne( self, file_dict={}, mod_time=None, *args, **kw ):
00436 
00437         archive_stream = StringIO()
00438         archive = TarFile.open('test.tar.gz', 'w:gz', archive_stream)
00439 
00440         def _addOneMember(path, data, modtime):
00441             stream = StringIO(v)
00442             info = TarInfo(k)
00443             info.size = len(v)
00444             info.mtime = mod_time
00445             archive.addfile(info, stream)
00446 
00447         def _addMember(path, data, modtime):
00448             from tarfile import DIRTYPE
00449             elements = path.split('/')
00450             parents = filter(None, [elements[x] for x in range(len(elements))])
00451             for parent in parents:
00452                 info = TarInfo()
00453                 info.name = parent
00454                 info.size = 0
00455                 info.mtime = mod_time
00456                 info.type = DIRTYPE
00457                 archive.addfile(info, StringIO())
00458             _addOneMember(path, data, modtime)
00459 
00460         file_items = file_dict.items() or [('dummy', '')] # empty archive barfs
00461 
00462         if mod_time is None:
00463             mod_time = time.time()
00464 
00465         for k, v in file_items:
00466             _addMember(k, v, mod_time)
00467 
00468         archive.close()
00469         bits = archive_stream.getvalue()
00470 
00471         site = DummySite('site').__of__(self.app)
00472         site._setObject( 'setup_tool', Folder( 'setup_tool' ) )
00473         tool = site._getOb( 'setup_tool' )
00474 
00475         ctx = self._getTargetClass()( tool, bits, *args, **kw )
00476 
00477         return site, tool, ctx.__of__( tool )
00478 
00479     def test_getLogger( self ):
00480 
00481         site, tool, ctx = self._makeOne()
00482         self.assertEqual( len( ctx.listNotes() ), 0 )
00483 
00484         logger = ctx.getLogger('foo')
00485         logger.info('bar')
00486 
00487         self.assertEqual( len( ctx.listNotes() ), 1 )
00488         level, component, message = ctx.listNotes()[0]
00489         self.assertEqual( level, logging.INFO )
00490         self.assertEqual( component, 'foo' )
00491         self.assertEqual( message, 'bar' )
00492 
00493         ctx.clearNotes()
00494         self.assertEqual( len( ctx.listNotes() ), 0 )
00495 
00496     def test_ctorparms( self ):
00497 
00498         ENCODING = 'latin-1'
00499         site, tool, ctx = self._makeOne( encoding=ENCODING
00500                                        , should_purge=True
00501                                        )
00502 
00503         self.assertEqual( ctx.getEncoding(), ENCODING )
00504         self.assertEqual( ctx.shouldPurge(), True )
00505 
00506     def test_empty( self ):
00507 
00508         site, tool, ctx = self._makeOne()
00509 
00510         self.assertEqual( ctx.getSite(), site )
00511         self.assertEqual( ctx.getEncoding(), None )
00512         self.assertEqual( ctx.shouldPurge(), False )
00513 
00514         # These methods are all specified to return 'None' for non-existing
00515         # paths / entities
00516         self.assertEqual( ctx.isDirectory( 'nonesuch/path' ), None )
00517         self.assertEqual( ctx.listDirectory( 'nonesuch/path' ), None )
00518 
00519     def test_readDataFile_nonesuch( self ):
00520 
00521         FILENAME = 'nonesuch.txt'
00522 
00523         site, tool, ctx = self._makeOne()
00524 
00525         self.assertEqual( ctx.readDataFile( FILENAME ), None )
00526         self.assertEqual( ctx.readDataFile( FILENAME, 'subdir' ), None )
00527 
00528     def test_readDataFile_simple( self ):
00529 
00530         from string import printable
00531 
00532         FILENAME = 'simple.txt'
00533 
00534         site, tool, ctx = self._makeOne( { FILENAME: printable } )
00535 
00536         self.assertEqual( ctx.readDataFile( FILENAME ), printable )
00537 
00538     def test_readDataFile_subdir( self ):
00539 
00540         from string import printable
00541 
00542         FILENAME = 'subdir.txt'
00543         SUBDIR = 'subdir'
00544 
00545         site, tool, ctx = self._makeOne( { '%s/%s' % (SUBDIR, FILENAME):
00546                                             printable } )
00547 
00548         self.assertEqual( ctx.readDataFile( FILENAME, SUBDIR ), printable )
00549 
00550     def test_getLastModified_nonesuch( self ):
00551 
00552         FILENAME = 'nonesuch.txt'
00553 
00554         site, tool, ctx = self._makeOne()
00555 
00556         self.assertEqual( ctx.getLastModified( FILENAME ), None )
00557 
00558     def test_getLastModified_simple( self ):
00559 
00560         from string import printable
00561 
00562         FILENAME = 'simple.txt'
00563         WHEN = DateTime( '2004-01-01T00:00:00Z' )
00564 
00565         site, tool, ctx = self._makeOne( { FILENAME : printable }
00566                                        , mod_time=WHEN )
00567 
00568         self.assertEqual( ctx.getLastModified( FILENAME ), WHEN )
00569 
00570     def test_getLastModified_subdir( self ):
00571 
00572         from string import printable
00573 
00574         FILENAME = 'subdir.txt'
00575         SUBDIR = 'subdir'
00576         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00577         WHEN = DateTime( '2004-01-01T00:00:00Z' )
00578 
00579         site, tool, ctx = self._makeOne( { PATH: printable }
00580                                        , mod_time=WHEN )
00581 
00582         self.assertEqual( ctx.getLastModified( PATH ), WHEN )
00583 
00584     def test_getLastModified_directory( self ):
00585 
00586         from string import printable
00587 
00588         FILENAME = 'subdir.txt'
00589         SUBDIR = 'subdir'
00590         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00591         WHEN = DateTime( '2004-01-01T00:00:00Z' )
00592 
00593         site, tool, ctx = self._makeOne( { PATH: printable }
00594                                        , mod_time=WHEN
00595                                        )
00596 
00597         self.assertEqual( ctx.getLastModified( SUBDIR ), WHEN )
00598 
00599     def test_isDirectory_nonesuch( self ):
00600 
00601         FILENAME = 'nonesuch.txt'
00602 
00603         site, tool, ctx = self._makeOne()
00604 
00605         self.assertEqual( ctx.isDirectory( FILENAME ), None )
00606 
00607     def test_isDirectory_simple( self ):
00608 
00609         from string import printable
00610 
00611         FILENAME = 'simple.txt'
00612 
00613         site, tool, ctx = self._makeOne( { FILENAME: printable } )
00614 
00615         self.assertEqual( ctx.isDirectory( FILENAME ), False )
00616 
00617     def test_isDirectory_nested( self ):
00618 
00619         from string import printable
00620 
00621         SUBDIR = 'subdir'
00622         FILENAME = 'nested.txt'
00623         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00624 
00625         site, tool, ctx = self._makeOne( { PATH: printable } )
00626 
00627         self.assertEqual( ctx.isDirectory( PATH ), False )
00628 
00629     def test_isDirectory_subdir( self ):
00630 
00631         from string import printable
00632 
00633         SUBDIR = 'subdir'
00634         FILENAME = 'nested.txt'
00635         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00636 
00637         site, tool, ctx = self._makeOne( { PATH: printable } )
00638 
00639         self.assertEqual( ctx.isDirectory( SUBDIR ), True )
00640 
00641     def test_listDirectory_nonesuch( self ):
00642 
00643         SUBDIR = 'nonesuch/path'
00644 
00645         site, tool, ctx = self._makeOne()
00646 
00647         self.assertEqual( ctx.listDirectory( SUBDIR ), None )
00648 
00649     def test_listDirectory_root( self ):
00650 
00651         from string import printable
00652 
00653         FILENAME = 'simple.txt'
00654 
00655         site, tool, ctx = self._makeOne( { FILENAME: printable } )
00656 
00657         self.assertEqual( len( ctx.listDirectory( None ) ), 1 )
00658         self.failUnless( FILENAME in ctx.listDirectory( None ) )
00659 
00660     def test_listDirectory_simple( self ):
00661 
00662         from string import printable
00663 
00664         FILENAME = 'simple.txt'
00665 
00666         site, tool, ctx = self._makeOne( { FILENAME: printable } )
00667 
00668         self.assertEqual( ctx.listDirectory( FILENAME ), None )
00669 
00670     def test_listDirectory_nested( self ):
00671 
00672         from string import printable
00673 
00674         SUBDIR = 'subdir'
00675         FILENAME = 'nested.txt'
00676         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00677 
00678         site, tool, ctx = self._makeOne( { PATH: printable } )
00679 
00680         self.assertEqual( ctx.listDirectory( PATH ), None )
00681 
00682     def test_listDirectory_single( self ):
00683 
00684         from string import printable
00685 
00686         SUBDIR = 'subdir'
00687         FILENAME = 'nested.txt'
00688         PATH = '%s/%s' % ( SUBDIR, FILENAME )
00689 
00690         site, tool, ctx = self._makeOne( { PATH: printable } )
00691 
00692         names = ctx.listDirectory( SUBDIR )
00693         self.assertEqual( len( names ), 1 )
00694         self.failUnless( FILENAME in names )
00695 
00696     def test_listDirectory_multiple( self ):
00697 
00698         from string import printable, uppercase
00699 
00700         SUBDIR = 'subdir'
00701         FILENAME1 = 'nested.txt'
00702         PATH1 = '%s/%s' % ( SUBDIR, FILENAME1 )
00703         FILENAME2 = 'another.txt'
00704         PATH2 = '%s/%s' % ( SUBDIR, FILENAME2 )
00705 
00706         site, tool, ctx = self._makeOne( { PATH1: printable
00707                                          , PATH2: uppercase
00708                                          } )
00709                                              
00710         names = ctx.listDirectory( SUBDIR )
00711         self.assertEqual( len( names ), 2 )
00712         self.failUnless( FILENAME1 in names )
00713         self.failUnless( FILENAME2 in names )
00714 
00715     def test_listDirectory_skip( self ):
00716 
00717         from string import printable, uppercase
00718 
00719         SUBDIR = 'subdir'
00720         FILENAME1 = 'nested.txt'
00721         PATH1 = '%s/%s' % ( SUBDIR, FILENAME1 )
00722         FILENAME2 = 'another.txt'
00723         PATH2 = '%s/%s' % ( SUBDIR, FILENAME2 )
00724         FILENAME3 = 'another.bak'
00725         PATH3 = '%s/%s' % ( SUBDIR, FILENAME3 )
00726 
00727         site, tool, ctx = self._makeOne( { PATH1: printable
00728                                          , PATH2: uppercase
00729                                          , PATH3: 'xyz'
00730                                          } )
00731 
00732         names = ctx.listDirectory(SUBDIR, skip=(FILENAME1,),
00733                                   skip_suffixes=('.bak',))
00734         self.assertEqual( len( names ), 1 )
00735         self.failIf( FILENAME1 in names )
00736         self.failUnless( FILENAME2 in names )
00737         self.failIf( FILENAME3 in names )
00738 
00739 
00740 class TarballExportContextTests(ZopeTestCase, ConformsToISetupContext,
00741                                 ConformsToIExportContext, TarballTester):
00742 
00743     def _getTargetClass( self ):
00744 
00745         from Products.GenericSetup.context import TarballExportContext
00746         return TarballExportContext
00747 
00748     def test_getLogger( self ):
00749 
00750         site = DummySite('site').__of__(self.app)
00751         ctx = self._getTargetClass()( site )
00752 
00753         self.assertEqual( len( ctx.listNotes() ), 0 )
00754 
00755         logger = ctx.getLogger('foo')
00756         logger.info('bar')
00757 
00758         self.assertEqual( len( ctx.listNotes() ), 1 )
00759         level, component, message = ctx.listNotes()[0]
00760         self.assertEqual( level, logging.INFO )
00761         self.assertEqual( component, 'foo' )
00762         self.assertEqual( message, 'bar' )
00763 
00764         ctx.clearNotes()
00765         self.assertEqual( len( ctx.listNotes() ), 0 )
00766 
00767     def test_writeDataFile_simple( self ):
00768 
00769         from string import printable
00770         now = long( time.time() )
00771 
00772         site = DummySite('site').__of__(self.app)
00773         ctx = self._getTargetClass()( site )
00774 
00775         ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
00776 
00777         fileish = StringIO( ctx.getArchive() )
00778 
00779         self._verifyTarballContents( fileish, [ 'foo.txt' ], now )
00780         self._verifyTarballEntry( fileish, 'foo.txt', printable )
00781 
00782     def test_writeDataFile_multiple( self ):
00783 
00784         from string import printable
00785         from string import digits
00786 
00787         site = DummySite('site').__of__(self.app)
00788         ctx = self._getTargetClass()( site )
00789 
00790         ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
00791         ctx.writeDataFile( 'bar.txt', digits, 'text/plain' )
00792 
00793         fileish = StringIO( ctx.getArchive() )
00794 
00795         self._verifyTarballContents( fileish, [ 'foo.txt', 'bar.txt' ] )
00796         self._verifyTarballEntry( fileish, 'foo.txt', printable )
00797         self._verifyTarballEntry( fileish, 'bar.txt', digits )
00798 
00799     def test_writeDataFile_subdir( self ):
00800 
00801         from string import printable
00802         from string import digits
00803 
00804         site = DummySite('site').__of__(self.app)
00805         ctx = self._getTargetClass()( site )
00806 
00807         ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
00808         ctx.writeDataFile( 'bar/baz.txt', digits, 'text/plain' )
00809 
00810         fileish = StringIO( ctx.getArchive() )
00811 
00812         self._verifyTarballContents( fileish, [ 'foo.txt', 'bar/baz.txt' ] )
00813         self._verifyTarballEntry( fileish, 'foo.txt', printable )
00814         self._verifyTarballEntry( fileish, 'bar/baz.txt', digits )
00815 
00816 
00817 class SnapshotExportContextTests(ZopeTestCase, ConformsToISetupContext,
00818                                  ConformsToIExportContext):
00819 
00820     def _getTargetClass( self ):
00821 
00822         from Products.GenericSetup.context import SnapshotExportContext
00823         return SnapshotExportContext
00824 
00825     def _makeOne( self, *args, **kw ):
00826 
00827         return self._getTargetClass()( *args, **kw )
00828 
00829     def test_getLogger( self ):
00830 
00831         site = DummySite('site').__of__(self.app)
00832         site.setup_tool = DummyTool( 'setup_tool' )
00833         tool = site.setup_tool
00834         ctx = self._makeOne( tool, 'simple' )
00835 
00836         self.assertEqual( len( ctx.listNotes() ), 0 )
00837 
00838         logger = ctx.getLogger('foo')
00839         logger.info('bar')
00840 
00841         self.assertEqual( len( ctx.listNotes() ), 1 )
00842         level, component, message = ctx.listNotes()[0]
00843         self.assertEqual( level, logging.INFO )
00844         self.assertEqual( component, 'foo' )
00845         self.assertEqual( message, 'bar' )
00846 
00847         ctx.clearNotes()
00848         self.assertEqual( len( ctx.listNotes() ), 0 )
00849 
00850     def test_writeDataFile_simple_image( self ):
00851 
00852         from OFS.Image import Image
00853         FILENAME = 'simple.txt'
00854         CONTENT_TYPE = 'image/png'
00855         png_filename = os.path.join( os.path.split( __file__ )[0]
00856                                    , 'simple.png' )
00857         png_file = open( png_filename, 'rb' )
00858         png_data = png_file.read()
00859         png_file.close()
00860 
00861         site = DummySite('site').__of__(self.app)
00862         site.setup_tool = DummyTool( 'setup_tool' )
00863         tool = site.setup_tool
00864         ctx = self._makeOne( tool, 'simple' )
00865 
00866         ctx.writeDataFile( FILENAME, png_data, CONTENT_TYPE )
00867 
00868         snapshot = tool.snapshots._getOb( 'simple' )
00869 
00870         self.assertEqual( len( snapshot.objectIds() ), 1 )
00871         self.failUnless( FILENAME in snapshot.objectIds() )
00872 
00873         fileobj = snapshot._getOb( FILENAME )
00874 
00875         self.assertEqual( fileobj.getId(), FILENAME )
00876         self.assertEqual( fileobj.meta_type, Image.meta_type )
00877         self.assertEqual( fileobj.getContentType(), CONTENT_TYPE )
00878         self.assertEqual( fileobj.data, png_data )
00879 
00880     def test_writeDataFile_simple_plain_text( self ):
00881 
00882         from string import digits
00883         from OFS.Image import File
00884         FILENAME = 'simple.txt'
00885         CONTENT_TYPE = 'text/plain'
00886 
00887         site = DummySite('site').__of__(self.app)
00888         site.setup_tool = DummyTool( 'setup_tool' )
00889         tool = site.setup_tool
00890         ctx = self._makeOne( tool, 'simple' )
00891 
00892         ctx.writeDataFile( FILENAME, digits, CONTENT_TYPE )
00893 
00894         snapshot = tool.snapshots._getOb( 'simple' )
00895 
00896         self.assertEqual( len( snapshot.objectIds() ), 1 )
00897         self.failUnless( FILENAME in snapshot.objectIds() )
00898 
00899         fileobj = snapshot._getOb( FILENAME )
00900 
00901         self.assertEqual( fileobj.getId(), FILENAME )
00902         self.assertEqual( fileobj.meta_type, File.meta_type )
00903         self.assertEqual( fileobj.getContentType(), CONTENT_TYPE )
00904         self.assertEqual( str( fileobj ), digits )
00905 
00906     def test_writeDataFile_simple_plain_text_unicode( self ):
00907 
00908         from string import digits
00909         from OFS.Image import File
00910         FILENAME = 'simple.txt'
00911         CONTENT_TYPE = 'text/plain'
00912         CONTENT = u'Unicode, with non-ASCII: %s.' % unichr(150)
00913 
00914         site = DummySite('site').__of__(self.app)
00915         site.setup_tool = DummyTool( 'setup_tool' )
00916         tool = site.setup_tool
00917         ctx = self._makeOne( tool, 'simple', 'latin_1' )
00918 
00919         ctx.writeDataFile( FILENAME, CONTENT, CONTENT_TYPE )
00920 
00921         snapshot = tool.snapshots._getOb( 'simple' )
00922 
00923         self.assertEqual( len( snapshot.objectIds() ), 1 )
00924         self.failUnless( FILENAME in snapshot.objectIds() )
00925 
00926         fileobj = snapshot._getOb( FILENAME )
00927 
00928         self.assertEqual( fileobj.getId(), FILENAME )
00929         self.assertEqual( fileobj.meta_type, File.meta_type )
00930         self.assertEqual( fileobj.getContentType(), CONTENT_TYPE )
00931         saved = fileobj.manage_FTPget().decode('latin_1')
00932         self.assertEqual( saved, CONTENT )
00933 
00934     def test_writeDataFile_simple_xml( self ):
00935 
00936         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00937         FILENAME = 'simple.xml'
00938         CONTENT_TYPE = 'text/xml'
00939         _XML = """<?xml version="1.0"?><simple />"""
00940 
00941         site = DummySite('site').__of__(self.app)
00942         site.setup_tool = DummyTool( 'setup_tool' )
00943         tool = site.setup_tool
00944         ctx = self._makeOne( tool, 'simple' )
00945 
00946         ctx.writeDataFile( FILENAME, _XML, CONTENT_TYPE )
00947 
00948         snapshot = tool.snapshots._getOb( 'simple' )
00949 
00950         self.assertEqual( len( snapshot.objectIds() ), 1 )
00951         self.failUnless( FILENAME in snapshot.objectIds() )
00952 
00953         template = snapshot._getOb( FILENAME )
00954 
00955         self.assertEqual( template.getId(), FILENAME )
00956         self.assertEqual( template.meta_type, ZopePageTemplate.meta_type )
00957         self.assertEqual( template.read(), _XML )
00958         self.failIf( template.html() )
00959 
00960     def test_writeDataFile_unicode_xml( self ):
00961 
00962         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
00963         FILENAME = 'simple.xml'
00964         CONTENT_TYPE = 'text/xml'
00965         _XML = u"""<?xml version="1.0"?><simple />"""
00966 
00967         site = DummySite('site').__of__(self.app)
00968         site.setup_tool = DummyTool( 'setup_tool' )
00969         tool = site.setup_tool
00970         ctx = self._makeOne( tool, 'simple' )
00971 
00972         ctx.writeDataFile( FILENAME, _XML, CONTENT_TYPE )
00973 
00974         snapshot = tool.snapshots._getOb( 'simple' )
00975 
00976         self.assertEqual( len( snapshot.objectIds() ), 1 )
00977         self.failUnless( FILENAME in snapshot.objectIds() )
00978 
00979         template = snapshot._getOb( FILENAME )
00980 
00981         self.assertEqual( template.getId(), FILENAME )
00982         self.assertEqual( template.meta_type, ZopePageTemplate.meta_type )
00983         self.assertEqual( template.read(), _XML )
00984         self.failIf( template.html() )
00985 
00986     def test_writeDataFile_subdir_dtml( self ):
00987 
00988         from OFS.DTMLDocument import DTMLDocument
00989         FILENAME = 'simple.dtml'
00990         CONTENT_TYPE = 'text/html'
00991         _HTML = """<html><body><h1>HTML</h1></body></html>"""
00992 
00993         site = DummySite('site').__of__(self.app)
00994         site.setup_tool = DummyTool( 'setup_tool' )
00995         tool = site.setup_tool
00996         ctx = self._makeOne( tool, 'simple' )
00997 
00998         ctx.writeDataFile( FILENAME, _HTML, CONTENT_TYPE, 'sub1' )
00999 
01000         snapshot = tool.snapshots._getOb( 'simple' )
01001         sub1 = snapshot._getOb( 'sub1' )
01002 
01003         self.assertEqual( len( sub1.objectIds() ), 1 )
01004         self.failUnless( FILENAME in sub1.objectIds() )
01005 
01006         template = sub1._getOb( FILENAME )
01007 
01008         self.assertEqual( template.getId(), FILENAME )
01009         self.assertEqual( template.meta_type, DTMLDocument.meta_type )
01010         self.assertEqual( template.read(), _HTML )
01011 
01012         ctx.writeDataFile( 'sub1/%s2' % FILENAME, _HTML, CONTENT_TYPE)
01013         self.assertEqual( len( sub1.objectIds() ), 2 )
01014 
01015     def test_writeDataFile_nested_subdirs_html( self ):
01016 
01017         from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
01018         FILENAME = 'simple.html'
01019         CONTENT_TYPE = 'text/html'
01020         _HTML = """<html><body><h1>HTML</h1></body></html>"""
01021 
01022         site = DummySite('site').__of__(self.app)
01023         site.setup_tool = DummyTool( 'setup_tool' )
01024         tool = site.setup_tool
01025         ctx = self._makeOne( tool, 'simple' )
01026 
01027         ctx.writeDataFile( FILENAME, _HTML, CONTENT_TYPE, 'sub1/sub2' )
01028 
01029         snapshot = tool.snapshots._getOb( 'simple' )
01030         sub1 = snapshot._getOb( 'sub1' )
01031         sub2 = sub1._getOb( 'sub2' )
01032 
01033         self.assertEqual( len( sub2.objectIds() ), 1 )
01034         self.failUnless( FILENAME in sub2.objectIds() )
01035 
01036         template = sub2._getOb( FILENAME )
01037 
01038         self.assertEqual( template.getId(), FILENAME )
01039         self.assertEqual( template.meta_type, ZopePageTemplate.meta_type )
01040         self.assertEqual( template.read(), _HTML )
01041         self.failUnless( template.html() )
01042 
01043     def test_writeDataFile_multiple( self ):
01044 
01045         from string import printable
01046         from string import digits
01047 
01048         site = DummySite('site').__of__(self.app)
01049         site.setup_tool = DummyTool( 'setup_tool' )
01050         tool = site.setup_tool
01051         ctx = self._makeOne( tool, 'multiple' )
01052 
01053         ctx.writeDataFile( 'foo.txt', printable, 'text/plain' )
01054         ctx.writeDataFile( 'bar.txt', digits, 'text/plain' )
01055 
01056         snapshot = tool.snapshots._getOb( 'multiple' )
01057 
01058         self.assertEqual( len( snapshot.objectIds() ), 2 )
01059 
01060         for id in [ 'foo.txt', 'bar.txt' ]:
01061             self.failUnless( id in snapshot.objectIds() )
01062 
01063 
01064 class SnapshotImportContextTests(ZopeTestCase, ConformsToISetupContext,
01065                                  ConformsToIImportContext):
01066 
01067     def _getTargetClass( self ):
01068 
01069         from Products.GenericSetup.context import SnapshotImportContext
01070         return SnapshotImportContext
01071 
01072     def _makeOne( self, context_id, *args, **kw ):
01073 
01074         site = DummySite('site').__of__(self.app)
01075         site._setObject( 'setup_tool', Folder( 'setup_tool' ) )
01076         tool = site._getOb( 'setup_tool' )
01077 
01078         tool._setObject( 'snapshots', Folder( 'snapshots' ) )
01079         tool.snapshots._setObject( context_id, Folder( context_id ) )
01080 
01081         ctx = self._getTargetClass()( tool, context_id, *args, **kw )
01082 
01083         return site, tool, ctx.__of__( tool )
01084 
01085     def _makeFile( self
01086                  , tool
01087                  , snapshot_id
01088                  , filename
01089                  , contents
01090                  , content_type='text/plain'
01091                  , mod_time=None
01092                  , subdir=None
01093                  ):
01094 
01095         snapshots = tool._getOb( 'snapshots' )
01096         folder = snapshot = snapshots._getOb( snapshot_id )
01097 
01098         if subdir is not None:
01099 
01100             for element in subdir.split( '/' ):
01101 
01102                 try:
01103                     folder = folder._getOb( element )
01104                 except AttributeError:
01105                     folder._setObject( element, Folder( element ) )
01106                     folder = folder._getOb( element )
01107 
01108         file = File( filename, '', contents, content_type )
01109         folder._setObject( filename, file )
01110 
01111         if mod_time is not None:
01112 
01113             def __faux_mod_time():
01114                 return mod_time
01115 
01116             folder.bobobase_modification_time = \
01117             file.bobobase_modification_time = __faux_mod_time
01118 
01119         return folder._getOb( filename )
01120 
01121     def test_getLogger( self ):
01122 
01123         SNAPSHOT_ID = 'note'
01124         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01125 
01126         self.assertEqual( len( ctx.listNotes() ), 0 )
01127 
01128         logger = ctx.getLogger('foo')
01129         logger.info('bar')
01130 
01131         self.assertEqual( len( ctx.listNotes() ), 1 )
01132         level, component, message = ctx.listNotes()[0]
01133         self.assertEqual( level, logging.INFO )
01134         self.assertEqual( component, 'foo' )
01135         self.assertEqual( message, 'bar' )
01136 
01137         ctx.clearNotes()
01138         self.assertEqual( len( ctx.listNotes() ), 0 )
01139 
01140     def test_ctorparms( self ):
01141 
01142         SNAPSHOT_ID = 'ctorparms'
01143         ENCODING = 'latin-1'
01144         site, tool, ctx = self._makeOne( SNAPSHOT_ID
01145                                        , encoding=ENCODING
01146                                        , should_purge=True
01147                                        )
01148 
01149         self.assertEqual( ctx.getEncoding(), ENCODING )
01150         self.assertEqual( ctx.shouldPurge(), True )
01151 
01152     def test_empty( self ):
01153 
01154         SNAPSHOT_ID = 'empty'
01155         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01156 
01157         self.assertEqual( ctx.getSite(), site )
01158         self.assertEqual( ctx.getEncoding(), None )
01159         self.assertEqual( ctx.shouldPurge(), False )
01160 
01161         # These methods are all specified to return 'None' for non-existing
01162         # paths / entities
01163         self.assertEqual( ctx.isDirectory( 'nonesuch/path' ), None )
01164         self.assertEqual( ctx.listDirectory( 'nonesuch/path' ), None )
01165 
01166     def test_readDataFile_nonesuch( self ):
01167 
01168         SNAPSHOT_ID = 'readDataFile_nonesuch'
01169         FILENAME = 'nonesuch.txt'
01170 
01171         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01172 
01173         self.assertEqual( ctx.readDataFile( FILENAME ), None )
01174         self.assertEqual( ctx.readDataFile( FILENAME, 'subdir' ), None )
01175 
01176     def test_readDataFile_simple( self ):
01177 
01178         from string import printable
01179 
01180         SNAPSHOT_ID = 'readDataFile_simple'
01181         FILENAME = 'simple.txt'
01182 
01183         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01184         self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable )
01185 
01186         self.assertEqual( ctx.readDataFile( FILENAME ), printable )
01187 
01188     def test_readDataFile_subdir( self ):
01189 
01190         from string import printable
01191 
01192         SNAPSHOT_ID = 'readDataFile_subdir'
01193         FILENAME = 'subdir.txt'
01194         SUBDIR = 'subdir'
01195 
01196         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01197         self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01198                       , subdir=SUBDIR )
01199 
01200         self.assertEqual( ctx.readDataFile( FILENAME, SUBDIR ), printable )
01201         self.assertEqual( ctx.readDataFile( '%s/%s' % (SUBDIR, FILENAME) ),
01202                                             printable )
01203 
01204     def test_getLastModified_nonesuch( self ):
01205 
01206         SNAPSHOT_ID = 'getLastModified_nonesuch'
01207         FILENAME = 'nonesuch.txt'
01208 
01209         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01210 
01211         self.assertEqual( ctx.getLastModified( FILENAME ), None )
01212 
01213     def test_getLastModified_simple( self ):
01214 
01215         from string import printable
01216 
01217         SNAPSHOT_ID = 'getLastModified_simple'
01218         FILENAME = 'simple.txt'
01219         WHEN = DateTime( '2004-01-01T00:00:00Z' )
01220 
01221         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01222         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01223                              , mod_time=WHEN )
01224 
01225         self.assertEqual( ctx.getLastModified( FILENAME ), WHEN )
01226 
01227     def test_getLastModified_subdir( self ):
01228 
01229         from string import printable
01230 
01231         SNAPSHOT_ID = 'getLastModified_subdir'
01232         FILENAME = 'subdir.txt'
01233         SUBDIR = 'subdir'
01234         PATH = '%s/%s' % ( SUBDIR, FILENAME )
01235         WHEN = DateTime( '2004-01-01T00:00:00Z' )
01236 
01237         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01238         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01239                              , mod_time=WHEN, subdir=SUBDIR )
01240 
01241         self.assertEqual( ctx.getLastModified( PATH ), WHEN )
01242 
01243     def test_getLastModified_directory( self ):
01244 
01245         from string import printable
01246 
01247         SNAPSHOT_ID = 'readDataFile_subdir'
01248         FILENAME = 'subdir.txt'
01249         SUBDIR = 'subdir'
01250         WHEN = DateTime( '2004-01-01T00:00:00Z' )
01251 
01252         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01253         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01254                              , mod_time=WHEN, subdir=SUBDIR )
01255 
01256         self.assertEqual( ctx.getLastModified( SUBDIR ), WHEN )
01257 
01258     def test_isDirectory_nonesuch( self ):
01259 
01260         SNAPSHOT_ID = 'isDirectory_nonesuch'
01261         FILENAME = 'nonesuch.txt'
01262 
01263         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01264 
01265         self.assertEqual( ctx.isDirectory( FILENAME ), None )
01266 
01267     def test_isDirectory_simple( self ):
01268 
01269         from string import printable
01270 
01271         SNAPSHOT_ID = 'isDirectory_simple'
01272         FILENAME = 'simple.txt'
01273 
01274         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01275         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable )
01276 
01277         self.assertEqual( ctx.isDirectory( FILENAME ), False )
01278 
01279     def test_isDirectory_nested( self ):
01280 
01281         from string import printable
01282 
01283         SNAPSHOT_ID = 'isDirectory_nested'
01284         SUBDIR = 'subdir'
01285         FILENAME = 'nested.txt'
01286         PATH = '%s/%s' % ( SUBDIR, FILENAME )
01287 
01288         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01289         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01290                              , subdir=SUBDIR )
01291 
01292         self.assertEqual( ctx.isDirectory( PATH ), False )
01293 
01294     def test_isDirectory_subdir( self ):
01295 
01296         from string import printable
01297 
01298         SNAPSHOT_ID = 'isDirectory_subdir'
01299         SUBDIR = 'subdir'
01300         FILENAME = 'nested.txt'
01301         PATH = '%s/%s' % ( SUBDIR, FILENAME )
01302 
01303         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01304         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01305                              , subdir=SUBDIR )
01306 
01307         self.assertEqual( ctx.isDirectory( SUBDIR ), True )
01308 
01309     def test_listDirectory_nonesuch( self ):
01310 
01311         SNAPSHOT_ID = 'listDirectory_nonesuch'
01312         SUBDIR = 'nonesuch/path'
01313 
01314         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01315 
01316         self.assertEqual( ctx.listDirectory( SUBDIR ), None )
01317 
01318     def test_listDirectory_root( self ):
01319 
01320         from string import printable
01321 
01322         SNAPSHOT_ID = 'listDirectory_root'
01323         FILENAME = 'simple.txt'
01324 
01325         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01326         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable )
01327 
01328         self.assertEqual( len( ctx.listDirectory( None ) ), 1 )
01329         self.failUnless( FILENAME in ctx.listDirectory( None ) )
01330 
01331     def test_listDirectory_simple( self ):
01332 
01333         from string import printable
01334 
01335         SNAPSHOT_ID = 'listDirectory_simple'
01336         FILENAME = 'simple.txt'
01337 
01338         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01339         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable )
01340 
01341         self.assertEqual( ctx.listDirectory( FILENAME ), None )
01342 
01343     def test_listDirectory_nested( self ):
01344 
01345         from string import printable
01346 
01347         SNAPSHOT_ID = 'listDirectory_nested'
01348         SUBDIR = 'subdir'
01349         FILENAME = 'nested.txt'
01350         PATH = '%s/%s' % ( SUBDIR, FILENAME )
01351 
01352         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01353         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01354                              , subdir=SUBDIR )
01355 
01356         self.assertEqual( ctx.listDirectory( PATH ), None )
01357 
01358     def test_listDirectory_single( self ):
01359 
01360         from string import printable
01361 
01362         SNAPSHOT_ID = 'listDirectory_nested'
01363         SUBDIR = 'subdir'
01364         FILENAME = 'nested.txt'
01365 
01366         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01367         file = self._makeFile( tool, SNAPSHOT_ID, FILENAME, printable
01368                              , subdir=SUBDIR )
01369 
01370         names = ctx.listDirectory( SUBDIR )
01371         self.assertEqual( len( names ), 1 )
01372         self.failUnless( FILENAME in names )
01373 
01374     def test_listDirectory_multiple( self ):
01375 
01376         from string import printable, uppercase
01377 
01378         SNAPSHOT_ID = 'listDirectory_nested'
01379         SUBDIR = 'subdir'
01380         FILENAME1 = 'nested.txt'
01381         FILENAME2 = 'another.txt'
01382 
01383         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01384         file1 = self._makeFile( tool, SNAPSHOT_ID, FILENAME1, printable
01385                               , subdir=SUBDIR )
01386         file2 = self._makeFile( tool, SNAPSHOT_ID, FILENAME2, uppercase
01387                               , subdir=SUBDIR )
01388 
01389         names = ctx.listDirectory( SUBDIR )
01390         self.assertEqual( len( names ), 2 )
01391         self.failUnless( FILENAME1 in names )
01392         self.failUnless( FILENAME2 in names )
01393 
01394     def test_listDirectory_skip( self ):
01395 
01396         from string import printable, uppercase
01397 
01398         SNAPSHOT_ID = 'listDirectory_nested'
01399         SUBDIR = 'subdir'
01400         FILENAME1 = 'nested.txt'
01401         FILENAME2 = 'another.txt'
01402         FILENAME3 = 'another.bak'
01403 
01404         site, tool, ctx = self._makeOne( SNAPSHOT_ID )
01405         file1 = self._makeFile( tool, SNAPSHOT_ID, FILENAME1, printable
01406                               , subdir=SUBDIR )
01407         file2 = self._makeFile( tool, SNAPSHOT_ID, FILENAME2, uppercase
01408                               , subdir=SUBDIR )
01409         file3 = self._makeFile( tool, SNAPSHOT_ID, FILENAME3, 'abc'
01410                               , subdir=SUBDIR )
01411 
01412         names = ctx.listDirectory(SUBDIR, skip=(FILENAME1,),
01413                                   skip_suffixes=('.bak',))
01414         self.assertEqual( len( names ), 1 )
01415         self.failIf( FILENAME1 in names )
01416         self.failUnless( FILENAME2 in names )
01417         self.failIf( FILENAME3 in names )
01418 
01419 
01420 def test_suite():
01421     return unittest.TestSuite((
01422         unittest.makeSuite( DirectoryImportContextTests ),
01423         unittest.makeSuite( DirectoryExportContextTests ),
01424         unittest.makeSuite( TarballImportContextTests ),
01425         unittest.makeSuite( TarballExportContextTests ),
01426         unittest.makeSuite( SnapshotExportContextTests ),
01427         unittest.makeSuite( SnapshotImportContextTests ),
01428         ))
01429 
01430 if __name__ == '__main__':
01431     unittest.main(defaultTest='test_suite')