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 unittest
|
3
|
23 |
import os
|
0
|
24 |
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
|
|
25 |
|
3
|
26 |
from cone.public.exceptions import StorageException
|
0
|
27 |
from cone.public import api
|
|
28 |
from cone.storage import filestorage
|
|
29 |
|
|
30 |
|
|
31 |
class TestFileResource(unittest.TestCase):
|
|
32 |
def setUp(self):
|
|
33 |
self.spath = os.path.join(ROOT_PATH,"fileres_test")
|
|
34 |
self.storage = filestorage.FileStorage(self.spath)
|
|
35 |
|
|
36 |
def tearDown(self):
|
|
37 |
self.storage.close()
|
|
38 |
|
|
39 |
def test_open_and_write_resource(self):
|
|
40 |
res = self.storage.open_resource("testwrite.txt","w")
|
|
41 |
res.write("Testing writing func!")
|
|
42 |
res.close()
|
|
43 |
f = open(self.spath+"/testwrite.txt","r")
|
|
44 |
data = f.read()
|
|
45 |
self.assertEquals(data,"Testing writing func!")
|
|
46 |
f.close()
|
|
47 |
os.unlink(os.path.join(ROOT_PATH,"fileres_test/testwrite.txt"))
|
|
48 |
|
|
49 |
def test_open_and_read_resource(self):
|
|
50 |
res = self.storage.open_resource("testread.txt","r")
|
|
51 |
resdata = res.read()
|
|
52 |
res.close()
|
|
53 |
f = open(self.spath+"/testread.txt","r")
|
|
54 |
data = f.read()
|
|
55 |
f.close()
|
|
56 |
self.assertEquals(data,resdata)
|
|
57 |
|
|
58 |
def test_open_and_write_resource_and_trunk_it(self):
|
|
59 |
res = self.storage.open_resource("testtrunk.txt","w")
|
|
60 |
res.write("Testing writing func!")
|
|
61 |
res.truncate()
|
|
62 |
res.close()
|
|
63 |
f = open(self.spath+"/testtrunk.txt","r")
|
|
64 |
data = f.read()
|
|
65 |
self.assertEquals(data,"")
|
|
66 |
f.close()
|
|
67 |
os.unlink(os.path.join(ROOT_PATH,"fileres_test/testtrunk.txt"))
|
|
68 |
|
|
69 |
def test_get_size(self):
|
|
70 |
res = self.storage.open_resource("testread.txt", "r")
|
|
71 |
self.assertEquals(res.get_size(), 15)
|
|
72 |
# Try a second time just in case
|
|
73 |
self.assertEquals(res.get_size(), 15)
|
|
74 |
res.close()
|
|
75 |
|
|
76 |
def test_get_size_largefile(self):
|
|
77 |
res = self.storage.open_resource("largefile.bin", "r")
|
|
78 |
self.assertEquals(res.get_size(), 25000)
|
|
79 |
# Try a second time just in case
|
|
80 |
self.assertEquals(res.get_size(), 25000)
|
|
81 |
res.close()
|
|
82 |
|
|
83 |
def test_get_size_fails_in_write_mode(self):
|
|
84 |
res = self.storage.open_resource("test_getsize.txt","w")
|
|
85 |
res.write("Writing foobar")
|
|
86 |
self.assertRaises(StorageException, res.get_size)
|
|
87 |
res.close()
|
|
88 |
|
|
89 |
def test_bmp_content_info_24bit(self):
|
|
90 |
res = self.storage.open_resource("image-bmp-24bit.bmp", "r")
|
|
91 |
content_info = res.get_content_info()
|
|
92 |
self.assertEquals(api.BmpImageContentInfo, type(content_info))
|
|
93 |
self.assertEquals(24, res.get_content_info().color_depth)
|
|
94 |
self.assertEquals(24, content_info.color_depth)
|
|
95 |
self.assertEquals('image', content_info.mimetype)
|
|
96 |
self.assertEquals('bmp', content_info.mimesubtype)
|
|
97 |
self.assertEquals('image/bmp', content_info.content_type)
|
|
98 |
res.close()
|
|
99 |
|
|
100 |
def test_bmp_content_info_8bit(self):
|
|
101 |
res = self.storage.open_resource("image-bmp-8bit.bmp", "r")
|
|
102 |
content_info = res.get_content_info()
|
|
103 |
self.assertEquals(api.BmpImageContentInfo, type(content_info))
|
|
104 |
self.assertEquals(8, res.get_content_info().color_depth)
|
|
105 |
self.assertEquals(8, content_info.color_depth)
|
|
106 |
self.assertEquals('image', content_info.mimetype)
|
|
107 |
self.assertEquals('bmp', content_info.mimesubtype)
|
|
108 |
self.assertEquals('image/bmp', content_info.content_type)
|
|
109 |
res.close()
|
|
110 |
|
|
111 |
def test_bmp_content_info_4bit(self):
|
|
112 |
res = self.storage.open_resource("image-bmp-4bit.bmp", "r")
|
|
113 |
content_info = res.get_content_info()
|
|
114 |
self.assertEquals(api.BmpImageContentInfo, type(content_info))
|
|
115 |
self.assertEquals(4, res.get_content_info().color_depth)
|
|
116 |
self.assertEquals(4, content_info.color_depth)
|
|
117 |
self.assertEquals('image', content_info.mimetype)
|
|
118 |
self.assertEquals('bmp', content_info.mimesubtype)
|
|
119 |
self.assertEquals('image/bmp', content_info.content_type)
|
|
120 |
res.close()
|
|
121 |
|
|
122 |
def test_bmp_content_info_1bit(self):
|
|
123 |
res = self.storage.open_resource("image-bmp-1bit.bmp", "r")
|
|
124 |
content_info = res.get_content_info()
|
|
125 |
self.assertEquals(api.BmpImageContentInfo, type(content_info))
|
|
126 |
self.assertEquals(1, res.get_content_info().color_depth)
|
|
127 |
self.assertEquals(1, content_info.color_depth)
|
|
128 |
self.assertEquals('image', content_info.mimetype)
|
|
129 |
self.assertEquals('bmp', content_info.mimesubtype)
|
|
130 |
self.assertEquals('image/bmp', content_info.content_type)
|
|
131 |
res.close()
|
|
132 |
|
|
133 |
def test_content_info_with_jpg(self):
|
|
134 |
res = self.storage.open_resource("image-jpeg-1bit.jpg", "r")
|
|
135 |
content_info = res.get_content_info()
|
|
136 |
self.assertEquals('image/jpeg', content_info.content_type)
|
|
137 |
self.assertEquals('image', content_info.mimetype)
|
|
138 |
self.assertEquals('jpeg', content_info.mimesubtype)
|
|
139 |
res.close()
|
|
140 |
|
|
141 |
|
|
142 |
def test_content_info_invalid_bmp(self):
|
|
143 |
res = self.storage.open_resource("invalid.bmp", "r")
|
|
144 |
content_info = res.get_content_info()
|
|
145 |
self.assertEquals('image/bmp', content_info.content_type)
|
|
146 |
self.assertEquals('image', content_info.mimetype)
|
|
147 |
self.assertEquals('bmp', content_info.mimesubtype)
|
|
148 |
res.close()
|
|
149 |
|
|
150 |
def test_get_content_info_and_read_data(self):
|
3
|
151 |
res = self.storage.open_resource("testread.txt", "rb")
|
0
|
152 |
ci = res.get_content_info()
|
|
153 |
self.assertEquals('text/plain', ci.content_type)
|
|
154 |
data = res.read()
|
3
|
155 |
self.assertEquals('foo bar test.\r\n', data)
|
0
|
156 |
res.close()
|
|
157 |
|
|
158 |
if __name__ == '__main__':
|
3
|
159 |
unittest.main()
|