1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
# $Id: refcount.py 6521 2007-08-08 20:39:17Z hobu $
#
# Project: MapServer
# Purpose: xUnit style Python mapscript tests of Map
# Author: Umberto Nicoletti, unicoletti@prometeo.it
#
# ===========================================================================
# Copyright (c) 2004, Sean Gillies
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# ===========================================================================
#
# Execute this module as a script from mapserver/mapscript/python
#
# python tests/cases/refcount.py -v
#
# ===========================================================================
import os, sys
import unittest
# the testing module helps us import the pre-installed mapscript
from testing import mapscript, MapTestCase, TESTMAPFILE
# ===========================================================================
# Test begins now
class ReferenceCountingTestCase(unittest.TestCase):
def initMap(self):
self.map=mapscript.mapObj(TESTMAPFILE)
def testMap(self):
"""ReferenceCountingTestCase.testMap: test map constructor with no argument"""
test_map = mapscript.mapObj()
maptype = type(test_map)
assert str(maptype) == "<class 'mapscript.mapObj'>", maptype
assert test_map.thisown == 1
assert test_map.refcount == 1
def testMapWithDefaultMap(self):
"""ReferenceCountingTestCase.testTestMap: test map constructor with default map file"""
test_map = mapscript.mapObj(TESTMAPFILE)
maptype = type(test_map)
assert str(maptype) == "<class 'mapscript.mapObj'>", maptype
assert test_map.thisown == 1
assert test_map.refcount == 1
assert test_map.getLayer(0).refcount == 2
def testMapLayersCounting(self):
"""ReferenceCountingTestCase.testTestMap: test map constructor with default map file"""
self.initMap()
layerRef1=self.map.getLayer(0)
assert layerRef1.refcount == 2
layerRef2=self.map.getLayer(0)
assert layerRef2.refcount == 3
def testMapInsertedLayer(self):
"""MapLayersTestCase.testMapInsertedLayer: test insertion of a new layer at default (last) index"""
self.initMap()
n = self.map.numlayers
layer = mapscript.layerObj()
layer.name = 'new'
assert layer.refcount == 1
assert layer.thisown == 1
index = self.map.insertLayer(layer)
assert index == n, index
assert self.map.numlayers == n + 1
assert layer.refcount == 2
assert layer.thisown == 1
assert self.map.getLayer(index).refcount == 3
assert self.map.getLayer(index).thisown == 1
assert layer.refcount == 2
assert layer.thisown == 1
def testMapInsertedLayerWithIndex(self):
"""MapLayersTestCase.testMapInsertedLayerWithIndex: test insertion of a new layer at index 0"""
self.initMap()
n = self.map.numlayers
layer = mapscript.layerObj()
layer.name = 'new'
assert layer.refcount == 1
assert layer.thisown == 1
index = self.map.insertLayer(layer,0)
assert index == 0, index
assert self.map.numlayers == n + 1
assert layer.refcount == 2
assert layer.thisown == 1
assert self.map.getLayer(index).refcount == 3
assert self.map.getLayer(index).thisown == 1
assert layer.refcount == 2
assert layer.thisown == 1
def testMapRemovedLayerAtTail(self):
"""removal of highest index (tail) layer"""
self.initMap()
n = self.map.numlayers
layer = self.map.removeLayer(n-1)
assert self.map.numlayers == n-1
assert layer.name == 'INLINE-PIXMAP-PCT'
assert layer.thisown == 1
assert layer.refcount == 1, layer.refcount
self.map.draw()
def testMapRemovedLayerAtZero(self):
"""removal of lowest index (0) layer"""
self.initMap()
n = self.map.numlayers
layer = self.map.removeLayer(0)
assert self.map.numlayers == n-1
assert layer.name == 'RASTER'
assert layer.thisown == 1
assert layer.refcount == 1, layer.refcount
self.map.draw()
def testBehaveWhenParentIsNull(self):
"""behave when parent (map) is null"""
self.initMap()
layer = mapscript.layerObj()
layer.name = 'new'
index = self.map.insertLayer(layer,0)
assert index == 0, index
self.map=None
assert layer.map == None, layer.map
exception=None
try:
layer.open()
except:
assert True
exception=True
if not exception:
fail
def testLayerClone(self):
"""Clone a layer"""
layer = mapscript.layerObj()
layer.name='sample'
copy = layer.clone()
assert layer.refcount == 1, layer.refcount
assert copy.refcount == 1, copy.refcount
assert layer.name == copy.name
assert copy.map == None
def testMapGetLayerByName(self):
"""behave when parent (map) is null"""
self.initMap()
layer = self.map.getLayerByName("RASTER")
assert layer.refcount == 2, layer.refcount
assert layer.thisown == 1, layer.thisown
def testBehaveWhenParentIsNotNull(self):
"""behave when parent (map) is not null"""
self.initMap()
layer = mapscript.layerObj(self.map)
layer.name = 'new'
assert layer.refcount == 2, layer.refcount
def testDummyClass(self):
"""basic refcounting for classObj"""
clazz = mapscript.classObj()
assert clazz.refcount == 1, clazz.refcount
def testClassWithArgument(self):
"""classObj constructor with not null layer"""
self.initMap()
clazz = mapscript.classObj(self.map.getLayer(0))
assert clazz.refcount == 2, clazz.refcount
def testClassGetter(self):
"""classObj getter"""
self.initMap()
clazz = self.map.getLayer(1).getClass(0)
assert clazz.refcount == 2, clazz.refcount
def testClassClone(self):
"""classObj clone"""
self.initMap()
clazz = self.map.getLayer(1).getClass(0)
assert clazz.refcount == 2, clazz.refcount
clone = clazz.clone()
assert clone.refcount == 1, clone.refcount
def testClassInsert(self):
"""classObj insert at end"""
self.initMap()
clazz = mapscript.classObj()
clazz.minscaledenom = 666
assert clazz.refcount == 1, clazz.refcount
idx=self.map.getLayer(1).insertClass(clazz)
assert clazz.refcount == 2, clazz.refcount
assert self.map.getLayer(1).getClass(idx).refcount == 3, self.map.getLayer(1).getClass(idx).refcount
assert self.map.getLayer(1).getClass(idx).minscaledenom == 666, self.map.getLayer(1).getClass(idx).minscaledenom
def testClassInsertAtMiddle(self):
"""classObj insert at pos. 1"""
self.initMap()
clazz = mapscript.classObj()
clazz.minscaledenom = 666
assert clazz.refcount == 1, clazz.refcount
idx=self.map.getLayer(1).insertClass(clazz, 1)
assert idx == 1, idx
assert clazz.refcount == 2, clazz.refcount
assert self.map.getLayer(1).getClass(idx).refcount == 3, self.map.getLayer(1).getClass(idx).refcount
assert self.map.getLayer(1).getClass(idx).thisown , self.map.getLayer(1).getClass(idx).thisown
assert self.map.getLayer(1).getClass(idx).minscaledenom == 666, self.map.getLayer(1).getClass(idx).minscaledenom
def testRemoveFirstClass(self):
"""removal of first class"""
self.initMap()
clazz = self.map.getLayerByName("POLYGON").removeClass(0)
assert clazz != None
assert clazz.thisown == 1
assert clazz.refcount == 1, clazz.refcount
self.map.draw()
def testMapClone(self):
"""cloning a mapObj"""
self.initMap()
clone = self.map.clone()
assert clone.refcount == 1, clone.refcount
def testStyleGetter(self):
"""styleObj getter"""
self.initMap()
style = self.map.getLayer(1).getClass(0).getStyle(0)
assert style.refcount == 2, style.refcount
assert style.thisown == 1, style.thisown
def testStyleConstructor(self):
"""styleObj Constructor"""
self.initMap()
style = mapscript.styleObj( self.map.getLayer(1).getClass(0) )
assert style.refcount == 2, style.refcount
assert style.thisown == 1, style.thisown
style = mapscript.styleObj()
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
def testRemoveStyleAtBeginning(self):
"""styleObj remove first one"""
self.initMap()
style = mapscript.styleObj()
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
idx = self.map.getLayer(1).getClass(0).insertStyle(style)
assert style.refcount == 2, style.refcount
assert style.thisown == 1, style.thisown
style = self.map.getLayer(1).getClass(0).removeStyle(0)
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
def testInsertStyle(self):
"""styleObj Insert"""
self.initMap()
style = mapscript.styleObj()
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
idx = self.map.getLayer(1).getClass(0).insertStyle(style)
assert style.refcount == 2, style.refcount
assert style.thisown == 1, style.thisown
def testCloneStyle(self):
"""styleObj Clone"""
self.initMap()
style = mapscript.styleObj()
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
clone = style.clone()
assert clone.refcount == 1, clone.refcount
assert clone.thisown == 1, clone.thisown
assert style.refcount == 1, style.refcount
assert style.thisown == 1, style.thisown
style = self.map.getLayer(1).getClass(0).getStyle(0)
clone = style.clone()
assert clone.refcount == 1, clone.refcount
assert clone.thisown == 1, clone.thisown
def testBasicSymbolRefcounting(self):
"""symbolObj refcounting """
self.initMap()
symb = self.map.symbolset.getSymbol(0)
assert symb.refcount == 2, symb.refcount
assert symb.thisown == 1, symb.thisown
symb = self.map.symbolset.getSymbolByName("home-png")
assert symb.refcount == 2, symb.refcount
assert symb.thisown == 1, symb.thisown
symb2 = self.map.symbolset.getSymbolByName("home-png")
assert symb2.refcount == 3, symb2.refcount
assert symb2.thisown == 1, symb2.thisown
def testSymbolAppendRemove(self):
"""appendSymbolObj refcounting """
self.initMap()
symb = mapscript.symbolObj("ANEWSYMBOL")
assert symb.refcount == 1, symb.refcount
assert symb.thisown == 1, symb.thisown
idx = self.map.symbolset.appendSymbol(symb)
assert symb.refcount == 2, symb.refcount
assert symb.thisown == 1, symb.thisown
symb = self.map.symbolset.removeSymbol(idx)
assert symb.refcount == 1, symb.refcount
assert symb.thisown == 1, symb.thisown
# ===========================================================================
# Run the tests outside of the main suite
if __name__ == '__main__':
unittest.main()
|