Back to index

plone3  3.1.7
test_PluginRegistry.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights
00004 # Reserved.
00005 #
00006 # This software is subject to the provisions of the Zope Public License,
00007 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this
00008 # distribution.
00009 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00010 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00011 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00012 # FOR A PARTICULAR PURPOSE
00013 #
00014 ##############################################################################
00015 import unittest
00016 from OFS.Folder import Folder
00017 
00018 try:
00019     from zope.interface import Interface
00020 except ImportError:  # Zope < 2.8.0
00021     from Interface import Interface
00022 
00023 from Acquisition import Implicit
00024 
00025 from Products.PluginRegistry.utils import directlyProvides
00026 
00027 class INonesuch(Interface):
00028     pass
00029 
00030 class IFoo(Interface):
00031     def foo():
00032         """ Foo. """
00033 
00034 class IBar(Interface):
00035     def bar():
00036         """ Bar. """
00037 
00038 class DummyFolder(Folder):
00039     pass
00040 
00041 class DummyPlugin(Implicit):
00042     pass
00043 
00044 _PLUGIN_INFO = ( ( IFoo, 'IFoo', 'foo', 'Foo test' )
00045                , ( IBar, 'IBar', 'bar', 'Bar test' )
00046                )
00047 
00048 class PluginRegistryTests( unittest.TestCase ):
00049 
00050     def _getTargetClass( self ):
00051 
00052         from Products.PluginRegistry.PluginRegistry import PluginRegistry
00053 
00054         return PluginRegistry
00055 
00056     def _makeOne( self, plugin_info=_PLUGIN_INFO, *args, **kw ):
00057 
00058         return self._getTargetClass()( plugin_info, *args, **kw )
00059 
00060     def test_conformance_to_IPluginRegistry( self ):
00061 
00062         from Products.PluginRegistry.interfaces import IPluginRegistry
00063         from Products.PluginRegistry.interfaces import _HAS_Z3_INTERFACES
00064 
00065         if _HAS_Z3_INTERFACES:
00066             from zope.interface.verify import verifyClass
00067         else:
00068             from Interface.Verify import verifyClass
00069 
00070         verifyClass( IPluginRegistry, self._getTargetClass() )
00071 
00072     def test_empty( self ):
00073 
00074         preg = self._makeOne()
00075 
00076         self.assertRaises( KeyError, preg.listPlugins, INonesuch )
00077         self.assertEqual( len( preg.listPlugins( IFoo ) ), 0 )
00078         self.assertEqual( len( preg.listPluginIds( IFoo ) ), 0 )
00079 
00080     def test_listPluginTypeInfo( self ):
00081 
00082         pref = self._makeOne()
00083 
00084         pti = pref.listPluginTypeInfo()
00085         self.assertEqual( pti[0]['interface'], IFoo )
00086         self.assertEqual( pti[1]['title'], 'bar' )
00087 
00088     def test_activatePluginNoChild( self ):
00089 
00090         parent = DummyFolder()
00091 
00092         preg = self._makeOne().__of__(parent)
00093 
00094         self.assertRaises( AttributeError, preg.activatePlugin, IFoo,
00095                            'foo_plugin' )
00096 
00097     def test_activatePluginInterfaceNonconformance( self ):
00098 
00099         parent = DummyFolder()
00100         foo_plugin = DummyPlugin()
00101         parent._setObject( 'foo_plugin', foo_plugin )
00102 
00103         preg = self._makeOne().__of__(parent)
00104 
00105         self.assertRaises( ValueError, preg.activatePlugin, IFoo,
00106                            'foo_plugin' )
00107 
00108     def test_activatePlugin( self ):
00109 
00110         parent = DummyFolder()
00111         foo_plugin = DummyPlugin()
00112         directlyProvides( foo_plugin,  ( IFoo, ) )
00113         parent._setObject( 'foo_plugin', foo_plugin )
00114 
00115         preg = self._makeOne().__of__(parent)
00116 
00117         preg.activatePlugin( IFoo, 'foo_plugin')
00118 
00119         idlist = preg.listPluginIds( IFoo )
00120         self.assertEqual( len( idlist ), 1 )
00121         self.assertEqual( idlist[0], 'foo_plugin' )
00122 
00123         plugins = preg.listPlugins( IFoo )
00124         self.assertEqual( len( plugins ), 1 )
00125         plugin = plugins[0]
00126         self.assertEqual( plugin[0], 'foo_plugin' )
00127         self.assertEqual( plugin[1], preg.foo_plugin )
00128 
00129     def test_activatePlugin_then_remove_interface( self ):
00130 
00131         parent = DummyFolder()
00132         foo_plugin = DummyPlugin()
00133         directlyProvides( foo_plugin,  ( IFoo, ) )
00134         parent._setObject( 'foo_plugin', foo_plugin )
00135 
00136         preg = self._makeOne().__of__(parent)
00137 
00138         preg.activatePlugin( IFoo, 'foo_plugin')
00139 
00140         replacement = DummyPlugin()
00141         parent._delObject( 'foo_plugin' )
00142         parent._setObject( 'foo_plugin', replacement )
00143 
00144         idlist = preg.listPluginIds( IFoo )
00145         self.assertEqual( len( idlist ), 1 )  # note discrepancy
00146 
00147         plugins = preg.listPlugins( IFoo )
00148         self.assertEqual( len( plugins ), 0 )
00149 
00150     def test_deactivatePlugin( self ):
00151 
00152         parent = DummyFolder()
00153         foo_plugin = DummyPlugin()
00154         directlyProvides( foo_plugin, ( IFoo, ) )
00155         parent._setObject( 'foo_plugin', foo_plugin )
00156 
00157         bar_plugin = DummyPlugin()
00158         directlyProvides( bar_plugin, ( IFoo, ) )
00159         parent._setObject( 'bar_plugin', bar_plugin )
00160 
00161         baz_plugin = DummyPlugin()
00162         directlyProvides( baz_plugin, ( IFoo, ) )
00163         parent._setObject( 'baz_plugin', baz_plugin )
00164 
00165         preg = self._makeOne().__of__(parent)
00166 
00167         preg.activatePlugin( IFoo, 'foo_plugin' )
00168         preg.activatePlugin( IFoo, 'bar_plugin' )
00169         preg.activatePlugin( IFoo, 'baz_plugin' )
00170 
00171         preg.deactivatePlugin( IFoo, 'bar_plugin' )
00172 
00173         idlist = preg.listPluginIds( IFoo )
00174         self.assertEqual( len( idlist ), 2 )
00175         self.assertEqual( idlist[0], 'foo_plugin' )
00176         self.assertEqual( idlist[1], 'baz_plugin' )
00177 
00178     def test_movePluginsUp( self ):
00179 
00180         parent = DummyFolder()
00181         foo_plugin = DummyPlugin()
00182         directlyProvides( foo_plugin, ( IFoo, ) )
00183         parent._setObject( 'foo_plugin', foo_plugin )
00184 
00185         bar_plugin = DummyPlugin()
00186         directlyProvides( bar_plugin, ( IFoo, ) )
00187         parent._setObject( 'bar_plugin', bar_plugin )
00188 
00189         baz_plugin = DummyPlugin()
00190         directlyProvides( baz_plugin, ( IFoo, ) )
00191         parent._setObject( 'baz_plugin', baz_plugin )
00192 
00193         preg = self._makeOne().__of__(parent)
00194 
00195         preg.activatePlugin( IFoo, 'foo_plugin' )
00196         preg.activatePlugin( IFoo, 'bar_plugin' )
00197         preg.activatePlugin( IFoo, 'baz_plugin' )
00198 
00199         self.assertRaises( ValueError, preg.movePluginsUp
00200                          , IFoo, ( 'quux_plugin', ) )
00201 
00202         preg.movePluginsUp( IFoo, ( 'bar_plugin', 'baz_plugin' ) )
00203 
00204         idlist = preg.listPluginIds( IFoo )
00205         self.assertEqual( len( idlist ), 3 )
00206 
00207         self.assertEqual( idlist[0], 'bar_plugin' )
00208         self.assertEqual( idlist[1], 'baz_plugin' )
00209         self.assertEqual( idlist[2], 'foo_plugin' )
00210 
00211     def test_movePluginsDown( self ):
00212 
00213         parent = DummyFolder()
00214         foo_plugin = DummyPlugin()
00215         directlyProvides( foo_plugin, ( IFoo, ) )
00216         parent._setObject( 'foo_plugin', foo_plugin )
00217 
00218         bar_plugin = DummyPlugin()
00219         directlyProvides( bar_plugin, ( IFoo, ) )
00220         parent._setObject( 'bar_plugin', bar_plugin )
00221 
00222         baz_plugin = DummyPlugin()
00223         directlyProvides( baz_plugin, ( IFoo, ) )
00224         parent._setObject( 'baz_plugin', baz_plugin )
00225 
00226         preg = self._makeOne().__of__(parent)
00227 
00228         preg.activatePlugin( IFoo, 'foo_plugin' )
00229         preg.activatePlugin( IFoo, 'bar_plugin' )
00230         preg.activatePlugin( IFoo, 'baz_plugin' )
00231 
00232         self.assertRaises( ValueError, preg.movePluginsDown
00233                          , IFoo, ( 'quux_plugin', ) )
00234 
00235         preg.movePluginsDown( IFoo, ( 'foo_plugin', 'bar_plugin' ) )
00236 
00237         idlist = preg.listPluginIds( IFoo )
00238         self.assertEqual( len( idlist ), 3 )
00239 
00240         self.assertEqual( idlist[0], 'baz_plugin' )
00241         self.assertEqual( idlist[1], 'foo_plugin' )
00242         self.assertEqual( idlist[2], 'bar_plugin' )
00243 
00244     def test_getAllPlugins( self ):
00245 
00246         parent = DummyFolder()
00247         foo_plugin = DummyPlugin()
00248         directlyProvides( foo_plugin, ( IFoo, ) )
00249         parent._setObject( 'foo_plugin', foo_plugin )
00250 
00251         bar_plugin = DummyPlugin()
00252         directlyProvides( bar_plugin, ( IFoo, ) )
00253         parent._setObject( 'bar_plugin', bar_plugin )
00254 
00255         baz_plugin = DummyPlugin()
00256         directlyProvides( baz_plugin, ( IFoo, ) )
00257         parent._setObject( 'baz_plugin', baz_plugin )
00258 
00259         preg = self._makeOne().__of__( parent )
00260 
00261         first = preg.getAllPlugins( 'IFoo' )
00262 
00263         self.assertEqual( len( first[ 'active' ] ), 0 )
00264 
00265         self.assertEqual( len( first[ 'available' ] ), 3 )
00266         self.failUnless( 'foo_plugin' in first[ 'available' ] )
00267         self.failUnless( 'bar_plugin' in first[ 'available' ] )
00268         self.failUnless( 'baz_plugin' in first[ 'available' ] )
00269 
00270         preg.activatePlugin( IFoo, 'foo_plugin' )
00271 
00272         second = preg.getAllPlugins( 'IFoo' )
00273 
00274         self.assertEqual( len( second[ 'active' ] ), 1 )
00275         self.failUnless( 'foo_plugin' in second[ 'active' ] )
00276 
00277         self.assertEqual( len( second[ 'available' ] ), 2 )
00278         self.failIf( 'foo_plugin' in second[ 'available' ] )
00279         self.failUnless( 'bar_plugin' in second[ 'available' ] )
00280         self.failUnless( 'baz_plugin' in second[ 'available' ] )
00281 
00282         preg.activatePlugin( IFoo, 'bar_plugin' )
00283         preg.activatePlugin( IFoo, 'baz_plugin' )
00284 
00285         third = preg.getAllPlugins( 'IFoo' )
00286 
00287         self.assertEqual( len( third[ 'active' ] ), 3 )
00288         self.failUnless( 'foo_plugin' in third[ 'active' ] )
00289         self.failUnless( 'bar_plugin' in third[ 'active' ] )
00290         self.failUnless( 'baz_plugin' in third[ 'active' ] )
00291 
00292         self.assertEqual( len( third[ 'available' ] ), 0 )
00293 
00294     def test_removePluginById( self ):
00295 
00296         parent = DummyFolder()
00297         foo_plugin = DummyPlugin()
00298         directlyProvides( foo_plugin, ( IFoo, IBar ) )
00299         parent._setObject( 'foo_plugin', foo_plugin )
00300 
00301         bar_plugin = DummyPlugin()
00302         directlyProvides( bar_plugin, ( IFoo, ) )
00303         parent._setObject( 'bar_plugin', bar_plugin )
00304 
00305         baz_plugin = DummyPlugin()
00306         directlyProvides( baz_plugin, ( IBar, ) )
00307         parent._setObject( 'baz_plugin', baz_plugin )
00308 
00309         preg = self._makeOne().__of__(parent)
00310 
00311         preg.activatePlugin( IFoo, 'foo_plugin' )
00312         preg.activatePlugin( IBar, 'foo_plugin' )
00313         preg.activatePlugin( IFoo, 'bar_plugin' )
00314         preg.activatePlugin( IBar, 'baz_plugin' )
00315 
00316         preg.removePluginById( 'foo_plugin' )
00317 
00318         idlist = preg.listPluginIds( IFoo )
00319         self.assertEqual( len( idlist ), 1 )
00320         self.assertEqual( idlist[0], 'bar_plugin' )
00321 
00322         idlist = preg.listPluginIds( IBar )
00323         self.assertEqual( len( idlist ), 1 )
00324         self.assertEqual( idlist[0], 'baz_plugin' )
00325 
00326 if __name__ == "__main__":
00327     unittest.main()
00328 
00329 def test_suite():
00330     return unittest.TestSuite((
00331         unittest.makeSuite( PluginRegistryTests ),
00332         ))