1 |
joey |
2122 |
# Copyright (c) 2004 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Martin Schulze <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software; you can redistribute it and/or modify |
6 |
|
|
# it under the terms of the GNU General Public License as published by |
7 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
# (at your option) any later version. |
9 |
|
|
# |
10 |
|
|
# This program is distributed in the hope that it will be useful, |
11 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
# GNU General Public License for more details. |
14 |
|
|
# |
15 |
|
|
# You should have received a copy of the GNU General Public License |
16 |
|
|
# along with this program; if not, write to the Free Software |
17 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
|
|
19 |
|
|
""" |
20 |
|
|
Test for the domutils module |
21 |
|
|
|
22 |
|
|
""" |
23 |
|
|
|
24 |
|
|
__version__ = "$Revision$" |
25 |
|
|
# $Source$ |
26 |
|
|
# $Id$ |
27 |
|
|
|
28 |
|
|
import unittest |
29 |
|
|
import xml.dom.minidom |
30 |
|
|
|
31 |
|
|
import adjustpath |
32 |
|
|
|
33 |
|
|
from Extensions.wms.domutils import getElementByName, getElementsByName |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
class TestDOMutils(unittest.TestCase): |
37 |
|
|
""" |
38 |
|
|
Defines a test environment for the class WMSCapabilities. |
39 |
|
|
""" |
40 |
|
|
|
41 |
|
|
root = None |
42 |
|
|
|
43 |
|
|
def setUp(self): |
44 |
|
|
""" |
45 |
|
|
Set up the XML test string, parse it into a DOM element and |
46 |
|
|
hook it to the internal root variable. |
47 |
|
|
""" |
48 |
|
|
data = "\n".join(['<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>', |
49 |
|
|
'<!DOCTYPE WMT_MS_Capabilities SYSTEM "http://www.digitalearth.gov/wmt/xml/capabilities_1_0_8.dtd">', |
50 |
|
|
'<WMT_MS_Capabilities version="1.0.8" updateSequence="0">', |
51 |
|
|
' <Layer>', |
52 |
|
|
' <Name>Osnabrueck</Name>', |
53 |
|
|
' <Layer>', |
54 |
|
|
' <Name>gewaesser</Name>', |
55 |
|
|
' <Layer queryable="0" opaque="0" cascaded="0">', |
56 |
|
|
' <Name>gewaesserpolyl</Name>', |
57 |
|
|
' </Layer>', |
58 |
|
|
' </Layer>', |
59 |
|
|
' <Layer queryable="0" opaque="0" cascaded="0">', |
60 |
|
|
' <Name>gruenflaechen</Name>', |
61 |
|
|
' </Layer>', |
62 |
|
|
' </Layer>', |
63 |
|
|
'</WMT_MS_Capabilities>']) |
64 |
|
|
self.root = xml.dom.minidom.parseString(data).documentElement |
65 |
|
|
|
66 |
|
|
|
67 |
|
|
def compareLists(self, foo, bar): |
68 |
|
|
""" |
69 |
|
|
Compare two lists |
70 |
|
|
- check same number of elements |
71 |
|
|
- check whether all elements in the first list are part of the second |
72 |
|
|
""" |
73 |
|
|
|
74 |
|
|
# Check for same number of elements |
75 |
|
|
if len(foo) != len(bar): |
76 |
|
|
self.fail("Different number of elements"); |
77 |
|
|
|
78 |
|
|
# Loop through all elements for existance |
79 |
|
|
for elm in foo: |
80 |
|
|
if elm not in bar: |
81 |
|
|
self.fail("%s not in second list" % elm); |
82 |
|
|
|
83 |
|
|
|
84 |
|
|
def compareNodeLists (self, result, nodelist): |
85 |
|
|
""" |
86 |
|
|
Compares the Name children versus the given result list. |
87 |
|
|
""" |
88 |
|
|
|
89 |
|
|
names = [] |
90 |
|
|
for pivot in nodelist: |
91 |
|
|
for i in range (pivot.childNodes.length): |
92 |
|
|
if pivot.childNodes[i].nodeName == 'Name': |
93 |
|
|
names.append(pivot.childNodes[i].childNodes[0].data) |
94 |
|
|
|
95 |
|
|
self.compareLists(result, names) |
96 |
|
|
|
97 |
|
|
|
98 |
|
|
def compareNode (self, result, node): |
99 |
|
|
""" |
100 |
|
|
Compares the Name child versus the given result. |
101 |
|
|
""" |
102 |
|
|
|
103 |
|
|
for i in range (node.childNodes.length): |
104 |
|
|
if node.childNodes[i].nodeName == 'Name': |
105 |
|
|
self.assertEquals(result, node.childNodes[i].childNodes[0].data) |
106 |
|
|
break |
107 |
|
|
else: |
108 |
|
|
self.fail("No Name child found."); |
109 |
|
|
|
110 |
|
|
|
111 |
|
|
def test_compareLists (self): |
112 |
|
|
""" |
113 |
|
|
Test the internal compareLists function. |
114 |
|
|
""" |
115 |
|
|
|
116 |
|
|
# Zero element |
117 |
|
|
self.compareLists([], []) |
118 |
|
|
|
119 |
|
|
# Single element |
120 |
|
|
self.compareLists(['alpha'], ['alpha']) |
121 |
|
|
|
122 |
|
|
# Multiple elements |
123 |
|
|
self.compareLists(['alpha', 'beta', 'gamma'], ['alpha', 'beta', 'gamma']) |
124 |
|
|
|
125 |
|
|
# Multiple elements, different order |
126 |
|
|
self.compareLists(['alpha', 'beta', 'gamma'], ['gamma', 'alpha', 'beta']) |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
def test_getElementsByName (self): |
130 |
|
|
""" |
131 |
|
|
Test for the getElementsByName function. |
132 |
|
|
""" |
133 |
|
|
|
134 |
|
|
# Test for level 1 |
135 |
|
|
result = ['Osnabrueck'] |
136 |
|
|
self.compareNodeLists(result, getElementsByName(self.root, 'Layer')) |
137 |
|
|
|
138 |
|
|
# Test for level 2 |
139 |
|
|
result = ['gruenflaechen', 'gewaesser'] |
140 |
|
|
self.compareNodeLists(result, getElementsByName(getElementsByName(self.root, 'Layer')[0], 'Layer')) |
141 |
|
|
|
142 |
|
|
# Test for level 3 |
143 |
|
|
result = ['gewaesserpolyl'] |
144 |
|
|
self.compareNodeLists(result, |
145 |
|
|
getElementsByName |
146 |
|
|
(getElementsByName |
147 |
|
|
(getElementsByName |
148 |
|
|
(self.root, 'Layer')[0], 'Layer')[0], 'Layer')) |
149 |
|
|
|
150 |
|
|
|
151 |
|
|
def test_getElementByName (self): |
152 |
|
|
""" |
153 |
|
|
Test for the getElementByName function. |
154 |
|
|
""" |
155 |
|
|
|
156 |
|
|
# Test for level 1 |
157 |
|
|
self.compareNode('Osnabrueck', getElementByName(self.root, 'Layer')) |
158 |
|
|
|
159 |
|
|
# Test for level 2 |
160 |
|
|
self.compareNode('gewaesser', getElementByName(getElementByName(self.root, 'Layer'), 'Layer')) |
161 |
|
|
|
162 |
|
|
# Test for level 3 |
163 |
|
|
self.compareNode('gewaesserpolyl', |
164 |
|
|
getElementByName |
165 |
|
|
(getElementByName |
166 |
|
|
(getElementByName |
167 |
|
|
(self.root, 'Layer'), 'Layer'), 'Layer')) |
168 |
|
|
|
169 |
|
|
|
170 |
|
|
if __name__ == "__main__": |
171 |
|
|
unittest.main() |