| 1 | // |
| 2 | // DirectoryWatcherTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "DirectoryWatcherTest.h" |
| 12 | |
| 13 | |
| 14 | #ifndef POCO_NO_INOTIFY |
| 15 | |
| 16 | |
| 17 | #include "Poco/CppUnit/TestCaller.h" |
| 18 | #include "Poco/CppUnit/TestSuite.h" |
| 19 | #include "Poco/DirectoryWatcher.h" |
| 20 | #include "Poco/Delegate.h" |
| 21 | #include "Poco/FileStream.h" |
| 22 | |
| 23 | |
| 24 | using Poco::DirectoryWatcher; |
| 25 | |
| 26 | |
| 27 | DirectoryWatcherTest::DirectoryWatcherTest(const std::string& rName): |
| 28 | CppUnit::TestCase(rName), |
| 29 | _error(false) |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | |
| 34 | DirectoryWatcherTest::~DirectoryWatcherTest() |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | |
| 39 | void DirectoryWatcherTest::testAdded() |
| 40 | { |
| 41 | DirectoryWatcher dw(path().toString(), DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2); |
| 42 | |
| 43 | dw.itemAdded += Poco::delegate(this, &DirectoryWatcherTest::onItemAdded); |
| 44 | dw.itemRemoved += Poco::delegate(this, &DirectoryWatcherTest::onItemRemoved); |
| 45 | dw.itemModified += Poco::delegate(this, &DirectoryWatcherTest::onItemModified); |
| 46 | dw.itemMovedFrom += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedFrom); |
| 47 | dw.itemMovedTo += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedTo); |
| 48 | |
| 49 | Poco::Thread::sleep(1000); |
| 50 | |
| 51 | Poco::Path p(path()); |
| 52 | p.setFileName("test.txt" ); |
| 53 | Poco::FileOutputStream fos(p.toString()); |
| 54 | fos << "Hello, world!" ; |
| 55 | fos.close(); |
| 56 | |
| 57 | Poco::Thread::sleep(2000*dw.scanInterval()); |
| 58 | |
| 59 | assertTrue (_events.size() >= 1); |
| 60 | assertTrue (_events[0].callback == "onItemAdded" ); |
| 61 | assertTrue (Poco::Path(_events[0].path).getFileName() == "test.txt" ); |
| 62 | assertTrue (_events[0].type == DirectoryWatcher::DW_ITEM_ADDED); |
| 63 | assertTrue (!_error); |
| 64 | } |
| 65 | |
| 66 | |
| 67 | void DirectoryWatcherTest::testRemoved() |
| 68 | { |
| 69 | Poco::Path p(path()); |
| 70 | p.setFileName("test.txt" ); |
| 71 | Poco::FileOutputStream fos(p.toString()); |
| 72 | fos << "Hello, world!" ; |
| 73 | fos.close(); |
| 74 | |
| 75 | DirectoryWatcher dw(path().toString(), DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2); |
| 76 | |
| 77 | dw.itemAdded += Poco::delegate(this, &DirectoryWatcherTest::onItemAdded); |
| 78 | dw.itemRemoved += Poco::delegate(this, &DirectoryWatcherTest::onItemRemoved); |
| 79 | dw.itemModified += Poco::delegate(this, &DirectoryWatcherTest::onItemModified); |
| 80 | dw.itemMovedFrom += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedFrom); |
| 81 | dw.itemMovedTo += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedTo); |
| 82 | |
| 83 | Poco::Thread::sleep(1000); |
| 84 | |
| 85 | Poco::File f(p.toString()); |
| 86 | f.remove(); |
| 87 | |
| 88 | Poco::Thread::sleep(2000*dw.scanInterval()); |
| 89 | |
| 90 | assertTrue (_events.size() >= 1); |
| 91 | assertTrue (_events[0].callback == "onItemRemoved" ); |
| 92 | assertTrue (Poco::Path(_events[0].path).getFileName() == "test.txt" ); |
| 93 | assertTrue (_events[0].type == DirectoryWatcher::DW_ITEM_REMOVED); |
| 94 | assertTrue (!_error); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void DirectoryWatcherTest::testModified() |
| 99 | { |
| 100 | Poco::Path p(path()); |
| 101 | p.setFileName("test.txt" ); |
| 102 | Poco::FileOutputStream fos(p.toString()); |
| 103 | fos << "Hello, world!" ; |
| 104 | fos.close(); |
| 105 | |
| 106 | DirectoryWatcher dw(path().toString(), DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2); |
| 107 | |
| 108 | dw.itemAdded += Poco::delegate(this, &DirectoryWatcherTest::onItemAdded); |
| 109 | dw.itemRemoved += Poco::delegate(this, &DirectoryWatcherTest::onItemRemoved); |
| 110 | dw.itemModified += Poco::delegate(this, &DirectoryWatcherTest::onItemModified); |
| 111 | dw.itemMovedFrom += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedFrom); |
| 112 | dw.itemMovedTo += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedTo); |
| 113 | |
| 114 | Poco::Thread::sleep(1000); |
| 115 | |
| 116 | Poco::FileOutputStream fos2(p.toString(), std::ios::app); |
| 117 | fos2 << "Again!" ; |
| 118 | fos2.close(); |
| 119 | |
| 120 | Poco::Thread::sleep(2000*dw.scanInterval()); |
| 121 | |
| 122 | assertTrue (_events.size() >= 1); |
| 123 | assertTrue (_events[0].callback == "onItemModified" ); |
| 124 | assertTrue (Poco::Path(_events[0].path).getFileName() == "test.txt" ); |
| 125 | assertTrue (_events[0].type == DirectoryWatcher::DW_ITEM_MODIFIED); |
| 126 | assertTrue (!_error); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | void DirectoryWatcherTest::testMoved() |
| 131 | { |
| 132 | Poco::Path p(path()); |
| 133 | p.setFileName("test.txt" ); |
| 134 | Poco::FileOutputStream fos(p.toString()); |
| 135 | fos << "Hello, world!" ; |
| 136 | fos.close(); |
| 137 | |
| 138 | DirectoryWatcher dw(path().toString(), DirectoryWatcher::DW_FILTER_ENABLE_ALL, 2); |
| 139 | |
| 140 | dw.itemAdded += Poco::delegate(this, &DirectoryWatcherTest::onItemAdded); |
| 141 | dw.itemRemoved += Poco::delegate(this, &DirectoryWatcherTest::onItemRemoved); |
| 142 | dw.itemModified += Poco::delegate(this, &DirectoryWatcherTest::onItemModified); |
| 143 | dw.itemMovedFrom += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedFrom); |
| 144 | dw.itemMovedTo += Poco::delegate(this, &DirectoryWatcherTest::onItemMovedTo); |
| 145 | |
| 146 | Poco::Thread::sleep(1000); |
| 147 | |
| 148 | Poco::Path p2(path()); |
| 149 | p2.setFileName("test2.txt" ); |
| 150 | Poco::File f(p.toString()); |
| 151 | f.renameTo(p2.toString()); |
| 152 | |
| 153 | Poco::Thread::sleep(2000*dw.scanInterval()); |
| 154 | |
| 155 | if (dw.supportsMoveEvents()) |
| 156 | { |
| 157 | assertTrue (_events.size() >= 2); |
| 158 | assertTrue ( |
| 159 | (_events[0].callback == "onItemMovedFrom" && _events[1].callback == "onItemMovedTo" ) || |
| 160 | (_events[1].callback == "onItemMovedFrom" && _events[0].callback == "onItemMovedTo" ) |
| 161 | ); |
| 162 | assertTrue ( |
| 163 | (Poco::Path(_events[0].path).getFileName() == "test.txt" && Poco::Path(_events[1].path).getFileName() == "test2.txt" ) || |
| 164 | (Poco::Path(_events[1].path).getFileName() == "test.txt" && Poco::Path(_events[0].path).getFileName() == "test2.txt" ) |
| 165 | ); |
| 166 | assertTrue ( |
| 167 | (_events[0].type == DirectoryWatcher::DW_ITEM_MOVED_FROM && _events[1].type == DirectoryWatcher::DW_ITEM_MOVED_TO) || |
| 168 | (_events[1].type == DirectoryWatcher::DW_ITEM_MOVED_FROM && _events[0].type == DirectoryWatcher::DW_ITEM_MOVED_TO) |
| 169 | ); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | assertTrue (_events.size() >= 2); |
| 174 | assertTrue ( |
| 175 | (_events[0].callback == "onItemAdded" && _events[1].callback == "onItemRemoved" ) || |
| 176 | (_events[1].callback == "onItemAdded" && _events[0].callback == "onItemRemoved" ) |
| 177 | ); |
| 178 | assertTrue ( |
| 179 | (Poco::Path(_events[0].path).getFileName() == "test.txt" && Poco::Path(_events[1].path).getFileName() == "test2.txt" ) || |
| 180 | (Poco::Path(_events[1].path).getFileName() == "test.txt" && Poco::Path(_events[0].path).getFileName() == "test2.txt" ) |
| 181 | ); |
| 182 | assertTrue ( |
| 183 | (_events[0].type == DirectoryWatcher::DW_ITEM_ADDED && _events[1].type == DirectoryWatcher::DW_ITEM_REMOVED) || |
| 184 | (_events[1].type == DirectoryWatcher::DW_ITEM_ADDED && _events[0].type == DirectoryWatcher::DW_ITEM_REMOVED) |
| 185 | ); |
| 186 | } |
| 187 | assertTrue (!_error); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | void DirectoryWatcherTest::setUp() |
| 192 | { |
| 193 | _error = false; |
| 194 | _events.clear(); |
| 195 | |
| 196 | try |
| 197 | { |
| 198 | Poco::File d(path().toString()); |
| 199 | d.remove(true); |
| 200 | } |
| 201 | catch (...) |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | Poco::File d(path().toString()); |
| 206 | d.createDirectories(); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | void DirectoryWatcherTest::tearDown() |
| 211 | { |
| 212 | try |
| 213 | { |
| 214 | Poco::File d(path().toString()); |
| 215 | d.remove(true); |
| 216 | } |
| 217 | catch (...) |
| 218 | { |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | void DirectoryWatcherTest::onItemAdded(const Poco::DirectoryWatcher::DirectoryEvent& ev) |
| 224 | { |
| 225 | DirEvent de; |
| 226 | de.callback = "onItemAdded" ; |
| 227 | de.path = ev.item.path(); |
| 228 | de.type = ev.event; |
| 229 | _events.push_back(de); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | void DirectoryWatcherTest::onItemRemoved(const Poco::DirectoryWatcher::DirectoryEvent& ev) |
| 234 | { |
| 235 | DirEvent de; |
| 236 | de.callback = "onItemRemoved" ; |
| 237 | de.path = ev.item.path(); |
| 238 | de.type = ev.event; |
| 239 | _events.push_back(de); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | void DirectoryWatcherTest::onItemModified(const Poco::DirectoryWatcher::DirectoryEvent& ev) |
| 244 | { |
| 245 | DirEvent de; |
| 246 | de.callback = "onItemModified" ; |
| 247 | de.path = ev.item.path(); |
| 248 | de.type = ev.event; |
| 249 | _events.push_back(de); |
| 250 | } |
| 251 | |
| 252 | |
| 253 | void DirectoryWatcherTest::onItemMovedFrom(const Poco::DirectoryWatcher::DirectoryEvent& ev) |
| 254 | { |
| 255 | DirEvent de; |
| 256 | de.callback = "onItemMovedFrom" ; |
| 257 | de.path = ev.item.path(); |
| 258 | de.type = ev.event; |
| 259 | _events.push_back(de); |
| 260 | } |
| 261 | |
| 262 | |
| 263 | void DirectoryWatcherTest::onItemMovedTo(const Poco::DirectoryWatcher::DirectoryEvent& ev) |
| 264 | { |
| 265 | DirEvent de; |
| 266 | de.callback = "onItemMovedTo" ; |
| 267 | de.path = ev.item.path(); |
| 268 | de.type = ev.event; |
| 269 | _events.push_back(de); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | void DirectoryWatcherTest::onError(const Poco::Exception& exc) |
| 274 | { |
| 275 | _error = true; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | Poco::Path DirectoryWatcherTest::path() const |
| 280 | { |
| 281 | Poco::Path p(Poco::Path::current()); |
| 282 | p.pushDirectory("DirectoryWatcherTest" ); |
| 283 | return p; |
| 284 | } |
| 285 | |
| 286 | |
| 287 | CppUnit::Test* DirectoryWatcherTest::suite() |
| 288 | { |
| 289 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("DirectoryWatcherTest" ); |
| 290 | |
| 291 | CppUnit_addTest(pSuite, DirectoryWatcherTest, testAdded); |
| 292 | CppUnit_addTest(pSuite, DirectoryWatcherTest, testRemoved); |
| 293 | CppUnit_addTest(pSuite, DirectoryWatcherTest, testModified); |
| 294 | CppUnit_addTest(pSuite, DirectoryWatcherTest, testMoved); |
| 295 | |
| 296 | return pSuite; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | #endif // POCO_NO_INOTIFY |
| 301 | |