def setUp(self):
        unittest.TestCase.setUp(self)

        self.h = VerticalLayersXMLHandler()

        self.obj1 = VerticalLayers(Material.pure(29), Material.pure(30), None,
                                   tilt_rad=1.1, rotation_rad=2.2)
        self.obj1.add_layer(Material.pure(31), 500.0)

        self.obj2 = VerticalLayers(Material.pure(29), Material.pure(30))
        self.obj2.add_layer(Material.pure(31), 500.0)
        self.obj2.depth_m = 400.0

        etree.register_namespace('mc', 'http://pymontecarlo.sf.net')
        source = BytesIO(b'<mc:verticalLayers xmlns:mc="http://pymontecarlo.sf.net" depth="inf" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="2" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material><mc:material _index="3" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material></materials><leftSubstrate material="2" /><rightSubstrate material="3" /><layers><layer material="1" thickness="500.0" /></layers></mc:verticalLayers>')
        self.element1 = etree.parse(source).getroot()

        etree.register_namespace('mc', 'http://pymontecarlo.sf.net')
        source = BytesIO(b'<mc:verticalLayers xmlns:mc="http://pymontecarlo.sf.net" depth="400.0" rotation="0.0" tilt="0.0"><materials><mc:material _index="1" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="3" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material></materials><leftSubstrate material="1" /><rightSubstrate material="3" /><layers><layer material="2" thickness="500.0" /></layers></mc:verticalLayers>')
        self.element2 = etree.parse(source).getroot()
class TestVerticalLayersXMLHandler(unittest.TestCase):

    def setUp(self):
        unittest.TestCase.setUp(self)

        self.h = VerticalLayersXMLHandler()

        self.obj1 = VerticalLayers(Material.pure(29), Material.pure(30), None,
                                   tilt_rad=1.1, rotation_rad=2.2)
        self.obj1.add_layer(Material.pure(31), 500.0)

        self.obj2 = VerticalLayers(Material.pure(29), Material.pure(30))
        self.obj2.add_layer(Material.pure(31), 500.0)
        self.obj2.depth_m = 400.0

        etree.register_namespace('mc', 'http://pymontecarlo.sf.net')
        source = BytesIO(b'<mc:verticalLayers xmlns:mc="http://pymontecarlo.sf.net" depth="inf" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="2" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material><mc:material _index="3" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material></materials><leftSubstrate material="2" /><rightSubstrate material="3" /><layers><layer material="1" thickness="500.0" /></layers></mc:verticalLayers>')
        self.element1 = etree.parse(source).getroot()

        etree.register_namespace('mc', 'http://pymontecarlo.sf.net')
        source = BytesIO(b'<mc:verticalLayers xmlns:mc="http://pymontecarlo.sf.net" depth="400.0" rotation="0.0" tilt="0.0"><materials><mc:material _index="1" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material><mc:material _index="2" density="5910.0" name="Gallium"><composition><element weightFraction="1.0" z="31" /></composition></mc:material><mc:material _index="3" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material></materials><leftSubstrate material="1" /><rightSubstrate material="3" /><layers><layer material="2" thickness="500.0" /></layers></mc:verticalLayers>')
        self.element2 = etree.parse(source).getroot()

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def testcan_parse(self):
        self.assertTrue(self.h.can_parse(self.element1))
        self.assertTrue(self.h.can_parse(self.element2))

    def testparse(self):
        # Vertical layers 1
        obj = self.h.parse(self.element1)

        self.assertEqual('Copper', str(obj.left_substrate.material))
        self.assertEqual('Zinc', str(obj.right_substrate.material))

        self.assertEqual('Gallium', str(obj.layers[0].material))
        self.assertAlmostEqual(500.0, obj.layers[0].thickness_m, 4)

        self.assertAlmostEqual(1.1, obj.tilt_rad, 4)
        self.assertAlmostEqual(2.2, obj.rotation_rad, 4)

        # Vertical layers 2
        obj = self.h.parse(self.element2)

        self.assertAlmostEqual(400.0, obj.depth_m, 4)

        self.assertEqual('Copper', str(obj.left_substrate.material))
        self.assertEqual('Zinc', str(obj.right_substrate.material))

        self.assertEqual('Gallium', str(obj.layers[0].material))
        self.assertAlmostEqual(500.0, obj.layers[0].thickness_m, 4)

        self.assertAlmostEqual(0.0, obj.tilt_rad, 4)
        self.assertAlmostEqual(0.0, obj.rotation_rad, 4)

    def testcan_convert(self):
        self.assertTrue(self.h.can_convert(self.obj1))
        self.assertTrue(self.h.can_convert(self.obj2))

    def testconvert(self):
        # Vertical layers 1
        element = self.h.convert(self.obj1)

        self.assertAlmostEqual(1.1, float(element.get('tilt')), 4)
        self.assertAlmostEqual(2.2, float(element.get('rotation')), 4)
        self.assertEqual(3, len(list(element.find('materials'))))
        self.assertEqual(1, len(list(element.find('layers'))))

        # Vertical layers 2
        element = self.h.convert(self.obj2)

        self.assertAlmostEqual(0.0, float(element.get('tilt')), 4)
        self.assertAlmostEqual(0.0, float(element.get('rotation')), 4)
        self.assertEqual(3, len(list(element.find('materials'))))
        self.assertEqual(1, len(list(element.find('layers'))))