1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6
7 Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #include "FileSystemArchiveTests.h"
29 #include "OgreFileSystem.h"
30 #include "OgreException.h"
31
32 // Register the suite
33 //CPPUNIT_TEST_SUITE_REGISTRATION( FileSystemArchiveTests );
34
setUp()35 void FileSystemArchiveTests::setUp()
36 {
37 #if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
38 testPath = "../../../../Tests/OgreMain/misc/ArchiveTest/";
39 #else
40 testPath = "../../Tests/OgreMain/misc/ArchiveTest/";
41 #endif
42 }
tearDown()43 void FileSystemArchiveTests::tearDown()
44 {
45 }
46
testListNonRecursive()47 void FileSystemArchiveTests::testListNonRecursive()
48 {
49 try {
50 FileSystemArchive arch(testPath, "FileSystem", true);
51 arch.load();
52 StringVectorPtr vec = arch.list(false);
53
54 CPPUNIT_ASSERT_EQUAL((unsigned int)2, (unsigned int)vec->size());
55 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), vec->at(0));
56 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), vec->at(1));
57 }
58 catch (Exception& e)
59 {
60 std::cout << e.getFullDescription();
61 }
62
63 }
testListRecursive()64 void FileSystemArchiveTests::testListRecursive()
65 {
66 FileSystemArchive arch(testPath, "FileSystem", true);
67 arch.load();
68 StringVectorPtr vec = arch.list(true);
69
70 CPPUNIT_ASSERT_EQUAL((size_t)48, vec->size()); // 48 including CVS folders!
71 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), vec->at(0));
72 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), vec->at(1));
73 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file.material"), vec->at(2));
74 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file2.material"), vec->at(3));
75 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file3.material"), vec->at(22));
76 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file4.material"), vec->at(23));
77 }
testListFileInfoNonRecursive()78 void FileSystemArchiveTests::testListFileInfoNonRecursive()
79 {
80 FileSystemArchive arch(testPath, "FileSystem", true);
81 arch.load();
82 FileInfoListPtr vec = arch.listFileInfo(false);
83
84 //CPPUNIT_ASSERT_EQUAL((size_t)2, vec->size());
85 //FileInfo& fi1 = vec->at(0);
86 //CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.filename);
87 //CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.basename);
88 //CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi1.path);
89 //CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.compressedSize);
90 //CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.uncompressedSize);
91
92 //FileInfo& fi2 = vec->at(1);
93 //CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.filename);
94 //CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.basename);
95 //CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi2.path);
96 //CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.compressedSize);
97 //CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.uncompressedSize);
98 }
testListFileInfoRecursive()99 void FileSystemArchiveTests::testListFileInfoRecursive()
100 {
101 FileSystemArchive arch(testPath, "FileSystem", true);
102 arch.load();
103 FileInfoListPtr vec = arch.listFileInfo(true);
104
105 CPPUNIT_ASSERT_EQUAL((size_t)48, vec->size()); // 48 including CVS folders!
106 FileInfo& fi1 = vec->at(0);
107 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.filename);
108 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.basename);
109 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi1.path);
110 CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.compressedSize);
111 CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.uncompressedSize);
112
113 FileInfo& fi2 = vec->at(1);
114 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.filename);
115 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.basename);
116 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi2.path);
117 CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.compressedSize);
118 CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.uncompressedSize);
119
120 FileInfo& fi3 = vec->at(2);
121 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file.material"), fi3.filename);
122 CPPUNIT_ASSERT_EQUAL(String("file.material"), fi3.basename);
123 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/"), fi3.path);
124 CPPUNIT_ASSERT_EQUAL((size_t)0, fi3.compressedSize);
125 CPPUNIT_ASSERT_EQUAL((size_t)0, fi3.uncompressedSize);
126
127 FileInfo& fi4 = vec->at(3);
128 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file2.material"), fi4.filename);
129 CPPUNIT_ASSERT_EQUAL(String("file2.material"), fi4.basename);
130 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/"), fi4.path);
131 CPPUNIT_ASSERT_EQUAL((size_t)0, fi4.compressedSize);
132 CPPUNIT_ASSERT_EQUAL((size_t)0, fi4.uncompressedSize);
133
134
135 FileInfo& fi5 = vec->at(22);
136 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file3.material"), fi5.filename);
137 CPPUNIT_ASSERT_EQUAL(String("file3.material"), fi5.basename);
138 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/"), fi5.path);
139 CPPUNIT_ASSERT_EQUAL((size_t)0, fi5.compressedSize);
140 CPPUNIT_ASSERT_EQUAL((size_t)0, fi5.uncompressedSize);
141
142 FileInfo& fi6 = vec->at(23);
143 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file4.material"), fi6.filename);
144 CPPUNIT_ASSERT_EQUAL(String("file4.material"), fi6.basename);
145 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/"), fi6.path);
146 CPPUNIT_ASSERT_EQUAL((size_t)0, fi6.compressedSize);
147 CPPUNIT_ASSERT_EQUAL((size_t)0, fi6.uncompressedSize);
148 }
testFindNonRecursive()149 void FileSystemArchiveTests::testFindNonRecursive()
150 {
151 FileSystemArchive arch(testPath, "FileSystem", true);
152 arch.load();
153 StringVectorPtr vec = arch.find("*.txt", false);
154
155 CPPUNIT_ASSERT_EQUAL((size_t)2, vec->size());
156 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), vec->at(0));
157 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), vec->at(1));
158 }
testFindRecursive()159 void FileSystemArchiveTests::testFindRecursive()
160 {
161 FileSystemArchive arch(testPath, "FileSystem", true);
162 arch.load();
163 StringVectorPtr vec = arch.find("*.material", true);
164
165 CPPUNIT_ASSERT_EQUAL((size_t)4, vec->size());
166 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file.material"), vec->at(0));
167 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file2.material"), vec->at(1));
168 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file3.material"), vec->at(2));
169 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file4.material"), vec->at(3));
170 }
testFindFileInfoNonRecursive()171 void FileSystemArchiveTests::testFindFileInfoNonRecursive()
172 {
173 FileSystemArchive arch(testPath, "FileSystem", true);
174 arch.load();
175 FileInfoListPtr vec = arch.findFileInfo("*.txt", false);
176
177 CPPUNIT_ASSERT_EQUAL((size_t)2, vec->size());
178 FileInfo& fi1 = vec->at(0);
179 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.filename);
180 CPPUNIT_ASSERT_EQUAL(String("rootfile.txt"), fi1.basename);
181 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi1.path);
182 CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.compressedSize);
183 CPPUNIT_ASSERT_EQUAL((size_t)130, fi1.uncompressedSize);
184
185 FileInfo& fi2 = vec->at(1);
186 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.filename);
187 CPPUNIT_ASSERT_EQUAL(String("rootfile2.txt"), fi2.basename);
188 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, fi2.path);
189 CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.compressedSize);
190 CPPUNIT_ASSERT_EQUAL((size_t)156, fi2.uncompressedSize);
191 }
testFindFileInfoRecursive()192 void FileSystemArchiveTests::testFindFileInfoRecursive()
193 {
194 FileSystemArchive arch(testPath, "FileSystem", true);
195 arch.load();
196 FileInfoListPtr vec = arch.findFileInfo("*.material", true);
197
198 CPPUNIT_ASSERT_EQUAL((size_t)4, vec->size());
199
200 FileInfo& fi3 = vec->at(0);
201 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file.material"), fi3.filename);
202 CPPUNIT_ASSERT_EQUAL(String("file.material"), fi3.basename);
203 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/"), fi3.path);
204 CPPUNIT_ASSERT_EQUAL((size_t)0, fi3.compressedSize);
205 CPPUNIT_ASSERT_EQUAL((size_t)0, fi3.uncompressedSize);
206
207 FileInfo& fi4 = vec->at(1);
208 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/file2.material"), fi4.filename);
209 CPPUNIT_ASSERT_EQUAL(String("file2.material"), fi4.basename);
210 CPPUNIT_ASSERT_EQUAL(String("level1/materials/scripts/"), fi4.path);
211 CPPUNIT_ASSERT_EQUAL((size_t)0, fi4.compressedSize);
212 CPPUNIT_ASSERT_EQUAL((size_t)0, fi4.uncompressedSize);
213
214
215 FileInfo& fi5 = vec->at(2);
216 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file3.material"), fi5.filename);
217 CPPUNIT_ASSERT_EQUAL(String("file3.material"), fi5.basename);
218 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/"), fi5.path);
219 CPPUNIT_ASSERT_EQUAL((size_t)0, fi5.compressedSize);
220 CPPUNIT_ASSERT_EQUAL((size_t)0, fi5.uncompressedSize);
221
222 FileInfo& fi6 = vec->at(3);
223 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/file4.material"), fi6.filename);
224 CPPUNIT_ASSERT_EQUAL(String("file4.material"), fi6.basename);
225 CPPUNIT_ASSERT_EQUAL(String("level2/materials/scripts/"), fi6.path);
226 CPPUNIT_ASSERT_EQUAL((size_t)0, fi6.compressedSize);
227 CPPUNIT_ASSERT_EQUAL((size_t)0, fi6.uncompressedSize);
228 }
testFileRead()229 void FileSystemArchiveTests::testFileRead()
230 {
231 FileSystemArchive arch(testPath, "FileSystem", true);
232 arch.load();
233
234 DataStreamPtr stream = arch.open("rootfile.txt");
235 CPPUNIT_ASSERT_EQUAL(String("this is line 1 in file 1"), stream->getLine());
236 CPPUNIT_ASSERT_EQUAL(String("this is line 2 in file 1"), stream->getLine());
237 CPPUNIT_ASSERT_EQUAL(String("this is line 3 in file 1"), stream->getLine());
238 CPPUNIT_ASSERT_EQUAL(String("this is line 4 in file 1"), stream->getLine());
239 CPPUNIT_ASSERT_EQUAL(String("this is line 5 in file 1"), stream->getLine());
240 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, stream->getLine()); // blank at end of file
241 CPPUNIT_ASSERT(stream->eof());
242
243 }
testReadInterleave()244 void FileSystemArchiveTests::testReadInterleave()
245 {
246 // Test overlapping reads from same archive
247
248 FileSystemArchive arch(testPath, "FileSystem", true);
249 arch.load();
250
251 // File 1
252 DataStreamPtr stream1 = arch.open("rootfile.txt");
253 CPPUNIT_ASSERT_EQUAL(String("this is line 1 in file 1"), stream1->getLine());
254 CPPUNIT_ASSERT_EQUAL(String("this is line 2 in file 1"), stream1->getLine());
255
256 // File 2
257 DataStreamPtr stream2 = arch.open("rootfile2.txt");
258 CPPUNIT_ASSERT_EQUAL(String("this is line 1 in file 2"), stream2->getLine());
259 CPPUNIT_ASSERT_EQUAL(String("this is line 2 in file 2"), stream2->getLine());
260 CPPUNIT_ASSERT_EQUAL(String("this is line 3 in file 2"), stream2->getLine());
261
262
263 // File 1
264 CPPUNIT_ASSERT_EQUAL(String("this is line 3 in file 1"), stream1->getLine());
265 CPPUNIT_ASSERT_EQUAL(String("this is line 4 in file 1"), stream1->getLine());
266 CPPUNIT_ASSERT_EQUAL(String("this is line 5 in file 1"), stream1->getLine());
267 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, stream1->getLine()); // blank at end of file
268 CPPUNIT_ASSERT(stream1->eof());
269
270
271 // File 2
272 CPPUNIT_ASSERT_EQUAL(String("this is line 4 in file 2"), stream2->getLine());
273 CPPUNIT_ASSERT_EQUAL(String("this is line 5 in file 2"), stream2->getLine());
274 CPPUNIT_ASSERT_EQUAL(String("this is line 6 in file 2"), stream2->getLine());
275 CPPUNIT_ASSERT_EQUAL(StringUtil::BLANK, stream2->getLine()); // blank at end of file
276 CPPUNIT_ASSERT(stream2->eof());
277
278 }
279
testCreateAndRemoveFile()280 void FileSystemArchiveTests::testCreateAndRemoveFile()
281 {
282 FileSystemArchive arch("./", "FileSystem", false);
283 arch.load();
284
285 CPPUNIT_ASSERT(!arch.isReadOnly());
286
287 String fileName = "a_test_file.txt";
288 DataStreamPtr stream = arch.create(fileName);
289
290 String testString = "Some text here";
291 size_t written = stream->write((void*)testString.c_str(), testString.size());
292 CPPUNIT_ASSERT_EQUAL(testString.size(), written);
293
294 stream->close();
295
296 arch.remove(fileName);
297
298 CPPUNIT_ASSERT(!arch.exists(fileName));
299
300 }
301