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