configurationengine/source/cone/validation/tests/unittest_schemavalidation.py
changeset 3 e7e0ae78773e
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
       
     1 #
       
     2 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 #
       
    16 
       
    17 import unittest
       
    18 import os
       
    19 import StringIO
       
    20 
       
    21 from testautomation.base_testcase import BaseTestCase
       
    22 from cone.public import api, plugin, utils, exceptions
       
    23 from cone.validation import schemavalidation 
       
    24 
       
    25 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    26 
       
    27 CONFML1_NAMESPACE = 'http://www.s60.com/xml/confml/1'
       
    28 CONFML2_NAMESPACE = 'http://www.s60.com/xml/confml/2'
       
    29 
       
    30 class DummyResource(object):
       
    31     def __init__(self, data):
       
    32         self.data = data
       
    33     def read(self):
       
    34         return self.data
       
    35     def close(self):
       
    36         pass
       
    37 
       
    38 class DummyConfiguration(object):
       
    39     def __init__(self, resources):
       
    40         self.resources = resources
       
    41     
       
    42     def get_resource(self, ref):
       
    43         if ref in self.resources:
       
    44             return DummyResource(self.resources[ref])
       
    45         else:
       
    46             raise exceptions.NotFound("No such resource '%s'!" % ref)
       
    47 
       
    48 class TestConfmlSchemaValidation(BaseTestCase, schemavalidation.SchemaValidationTestMixin):
       
    49     
       
    50     def test_valid_confml2_files(self):
       
    51         self.assert_schemavalidation_succeeds(
       
    52             type = 'confml',
       
    53             dir = os.path.join(ROOT_PATH, 'testdata/schema/confml2/valid'),
       
    54             namespace = CONFML2_NAMESPACE)
       
    55     
       
    56     def test_invalid_confml2_files(self):
       
    57         self.assert_schemavalidation_fails(
       
    58             type = 'confml',
       
    59             dir = os.path.join(ROOT_PATH, 'testdata/schema/confml2/invalid'),
       
    60             namespace = CONFML2_NAMESPACE)
       
    61     
       
    62     def test_valid_confml1_files(self):
       
    63         self.assert_schemavalidation_succeeds(
       
    64             type = 'confml',
       
    65             dir = os.path.join(ROOT_PATH, 'testdata/schema/confml1/valid'),
       
    66             namespace = CONFML1_NAMESPACE)
       
    67     
       
    68     def test_invalid_confml1_files(self):
       
    69         self.assert_schemavalidation_fails(
       
    70             type = 'confml',
       
    71             dir = os.path.join(ROOT_PATH, 'testdata/schema/confml1/invalid'),
       
    72             namespace = CONFML1_NAMESPACE)
       
    73     
       
    74     def test_validate_confml_invalid_xml_data(self):
       
    75         config = DummyConfiguration({'foo.confml': 'foo'})
       
    76         problems = schemavalidation.validate_confml_file(config, 'foo.confml')
       
    77         self.assertEquals(len(problems), 1)
       
    78         prob = problems[0]
       
    79         #self.assertEquals(prob.type, api.Problem.TYPE_XML_PROBLEM)
       
    80         self.assertEquals(prob.severity, api.Problem.SEVERITY_ERROR)
       
    81         self.assertEquals(prob.line, 1)
       
    82     
       
    83     def test_validate_confml_invalid_xml_data_but_valid_root(self):
       
    84         REF = 'test.confml'
       
    85         DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
    86             <configuration xmlns="http://www.s60.com/xml/confml/2">
       
    87             <test someattr/>
       
    88             </configuration>""".encode('utf-8')
       
    89         config = DummyConfiguration({REF: DATA})
       
    90         problems = schemavalidation.validate_confml_file(config, REF)
       
    91         self.assertEquals(len(problems), 1)
       
    92         prob = problems[0]
       
    93         #self.assertEquals(prob.type, api.Problem.TYPE_XML_PROBLEM)
       
    94         self.assertEquals(prob.severity, api.Problem.SEVERITY_ERROR)
       
    95         self.assertEquals(prob.line, 3)
       
    96     
       
    97     def test_validate_confml_unsupported_namespace(self):
       
    98         DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
    99             <unsupported xmlns="http://www.test.com/xml/unsupported">
       
   100                 <test someattr="yay"/>
       
   101             </unsupported>""".encode('utf-8')
       
   102         self.assertRaises(exceptions.ConfmlParseError, schemavalidation.validate_confml_data, DATA)
       
   103 
       
   104 
       
   105 
       
   106 
       
   107 class TestImplmlSchemaValidation(BaseTestCase, schemavalidation.SchemaValidationTestMixin):
       
   108     
       
   109     DUMMY1_XSD_DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
   110     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       
   111                targetNamespace="http://www.dummy.com/dummy1"
       
   112                elementFormDefault="qualified">
       
   113         
       
   114         <xs:element name="dummy1">
       
   115             <xs:complexType>
       
   116                 <xs:choice minOccurs="0" maxOccurs="unbounded">
       
   117                     <xs:element name="myElem" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
       
   118                 </xs:choice>
       
   119             </xs:complexType>
       
   120         </xs:element>
       
   121     </xs:schema>"""
       
   122     
       
   123     DUMMY2_XSD_DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
   124     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
       
   125                targetNamespace="http://www.dummy.com/dummy2"
       
   126                elementFormDefault="qualified">
       
   127         
       
   128         <xs:element name="dummy2">
       
   129             <xs:complexType>
       
   130                 <xs:choice minOccurs="0" maxOccurs="unbounded">
       
   131                     <xs:element name="someElem" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
       
   132                 </xs:choice>
       
   133             </xs:complexType>
       
   134         </xs:element>
       
   135     </xs:schema>"""
       
   136     
       
   137     def setUp(self):
       
   138         class MockReader1(plugin.ReaderBase):
       
   139             NAMESPACE = "http://www.dummy.com/dummy1"
       
   140             NAMESPACE_ID = "dummy1ml"
       
   141             FILE_EXTENSIONS = ['dummy1ml']
       
   142             ROOT_ELEMENT_NAME = 'dummy1'
       
   143             @classmethod
       
   144             def get_schema_data(cls):
       
   145                 return self.DUMMY1_XSD_DATA
       
   146         class MockReader2(plugin.ReaderBase):
       
   147             NAMESPACE = "http://www.dummy.com/dummy2"
       
   148             NAMESPACE_ID = "dummy2ml"
       
   149             FILE_EXTENSIONS = ['dummy2ml']
       
   150             ROOT_ELEMENT_NAME = 'dummy2'
       
   151             @classmethod
       
   152             def get_schema_data(cls):
       
   153                 return self.DUMMY2_XSD_DATA
       
   154         class MockReader3(plugin.ReaderBase):
       
   155             NAMESPACE = "http://www.dummy.com/dummy3"
       
   156             NAMESPACE_ID = "dummy3ml"
       
   157             FILE_EXTENSIONS = ['dummy3ml']
       
   158             ROOT_ELEMENT_NAME = 'dummy3'
       
   159         plugin.ImplFactory.set_reader_classes_override([MockReader1, MockReader2, MockReader3])
       
   160     
       
   161     def tearDown(self):
       
   162         plugin.ImplFactory.set_reader_classes_override(None)
       
   163     
       
   164     def test_valid_implml_files(self):
       
   165         self.assert_schemavalidation_succeeds(
       
   166             type = 'implml',
       
   167             dir = os.path.join(ROOT_PATH, 'testdata/schema/implml/valid'))
       
   168     
       
   169     def test_invalid_implml_files(self):
       
   170         self.assert_schemavalidation_fails(
       
   171             type = 'implml',
       
   172             dir = os.path.join(ROOT_PATH, 'testdata/schema/implml/invalid'))
       
   173     
       
   174     def test_validate_implml_invalid_xml_data(self):
       
   175         self.assertRaises(exceptions.XmlParseError, schemavalidation.validate_implml_data, "foo")
       
   176     
       
   177     def test_validate_implml_invalid_xml_data_but_valid_root(self):
       
   178         DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
   179             <implml>
       
   180             <test someattr/>
       
   181             </implml>""".encode('utf-8')
       
   182         self.assertRaises(exceptions.ImplmlParseError, schemavalidation.validate_implml_data, DATA)
       
   183     
       
   184     def test_validate_implml_unsupported_namespace(self):
       
   185         DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
   186             <unsupported xmlns="http://www.test.com/xml/unsupported">
       
   187                 <test someattr="yay"/>
       
   188             </unsupported>""".encode('utf-8')
       
   189         self.assertRaises(exceptions.ImplmlParseError, schemavalidation.validate_implml_data, DATA)
       
   190 
       
   191 if __name__ == '__main__':
       
   192     unittest.main()