Debian Codegraph @

src/lib/object_store/test/DirectoryTests.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 DirectoryTests.cpp
29
30 Contains test cases to test the directory implementation
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "DirectoryTests.h"
37 #include "Directory.h"
38
39 CPPUNIT_TEST_SUITE_REGISTRATION(DirectoryTests);
40
41 void DirectoryTests::setUp()
42 {
43 #ifndef _WIN32
44 CPPUNIT_ASSERT(!system("mkdir testdir"));
45 CPPUNIT_ASSERT(!system("mkdir testdir/anotherdir"));
46 CPPUNIT_ASSERT(!system("mkdir testdir/anotherdir2"));
47 CPPUNIT_ASSERT(!system("mkdir testdir/anotherdir3"));
48 CPPUNIT_ASSERT(!system("echo someStuff > testdir/afile"));
49 CPPUNIT_ASSERT(!system("echo someOtherStuff > testdir/anotherFile"));
50 CPPUNIT_ASSERT(!system("echo justStuff > testdir/justaFile"));
51 #else
52 CPPUNIT_ASSERT(!system("mkdir testdir"));
53 CPPUNIT_ASSERT(!system("mkdir testdir\\anotherdir"));
54 CPPUNIT_ASSERT(!system("mkdir testdir\\anotherdir2"));
55 CPPUNIT_ASSERT(!system("mkdir testdir\\anotherdir3"));
56 CPPUNIT_ASSERT(!system("echo someStuff > testdir\\afile"));
57 CPPUNIT_ASSERT(!system("echo someOtherStuff > testdir\\anotherFile"));
58 CPPUNIT_ASSERT(!system("echo justStuff > testdir\\justaFile"));
59 #endif
60 }
61
62 void DirectoryTests::tearDown()
63 {
64 #ifndef _WIN32
65 CPPUNIT_ASSERT(!system("rm -rf testdir"));
66 #else
67 CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul"));
68 #endif
69 }
70
71 void DirectoryTests::testDirectory()
72 {
73 #ifndef _WIN32
74 Directory testdir("./testdir");
75 #else
76 Directory testdir(".\\testdir");
77 #endif
78
79 CPPUNIT_ASSERT(testdir.isValid());
80
81 std::vector<std::string> files = testdir.getFiles();
82 std::vector<std::string> subDirs = testdir.getSubDirs();
83
84 CPPUNIT_ASSERT(files.size() == 3);
85 CPPUNIT_ASSERT(subDirs.size() == 3);
86
87 CPPUNIT_ASSERT(testdir.refresh());
88
89 CPPUNIT_ASSERT(files.size() == 3);
90 CPPUNIT_ASSERT(subDirs.size() == 3);
91
92 bool fileSeen[3] = { false, false, false };
93
94 for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); i++)
95 {
96 if (!i->compare("afile"))
97 {
98 fileSeen[0] = true;
99 }
100 else if (!i->compare("anotherFile"))
101 {
102 fileSeen[1] = true;
103 }
104 else if (!i->compare("justaFile"))
105 {
106 fileSeen[2] = true;
107 }
108 else
109 {
110 CPPUNIT_ASSERT(false);
111 }
112 }
113
114 CPPUNIT_ASSERT(fileSeen[0] && fileSeen[1] && fileSeen[2]);
115
116 bool dirSeen[3] = { false, false, false };
117
118 for (std::vector<std::string>::iterator i = subDirs.begin(); i != subDirs.end(); i++)
119 {
120 if (!i->compare("anotherdir"))
121 {
122 dirSeen[0] = true;
123 }
124 else if (!i->compare("anotherdir2"))
125 {
126 dirSeen[1] = true;
127 }
128 else if (!i->compare("anotherdir3"))
129 {
130 dirSeen[2] = true;
131 }
132 else
133 {
134 CPPUNIT_ASSERT(false);
135 }
136 }
137
138 CPPUNIT_ASSERT(dirSeen[0] && dirSeen[1] && dirSeen[2]);
139
140 // Create a directory
141 CPPUNIT_ASSERT(testdir.mkdir("newDir"));
142
143 subDirs = testdir.getSubDirs();
144
145 bool dirSeen2[4] = { false, false, false, false };
146
147 for (std::vector<std::string>::iterator i = subDirs.begin(); i != subDirs.end(); i++)
148 {
149 if (!i->compare("anotherdir"))
150 {
151 dirSeen2[0] = true;
152 }
153 else if (!i->compare("anotherdir2"))
154 {
155 dirSeen2[1] = true;
156 }
157 else if (!i->compare("anotherdir3"))
158 {
159 dirSeen2[2] = true;
160 }
161 else if (!i->compare("newDir"))
162 {
163 dirSeen2[3] = true;
164 }
165 else
166 {
167 CPPUNIT_ASSERT(false);
168 }
169 }
170
171 CPPUNIT_ASSERT(dirSeen2[0] && dirSeen2[1] && dirSeen2[2] && dirSeen2[3]);
172
173 // Remove a directory
174 CPPUNIT_ASSERT(testdir.rmdir("anotherdir2", true));
175
176 subDirs = testdir.getSubDirs();
177
178 bool dirSeen3[3] = { false, false, false };
179
180 for (std::vector<std::string>::iterator i = subDirs.begin(); i != subDirs.end(); i++)
181 {
182 if (!i->compare("anotherdir"))
183 {
184 dirSeen3[0] = true;
185 }
186 else if (!i->compare("newDir"))
187 {
188 dirSeen3[1] = true;
189 }
190 else if (!i->compare("anotherdir3"))
191 {
192 dirSeen3[2] = true;
193 }
194 else
195 {
196 CPPUNIT_ASSERT(false);
197 }
198 }
199
200 CPPUNIT_ASSERT(dirSeen3[0] && dirSeen3[1] && dirSeen3[2]);
201
202 // Remove a file
203 CPPUNIT_ASSERT(testdir.remove("anotherFile"));
204
205 files = testdir.getFiles();
206
207 bool fileSeen2[2] = { false, false };
208
209 for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); i++)
210 {
211 if (!i->compare("afile"))
212 {
213 fileSeen2[0] = true;
214 }
215 else if (!i->compare("justaFile"))
216 {
217 fileSeen2[1] = true;
218 }
219 else
220 {
221 CPPUNIT_ASSERT(false);
222 }
223 }
224
225 CPPUNIT_ASSERT(fileSeen2[0] && fileSeen2[1]);
226 }
227