configurationengine/source/cone/public/tests/unittest_plugin_reader.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    15 #
    15 #
    16 
    16 
    17 import unittest
    17 import unittest
    18 import os
    18 import os
    19 import logging
    19 import logging
    20 import __init__
    20 
    21 from cone.public import *
    21 from cone.public import *
    22 from cone.public import _plugin_reader
    22 from cone.public import _plugin_reader
    23 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    23 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    24 
       
    25 
       
    26 class MockImpl(plugin.ImplBase):
       
    27     def __init__(self, data):
       
    28         self.data = data
       
    29         self.generate_invoked = False
       
    30     
       
    31     @classmethod
       
    32     def create(cls, resource_ref, configuration, data):
       
    33         impl = cls(data)
       
    34         plugin.ImplBase.__init__(impl, resource_ref, configuration)
       
    35         return impl
       
    36     
       
    37     def generate(self, context=None):
       
    38         if context and hasattr(context,'objects'):
       
    39             context.objects.append(self) 
       
    40         self.generate_invoked = True
       
    41     
       
    42     def __repr__(self):
       
    43         return "MockImpl(%r)" % self.data
       
    44     
       
    45     def __eq__(self, other):
       
    46         if type(self) == type(other):
       
    47             return self.data == other.data
       
    48         else:
       
    49             return False
       
    50     
       
    51     def __ne__(self, other):
       
    52         return not (self == other)
       
    53         
       
    54     def __lt__(self, other):
       
    55         if type(self) == type(other):
       
    56             return self.data < other.data
       
    57         else:
       
    58             return False
       
    59 
       
    60 class MockReaderBase(plugin.ReaderBase):
       
    61     @classmethod
       
    62     def read_impl(cls, resource_ref, configuration, root_elem):
       
    63         data = [cls.__name__, resource_ref]
       
    64         for elem in root_elem.findall('{%s}elem' % cls.NAMESPACE):
       
    65             data.append(elem.attrib)
       
    66         return MockImpl.create(resource_ref, configuration, data)
       
    67 
       
    68 class MockReader(MockReaderBase):
       
    69     NAMESPACE = "http://www.test.com/xml/1"
       
    70     NAMESPACE_ID = "mock"
       
    71     ROOT_ELEMENT_NAME = "impl"
       
    72     FILE_EXTENSIONS = ['mockml']
    24 
    73 
    25 class ImplTest(plugin.ImplBase):
    74 class ImplTest(plugin.ImplBase):
    26     def __init__(self,ref,configuration):
    75     def __init__(self,ref,configuration):
    27         plugin.ImplBase.__init__(self,ref,configuration)
    76         plugin.ImplBase.__init__(self,ref,configuration)
    28         self.generate_invoked = False
    77         self.generate_invoked = False
    45         pass
    94         pass
    46 
    95 
    47 class TestCommonNamespaceHandling(unittest.TestCase):
    96 class TestCommonNamespaceHandling(unittest.TestCase):
    48     
    97     
    49     def setUp(self):
    98     def setUp(self):
    50         pass
    99         plugin.ImplFactory.set_reader_classes_override([MockReader])
    51         
   100     
    52         
       
    53     def tearDown(self):
   101     def tearDown(self):
    54         pass
   102         plugin.ImplFactory.set_reader_classes_override(None)
    55     
   103     
    56     def test_implcontainer_reader_get_condition(self):
   104     def test_implcontainer_reader_get_condition(self):
    57         root = utils.etree.fromstring("<container/>")
   105         root = utils.etree.fromstring("<container/>")
    58         self.assertEquals(plugin.ImplContainerReader.get_condition(root), None)
   106         self.assertEquals(plugin.ImplContainerReader.get_condition(root), None)
    59         
   107         
    79             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   127             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
    80             </container>
   128             </container>
    81             """
   129             """
    82         container = plugin.ImplContainerReader.read_implementation(xml_data)
   130         container = plugin.ImplContainerReader.read_implementation(xml_data)
    83         self.assertTrue(isinstance(container, plugin.ImplContainer))
   131         self.assertTrue(isinstance(container, plugin.ImplContainer))
       
   132     
       
   133     def test_get_reader_for_namespace(self):
       
   134         self.assertEquals(plugin.ReaderBase.get_reader_for_namespace('http://www.symbianfoundation.org/Foo'), None)
       
   135         self.assertEquals(plugin.ReaderBase.get_reader_for_namespace('http://www.symbianfoundation.org/xml/implml/1'), 
       
   136                           plugin.ImplContainerReader)
       
   137 
       
   138     def test_read_container_via_common_readerbase(self):
       
   139         prj = api.Project(api.Storage.open(os.path.join(ROOT_PATH,'testdata')))
       
   140         config = prj.create_configuration("test.confml", True)
       
   141         container = plugin.read_impl_from_location('layer1/implml/test.implml', config, 4)
       
   142         self.assertEquals(container.invocation_phase(), 'normal')
       
   143         self.assertEquals(container.path, 'layer1/implml/test.implml')
       
   144         self.assertEquals(container.lineno, 4)
       
   145         self.assertEquals(container[0].invocation_phase(), 'normal')
       
   146         self.assertEquals(container[0].path, 'layer1/implml/test.implml')
       
   147         self.assertEquals(container[0].lineno, 5)
       
   148 
       
   149 
       
   150     def test_read_containers_from_location(self):
       
   151         prj = api.Project(api.Storage.open(os.path.join(ROOT_PATH,'testdata')))
       
   152         config = prj.create_configuration("test.confml", True)
       
   153         
       
   154         container = plugin.ImplContainerReader.read_impl_from_location('layer1/implml/test.implml', config, 4)
       
   155         self.assertEquals(container.invocation_phase(), 'normal')
       
   156         self.assertEquals(container.path, 'layer1/implml/test.implml')
       
   157         self.assertEquals(container.lineno, 4)
       
   158         self.assertEquals(container[0].invocation_phase(), 'normal')
       
   159         self.assertEquals(container[0].path, 'layer1/implml/test.implml')
       
   160         self.assertEquals(container[0].lineno, 5)
    84 
   161 
    85     def test_get_test_container_with_sub_containers(self):
   162     def test_get_test_container_with_sub_containers(self):
    86         xml_data = """<?xml version="1.0"?>
   163         xml_data = """<?xml version="1.0"?>
    87             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   164             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
    88               <container />
   165               <container />
    96         self.assertTrue(isinstance(container, plugin.ImplContainer))
   173         self.assertTrue(isinstance(container, plugin.ImplContainer))
    97         self.assertEquals(len(container), 3)
   174         self.assertEquals(len(container), 3)
    98     
   175     
    99     def test_containers_with_phases(self):
   176     def test_containers_with_phases(self):
   100         xml_data = """<?xml version="1.0"?>
   177         xml_data = """<?xml version="1.0"?>
   101             <implml:container xmlns:implml="http://www.symbianfoundation.org/xml/implml/1">
   178             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   102               <implml:container />
   179               <mock xmlns="http://www.test.com/xml/1"/>
   103               <implml:container />
   180               <container>
       
   181                 <mock xmlns="http://www.test.com/xml/1"/>
       
   182               </container>
   104               <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   183               <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   105                 <phase name="post"/>
   184                 <phase name="post"/>
       
   185                 <mock xmlns="http://www.test.com/xml/1"/>
   106               </container>
   186               </container>
   107             </implml:container>
   187             </container>
   108             """
   188             """
   109         container = plugin.ImplContainerReader.read_implementation(xml_data)
   189         container = plugin.ImplContainerReader.read_implementation(xml_data)
   110         self.assertEquals(container.invocation_phase(), ['post','normal'])
   190         self.assertEquals(container[0].invocation_phase(), 'normal')
   111         self.assertEquals(container[2].invocation_phase(), ['post'])
   191         self.assertEquals(container[1][0].invocation_phase(), 'normal')
       
   192         self.assertEquals(container[2][0].invocation_phase(), 'post')
   112     
   193     
   113     def test_containers_with_tags(self):
   194     def test_containers_with_tags(self):
   114         xml_data = """<?xml version="1.0"?>
   195         xml_data = """<?xml version="1.0"?>
   115             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   196             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   116               <tag name="target" value="rofs2"/>
   197               <tag name="target" value="rofs2"/>
   117               <tag name="foobar" value="test"/>
   198               <tag name="foobar" value="test"/>
   118               <container />
   199               
   119               <container />
   200               <container>
       
   201                 <mock xmlns="http://www.test.com/xml/1"/>
       
   202               </container>
       
   203               
   120               <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   204               <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   121                 <tag name="target" value="rofs3"/>
   205                 <tag name="target" value="rofs3"/>
   122                 <phase name="post"/>
   206                 <mock xmlns="http://www.test.com/xml/1"/>
   123               </container>
   207               </container>
   124             </container>
   208             </container>
   125             """
   209             """
   126         container = plugin.ImplContainerReader.read_implementation(xml_data)
   210         container = plugin.ImplContainerReader.read_implementation(xml_data)
   127         self.assertEquals(container[2].get_tags(), {'target': ['rofs3']})
   211         self.assertEquals(container[0][0].get_tags(), {'target': ['rofs2'],
   128         self.assertEquals(container.get_tags(), {'target': ['rofs2','rofs3'],
   212                                                        'foobar': ['test']})
   129                                                  'foobar': ['test']})
   213         self.assertEquals(container[1][0].get_tags(), {'target': ['rofs3']})
       
   214     
       
   215     def test_read_container_impl_line_numbers(self):
       
   216         xml_data = """<?xml version="1.0"?>
       
   217             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
       
   218                 <container>
       
   219                     <mock xmlns="http://www.test.com/xml/1"/>
       
   220                 </container>
       
   221               
       
   222                 <container>
       
   223                     <mock xmlns="http://www.test.com/xml/1"/>
       
   224                     <container>
       
   225                         <mock xmlns="http://www.test.com/xml/1"/>
       
   226                         <mock xmlns="http://www.test.com/xml/1"/>
       
   227                     </container>
       
   228                 </container>
       
   229             </container>
       
   230         """
       
   231         container = plugin.ImplContainerReader.read_implementation(xml_data)
       
   232         self.assertEquals(container[0].lineno, 3)
       
   233         self.assertEquals(container[0][0].lineno, 4)
       
   234         self.assertEquals(container[1].lineno, 7)
       
   235         self.assertEquals(container[1][0].lineno, 8)
       
   236         self.assertEquals(container[1][1].lineno, 9)
       
   237         self.assertEquals(container[1][1][0].lineno, 10)
       
   238         self.assertEquals(container[1][1][1].lineno, 11)
       
   239     
       
   240     def test_container_common_element_inheritance(self):
       
   241         xml_data = """<?xml version="1.0"?>
       
   242             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
       
   243                 
       
   244                 <!-- [0] -->
       
   245                 <mock xmlns="http://www.test.com/xml/1"/>
       
   246                 
       
   247                 <!-- [1] -->
       
   248                 <container>
       
   249                     <tag name="target" value="rofs2"/>
       
   250                     <tag name="target" value="rofs3"/>
       
   251                     <tag name="foo" value="bar"/>
       
   252                     <phase name="post"/>
       
   253                     <settingRefsOverride>
       
   254                         <settingRef value="Foo.Bar"/>
       
   255                         <settingRef value="Foo.Baz"/>
       
   256                     </settingRefsOverride>
       
   257                     <outputRootDir value="/foo/root"/>
       
   258                     <outputSubDir value="foosubdir"/>
       
   259                     
       
   260                     <!-- [1][0] -->
       
   261                     <mock xmlns="http://www.test.com/xml/1"/>
       
   262                     
       
   263                     <!-- [1][1] -->
       
   264                     <container>
       
   265                         <!-- [1][1][0] -->
       
   266                         <mock xmlns="http://www.test.com/xml/1"/>
       
   267                         
       
   268                         <!-- [1][1][1] -->
       
   269                         <container>
       
   270                             <!-- [1][1][1][0] -->
       
   271                             <mock xmlns="http://www.test.com/xml/1"/>
       
   272                         </container>
       
   273                     </container>
       
   274                     
       
   275                     <!-- [1][2] -->
       
   276                     <container>
       
   277                         <tag name="target" value="core"/>
       
   278                         <tag name="foo" value="baz"/>
       
   279                         <phase name="pre"/>
       
   280                         <settingRefsOverride refsIrrelevant="true"/>
       
   281                         <outputRootDir value="/foo/root2"/>
       
   282                         <outputSubDir value="foosubdir2"/>
       
   283                         
       
   284                         <!-- [1][2][0] -->
       
   285                         <mock xmlns="http://www.test.com/xml/1"/>
       
   286                         
       
   287                         <!-- [1][2][1] -->
       
   288                         <container>
       
   289                             <!-- [1][2][1][0] -->
       
   290                             <mock xmlns="http://www.test.com/xml/1"/>
       
   291                         </container>
       
   292                     </container>
       
   293                 </container>
       
   294             </container>
       
   295             """
       
   296         container = plugin.ImplContainerReader.read_implementation(xml_data)
       
   297         
       
   298         # First impl, with all defaults
       
   299         impl = container[0]
       
   300         self.assertEquals(impl.get_tags(), {})
       
   301         self.assertEquals(impl.invocation_phase(), 'normal')
       
   302         self.assertEquals(impl.get_refs(), None)
       
   303         self.assertEquals(impl.output, '')
       
   304         
       
   305         # The sub-container has things overridden, check that they
       
   306         # are all inherited correctly downwards in the implementation
       
   307         # tree
       
   308         def assert_is_expected(impl):
       
   309             self.assertEquals(impl.get_tags(), {'target': ['rofs2', 'rofs3'], 'foo': ['bar']})
       
   310             self.assertEquals(impl.invocation_phase(), 'post')
       
   311             self.assertEquals(impl.get_refs(), ['Foo.Bar', 'Foo.Baz'])
       
   312             self.assertEquals(impl.output, '/foo/root/foosubdir')
       
   313             self.assertEquals(impl.output_subdir, 'foosubdir')
       
   314         assert_is_expected(container[1][0])
       
   315         assert_is_expected(container[1][1][0])
       
   316         assert_is_expected(container[1][1][1][0])
       
   317         
       
   318         # The sub-container's second sub-container has things overridden
       
   319         # again, so check that those are correct
       
   320         def assert_is_expected_2(impl):
       
   321             self.assertEquals(impl.get_tags(), {'target': ['core'], 'foo': ['baz']})
       
   322             self.assertEquals(impl.invocation_phase(), 'pre')
       
   323             self.assertEquals(impl.get_refs(), None)
       
   324             self.assertEquals(impl.output, '/foo/root2/foosubdir2')
       
   325         assert_is_expected_2(container[1][2][0])
       
   326         assert_is_expected_2(container[1][2][1][0])
   130 
   327 
   131     def test_tempfeature_definitions(self):
   328     def test_tempfeature_definitions(self):
   132         xml_data = """<?xml version="1.0"?>
   329         xml_data = """<?xml version="1.0"?>
   133             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   330             <container xmlns="http://www.symbianfoundation.org/xml/implml/1">
   134                 <tempVariable ref="TempFeature.root" value="true"/>
   331                 <tempVariable ref="TempFeature.root" value="true"/>