1 |
bh |
292 |
# Copyright (c) 2002 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Test saving a thuban session as XML |
10 |
|
|
""" |
11 |
|
|
|
12 |
|
|
__version__ = "$Revision$" |
13 |
|
|
# $Source$ |
14 |
|
|
# $Id$ |
15 |
|
|
|
16 |
|
|
import os |
17 |
|
|
import unittest |
18 |
|
|
from StringIO import StringIO |
19 |
|
|
|
20 |
|
|
import xml.sax |
21 |
|
|
import xml.sax.handler |
22 |
|
|
from xml.sax import make_parser, ErrorHandler |
23 |
|
|
|
24 |
|
|
import support |
25 |
|
|
support.initthuban() |
26 |
|
|
|
27 |
|
|
from Thuban.Model.save import save_session |
28 |
|
|
from Thuban.Model.session import Session |
29 |
|
|
from Thuban.Model.map import Map |
30 |
|
|
from Thuban.Model.layer import Layer |
31 |
|
|
from Thuban.Model.proj import Projection |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
class SaxEventLister(xml.sax.handler.ContentHandler): |
35 |
|
|
|
36 |
|
|
def __init__(self): |
37 |
|
|
self.eventlist = [] |
38 |
|
|
|
39 |
|
|
def startElementNS(self, name, qname, attrs): |
40 |
|
|
items = attrs.items() |
41 |
|
|
items.sort() |
42 |
|
|
self.eventlist.append(("start", name, qname, items)) |
43 |
|
|
|
44 |
|
|
def endElementNS(self, name, qname): |
45 |
|
|
self.eventlist.append(("end", name, qname)) |
46 |
|
|
|
47 |
|
|
|
48 |
|
|
def sax_eventlist(data): |
49 |
|
|
"""Return a list of SAX event generated for the XML data. |
50 |
|
|
""" |
51 |
|
|
handler = SaxEventLister() |
52 |
|
|
parser = make_parser() |
53 |
|
|
parser.setContentHandler(handler) |
54 |
|
|
parser.setErrorHandler(ErrorHandler()) |
55 |
|
|
parser.setFeature(xml.sax.handler.feature_namespaces, 1) |
56 |
jonathan |
524 |
parser.setFeature(xml.sax.handler.feature_validation, 0) |
57 |
|
|
parser.setFeature(xml.sax.handler.feature_external_ges, 0) |
58 |
|
|
parser.setFeature(xml.sax.handler.feature_external_pes, 0) |
59 |
bh |
292 |
|
60 |
|
|
inpsrc = xml.sax.InputSource() |
61 |
|
|
inpsrc.setByteStream(StringIO(data)) |
62 |
|
|
parser.parse(inpsrc) |
63 |
|
|
|
64 |
|
|
return handler.eventlist |
65 |
|
|
|
66 |
|
|
class SaveSessionTest(unittest.TestCase, support.FileTestMixin): |
67 |
|
|
|
68 |
|
|
def compare_xml(self, xml1, xml2): |
69 |
|
|
self.assertEquals(sax_eventlist(xml1), sax_eventlist(xml2)) |
70 |
|
|
|
71 |
|
|
def testEmptySession(self): |
72 |
|
|
"""Save an empty session""" |
73 |
|
|
session = Session("empty session") |
74 |
|
|
filename = self.temp_file_name("save_emptysession.thuban") |
75 |
|
|
save_session(session, filename) |
76 |
|
|
session.Destroy() |
77 |
|
|
|
78 |
|
|
file = open(filename) |
79 |
|
|
written_contents = file.read() |
80 |
|
|
file.close() |
81 |
|
|
self.compare_xml(written_contents, |
82 |
|
|
'<?xml version="1.0" encoding="UTF-8"?>\n' |
83 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
84 |
|
|
'<session title="empty session">\n</session>\n') |
85 |
|
|
|
86 |
|
|
def testSingleLayer(self): |
87 |
|
|
"""Save a session with a single map with a single layer""" |
88 |
|
|
# deliberately put an apersand in the title :) |
89 |
|
|
session = Session("single map&layer") |
90 |
|
|
proj = Projection(["zone=26", "proj=utm", "ellps=clrk66"]) |
91 |
|
|
map = Map("Test Map", projection = proj) |
92 |
|
|
session.AddMap(map) |
93 |
|
|
# use shapefile from the example data |
94 |
|
|
shpfile = os.path.join(os.path.dirname(__file__), |
95 |
|
|
os.pardir, "Data", "iceland", "political.shp") |
96 |
|
|
layer = Layer("My Layer", shpfile) |
97 |
|
|
map.AddLayer(layer) |
98 |
|
|
|
99 |
|
|
filename = self.temp_file_name("save_singlemap.thuban") |
100 |
|
|
save_session(session, filename) |
101 |
|
|
session.Destroy() |
102 |
|
|
|
103 |
|
|
file = open(filename) |
104 |
|
|
written_contents = file.read() |
105 |
|
|
file.close() |
106 |
|
|
expected_contents = '''<?xml version="1.0" encoding="UTF-8"?> |
107 |
|
|
<!DOCTYPE session SYSTEM "thuban.dtd"> |
108 |
|
|
<session title="single map&layer"> |
109 |
|
|
<map title="Test Map"> |
110 |
|
|
<projection> |
111 |
|
|
<parameter value="zone=26"/> |
112 |
|
|
<parameter value="proj=utm"/> |
113 |
|
|
<parameter value="ellps=clrk66"/> |
114 |
|
|
</projection> |
115 |
|
|
<layer title="My Layer" filename="%s" |
116 |
|
|
fill="None" stroke="#000000" stroke_width="1"/> |
117 |
|
|
</map> |
118 |
|
|
</session>''' % os.path.join("..", "..", "Data", "iceland", |
119 |
|
|
"political.shp") |
120 |
jonathan |
494 |
#print written_contents |
121 |
|
|
#print "********************************************" |
122 |
|
|
#print expected_contents |
123 |
bh |
292 |
self.compare_xml(written_contents, expected_contents) |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
|
|
if __name__ == "__main__": |
128 |
|
|
# Fake the __file__ global because it's needed by a test |
129 |
|
|
import sys |
130 |
|
|
__file__ = sys.argv[0] |
131 |
|
|
unittest.main() |