Back to index

plone3  3.1.7
test_registry.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 """ Registry unit tests.
00014 
00015 $Id: test_registry.py 81973 2007-11-23 18:42:55Z wichert $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 from Products.GenericSetup.testing import ExportImportZCMLLayer
00023 from Products.GenericSetup.tests.common import BaseRegistryTests
00024 from Products.GenericSetup import EXTENSION
00025 from zope.interface import Interface
00026 
00027 from conformance import ConformsToIStepRegistry
00028 from conformance import ConformsToIImportStepRegistry
00029 from conformance import ConformsToIExportStepRegistry
00030 from conformance import ConformsToIToolsetRegistry
00031 from conformance import ConformsToIProfileRegistry
00032 
00033 
00034 #==============================================================================
00035 #   Dummy handlers
00036 #==============================================================================
00037 def ONE_FUNC( context ): pass
00038 def TWO_FUNC( context ): pass
00039 def THREE_FUNC( context ): pass
00040 def FOUR_FUNC( context ): pass
00041 def DOC_FUNC( site ):
00042     """This is the first line.
00043 
00044     This is the second line.
00045     """
00046 
00047 ONE_FUNC_NAME = '%s.%s' % ( __name__, ONE_FUNC.__name__ )
00048 TWO_FUNC_NAME = '%s.%s' % ( __name__, TWO_FUNC.__name__ )
00049 THREE_FUNC_NAME = '%s.%s' % ( __name__, THREE_FUNC.__name__ )
00050 FOUR_FUNC_NAME = '%s.%s' % ( __name__, FOUR_FUNC.__name__ )
00051 DOC_FUNC_NAME = '%s.%s' % ( __name__, DOC_FUNC.__name__ )
00052 
00053 #==============================================================================
00054 #   SSR tests
00055 #==============================================================================
00056 class ImportStepRegistryTests( BaseRegistryTests
00057                              , ConformsToIStepRegistry
00058                              , ConformsToIImportStepRegistry
00059                              ):
00060 
00061     layer = ExportImportZCMLLayer
00062 
00063     def _getTargetClass( self ):
00064 
00065         from Products.GenericSetup.registry import ImportStepRegistry
00066         return ImportStepRegistry
00067 
00068     def test_empty( self ):
00069 
00070         registry = self._makeOne()
00071 
00072         self.assertEqual( len( registry.listSteps() ), 0 )
00073         self.assertEqual( len( registry.listStepMetadata() ), 0 )
00074         self.assertEqual( len( registry.sortSteps() ), 0 )
00075 
00076     def test_getStep_nonesuch( self ):
00077 
00078         registry = self._makeOne()
00079 
00080         self.assertEqual( registry.getStep( 'nonesuch' ), None )
00081         self.assertEqual( registry.getStep( 'nonesuch' ), None )
00082         default = object()
00083         self.failUnless( registry.getStepMetadata( 'nonesuch'
00084                                                  , default ) is default )
00085         self.failUnless( registry.getStep( 'nonesuch', default ) is default )
00086         self.failUnless( registry.getStepMetadata( 'nonesuch'
00087                                                  , default ) is default )
00088 
00089     def test_getStep_defaulted( self ):
00090 
00091         registry = self._makeOne()
00092         default = object()
00093 
00094         self.failUnless( registry.getStep( 'nonesuch', default ) is default )
00095         self.assertEqual( registry.getStepMetadata( 'nonesuch', {} ), {} )
00096 
00097     def test_registerStep_docstring( self ):
00098 
00099         registry = self._makeOne()
00100 
00101         registry.registerStep( id='docstring'
00102                              , version='1'
00103                              , handler=DOC_FUNC
00104                              , dependencies=()
00105                              )
00106 
00107         info = registry.getStepMetadata( 'docstring' )
00108         self.assertEqual( info[ 'id' ], 'docstring' )
00109         self.assertEqual( info[ 'handler' ], DOC_FUNC_NAME )
00110         self.assertEqual( info[ 'dependencies' ], () )
00111         self.assertEqual( info[ 'title' ], 'This is the first line.' )
00112         self.assertEqual( info[ 'description' ] , 'This is the second line.' )
00113 
00114     def test_registerStep_docstring_override( self ):
00115 
00116         registry = self._makeOne()
00117 
00118         registry.registerStep( id='docstring'
00119                              , version='1'
00120                              , handler=DOC_FUNC
00121                              , dependencies=()
00122                              , title='Title'
00123                              )
00124 
00125         info = registry.getStepMetadata( 'docstring' )
00126         self.assertEqual( info[ 'id' ], 'docstring' )
00127         self.assertEqual( info[ 'handler' ], DOC_FUNC_NAME )
00128         self.assertEqual( info[ 'dependencies' ], () )
00129         self.assertEqual( info[ 'title' ], 'Title' )
00130         self.assertEqual( info[ 'description' ] , 'This is the second line.' )
00131 
00132     def test_registerStep_single( self ):
00133 
00134         registry = self._makeOne()
00135 
00136         registry.registerStep( id='one'
00137                              , version='1'
00138                              , handler=ONE_FUNC
00139                              , dependencies=( 'two', 'three' )
00140                              , title='One Step'
00141                              , description='One small step'
00142                              )
00143 
00144         steps = registry.listSteps()
00145         self.assertEqual( len( steps ), 1 )
00146         self.failUnless( 'one' in steps )
00147         self.assertEqual( registry.getStep( 'one' ), ONE_FUNC )
00148 
00149         info = registry.getStepMetadata( 'one' )
00150         self.assertEqual( info[ 'id' ], 'one' )
00151         self.assertEqual( info[ 'version' ], '1' )
00152         self.assertEqual( info[ 'handler' ], ONE_FUNC_NAME )
00153         self.assertEqual( info[ 'dependencies' ], ( 'two', 'three' ) )
00154         self.assertEqual( info[ 'title' ], 'One Step' )
00155         self.assertEqual( info[ 'description' ], 'One small step' )
00156 
00157         info_list = registry.listStepMetadata()
00158         self.assertEqual( len( info_list ), 1 )
00159         self.assertEqual( info, info_list[ 0 ] )
00160 
00161     def test_registerStep_conflict( self ):
00162 
00163         registry = self._makeOne()
00164 
00165         registry.registerStep( id='one', version='1', handler=ONE_FUNC_NAME )
00166 
00167         self.assertRaises( KeyError
00168                          , registry.registerStep
00169                          , id='one'
00170                          , version='0'
00171                          , handler=ONE_FUNC
00172                          )
00173 
00174         registry.registerStep( id='one', version='1', handler=ONE_FUNC_NAME )
00175 
00176         info_list = registry.listStepMetadata()
00177         self.assertEqual( len( info_list ), 1 )
00178 
00179     def test_registerStep_replacement( self ):
00180 
00181         registry = self._makeOne()
00182 
00183         registry.registerStep( id='one'
00184                              , version='1'
00185                              , handler=ONE_FUNC
00186                              , dependencies=( 'two', 'three' )
00187                              , title='One Step'
00188                              , description='One small step'
00189                              )
00190 
00191         registry.registerStep( id='one'
00192                              , version='1.1'
00193                              , handler=ONE_FUNC
00194                              , dependencies=()
00195                              , title='Leads to Another'
00196                              , description='Another small step'
00197                              )
00198 
00199         info = registry.getStepMetadata( 'one' )
00200         self.assertEqual( info[ 'id' ], 'one' )
00201         self.assertEqual( info[ 'version' ], '1.1' )
00202         self.assertEqual( info[ 'dependencies' ], () )
00203         self.assertEqual( info[ 'title' ], 'Leads to Another' )
00204         self.assertEqual( info[ 'description' ], 'Another small step' )
00205 
00206     def test_registerStep_multiple( self ):
00207 
00208         registry = self._makeOne()
00209 
00210         registry.registerStep( id='one'
00211                              , version='1'
00212                              , handler=ONE_FUNC
00213                              , dependencies=()
00214                              )
00215 
00216         registry.registerStep( id='two'
00217                              , version='2'
00218                              , handler=TWO_FUNC
00219                              , dependencies=()
00220                              )
00221 
00222         registry.registerStep( id='three'
00223                              , version='3'
00224                              , handler=THREE_FUNC
00225                              , dependencies=()
00226                              )
00227 
00228         steps = registry.listSteps()
00229         self.assertEqual( len( steps ), 3 )
00230         self.failUnless( 'one' in steps )
00231         self.failUnless( 'two' in steps )
00232         self.failUnless( 'three' in steps )
00233 
00234     def test_sortStep_simple( self ):
00235 
00236         registry = self._makeOne()
00237 
00238         registry.registerStep( id='one'
00239                              , version='1'
00240                              , handler=ONE_FUNC
00241                              , dependencies=( 'two', )
00242                              )
00243 
00244         registry.registerStep( id='two'
00245                              , version='2'
00246                              , handler=TWO_FUNC
00247                              , dependencies=()
00248                              )
00249 
00250         steps = registry.sortSteps()
00251         self.assertEqual( len( steps ), 2 )
00252         one = steps.index( 'one' )
00253         two = steps.index( 'two' )
00254 
00255         self.failUnless( 0 <= two < one )
00256 
00257     def test_sortStep_chained( self ):
00258 
00259         registry = self._makeOne()
00260 
00261         registry.registerStep( id='one'
00262                              , version='1'
00263                              , handler=ONE_FUNC
00264                              , dependencies=( 'three', )
00265                              , title='One small step'
00266                              )
00267 
00268         registry.registerStep( id='two'
00269                              , version='2'
00270                              , handler=TWO_FUNC
00271                              , dependencies=( 'one', )
00272                              , title='Texas two step'
00273                              )
00274 
00275         registry.registerStep( id='three'
00276                              , version='3'
00277                              , handler=THREE_FUNC
00278                              , dependencies=()
00279                              , title='Gimme three steps'
00280                              )
00281        
00282         steps = registry.sortSteps()
00283         self.assertEqual( len( steps ), 3 )
00284         one = steps.index( 'one' )
00285         two = steps.index( 'two' )
00286         three = steps.index( 'three' )
00287 
00288         self.failUnless( 0 <= three < one < two )
00289 
00290 
00291     def test_sortStep_complex( self ):
00292 
00293         registry = self._makeOne()
00294 
00295         registry.registerStep( id='one'
00296                              , version='1'
00297                              , handler=ONE_FUNC
00298                              , dependencies=( 'two', )
00299                              , title='One small step'
00300                              )
00301 
00302         registry.registerStep( id='two'
00303                              , version='2'
00304                              , handler=TWO_FUNC
00305                              , dependencies=( 'four', )
00306                              , title='Texas two step'
00307                              )
00308 
00309         registry.registerStep( id='three'
00310                              , version='3'
00311                              , handler=THREE_FUNC
00312                              , dependencies=( 'four', )
00313                              , title='Gimme three steps'
00314                              )
00315 
00316         registry.registerStep( id='four'
00317                              , version='4'
00318                              , handler=FOUR_FUNC
00319                              , dependencies=()
00320                              , title='Four step program'
00321                              )
00322 
00323         steps = registry.sortSteps()
00324         self.assertEqual( len( steps ), 4 )
00325         one = steps.index( 'one' )
00326         two = steps.index( 'two' )
00327         three = steps.index( 'three' )
00328         four = steps.index( 'four' )
00329 
00330         self.failUnless( 0 <= four < two < one )
00331         self.failUnless( 0 <= four < three )
00332 
00333     def test_sortStep_equivalence( self ):
00334 
00335         registry = self._makeOne()
00336 
00337         registry.registerStep( id='one'
00338                              , version='1'
00339                              , handler=ONE_FUNC
00340                              , dependencies=( 'two', 'three' )
00341                              , title='One small step'
00342                              )
00343 
00344         registry.registerStep( id='two'
00345                              , version='2'
00346                              , handler=TWO_FUNC
00347                              , dependencies=( 'four', )
00348                              , title='Texas two step'
00349                              )
00350 
00351         registry.registerStep( id='three'
00352                              , version='3'
00353                              , handler=THREE_FUNC
00354                              , dependencies=( 'four', )
00355                              , title='Gimme three steps'
00356                              )
00357 
00358         registry.registerStep( id='four'
00359                              , version='4'
00360                              , handler=FOUR_FUNC
00361                              , dependencies=()
00362                              , title='Four step program'
00363                              )
00364 
00365         steps = registry.sortSteps()
00366         self.assertEqual( len( steps ), 4 )
00367         one = steps.index( 'one' )
00368         two = steps.index( 'two' )
00369         three = steps.index( 'three' )
00370         four = steps.index( 'four' )
00371 
00372         self.failUnless( 0 <= four < two < one )
00373         self.failUnless( 0 <= four < three < one )
00374 
00375     def test_checkComplete_simple( self ):
00376 
00377         registry = self._makeOne()
00378 
00379         registry.registerStep( id='one'
00380                              , version='1'
00381                              , handler=ONE_FUNC
00382                              , dependencies=( 'two', )
00383                              )
00384 
00385         incomplete = registry.checkComplete()
00386         self.assertEqual( len( incomplete ), 1 )
00387         self.failUnless( ( 'one', 'two' ) in incomplete )
00388 
00389         registry.registerStep( id='two'
00390                              , version='2'
00391                              , handler=TWO_FUNC
00392                              , dependencies=()
00393                              )
00394 
00395         self.assertEqual( len( registry.checkComplete() ), 0 )
00396 
00397     def test_checkComplete_double( self ):
00398 
00399         registry = self._makeOne()
00400 
00401         registry.registerStep( id='one'
00402                              , version='1'
00403                              , handler=ONE_FUNC
00404                              , dependencies=( 'two', 'three' )
00405                              )
00406 
00407         incomplete = registry.checkComplete()
00408         self.assertEqual( len( incomplete ), 2 )
00409         self.failUnless( ( 'one', 'two' ) in incomplete )
00410         self.failUnless( ( 'one', 'three' ) in incomplete )
00411 
00412         registry.registerStep( id='two'
00413                              , version='2'
00414                              , handler=TWO_FUNC
00415                              , dependencies=()
00416                              )
00417 
00418         incomplete = registry.checkComplete()
00419         self.assertEqual( len( incomplete ), 1 )
00420         self.failUnless( ( 'one', 'three' ) in incomplete )
00421 
00422         registry.registerStep( id='three'
00423                              , version='3'
00424                              , handler=THREE_FUNC
00425                              , dependencies=()
00426                              )
00427 
00428         self.assertEqual( len( registry.checkComplete() ), 0 )
00429 
00430         registry.registerStep( id='two'
00431                              , version='2.1'
00432                              , handler=TWO_FUNC
00433                              , dependencies=( 'four', )
00434                              )
00435 
00436         incomplete = registry.checkComplete()
00437         self.assertEqual( len( incomplete ), 1 )
00438         self.failUnless( ( 'two', 'four' ) in incomplete )
00439 
00440     def test_generateXML_empty( self ):
00441 
00442         registry = self._makeOne().__of__(self.app)
00443 
00444         xml = registry.generateXML()
00445 
00446         self._compareDOM( registry.generateXML(), _EMPTY_IMPORT_XML )
00447 
00448     def test_generateXML_single( self ):
00449 
00450         registry = self._makeOne().__of__(self.app)
00451 
00452         registry.registerStep( id='one'
00453                              , version='1'
00454                              , handler=ONE_FUNC
00455                              , dependencies=()
00456                              , title='One Step'
00457                              , description='One small step'
00458                              )
00459 
00460         self._compareDOM( registry.generateXML(), _SINGLE_IMPORT_XML )
00461 
00462     def test_generateXML_ordered( self ):
00463 
00464         registry = self._makeOne().__of__(self.app)
00465 
00466         registry.registerStep( id='one'
00467                              , version='1'
00468                              , handler=ONE_FUNC
00469                              , dependencies=( 'two', )
00470                              , title='One Step'
00471                              , description='One small step'
00472                              )
00473 
00474         registry.registerStep( id='two'
00475                              , version='2'
00476                              , handler=TWO_FUNC
00477                              , dependencies=( 'three', )
00478                              , title='Two Steps'
00479                              , description='Texas two step'
00480                              )
00481 
00482         registry.registerStep( id='three'
00483                              , version='3'
00484                              , handler=THREE_FUNC
00485                              , dependencies=()
00486                              , title='Three Steps'
00487                              , description='Gimme three steps'
00488                              )
00489 
00490         self._compareDOM( registry.generateXML(), _ORDERED_IMPORT_XML )
00491 
00492     def test_parseXML_empty( self ):
00493 
00494         registry = self._makeOne().__of__(self.app)
00495 
00496         registry.registerStep( id='one'
00497                              , version='1'
00498                              , handler=ONE_FUNC
00499                              , dependencies=()
00500                              , description='One small step'
00501                              )
00502 
00503         info_list = registry.parseXML( _EMPTY_IMPORT_XML )
00504 
00505         self.assertEqual( len( info_list ), 0 )
00506 
00507     def test_parseXML_single( self ):
00508 
00509         registry = self._makeOne().__of__(self.app)
00510 
00511         registry.registerStep( id='two'
00512                              , version='2'
00513                              , handler=TWO_FUNC
00514                              , dependencies=()
00515                              , title='Two Steps'
00516                              , description='Texas two step'
00517                              )
00518 
00519         info_list = registry.parseXML( _SINGLE_IMPORT_XML )
00520 
00521         self.assertEqual( len( info_list ), 1 )
00522 
00523         info = info_list[ 0 ]
00524         self.assertEqual( info[ 'id' ], 'one' )
00525         self.assertEqual( info[ 'version' ], '1' )
00526         self.assertEqual( info[ 'handler' ], ONE_FUNC_NAME )
00527         self.assertEqual( info[ 'dependencies' ], () )
00528         self.assertEqual( info[ 'title' ], 'One Step' )
00529         self.failUnless( 'One small step' in info[ 'description' ] )
00530 
00531 
00532 _EMPTY_IMPORT_XML = """\
00533 <?xml version="1.0"?>
00534 <import-steps>
00535 </import-steps>
00536 """
00537 
00538 _SINGLE_IMPORT_XML = """\
00539 <?xml version="1.0"?>
00540 <import-steps>
00541  <import-step id="one"
00542              version="1"
00543              handler="%s"
00544              title="One Step">
00545   One small step
00546  </import-step>
00547 </import-steps>
00548 """ % ( ONE_FUNC_NAME, )
00549 
00550 _ORDERED_IMPORT_XML = """\
00551 <?xml version="1.0"?>
00552 <import-steps>
00553  <import-step id="one"
00554              version="1"
00555              handler="%s"
00556              title="One Step">
00557   <dependency step="two" />
00558   One small step
00559  </import-step>
00560  <import-step id="three"
00561              version="3"
00562              handler="%s"
00563              title="Three Steps">
00564   Gimme three steps
00565  </import-step>
00566  <import-step id="two"
00567              version="2"
00568              handler="%s"
00569              title="Two Steps">
00570   <dependency step="three" />
00571   Texas two step
00572  </import-step>
00573 </import-steps>
00574 """ % ( ONE_FUNC_NAME, THREE_FUNC_NAME, TWO_FUNC_NAME )
00575 
00576 #==============================================================================
00577 #   ESR tests
00578 #==============================================================================
00579 class ExportStepRegistryTests( BaseRegistryTests
00580                              , ConformsToIStepRegistry
00581                              , ConformsToIExportStepRegistry
00582                              ):
00583 
00584     layer = ExportImportZCMLLayer
00585 
00586     def _getTargetClass( self ):
00587 
00588         from Products.GenericSetup.registry import ExportStepRegistry
00589         return ExportStepRegistry
00590 
00591     def _makeOne( self, *args, **kw ):
00592 
00593         return self._getTargetClass()( *args, **kw )
00594 
00595     def test_empty( self ):
00596 
00597         registry = self._makeOne()
00598         self.assertEqual( len( registry.listSteps() ), 0 )
00599         self.assertEqual( len( registry.listStepMetadata() ), 0 )
00600 
00601     def test_getStep_nonesuch( self ):
00602 
00603         registry = self._makeOne()
00604         self.assertEqual( registry.getStep( 'nonesuch' ), None )
00605 
00606     def test_getStep_defaulted( self ):
00607 
00608         registry = self._makeOne()
00609         default = lambda x: false
00610         self.assertEqual( registry.getStep( 'nonesuch', default ), default )
00611 
00612     def test_getStepMetadata_nonesuch( self ):
00613 
00614         registry = self._makeOne()
00615         self.assertEqual( registry.getStepMetadata( 'nonesuch' ), None )
00616 
00617     def test_getStepMetadata_defaulted( self ):
00618 
00619         registry = self._makeOne()
00620         self.assertEqual( registry.getStepMetadata( 'nonesuch', {} ), {} )
00621 
00622     def test_registerStep_simple( self ):
00623 
00624         registry = self._makeOne()
00625         registry.registerStep( 'one', ONE_FUNC_NAME )
00626         info = registry.getStepMetadata( 'one', {} )
00627 
00628         self.assertEqual( info[ 'id' ], 'one' )
00629         self.assertEqual( info[ 'handler' ], ONE_FUNC_NAME )
00630         self.assertEqual( info[ 'title' ], 'one' )
00631         self.assertEqual( info[ 'description' ], '' )
00632 
00633     def test_registerStep_docstring( self ):
00634 
00635         registry = self._makeOne()
00636         registry.registerStep( 'one', DOC_FUNC )
00637         info = registry.getStepMetadata( 'one', {} )
00638 
00639         self.assertEqual( info[ 'id' ], 'one' )
00640         self.assertEqual( info[ 'handler' ], DOC_FUNC_NAME )
00641         self.assertEqual( info[ 'title' ], 'This is the first line.' )
00642         self.assertEqual( info[ 'description' ] , 'This is the second line.' )
00643 
00644     def test_registerStep_docstring_with_override( self ):
00645 
00646         registry = self._makeOne()
00647         registry.registerStep( 'one', DOC_FUNC
00648                                , description='Description' )
00649         info = registry.getStepMetadata( 'one', {} )
00650 
00651         self.assertEqual( info[ 'id' ], 'one' )
00652         self.assertEqual( info[ 'handler' ], DOC_FUNC_NAME )
00653         self.assertEqual( info[ 'title' ], 'This is the first line.' )
00654         self.assertEqual( info[ 'description' ], 'Description' )
00655 
00656     def test_registerStep_collision( self ):
00657 
00658         registry = self._makeOne()
00659         registry.registerStep( 'one', ONE_FUNC )
00660         registry.registerStep( 'one', TWO_FUNC )
00661         info = registry.getStepMetadata( 'one', {} )
00662 
00663         self.assertEqual( info[ 'id' ], 'one' )
00664         self.assertEqual( info[ 'handler' ], TWO_FUNC_NAME )
00665         self.assertEqual( info[ 'title' ], 'one' )
00666         self.assertEqual( info[ 'description' ], '' )
00667 
00668     def test_generateXML_empty( self ):
00669 
00670         registry = self._makeOne().__of__(self.app)
00671 
00672         xml = registry.generateXML()
00673 
00674         self._compareDOM( registry.generateXML(), _EMPTY_EXPORT_XML )
00675 
00676     def test_generateXML_single( self ):
00677 
00678         registry = self._makeOne().__of__(self.app)
00679 
00680         registry.registerStep( id='one'
00681                              , handler=ONE_FUNC
00682                              , title='One Step'
00683                              , description='One small step'
00684                              )
00685 
00686         self._compareDOM( registry.generateXML(), _SINGLE_EXPORT_XML )
00687 
00688     def test_generateXML_ordered( self ):
00689 
00690         registry = self._makeOne().__of__(self.app)
00691 
00692         registry.registerStep( id='one'
00693                              , handler=ONE_FUNC
00694                              , title='One Step'
00695                              , description='One small step'
00696                              )
00697 
00698         registry.registerStep( id='two'
00699                              , handler=TWO_FUNC
00700                              , title='Two Steps'
00701                              , description='Texas two step'
00702                              )
00703 
00704         registry.registerStep( id='three'
00705                              , handler=THREE_FUNC
00706                              , title='Three Steps'
00707                              , description='Gimme three steps'
00708                              )
00709 
00710         self._compareDOM( registry.generateXML(), _ORDERED_EXPORT_XML )
00711 
00712     def test_parseXML_empty( self ):
00713 
00714         registry = self._makeOne().__of__(self.app)
00715 
00716         registry.registerStep( id='one'
00717                              , handler=ONE_FUNC
00718                              , description='One small step'
00719                              )
00720 
00721         info_list = registry.parseXML( _EMPTY_EXPORT_XML )
00722 
00723         self.assertEqual( len( info_list ), 0 )
00724 
00725     def test_parseXML_single( self ):
00726 
00727         registry = self._makeOne().__of__(self.app)
00728 
00729         registry.registerStep( id='two'
00730                              , handler=TWO_FUNC
00731                              , title='Two Steps'
00732                              , description='Texas two step'
00733                              )
00734 
00735         info_list = registry.parseXML( _SINGLE_EXPORT_XML )
00736 
00737         self.assertEqual( len( info_list ), 1 )
00738 
00739         info = info_list[ 0 ]
00740         self.assertEqual( info[ 'id' ], 'one' )
00741         self.assertEqual( info[ 'handler' ], ONE_FUNC_NAME )
00742         self.assertEqual( info[ 'title' ], 'One Step' )
00743         self.failUnless( 'One small step' in info[ 'description' ] )
00744 
00745     def test_parseXML_single_as_ascii( self ):
00746 
00747         registry = self._makeOne().__of__(self.app)
00748 
00749         registry.registerStep( id='two'
00750                              , handler=TWO_FUNC
00751                              , title='Two Steps'
00752                              , description='Texas two step'
00753                              )
00754 
00755         info_list = registry.parseXML( _SINGLE_EXPORT_XML, encoding='ascii' )
00756 
00757         self.assertEqual( len( info_list ), 1 )
00758 
00759         info = info_list[ 0 ]
00760         self.assertEqual( info[ 'id' ], 'one' )
00761         self.assertEqual( info[ 'handler' ], ONE_FUNC_NAME )
00762         self.assertEqual( info[ 'title' ], 'One Step' )
00763         self.failUnless( 'One small step' in info[ 'description' ] )
00764 
00765 
00766 _EMPTY_EXPORT_XML = """\
00767 <?xml version="1.0"?>
00768 <export-steps>
00769 </export-steps>
00770 """
00771 
00772 _SINGLE_EXPORT_XML = """\
00773 <?xml version="1.0"?>
00774 <export-steps>
00775  <export-step id="one"
00776                 handler="%s"
00777                 title="One Step">
00778   One small step
00779  </export-step>
00780 </export-steps>
00781 """ % ( ONE_FUNC_NAME, )
00782 
00783 _ORDERED_EXPORT_XML = """\
00784 <?xml version="1.0"?>
00785 <export-steps>
00786  <export-step id="one"
00787                 handler="%s"
00788                 title="One Step">
00789   One small step
00790  </export-step>
00791  <export-step id="three"
00792                 handler="%s"
00793                 title="Three Steps">
00794   Gimme three steps
00795  </export-step>
00796  <export-step id="two"
00797                 handler="%s"
00798                 title="Two Steps">
00799   Texas two step
00800  </export-step>
00801 </export-steps>
00802 """ % ( ONE_FUNC_NAME, THREE_FUNC_NAME, TWO_FUNC_NAME )
00803 
00804 #==============================================================================
00805 #   ToolsetRegistry tests
00806 #==============================================================================
00807 class ToolsetRegistryTests( BaseRegistryTests
00808                           , ConformsToIToolsetRegistry
00809                           ):
00810 
00811     layer = ExportImportZCMLLayer
00812 
00813     def _getTargetClass( self ):
00814 
00815         from Products.GenericSetup.registry import ToolsetRegistry
00816         return ToolsetRegistry
00817 
00818     def _initSite( self ):
00819 
00820         self.app.site = Folder(id='site')
00821         site = self.app.site
00822 
00823         return site
00824 
00825     def test_empty( self ):
00826 
00827         site = self._initSite()
00828         configurator = self._makeOne().__of__( site )
00829 
00830         self.assertEqual( len( configurator.listForbiddenTools() ), 0 )
00831         self.assertEqual( len( configurator.listRequiredTools() ), 0 )
00832         self.assertEqual( len( configurator.listRequiredToolInfo() ), 0 )
00833 
00834         self.assertRaises( KeyError
00835                          , configurator.getRequiredToolInfo, 'nonesuch' )
00836 
00837     def test_addForbiddenTool_multiple( self ):
00838 
00839         VERBOTTEN = ( 'foo', 'bar', 'bam' )
00840 
00841         site = self._initSite()
00842         configurator = self._makeOne().__of__( site )
00843 
00844         for verbotten in VERBOTTEN:
00845             configurator.addForbiddenTool( verbotten )
00846 
00847         self.assertEqual( len( configurator.listForbiddenTools() )
00848                         , len( VERBOTTEN ) )
00849 
00850         for verbotten in configurator.listForbiddenTools():
00851             self.failUnless( verbotten in VERBOTTEN )
00852 
00853     def test_addForbiddenTool_duplicate( self ):
00854 
00855         site = self._initSite()
00856         configurator = self._makeOne().__of__( site )
00857 
00858         configurator.addForbiddenTool( 'once' )
00859         configurator.addForbiddenTool( 'once' )
00860 
00861         self.assertEqual( len( configurator.listForbiddenTools() ), 1 )
00862 
00863     def test_addForbiddenTool_but_required( self ):
00864 
00865         site = self._initSite()
00866         configurator = self._makeOne().__of__( site )
00867 
00868         configurator.addRequiredTool( 'required', 'some.dotted.name' )
00869 
00870         self.assertRaises( ValueError
00871                          , configurator.addForbiddenTool, 'required' )
00872 
00873     def test_addRequiredTool_multiple( self ):
00874 
00875         REQUIRED = ( ( 'one', 'path.to.one' )
00876                    , ( 'two', 'path.to.two' )
00877                    , ( 'three', 'path.to.three' )
00878                    )
00879 
00880         site = self._initSite()
00881         configurator = self._makeOne().__of__( site )
00882 
00883         for tool_id, dotted_name in REQUIRED:
00884             configurator.addRequiredTool( tool_id, dotted_name )
00885 
00886         self.assertEqual( len( configurator.listRequiredTools() )
00887                         , len( REQUIRED ) )
00888 
00889         for id in [ x[0] for x in REQUIRED ]:
00890             self.failUnless( id in configurator.listRequiredTools() )
00891 
00892         self.assertEqual( len( configurator.listRequiredToolInfo() )
00893                         , len( REQUIRED ) )
00894 
00895         for tool_id, dotted_name in REQUIRED:
00896             info = configurator.getRequiredToolInfo( tool_id )
00897             self.assertEqual( info[ 'id' ], tool_id )
00898             self.assertEqual( info[ 'class' ], dotted_name )
00899 
00900     def test_addRequiredTool_duplicate( self ):
00901 
00902         site = self._initSite()
00903         configurator = self._makeOne().__of__( site )
00904 
00905         configurator.addRequiredTool( 'required', 'some.dotted.name' )
00906         configurator.addRequiredTool( 'required', 'another.name' )
00907 
00908         info = configurator.getRequiredToolInfo( 'required' )
00909         self.assertEqual( info[ 'id' ], 'required' )
00910         self.assertEqual( info[ 'class' ], 'another.name' )
00911 
00912     def test_addRequiredTool_but_forbidden( self ):
00913 
00914         site = self._initSite()
00915         configurator = self._makeOne().__of__( site )
00916 
00917         configurator.addForbiddenTool( 'forbidden' )
00918 
00919         self.assertRaises( ValueError
00920                          , configurator.addRequiredTool
00921                          , 'forbidden'
00922                          , 'a.name'
00923                          )
00924 
00925     def test_generateXML_empty( self ):
00926 
00927         site = self._initSite()
00928         configurator = self._makeOne().__of__( site )
00929 
00930         self._compareDOM( configurator.generateXML(), _EMPTY_TOOLSET_XML )
00931 
00932     def test_generateXML_normal( self ):
00933 
00934         site = self._initSite()
00935         configurator = self._makeOne().__of__( site )
00936 
00937         configurator.addForbiddenTool( 'doomed' )
00938         configurator.addRequiredTool( 'mandatory', 'path.to.one' )
00939         configurator.addRequiredTool( 'obligatory', 'path.to.another' )
00940 
00941         configurator.parseXML( _NORMAL_TOOLSET_XML )
00942 
00943     def test_parseXML_empty( self ):
00944 
00945         site = self._initSite()
00946         configurator = self._makeOne().__of__( site )
00947 
00948         configurator.parseXML( _EMPTY_TOOLSET_XML )
00949 
00950         self.assertEqual( len( configurator.listForbiddenTools() ), 0 )
00951         self.assertEqual( len( configurator.listRequiredTools() ), 0 )
00952 
00953     def test_parseXML_normal( self ):
00954 
00955         site = self._initSite()
00956         configurator = self._makeOne().__of__( site )
00957 
00958         configurator.parseXML( _NORMAL_TOOLSET_XML )
00959 
00960         self.assertEqual( len( configurator.listForbiddenTools() ), 1 )
00961         self.failUnless( 'doomed' in configurator.listForbiddenTools() )
00962 
00963         self.assertEqual( len( configurator.listRequiredTools() ), 2 )
00964 
00965         self.failUnless( 'mandatory' in configurator.listRequiredTools() )
00966         info = configurator.getRequiredToolInfo( 'mandatory' )
00967         self.assertEqual( info[ 'class' ], 'path.to.one' )
00968 
00969         self.failUnless( 'obligatory' in configurator.listRequiredTools() )
00970         info = configurator.getRequiredToolInfo( 'obligatory' )
00971         self.assertEqual( info[ 'class' ], 'path.to.another' )
00972 
00973     def test_parseXML_confused( self ):
00974 
00975         site = self._initSite()
00976         configurator = self._makeOne().__of__( site )
00977 
00978         self.assertRaises( ValueError
00979                          , configurator.parseXML, _CONFUSED_TOOLSET_XML )
00980 
00981 
00982 _EMPTY_TOOLSET_XML = """\
00983 <?xml version="1.0"?>
00984 <tool-setup>
00985 </tool-setup>
00986 """
00987 
00988 _NORMAL_TOOLSET_XML = """\
00989 <?xml version="1.0"?>
00990 <tool-setup>
00991  <forbidden tool_id="doomed" />
00992  <required tool_id="mandatory" class="path.to.one" />
00993  <required tool_id="obligatory" class="path.to.another" />
00994 </tool-setup>
00995 """
00996 
00997 _CONFUSED_TOOLSET_XML = """\
00998 <?xml version="1.0"?>
00999 <tool-setup>
01000  <forbidden tool_id="confused" />
01001  <required tool_id="confused" class="path.to.one" />
01002 </tool-setup>
01003 """
01004 
01005 class ISite(Interface):
01006     pass
01007 
01008 class IDerivedSite(ISite):
01009     pass
01010 
01011 class IAnotherSite(Interface):
01012     pass
01013 
01014 
01015 class ProfileRegistryTests( BaseRegistryTests
01016                           , ConformsToIProfileRegistry
01017                           ):
01018 
01019     def _getTargetClass( self ):
01020 
01021         from Products.GenericSetup.registry import ProfileRegistry
01022         return ProfileRegistry
01023 
01024     def test_empty( self ):
01025 
01026         registry = self._makeOne()
01027 
01028         self.assertEqual( len( registry.listProfiles() ), 0 )
01029         self.assertEqual( len( registry.listProfiles() ), 0 )
01030         self.assertRaises( KeyError, registry.getProfileInfo, 'nonesuch' )
01031 
01032     def test_registerProfile_normal( self ):
01033 
01034         NAME = 'one'
01035         TITLE = 'One'
01036         DESCRIPTION = 'One profile'
01037         PATH = '/path/to/one'
01038         PRODUCT = 'TestProduct'
01039         PROFILE_TYPE = EXTENSION
01040         PROFILE_ID = 'TestProduct:one'
01041 
01042         registry = self._makeOne()
01043         registry.registerProfile( NAME
01044                                 , TITLE
01045                                 , DESCRIPTION
01046                                 , PATH
01047                                 , PRODUCT
01048                                 , PROFILE_TYPE
01049                                 )
01050 
01051         self.assertEqual( len( registry.listProfiles() ), 1 )
01052         self.assertEqual( len( registry.listProfileInfo() ), 1 )
01053 
01054         info = registry.getProfileInfo( PROFILE_ID )
01055 
01056         self.assertEqual( info[ 'id' ], PROFILE_ID )
01057         self.assertEqual( info[ 'title' ], TITLE )
01058         self.assertEqual( info[ 'description' ], DESCRIPTION )
01059         self.assertEqual( info[ 'path' ], PATH )
01060         self.assertEqual( info[ 'product' ], PRODUCT )
01061         self.assertEqual( info[ 'type' ], PROFILE_TYPE )
01062         self.assertEqual( info[ 'for' ], None )
01063 
01064     def test_registerProfile_duplicate( self ):
01065 
01066         NAME = 'one'
01067         TITLE = 'One'
01068         DESCRIPTION = 'One profile'
01069         PATH = '/path/to/one'
01070 
01071         registry = self._makeOne()
01072         registry.registerProfile( NAME, TITLE, DESCRIPTION, PATH )
01073         self.assertRaises( KeyError
01074                          , registry.registerProfile
01075                          , NAME, TITLE, DESCRIPTION, PATH )
01076 
01077 
01078     def test_registerProfile_site_type( self ):
01079 
01080         NAME = 'one'
01081         TITLE = 'One'
01082         DESCRIPTION = 'One profile'
01083         PATH = '/path/to/one'
01084         PRODUCT = 'TestProduct'
01085         PROFILE_ID = 'TestProduct:one'
01086         PROFILE_TYPE = EXTENSION
01087         FOR = ISite
01088         NOT_FOR = IAnotherSite
01089         DERIVED_FOR = IDerivedSite
01090 
01091         registry = self._makeOne()
01092         registry.registerProfile( NAME
01093                                 , TITLE
01094                                 , DESCRIPTION
01095                                 , PATH
01096                                 , PRODUCT
01097                                 , PROFILE_TYPE
01098                                 , for_=FOR
01099                                 )
01100 
01101 
01102         self.assertEqual( len( registry.listProfiles() ), 1 )
01103         self.assertEqual( len( registry.listProfiles( for_=FOR ) ), 1 )
01104         self.assertEqual( len( registry.listProfiles( for_=DERIVED_FOR ) )
01105                         , 1 )
01106         self.assertEqual( len( registry.listProfiles( for_=NOT_FOR ) )
01107                         , 0 )
01108 
01109         self.assertEqual( len( registry.listProfileInfo() ), 1 )
01110         self.assertEqual( len( registry.listProfileInfo( for_=FOR ) ), 1 )
01111         self.assertEqual( len( registry.listProfileInfo( for_=DERIVED_FOR ) )
01112                         , 1 )
01113         self.assertEqual( len( registry.listProfileInfo( for_=NOT_FOR ) )
01114                         , 0 )
01115 
01116         # Verify that these lookups succeed...
01117         info1 = registry.getProfileInfo( PROFILE_ID )
01118         info2 = registry.getProfileInfo( PROFILE_ID, for_=FOR )
01119         info3 = registry.getProfileInfo( PROFILE_ID, for_=DERIVED_FOR )
01120 
01121         self.assertEqual(info1, info2)
01122         self.assertEqual(info1, info3)
01123 
01124         # ...and that this one fails.
01125         self.assertRaises( KeyError
01126                          , registry.getProfileInfo
01127                          , PROFILE_ID
01128                          , for_=NOT_FOR
01129                          )
01130 
01131         info = registry.getProfileInfo( PROFILE_ID , for_=FOR )
01132 
01133         self.assertEqual( info[ 'id' ], PROFILE_ID )
01134         self.assertEqual( info[ 'title' ], TITLE )
01135         self.assertEqual( info[ 'description' ], DESCRIPTION )
01136         self.assertEqual( info[ 'path' ], PATH )
01137         self.assertEqual( info[ 'product' ], PRODUCT )
01138         self.assertEqual( info[ 'type' ], PROFILE_TYPE )
01139         self.assertEqual( info[ 'for' ], FOR )
01140 
01141 
01142 def test_suite():
01143     return unittest.TestSuite((
01144         unittest.makeSuite( ImportStepRegistryTests ),
01145         unittest.makeSuite( ExportStepRegistryTests ),
01146         unittest.makeSuite( ToolsetRegistryTests ),
01147         unittest.makeSuite( ProfileRegistryTests ),
01148         ))
01149 
01150 if __name__ == '__main__':
01151     from Products.GenericSetup.testing import run
01152     run(test_suite())