| 1 | // |
| 2 | // URIStreamOpenerTest.cpp |
| 3 | // |
| 4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
| 5 | // and Contributors. |
| 6 | // |
| 7 | // SPDX-License-Identifier: BSL-1.0 |
| 8 | // |
| 9 | |
| 10 | |
| 11 | #include "URIStreamOpenerTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/URIStreamOpener.h" |
| 15 | #include "Poco/URIStreamFactory.h" |
| 16 | #include "Poco/URI.h" |
| 17 | #include "Poco/TemporaryFile.h" |
| 18 | #include "Poco/Path.h" |
| 19 | #include <fstream> |
| 20 | #include <sstream> |
| 21 | |
| 22 | |
| 23 | using Poco::URIStreamOpener; |
| 24 | using Poco::URIStreamFactory; |
| 25 | using Poco::URI; |
| 26 | using Poco::TemporaryFile; |
| 27 | using Poco::Path; |
| 28 | |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | class StringStreamFactory: public URIStreamFactory |
| 33 | { |
| 34 | public: |
| 35 | StringStreamFactory() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | std::istream* open(const URI& uri) |
| 40 | { |
| 41 | return new std::istringstream(uri.toString()); |
| 42 | } |
| 43 | }; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | URIStreamOpenerTest::URIStreamOpenerTest(const std::string& rName): CppUnit::TestCase(rName) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | |
| 52 | URIStreamOpenerTest::~URIStreamOpenerTest() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void URIStreamOpenerTest::testStreamOpenerFile() |
| 58 | { |
| 59 | TemporaryFile tempFile; |
| 60 | std::string path = tempFile.path(); |
| 61 | std::ofstream ostr(path.c_str()); |
| 62 | assertTrue (ostr.good()); |
| 63 | ostr << "Hello, world!" << std::endl; |
| 64 | ostr.close(); |
| 65 | |
| 66 | URI uri; |
| 67 | uri.setScheme("file" ); |
| 68 | uri.setPath(Path(path).toString(Path::PATH_UNIX)); |
| 69 | std::string uriString = uri.toString(); |
| 70 | |
| 71 | URIStreamOpener opener; |
| 72 | std::istream* istr = opener.open(uri); |
| 73 | assertTrue (istr != 0); |
| 74 | assertTrue (istr->good()); |
| 75 | delete istr; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | void URIStreamOpenerTest::testStreamOpenerRelative() |
| 80 | { |
| 81 | TemporaryFile tempFile; |
| 82 | std::string path = tempFile.path(); |
| 83 | std::ofstream ostr(path.c_str()); |
| 84 | assertTrue (ostr.good()); |
| 85 | ostr << "Hello, world!" << std::endl; |
| 86 | ostr.close(); |
| 87 | |
| 88 | URI uri(Path(path).toString(Path::PATH_UNIX)); |
| 89 | std::string uriString = uri.toString(); |
| 90 | |
| 91 | URIStreamOpener opener; |
| 92 | std::istream* istr = opener.open(uri); |
| 93 | assertTrue (istr != 0); |
| 94 | assertTrue (istr->good()); |
| 95 | delete istr; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | void URIStreamOpenerTest::testStreamOpenerURI() |
| 100 | { |
| 101 | TemporaryFile tempFile; |
| 102 | std::string path = tempFile.path(); |
| 103 | std::ofstream ostr(path.c_str()); |
| 104 | assertTrue (ostr.good()); |
| 105 | ostr << "Hello, world!" << std::endl; |
| 106 | ostr.close(); |
| 107 | |
| 108 | URI uri; |
| 109 | uri.setScheme("file" ); |
| 110 | uri.setPath(Path(path).toString(Path::PATH_UNIX)); |
| 111 | std::string uriString = uri.toString(); |
| 112 | |
| 113 | URIStreamOpener opener; |
| 114 | std::istream* istr = opener.open(uriString); |
| 115 | assertTrue (istr != 0); |
| 116 | assertTrue (istr->good()); |
| 117 | delete istr; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void URIStreamOpenerTest::testStreamOpenerURIResolve() |
| 122 | { |
| 123 | TemporaryFile tempFile; |
| 124 | std::string path = tempFile.path(); |
| 125 | std::ofstream ostr(path.c_str()); |
| 126 | assertTrue (ostr.good()); |
| 127 | ostr << "Hello, world!" << std::endl; |
| 128 | ostr.close(); |
| 129 | |
| 130 | Path p(path); |
| 131 | p.makeAbsolute(); |
| 132 | Path parent(p.parent()); |
| 133 | |
| 134 | URI uri; |
| 135 | uri.setScheme("file" ); |
| 136 | uri.setPath(parent.toString(Path::PATH_UNIX)); |
| 137 | std::string uriString = uri.toString(); |
| 138 | |
| 139 | URIStreamOpener opener; |
| 140 | std::istream* istr = opener.open(uriString, p.getFileName()); |
| 141 | assertTrue (istr != 0); |
| 142 | assertTrue (istr->good()); |
| 143 | delete istr; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void URIStreamOpenerTest::testStreamOpenerPath() |
| 148 | { |
| 149 | TemporaryFile tempFile; |
| 150 | std::string path = tempFile.path(); |
| 151 | std::ofstream ostr(path.c_str()); |
| 152 | assertTrue (ostr.good()); |
| 153 | ostr << "Hello, world!" << std::endl; |
| 154 | ostr.close(); |
| 155 | |
| 156 | URIStreamOpener opener; |
| 157 | std::istream* istr = opener.open(path); |
| 158 | assertTrue (istr != 0); |
| 159 | assertTrue (istr->good()); |
| 160 | delete istr; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | void URIStreamOpenerTest::testStreamOpenerPathResolve() |
| 165 | { |
| 166 | TemporaryFile tempFile; |
| 167 | std::string path = tempFile.path(); |
| 168 | std::ofstream ostr(path.c_str()); |
| 169 | assertTrue (ostr.good()); |
| 170 | ostr << "Hello, world!" << std::endl; |
| 171 | ostr.close(); |
| 172 | |
| 173 | Path p(path); |
| 174 | Path parent(p.parent()); |
| 175 | std::string base = parent.toString(); |
| 176 | |
| 177 | URIStreamOpener opener; |
| 178 | std::istream* istr = opener.open(base, p.getFileName()); |
| 179 | assertTrue (istr != 0); |
| 180 | assertTrue (istr->good()); |
| 181 | delete istr; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | void URIStreamOpenerTest::testRegisterUnregister() |
| 186 | { |
| 187 | URIStreamOpener opener; |
| 188 | assertTrue (!opener.supportsScheme("string" )); |
| 189 | opener.registerStreamFactory("string" , new StringStreamFactory); |
| 190 | assertTrue (opener.supportsScheme("string" )); |
| 191 | URI uri("string:foobar" ); |
| 192 | std::istream* istr = opener.open(uri); |
| 193 | assertTrue (istr != 0); |
| 194 | assertTrue (istr->good()); |
| 195 | delete istr; |
| 196 | opener.unregisterStreamFactory("string" ); |
| 197 | assertTrue (!opener.supportsScheme("string" )); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | void URIStreamOpenerTest::setUp() |
| 202 | { |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void URIStreamOpenerTest::tearDown() |
| 207 | { |
| 208 | } |
| 209 | |
| 210 | |
| 211 | CppUnit::Test* URIStreamOpenerTest::suite() |
| 212 | { |
| 213 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("URIStreamOpenerTest" ); |
| 214 | |
| 215 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerFile); |
| 216 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerRelative); |
| 217 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerURI); |
| 218 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerURIResolve); |
| 219 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerPath); |
| 220 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testStreamOpenerPathResolve); |
| 221 | CppUnit_addTest(pSuite, URIStreamOpenerTest, testRegisterUnregister); |
| 222 | |
| 223 | return pSuite; |
| 224 | } |
| 225 | |