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 |
"""
|
|
19 |
Test the CPF root file parsing routines
|
|
20 |
"""
|
|
21 |
|
|
22 |
import zipfile
|
|
23 |
import unittest
|
3
|
24 |
import os,shutil
|
0
|
25 |
|
|
26 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
27 |
|
3
|
28 |
from cone.public import exceptions
|
0
|
29 |
from cone.storage import zipstorage
|
|
30 |
|
|
31 |
TEMP_DIR = os.path.join(ROOT_PATH, 'temp')
|
|
32 |
tempzip = os.path.join(ROOT_PATH,"zipres_test.zip")
|
|
33 |
|
|
34 |
|
|
35 |
class TestFileResource(unittest.TestCase):
|
|
36 |
|
|
37 |
def _prepare_tempzip(self, filename):
|
|
38 |
"""
|
|
39 |
Prepare a temporary ZIP file with the given name.
|
|
40 |
@return: Absolute path to the newly copied ZIP file.
|
|
41 |
"""
|
|
42 |
new_zip = os.path.join(TEMP_DIR, '%s' % filename)
|
|
43 |
|
|
44 |
if os.path.exists(new_zip):
|
|
45 |
os.unlink(new_zip)
|
|
46 |
|
|
47 |
if not os.path.exists(TEMP_DIR):
|
|
48 |
os.makedirs(TEMP_DIR)
|
|
49 |
|
|
50 |
shutil.copyfile(tempzip, new_zip)
|
|
51 |
return new_zip
|
|
52 |
|
|
53 |
def test_open_and_write_resource(self):
|
|
54 |
ZIPFILE = self._prepare_tempzip('test1.zip')
|
|
55 |
store = zipstorage.ZipStorage(ZIPFILE,"a")
|
|
56 |
res = store.open_resource("temp/testwrite.txt","w")
|
|
57 |
testdata = "Testing writing func!"
|
|
58 |
res.write(testdata)
|
|
59 |
res.close()
|
|
60 |
store.close()
|
|
61 |
zf = zipfile.ZipFile(ZIPFILE,"r")
|
|
62 |
data = zf.read("temp/testwrite.txt")
|
|
63 |
self.assertEquals(data,testdata)
|
|
64 |
|
|
65 |
def test_open_and_read_resource(self):
|
|
66 |
ZIPFILE = self._prepare_tempzip('test2.zip')
|
|
67 |
store = zipstorage.ZipStorage(ZIPFILE,"a")
|
|
68 |
res = store.open_resource("temp/testread.txt","r")
|
|
69 |
resdata = res.read()
|
|
70 |
res.close()
|
|
71 |
store.close()
|
|
72 |
self.assertTrue(resdata.startswith("Hello"))
|
|
73 |
|
|
74 |
def test_get_size(self):
|
|
75 |
ZIPFILE = self._prepare_tempzip('test_getsize.zip')
|
|
76 |
store = zipstorage.ZipStorage(ZIPFILE, "r")
|
|
77 |
res = store.open_resource("temp/testread.txt","r")
|
|
78 |
self.assertEquals(res.get_size(), 28)
|
|
79 |
# Try a second time just in case
|
|
80 |
self.assertEquals(res.get_size(), 28)
|
|
81 |
res.close()
|
|
82 |
store.close()
|
|
83 |
|
|
84 |
def test_get_size_largefile(self):
|
|
85 |
ZIPFILE = self._prepare_tempzip('test_getsize_largefile.zip')
|
|
86 |
store = zipstorage.ZipStorage(ZIPFILE, "r")
|
|
87 |
res = store.open_resource("largefile.bin","r")
|
|
88 |
self.assertEquals(res.get_size(), 25000)
|
|
89 |
# Try a second time just in case
|
|
90 |
self.assertEquals(res.get_size(), 25000)
|
|
91 |
res.close()
|
|
92 |
store.close()
|
|
93 |
|
|
94 |
def test_get_size_fails_in_write_mode(self):
|
|
95 |
ZIPFILE = os.path.join(TEMP_DIR, 'getsize_fails_in_write_mode.zip')
|
|
96 |
store = zipstorage.ZipStorage(ZIPFILE, "w")
|
|
97 |
res = store.open_resource("test_getsize.txt", "w")
|
|
98 |
res.write("Writing foobar")
|
3
|
99 |
self.assertRaises(exceptions.StorageException, res.get_size)
|
0
|
100 |
res.close()
|
|
101 |
store.close()
|
|
102 |
|
|
103 |
def test_get_content_info_and_read_data(self):
|
|
104 |
ZIPFILE = self._prepare_tempzip('test2.zip')
|
|
105 |
store = zipstorage.ZipStorage(ZIPFILE,"a")
|
|
106 |
res = store.open_resource("temp/testread.txt","r")
|
|
107 |
ci = res.get_content_info()
|
|
108 |
self.assertEquals('text/plain', ci.content_type)
|
|
109 |
resdata = res.read()
|
|
110 |
self.assertEquals('Hello!\r\nHow is my reading?\r\n', resdata)
|
|
111 |
res.close()
|
|
112 |
store.close()
|
|
113 |
|
|
114 |
|
|
115 |
if __name__ == '__main__':
|
3
|
116 |
unittest.main()
|