0
|
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 |
"""
|
|
18 |
Test Respource
|
|
19 |
"""
|
|
20 |
import unittest
|
|
21 |
import string
|
|
22 |
import sys,os
|
|
23 |
import pickle
|
|
24 |
|
|
25 |
from cone.public import api, exceptions, utils
|
|
26 |
|
|
27 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
28 |
temp_dir = os.path.join(ROOT_PATH, "temp/storage")
|
|
29 |
storage_path = os.path.join(temp_dir, "layertest.pk")
|
|
30 |
|
|
31 |
class TestStorage(unittest.TestCase):
|
|
32 |
def setUp(self):
|
|
33 |
pass
|
|
34 |
|
|
35 |
def test_create_storage(self):
|
|
36 |
res = api.Storage("")
|
|
37 |
self.assertTrue(res)
|
|
38 |
|
|
39 |
def test_get_root(self):
|
|
40 |
path = os.path.join(temp_dir, "foo/faa.pk")
|
|
41 |
store = api.Storage.open(path,"w")
|
|
42 |
self.assertEquals(store.get_path(),path)
|
|
43 |
|
|
44 |
def test_set_path(self):
|
|
45 |
path = os.path.join(temp_dir, "foo/faa.pk")
|
|
46 |
store = api.Storage.open(path,"w")
|
|
47 |
self.assertEquals(store.get_path(),path)
|
|
48 |
store.set_current_path("faa")
|
|
49 |
self.assertEquals(store.get_current_path(),"faa")
|
|
50 |
|
|
51 |
def test_get_more_read(self):
|
|
52 |
storage = api.Storage("")
|
|
53 |
self.assertEquals(storage.get_mode("r"),storage.MODE_READ)
|
|
54 |
self.assertEquals(storage.get_mode("rb"),storage.MODE_READ)
|
|
55 |
|
|
56 |
def test_get_more_write(self):
|
|
57 |
storage = api.Storage("")
|
|
58 |
self.assertEquals(storage.get_mode("w"),storage.MODE_WRITE)
|
|
59 |
self.assertEquals(storage.get_mode("wb"),storage.MODE_WRITE)
|
|
60 |
|
|
61 |
def test_get_more_append(self):
|
|
62 |
storage = api.Storage("")
|
|
63 |
self.assertEquals(storage.get_mode("a"),storage.MODE_APPEND)
|
|
64 |
self.assertEquals(storage.get_mode("ab"),storage.MODE_APPEND)
|
|
65 |
|
|
66 |
def test_get_more_unknown(self):
|
|
67 |
storage = api.Storage("")
|
|
68 |
self.assertEquals(storage.get_mode("1"),storage.MODE_UNKNOWN)
|
|
69 |
self.assertEquals(storage.get_mode("2b"),storage.MODE_UNKNOWN)
|
|
70 |
|
|
71 |
class TestStorageGeneric(unittest.TestCase):
|
|
72 |
def test_list_resources(self):
|
|
73 |
store = api.Storage.open(storage_path,"w")
|
|
74 |
self.assertTrue(store)
|
|
75 |
self.assertEquals(store.list_resources(""), [])
|
|
76 |
|
|
77 |
def test_open_resource_nonexisting(self):
|
|
78 |
store = api.Storage.open(storage_path,"w")
|
|
79 |
self.assertTrue(store)
|
|
80 |
try:
|
|
81 |
res = store.open_resource("test")
|
|
82 |
res.close()
|
|
83 |
self.fail("Opening non existing resource succeeds")
|
|
84 |
except exceptions.NotResource,e:
|
|
85 |
pass
|
|
86 |
|
|
87 |
def test_is_resource_nonexisting(self):
|
|
88 |
store = api.Storage.open(storage_path,"w")
|
|
89 |
self.assertTrue(store)
|
|
90 |
self.assertFalse(store.is_resource("test"))
|
|
91 |
|
|
92 |
def test_open_resource_is_resource(self):
|
|
93 |
store = api.Storage.open(storage_path,"w")
|
|
94 |
self.assertTrue(store)
|
|
95 |
res = store.open_resource("test","w")
|
|
96 |
res.write("Testing writing more")
|
|
97 |
res.close()
|
|
98 |
self.assertTrue(store.is_resource('test'))
|
|
99 |
|
|
100 |
def test_open_resource_write_and_write(self):
|
|
101 |
store = api.Storage.open(storage_path,"w")
|
|
102 |
self.assertTrue(store)
|
|
103 |
res = store.open_resource("test","w")
|
|
104 |
res.write("Testing writing more")
|
|
105 |
res.close()
|
|
106 |
res = store.open_resource("test","w")
|
|
107 |
res.write("writing")
|
|
108 |
res.close()
|
|
109 |
self.assertEquals(store._get('test').data, 'writing')
|
|
110 |
|
|
111 |
def test_get_size_on_write_only_resource_fails(self):
|
|
112 |
store = api.Storage.open(storage_path,"w")
|
|
113 |
self.assertTrue(store)
|
|
114 |
res = store.open_resource("test","w")
|
|
115 |
res.write("Writing foobar")
|
|
116 |
self.assertRaises(exceptions.StorageException, res.get_size)
|
|
117 |
res.close()
|
|
118 |
|
|
119 |
def test_open_resource_and_get_size(self):
|
|
120 |
store = api.Storage.open(storage_path,"w")
|
|
121 |
self.assertTrue(store)
|
|
122 |
res = store.open_resource("test","w")
|
|
123 |
res.write("Writing foobar")
|
|
124 |
res.close()
|
|
125 |
|
|
126 |
res = store.open_resource("test","r")
|
|
127 |
self.assertEquals(res.get_size(), 14)
|
|
128 |
res.close()
|
|
129 |
|
|
130 |
def test_write_fails_on_read_mode(self):
|
|
131 |
store = api.Storage.open(storage_path, "w")
|
|
132 |
self.assertTrue(store)
|
|
133 |
res = store.open_resource("test","w")
|
|
134 |
res.write("Testing writing more")
|
|
135 |
res.close()
|
|
136 |
resr = store.open_resource("test","r")
|
|
137 |
try:
|
|
138 |
resr.write("Testing writing more")
|
|
139 |
resr.close()
|
|
140 |
self.fail("Writing succeeds on read mode?")
|
|
141 |
except exceptions.StorageException, e:
|
|
142 |
pass
|
|
143 |
|
|
144 |
def test_open_resource_append(self):
|
|
145 |
store = api.Storage.open(storage_path,"w")
|
|
146 |
self.assertTrue(store)
|
|
147 |
res = store.open_resource("test","a")
|
|
148 |
res.write("Testing append.\n")
|
|
149 |
res.close()
|
|
150 |
res = store.open_resource("test","a")
|
|
151 |
res.write("appending!")
|
|
152 |
res.close()
|
|
153 |
self.assertEquals(store.test.data, 'Testing append.\nappending!')
|
|
154 |
|
|
155 |
def test_open_multiple_no_close_and_write_closed(self):
|
|
156 |
store = api.Storage.open(storage_path,"w")
|
|
157 |
self.assertTrue(store)
|
|
158 |
res1 = store.open_resource("test","a")
|
|
159 |
res1.write("Testing append.\n")
|
|
160 |
res1.close()
|
|
161 |
res2 = store.open_resource("test","a")
|
|
162 |
res2.write("appending!")
|
|
163 |
res2.close()
|
|
164 |
try:
|
|
165 |
res2.write("sss")
|
|
166 |
self.fail("writing on closed object succeeds?")
|
|
167 |
except ValueError:
|
|
168 |
pass
|
|
169 |
self.assertEquals(store.test.data, 'Testing append.\nappending!')
|
|
170 |
|
|
171 |
def test_open_many_to_one(self):
|
|
172 |
store = api.Storage.open(storage_path,"w")
|
|
173 |
self.assertTrue(store)
|
|
174 |
res1 = store.open_resource("test/foo.txt","a")
|
|
175 |
res1.write("Testing\n")
|
|
176 |
res1.close()
|
|
177 |
res1 = store.open_resource("test/foo.txt","w")
|
|
178 |
res1.close()
|
3
|
179 |
self.assertEquals(store.list_resources('', recurse=True),['test/foo.txt'])
|
0
|
180 |
|
|
181 |
def test_open_many(self):
|
|
182 |
store = api.Storage.open(storage_path,"w")
|
|
183 |
self.assertTrue(store)
|
|
184 |
res1 = store.open_resource("test/foo.txt","a")
|
|
185 |
res1.write("Testing\n")
|
|
186 |
res1.close()
|
|
187 |
res2 = store.open_resource("test/bar.txt","a")
|
|
188 |
res2.write("Writing bar!")
|
|
189 |
res2.close()
|
|
190 |
self.assertEquals(store.test.foo.data, 'Testing\n')
|
|
191 |
self.assertEquals(store.test.bar.data, 'Writing bar!')
|
|
192 |
|
|
193 |
def test_open_many_to_a_hierarchy_and_list_folders(self):
|
|
194 |
store = api.Storage.open(storage_path,"w")
|
|
195 |
self.assertTrue(store)
|
|
196 |
root= store.open_resource("root.txt","a")
|
|
197 |
root.write("root\n")
|
|
198 |
root.close()
|
|
199 |
res1 = store.open_resource("test/foo.txt","a")
|
|
200 |
res1.write("Testing\n")
|
|
201 |
res1.close()
|
|
202 |
res2 = store.open_resource("test/bar.txt","a")
|
|
203 |
res2.write("Writing bar!")
|
|
204 |
res2.close()
|
|
205 |
self.assertEquals(store.list_resources('/'), ['root.txt'])
|
|
206 |
self.assertEquals(store.list_resources('/test'), ['test/bar.txt',
|
|
207 |
'test/foo.txt'])
|
3
|
208 |
self.assertEquals(store.list_resources('/',recurse=True), ['root.txt',
|
0
|
209 |
'test/bar.txt',
|
|
210 |
'test/foo.txt'])
|
|
211 |
|
|
212 |
def test_open_resource_and_delete(self):
|
|
213 |
store = api.Storage.open(storage_path,"w")
|
|
214 |
self.assertTrue(store)
|
|
215 |
res = store.open_resource("test1.txt","a")
|
|
216 |
res.write("Testing append.\n")
|
|
217 |
res.close()
|
|
218 |
res = store.open_resource("test2.txt","w")
|
|
219 |
res.write("Testing append.\n")
|
|
220 |
res.close()
|
|
221 |
res = store.open_resource("test3.txt","w")
|
|
222 |
res.write("Testing append.\n")
|
|
223 |
res.close()
|
|
224 |
for res in store.list_resources(''):
|
|
225 |
store.delete_resource(res)
|
|
226 |
self.assertEquals(store.list_resources(''), [])
|
|
227 |
|
|
228 |
def test_open_resources_and_read(self):
|
|
229 |
store = api.Storage.open(storage_path,"w")
|
|
230 |
self.assertTrue(store)
|
|
231 |
res = store.open_resource("test1.txt","w")
|
|
232 |
res.write("Testing reading.\n")
|
|
233 |
res.close()
|
|
234 |
res = store.open_resource("test2.txt","w")
|
|
235 |
res.write("Testing reading.\n")
|
|
236 |
res.close()
|
|
237 |
res = store.open_resource("test3.txt","w")
|
|
238 |
res.write("Testing reading.\n")
|
|
239 |
res.close()
|
|
240 |
for res in store.list_resources(''):
|
|
241 |
res = store.open_resource(res)
|
|
242 |
self.assertEquals(res.read(), "Testing reading.\n")
|
|
243 |
|
|
244 |
def test_open_resources_and_save_and_dump(self):
|
|
245 |
temp_file = os.path.join(temp_dir, "FooStore.pk")
|
|
246 |
store = api.Storage.open(temp_file, "w")
|
|
247 |
self.assertTrue(store)
|
|
248 |
res1 = store.open_resource("test1.txt","w")
|
|
249 |
res1.write("Testing reading.\n")
|
|
250 |
res2 = store.open_resource("test2.txt","w")
|
|
251 |
res2.write("Testing reading.\n")
|
|
252 |
res3 = store.open_resource("test3.txt","w")
|
|
253 |
res3.write("Testing reading.\n")
|
|
254 |
store.save()
|
|
255 |
|
|
256 |
def test_open_resources_and_load(self):
|
|
257 |
temp_file = os.path.join(temp_dir, "store.pk")
|
|
258 |
store = api.Storage.open(temp_file,"w")
|
|
259 |
self.assertTrue(store)
|
|
260 |
res1 = store.open_resource("test1.txt","w")
|
|
261 |
res1.write("Testing reading.\n")
|
|
262 |
res2 = store.open_resource("test2.txt","w")
|
|
263 |
res2.write("Testing reading.\n")
|
|
264 |
res3 = store.open_resource("test3.txt","w")
|
|
265 |
res3.write("Testing reading.\n")
|
|
266 |
store.close()
|
|
267 |
store2 = api.Storage.open(temp_file)
|
|
268 |
self.assertEquals(store2.list_resources(''), ['test1.txt',
|
|
269 |
'test2.txt',
|
|
270 |
'test3.txt'])
|
|
271 |
self.assertEquals(store2.open_resource("test1.txt").read(),'Testing reading.\n')
|
|
272 |
self.assertEquals(store2.open_resource("test2.txt").read(),'Testing reading.\n')
|
|
273 |
self.assertEquals(store2.open_resource("test3.txt").read(),'Testing reading.\n')
|
|
274 |
|
|
275 |
def test_import_resources(self):
|
|
276 |
temp_file = os.path.join(temp_dir, "importsource.pk")
|
|
277 |
store = api.Storage.open(temp_file, "w")
|
|
278 |
self.assertTrue(store)
|
|
279 |
res1 = store.open_resource("test1.txt","w")
|
|
280 |
res1.write("Testing reading.\n")
|
|
281 |
res2 = store.open_resource("test2.txt","w")
|
|
282 |
res2.write("Testing reading.\n")
|
|
283 |
res3 = store.open_resource("test3.txt","w")
|
|
284 |
res3.write("Testing reading.\n")
|
|
285 |
store.save()
|
|
286 |
res3.close()
|
|
287 |
store2 = api.Storage.open(temp_file, "w")
|
|
288 |
store2.import_resources(store.list_resources(''), store)
|
|
289 |
self.assertEquals(store2.open_resource("test1.txt").read(),store.open_resource('test1.txt').read())
|
|
290 |
self.assertEquals(store2.open_resource("test2.txt").read(),store.open_resource('test2.txt').read())
|
|
291 |
self.assertEquals(store2.open_resource("test3.txt").read(),store.open_resource('test3.txt').read())
|
|
292 |
store2.close()
|
|
293 |
|
|
294 |
def test_export_resources(self):
|
|
295 |
temp_file = os.path.join(temp_dir, "exportsource.pk")
|
|
296 |
store = api.Storage.open(temp_file, "w")
|
|
297 |
self.assertTrue(store)
|
|
298 |
res1 = store.open_resource("test1.txt","w")
|
|
299 |
res1.write("Testing reading.\n")
|
|
300 |
res2 = store.open_resource("test2.txt","w")
|
|
301 |
res2.write("Testing reading.\n")
|
|
302 |
res3 = store.open_resource("test3.txt","w")
|
|
303 |
res3.write("Testing reading.\n")
|
|
304 |
store.save()
|
|
305 |
store2 = api.Storage.open(temp_file, "w")
|
|
306 |
store.export_resources(store.list_resources(''), store2)
|
|
307 |
res2 = store2.open_resource("test1.txt")
|
|
308 |
res1 = store.open_resource('test1.txt')
|
|
309 |
self.assertEquals(res1.read(),res2.read())
|
|
310 |
self.assertEquals(store2.open_resource("test2.txt").read(),store.open_resource('test2.txt').read())
|
|
311 |
self.assertEquals(store2.open_resource("test3.txt").read(),store.open_resource('test3.txt').read())
|
|
312 |
store2.close()
|
|
313 |
|
|
314 |
def test_export_modify_and_close_and_open(self):
|
|
315 |
temp_file = os.path.join(temp_dir, "exportsource.pk")
|
|
316 |
store = api.Storage.open(temp_file,"w")
|
|
317 |
self.assertTrue(store)
|
|
318 |
res1 = store.open_resource("test1.txt","w")
|
|
319 |
res1.write("Testing reading.\n")
|
|
320 |
res1.close()
|
|
321 |
|
|
322 |
res2 = store.open_resource("test2.txt","w")
|
|
323 |
res2.write("Testing reading.\n")
|
|
324 |
res3 = store.open_resource("test3.txt","w")
|
|
325 |
res3.write("Testing reading.\n")
|
|
326 |
|
|
327 |
res1 = store.open_resource("test1.txt","w")
|
|
328 |
res1.write("Testing reading too.\n")
|
|
329 |
res1.close()
|
|
330 |
|
|
331 |
store.close()
|
|
332 |
|
|
333 |
modified_temp_file = os.path.join(temp_dir, "modified.pk")
|
|
334 |
store2 = api.Storage.open(modified_temp_file, "w")
|
|
335 |
store.export_resources(store.list_resources(''), store2)
|
|
336 |
store2.delete_resource('test3.txt')
|
|
337 |
resr = store2.open_resource('test2.txt')
|
|
338 |
resw = store2.open_resource('test2.txt','w')
|
|
339 |
resw.write("Now this sould be different")
|
|
340 |
store2.close()
|
|
341 |
store3 = api.Storage.open(modified_temp_file)
|
|
342 |
self.assertEquals(store3.list_resources(''), ['test1.txt',
|
|
343 |
'test2.txt'])
|
|
344 |
self.assertEquals(store3.open_resource("test2.txt").read(),'Now this sould be different')
|
|
345 |
self.assertEquals(store3.open_resource("test1.txt").read(),'Testing reading too.\n')
|
|
346 |
|
|
347 |
|
|
348 |
def test_get_path_set_path(self):
|
|
349 |
temp_file = os.path.join(temp_dir, "subpath.pk")
|
|
350 |
store = api.Storage.open(temp_file,"w")
|
|
351 |
self.assertEquals(store.get_path(),temp_file)
|
|
352 |
self.assertEquals(store.get_current_path(),"")
|
|
353 |
store.set_current_path("subdir")
|
|
354 |
self.assertEquals(store.get_current_path(),"subdir")
|
|
355 |
|
|
356 |
def test_set_path_open_resource(self):
|
|
357 |
temp_file = os.path.join(temp_dir, "subpath.pk")
|
|
358 |
if os.path.exists(temp_file): os.unlink(temp_file)
|
|
359 |
|
|
360 |
store = api.Storage.open(temp_file,"w")
|
|
361 |
self.assertEquals(store.get_path(),temp_file)
|
|
362 |
store.set_current_path("subdir")
|
|
363 |
self.assertEquals(store.get_current_path(),"subdir")
|
|
364 |
res = store.open_resource("foo.txt","w")
|
|
365 |
res.write("foo")
|
|
366 |
res.close()
|
|
367 |
self.assertEquals(store.list_resources(""), ["foo.txt"])
|
|
368 |
store.set_current_path("/")
|
3
|
369 |
self.assertEquals(store.list_resources("", recurse=True), ["subdir/foo.txt"])
|
0
|
370 |
store.close()
|
|
371 |
os.unlink(temp_file)
|
|
372 |
|
|
373 |
def test_create_folder(self):
|
|
374 |
temp_file = os.path.join(temp_dir, "subpath.pk")
|
|
375 |
store = api.Storage.open(temp_file,"w")
|
|
376 |
store.create_folder("subdir/test")
|
|
377 |
self.assertTrue(store.is_folder("subdir/test"))
|
|
378 |
|
|
379 |
def test_create_folder_and_delete_folder(self):
|
|
380 |
temp_file = os.path.join(temp_dir, "subpath.pk")
|
|
381 |
store = api.Storage.open(temp_file,"w")
|
|
382 |
store.create_folder("subdir/test")
|
|
383 |
self.assertTrue(store.is_folder("subdir/test"))
|
|
384 |
store.delete_folder("subdir/test")
|
|
385 |
self.assertFalse(store.is_folder("subdir/test"))
|
|
386 |
self.assertTrue(store.is_folder("subdir"))
|
|
387 |
|
|
388 |
class TestFolder(unittest.TestCase):
|
|
389 |
|
|
390 |
def test_create_folder(self):
|
|
391 |
store = api.Storage.open(storage_path,"w")
|
|
392 |
layer = api.Folder(store, "foo")
|
|
393 |
self.assertTrue(layer)
|
|
394 |
|
|
395 |
def test_get_path(self):
|
|
396 |
store = api.Storage.open(storage_path,"w")
|
|
397 |
layer = api.Folder(store, "foo")
|
|
398 |
self.assertTrue(layer)
|
|
399 |
self.assertEquals(layer.get_current_path(),"foo")
|
|
400 |
|
|
401 |
def test_open_resource(self):
|
|
402 |
store = api.Storage.open(storage_path,"w")
|
|
403 |
layer = api.Folder(store, "foo")
|
|
404 |
self.assertTrue(layer)
|
|
405 |
res = layer.open_resource("confml/test.confml","w")
|
|
406 |
res.write("foo.conf")
|
|
407 |
res.close()
|
3
|
408 |
self.assertEquals(layer.list_resources("", recurse=True),["confml/test.confml"])
|
|
409 |
self.assertEquals(store.list_resources("", recurse=True),["foo/confml/test.confml"])
|
0
|
410 |
|
|
411 |
def test_create_two_layers_and_open_resource(self):
|
|
412 |
store = api.Storage.open(storage_path,"w")
|
|
413 |
foo_layer = api.Folder(store, "foo")
|
|
414 |
bar_layer = api.Folder(store, "bar")
|
|
415 |
res = foo_layer.open_resource("confml/test.confml","w")
|
|
416 |
res.write("foo.conf")
|
|
417 |
res.close()
|
|
418 |
res = foo_layer.open_resource("root.confml","w")
|
|
419 |
res.close()
|
|
420 |
res = bar_layer.open_resource("confml/root.confml","w")
|
|
421 |
res.write("foo.conf")
|
|
422 |
res.close()
|
3
|
423 |
self.assertEquals(foo_layer.list_resources("", recurse=True),['root.confml', 'confml/test.confml'])
|
|
424 |
self.assertEquals(store.list_resources("", recurse=True),['bar/confml/root.confml',
|
|
425 |
'foo/root.confml',
|
|
426 |
'foo/confml/test.confml'])
|
0
|
427 |
|
|
428 |
foo_layer.delete_resource("confml/test.confml")
|
3
|
429 |
self.assertEquals(foo_layer.list_resources("", recurse=True),["root.confml"])
|
|
430 |
self.assertEquals(store.list_resources("", recurse=True),["bar/confml/root.confml",
|
|
431 |
"foo/root.confml"])
|
0
|
432 |
|
|
433 |
|
|
434 |
if __name__ == '__main__':
|
|
435 |
unittest.main()
|
|
436 |
|