configurationengine/source/plugins/example/ConeExamplePlugin/examplemlplugin/tests/unittest_exampleml_reader.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     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 sys, os, unittest
       
    18 import __init__
       
    19 
       
    20 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    21 
       
    22 try:
       
    23     from cElementTree import ElementTree
       
    24 except ImportError:
       
    25     try:    
       
    26         from elementtree import ElementTree
       
    27     except ImportError:
       
    28         try:
       
    29             from xml.etree import cElementTree as ElementTree
       
    30         except ImportError:
       
    31             from xml.etree import ElementTree
       
    32 
       
    33 from cone.public import exceptions, plugin, api
       
    34 
       
    35 from examplemlplugin.exampleml_reader import ExamplemlReader
       
    36 from examplemlplugin.exampleml_model import Output
       
    37 
       
    38 class TestExamplemlReader(unittest.TestCase):
       
    39     
       
    40     NAMESPACE = ExamplemlReader.NAMESPACE
       
    41     
       
    42     def setUp(self):
       
    43         self.reader = ExamplemlReader()
       
    44     
       
    45     def assert_read_output_equals(self, data, expected):
       
    46         etree = ElementTree.fromstring(data)
       
    47         output = self.reader._read_output_elem(etree)
       
    48         self.assertEquals(expected, output)
       
    49     
       
    50     def test_read_output(self):
       
    51         data = """<output file="test1.txt" encoding="UTF-16">Test</output>"""
       
    52         self.assert_read_output_equals(data, Output('test1.txt', 'UTF-16', 'Test'))
       
    53         
       
    54         data = """<output file="test2.txt">Test</output>"""
       
    55         self.assert_read_output_equals(data, Output('test2.txt', 'UTF-8', 'Test'))
       
    56         
       
    57         data = """<output file="test3.txt"/>"""
       
    58         self.assert_read_output_equals(data, Output('test3.txt', 'UTF-8', ''))
       
    59         
       
    60         data = """<output/>"""
       
    61         self.assertRaises(exceptions.ParseError, self.reader._read_output_elem, ElementTree.fromstring(data))
       
    62     
       
    63     def test_read_outputs(self):
       
    64         data = """<?xml version="1.0" encoding="UTF-8"?>
       
    65                 <printml xmlns="%s">
       
    66                     <output file="test1.txt" encoding="UTF-16-LE">Test 1</output>
       
    67                     <output file="test2.txt">Test 2</output>
       
    68                     <output file="test3.txt"/>
       
    69                 </printml>""" % self.NAMESPACE
       
    70         outputs = self.reader._read_outputs(ElementTree.fromstring(data))
       
    71         self.assertEquals(outputs,
       
    72                           [Output('test1.txt', 'UTF-16-LE', 'Test 1'),
       
    73                            Output('test2.txt', 'UTF-8', 'Test 2'),
       
    74                            Output('test3.txt', 'UTF-8', '')])