Back to index

plone3  3.1.7
test_cachingpolicymgr.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # Copyright (c) 2005 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 """Caching policy manager xml adapter and setup handler unit tests.
00014 
00015 $Id: test_cachingpolicymgr.py 77113 2007-06-26 20:36:26Z yuppie $
00016 """
00017 
00018 import unittest
00019 import Testing
00020 
00021 from OFS.Folder import Folder
00022 
00023 from Products.GenericSetup.testing import BodyAdapterTestCase
00024 from Products.GenericSetup.testing import NodeAdapterTestCase
00025 from Products.GenericSetup.tests.common import BaseRegistryTests
00026 from Products.GenericSetup.tests.common import DummyExportContext
00027 from Products.GenericSetup.tests.common import DummyImportContext
00028 
00029 from Products.CMFCore.testing import ExportImportZCMLLayer
00030 
00031 _CP_XML = """\
00032 <caching-policy name="foo_policy" enable_304s="False" etag_func=""
00033    last_modified="True" max_age_secs="0" mtime_func="object/modified"
00034    must_revalidate="False" no_cache="False" no_store="False"
00035    no_transform="False" predicate="python:1" private="False"
00036    proxy_revalidate="False" public="False" vary=""/>
00037 """
00038 
00039 _CPM_BODY = """\
00040 <?xml version="1.0"?>
00041 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager">
00042  <caching-policy name="foo_policy" enable_304s="False" etag_func=""
00043     last_modified="True" max_age_secs="600" mtime_func="object/modified"
00044     must_revalidate="False" no_cache="False" no_store="False"
00045     no_transform="False"
00046     predicate="python:object.getPortalTypeName() == 'Foo'" private="False"
00047     proxy_revalidate="False" public="False" vary=""/>
00048 </object>
00049 """
00050 
00051 
00052 class CachingPolicyNodeAdapterTests(NodeAdapterTestCase):
00053 
00054     layer = ExportImportZCMLLayer
00055 
00056     def _getTargetClass(self):
00057         from Products.CMFCore.exportimport.cachingpolicymgr \
00058                 import CachingPolicyNodeAdapter
00059 
00060         return CachingPolicyNodeAdapter
00061 
00062     def setUp(self):
00063         from Products.CMFCore.CachingPolicyManager import CachingPolicy
00064 
00065         NodeAdapterTestCase.setUp(self)
00066         self._obj = CachingPolicy('foo_policy', max_age_secs=0)
00067         self._XML = _CP_XML
00068 
00069 
00070 class CachingPolicyManagerXMLAdapterTests(BodyAdapterTestCase):
00071 
00072     layer = ExportImportZCMLLayer
00073 
00074     def _getTargetClass(self):
00075         from Products.CMFCore.exportimport.cachingpolicymgr \
00076                 import CachingPolicyManagerXMLAdapter
00077 
00078         return CachingPolicyManagerXMLAdapter
00079 
00080     def _populate(self, obj):
00081         obj.addPolicy('foo_policy',
00082                       'python:object.getPortalTypeName() == \'Foo\'',
00083                       'object/modified', 600, 0, 0, 0, '', '')
00084 
00085     def setUp(self):
00086         from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
00087 
00088         BodyAdapterTestCase.setUp(self)
00089         self._obj = CachingPolicyManager()
00090         self._BODY = _CPM_BODY
00091 
00092 
00093 class _CachingPolicyManagerSetup(BaseRegistryTests):
00094 
00095     POLICY_ID = 'policy_id'
00096     PREDICATE = "python:object.getId() == 'foo'"
00097     MTIME_FUNC = "object/modified"
00098     MAX_AGE_SECS = 60
00099     VARY = "Test"
00100     ETAG_FUNC = "object/getETag"
00101     S_MAX_AGE_SECS = 120
00102     PRE_CHECK = 42
00103     POST_CHECK = 43
00104 
00105     _EMPTY_EXPORT = """\
00106 <?xml version="1.0"?>
00107 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager"/>
00108 """
00109 
00110     _WITH_POLICY_EXPORT = """\
00111 <?xml version="1.0"?>
00112 <object name="caching_policy_manager" meta_type="CMF Caching Policy Manager">
00113  <caching-policy name="%s" enable_304s="True"
00114     etag_func="%s" last_modified="False" max_age_secs="%d"
00115     mtime_func="%s" must_revalidate="True" no_cache="True"
00116     no_store="True" no_transform="True" post_check="%d" pre_check="%d"
00117     predicate="%s" private="True"
00118     proxy_revalidate="True" public="True" s_max_age_secs="%d" vary="%s"/>
00119 </object>
00120 """ % (POLICY_ID, ETAG_FUNC, MAX_AGE_SECS, MTIME_FUNC, POST_CHECK, PRE_CHECK,
00121        PREDICATE, S_MAX_AGE_SECS, VARY)
00122 
00123     def _initSite(self, with_policy=False):
00124         from Products.CMFCore.CachingPolicyManager import CachingPolicyManager
00125 
00126         self.root.site = Folder(id='site')
00127         site = self.root.site
00128         mgr = CachingPolicyManager()
00129         site._setObject( mgr.getId(), mgr )
00130 
00131         if with_policy:
00132             mgr.addPolicy( policy_id=self.POLICY_ID
00133                          , predicate=self.PREDICATE
00134                          , mtime_func=self.MTIME_FUNC
00135                          , max_age_secs=self.MAX_AGE_SECS
00136                          , no_cache=True
00137                          , no_store=True
00138                          , must_revalidate=True
00139                          , vary=self.VARY
00140                          , etag_func=self.ETAG_FUNC
00141                          , s_max_age_secs=self.S_MAX_AGE_SECS
00142                          , proxy_revalidate=True
00143                          , public=True
00144                          , private=True
00145                          , no_transform=True
00146                          , enable_304s=True
00147                          , last_modified=False
00148                          , pre_check=self.PRE_CHECK
00149                          , post_check=self.POST_CHECK
00150                          )
00151 
00152         return site
00153 
00154 
00155 class exportCachingPolicyManagerTests(_CachingPolicyManagerSetup):
00156 
00157     layer = ExportImportZCMLLayer
00158 
00159     def test_empty(self):
00160         from Products.CMFCore.exportimport.cachingpolicymgr \
00161                 import exportCachingPolicyManager
00162 
00163         site = self._initSite(with_policy=False)
00164         context = DummyExportContext(site)
00165         exportCachingPolicyManager(context)
00166 
00167         self.assertEqual(len(context._wrote), 1)
00168         filename, text, content_type = context._wrote[0]
00169         self.assertEqual(filename, 'cachingpolicymgr.xml')
00170         self._compareDOM(text, self._EMPTY_EXPORT)
00171         self.assertEqual(content_type, 'text/xml')
00172 
00173     def test_with_policy(self):
00174         from Products.CMFCore.exportimport.cachingpolicymgr \
00175                 import exportCachingPolicyManager
00176 
00177         site = self._initSite(with_policy=True)
00178         context = DummyExportContext(site)
00179         exportCachingPolicyManager(context)
00180 
00181         self.assertEqual(len(context._wrote), 1)
00182         filename, text, content_type = context._wrote[0]
00183         self.assertEqual(filename, 'cachingpolicymgr.xml')
00184         self._compareDOM(text, self._WITH_POLICY_EXPORT)
00185         self.assertEqual(content_type, 'text/xml')
00186 
00187 
00188 class importCachingPolicyManagerTests(_CachingPolicyManagerSetup):
00189 
00190     layer = ExportImportZCMLLayer
00191 
00192     def test_normal(self):
00193         from Products.CMFCore.exportimport.cachingpolicymgr \
00194                 import importCachingPolicyManager
00195 
00196         site = self._initSite(with_policy=False)
00197         cpm = site.caching_policy_manager
00198         self.assertEqual(len(cpm.listPolicies()), 0)
00199 
00200         context = DummyImportContext(site)
00201         context._files['cachingpolicymgr.xml'] = self._WITH_POLICY_EXPORT
00202         importCachingPolicyManager(context)
00203 
00204         self.assertEqual(len(cpm.listPolicies()), 1)
00205         policy_id, policy = cpm.listPolicies()[0]
00206         self.assertEqual(policy.getPolicyId(), self.POLICY_ID)
00207         self.assertEqual(policy.getPredicate(), self.PREDICATE)
00208         self.assertEqual(policy.getMTimeFunc(), self.MTIME_FUNC)
00209         self.assertEqual(policy.getVary(), self.VARY)
00210         self.assertEqual(policy.getETagFunc(), self.ETAG_FUNC)
00211         self.assertEqual(policy.getMaxAgeSecs(), self.MAX_AGE_SECS)
00212         self.assertEqual(policy.getSMaxAgeSecs(), self.S_MAX_AGE_SECS)
00213         self.assertEqual(policy.getPreCheck(), self.PRE_CHECK)
00214         self.assertEqual(policy.getPostCheck(), self.POST_CHECK)
00215         self.assertEqual(policy.getLastModified(), False)
00216         self.assertEqual(policy.getNoCache(), True)
00217         self.assertEqual(policy.getNoStore(), True)
00218         self.assertEqual(policy.getMustRevalidate(), True)
00219         self.assertEqual(policy.getProxyRevalidate(), True)
00220         self.assertEqual(policy.getNoTransform(), True)
00221         self.assertEqual(policy.getPublic(), True)
00222         self.assertEqual(policy.getPrivate(), True)
00223         self.assertEqual(policy.getEnable304s(), True)
00224 
00225 
00226 def test_suite():
00227     return unittest.TestSuite((
00228         unittest.makeSuite(CachingPolicyNodeAdapterTests),
00229         unittest.makeSuite(CachingPolicyManagerXMLAdapterTests),
00230         unittest.makeSuite(exportCachingPolicyManagerTests),
00231         unittest.makeSuite(importCachingPolicyManagerTests),
00232         ))
00233 
00234 if __name__ == '__main__':
00235     from Products.CMFCore.testing import run
00236     run(test_suite())