Package elisa :: Package core :: Package tests :: Module component_test_case
[hide private]
[frames] | no frames]

Source Code for Module elisa.core.tests.component_test_case

  1  # Elisa - Home multimedia server 
  2  # Copyright (C) 2006-2008 Fluendo Embedded S.L. (www.fluendo.com). 
  3  # All rights reserved. 
  4  # 
  5  # This file is available under one of two license agreements. 
  6  # 
  7  # This file is licensed under the GPL version 3. 
  8  # See "LICENSE.GPL" in the root of this distribution including a special 
  9  # exception to use Elisa with Fluendo's plugins. 
 10  # 
 11  # The GPL part of Elisa is also available under a commercial licensing 
 12  # agreement from Fluendo. 
 13  # See "LICENSE.Elisa" in the root directory of this distribution package 
 14  # for details on that license. 
 15   
 16  from elisa.core.tests import elisa_test_case 
 17  import sys, os 
 18  import inspect 
 19  from twisted.internet import defer 
 20   
21 -class ComponentTestCase(elisa_test_case.ElisaTestCase):
22 23 component_class = None 24 component_path = '.' 25 logged = False 26
27 - def __init__(self, methodName='runTest'):
28 elisa_test_case.ElisaTestCase.__init__(self, methodName=methodName) 29 30 component = self.get_component() 31 if component is None: 32 error = "Component %r not found" % self.component_class 33 self.warning(error) 34 self.skip = error 35 elif not self.logged: 36 self.info("Testing component: %r", component) 37 self.__class__.logged = True
38
39 - def get_component(self):
40 component = None 41 if self.component_class: 42 if isinstance(self.component_class, basestring): 43 44 # setup path 45 sys_path = os.getcwd() 46 if self.component_path == '.': 47 sys_path = inspect.getsourcefile(self.__class__) 48 sys_path = os.path.dirname(sys_path) 49 elif self.component_path.startswith('..'): 50 sys_path = inspect.getsourcefile(self.__class__) 51 sys_path = os.path.dirname(sys_path) 52 sys_path = os.path.dirname(sys_path[:-1]) 53 index = self.component_path.find(os.path.sep) 54 if index > -1: 55 dirname = self.component_path[index+1:] 56 sys_path = os.path.join(sys_path, dirname) 57 58 self.debug('Appending %r to sys.path', sys_path) 59 sys.path.insert(0, sys_path) 60 61 # import 62 path = self.component_class 63 mod_class = path.split(':') 64 65 try: 66 module = __import__(mod_class[0], globals(), 67 locals(), [mod_class[1]]) 68 except ImportError: 69 pass 70 else: 71 Class = getattr(module, mod_class[1]) 72 # avoid unecessary future imports 73 self.component_class = Class 74 component = Class() 75 76 # clean sys path 77 sys.path.remove(sys_path) 78 79 else: 80 component = self.component_class() 81 82 # TODO: perform extra stuff that's normally done by the 83 # plugin registry create_component() method 84 from elisa.core import common 85 app = common.application 86 if app: 87 self.debug("Initializing %r component", component.name) 88 component.load_config(app.config) 89 dfr = defer.maybeDeferred(component.initialize) 90 dfr.addCallback(lambda result: component) 91 92 return dfr 93 else: 94 self.debug("Application not set, skipping component initialization") 95 return component
96
97 - def setUp(self):
98 elisa_test_case.ElisaTestCase.setUp(self) 99 dfr = defer.maybeDeferred(self.get_component) 100 dfr.addCallback(lambda result: setattr(self, 'component', result)) 101 102 return dfr
103
104 - def tearDown(self):
105 return self.component.clean()
106