diff --git a/CHANGELOG b/CHANGELOG index 60daef91f..dbf9e1b50 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -62,6 +62,8 @@ Release 1.5.3 (2014-05-xx) - fixed GH #341: Compiling poco-1.5.2 for Cygwin - fixed GH #305: There are bugs in Buffer.h - fixed GH #321: trivial build fixes (BB QNX build) +- fixed GH #440: MongoDB ObjectId string formatting +- added SevenZip library (Guenter Obiltschnig) Release 1.5.2 (2013-09-16) ========================== diff --git a/MongoDB/include/Poco/MongoDB/ObjectId.h b/MongoDB/include/Poco/MongoDB/ObjectId.h index 375ae3c5d..289ecaa5e 100755 --- a/MongoDB/include/Poco/MongoDB/ObjectId.h +++ b/MongoDB/include/Poco/MongoDB/ObjectId.h @@ -46,7 +46,7 @@ class MongoDB_API ObjectId public: typedef SharedPtr Ptr; - ObjectId(); + ObjectId(const std::string& id = ""); /// Constructor virtual ~ObjectId(); @@ -55,8 +55,10 @@ public: Timestamp timestamp() const; /// Returns the timestamp which is stored in the first four bytes of the id - std::string toString() const; - /// Returns the id in string format + std::string toString(const std::string& fmt = "%x") const; + /// Returns the id in string format. The fmt parameter + /// specifies the formatting used for individual members + /// of the ID char array. private: unsigned char _id[12]; @@ -85,9 +87,11 @@ struct ElementTraits { enum { TypeId = 0x07 }; - static std::string toString(const ObjectId::Ptr& id, int indent = 0) + static std::string toString(const ObjectId::Ptr& id, + int indent = 0, + const std::string& fmt = "%x") { - return id->toString(); + return id->toString(fmt); } }; diff --git a/MongoDB/src/ObjectId.cpp b/MongoDB/src/ObjectId.cpp index c3acd7168..257fa1c18 100755 --- a/MongoDB/src/ObjectId.cpp +++ b/MongoDB/src/ObjectId.cpp @@ -24,8 +24,18 @@ namespace Poco { namespace MongoDB { -ObjectId::ObjectId() +ObjectId::ObjectId(const std::string& id) { + if (id.size() == 12) + { + std::string::const_iterator it = id.begin(); + std::string::const_iterator end = id.end(); + for (int i = 0; i < 12; ++it, ++i) _id[i] = *it; + } + else if (id.size()) + { + throw Poco::InvalidArgumentException("ID must be 12 characters long."); + } } @@ -34,15 +44,16 @@ ObjectId::~ObjectId() } -std::string ObjectId::toString() const +std::string ObjectId::toString(const std::string& fmt) const { std::string s; for(int i = 0; i < 12; ++i) { - s += format("%x", (unsigned int) _id[i]); + s += format(fmt, (unsigned int) _id[i]); } return s; } + } } // namespace Poco::MongoDB diff --git a/MongoDB/testsuite/src/MongoDBTest.cpp b/MongoDB/testsuite/src/MongoDBTest.cpp index 33f87e7ce..0714923cd 100755 --- a/MongoDB/testsuite/src/MongoDBTest.cpp +++ b/MongoDB/testsuite/src/MongoDBTest.cpp @@ -20,6 +20,7 @@ #include "Poco/MongoDB/PoolableConnectionFactory.h" #include "Poco/MongoDB/Database.h" #include "Poco/MongoDB/Cursor.h" +#include "Poco/MongoDB/ObjectId.h" #include "Poco/Net/NetException.h" @@ -30,46 +31,58 @@ using namespace Poco::MongoDB; -MongoDBTest::MongoDBTest(const std::string& name) - : CppUnit::TestCase("MongoDB") - , _connected(false) +bool MongoDBTest::_connected = false; +Poco::MongoDB::Connection MongoDBTest::_mongo; + + +MongoDBTest::MongoDBTest(const std::string& name): + CppUnit::TestCase("MongoDB"), + _host("localhost"), + _port(27017) { + if (!_connected) + { + try + { + _mongo.connect(_host, _port); + _connected = true; + std::cout << "Connected to [" << _host << ':' << _port << ']' << std::endl; + } + catch (Poco::Net::ConnectionRefusedException& e) + { + std::cout << "Couldn't connect to " << e.message() << ". " << std::endl; + } + } } MongoDBTest::~MongoDBTest() { + if (_connected) + { + _mongo.disconnect(); + _connected = false; + std::cout << "Disconnected from [" << _host << ':' << _port << ']' << std::endl; + } } void MongoDBTest::setUp() { - try - { - _mongo.connect("localhost", 27017); - _connected = true; - } - catch(Poco::Net::ConnectionRefusedException& e) - { - std::cout << "Couldn't connect to " << e.message() << ". "; - } + } void MongoDBTest::tearDown() { - if ( _connected ) - { - _mongo.disconnect(); - } } void MongoDBTest::testInsertRequest() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -97,9 +110,9 @@ void MongoDBTest::testInsertRequest() void MongoDBTest::testQueryRequest() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -145,9 +158,9 @@ void MongoDBTest::testQueryRequest() void MongoDBTest::testDBQueryRequest() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -191,9 +204,9 @@ void MongoDBTest::testDBQueryRequest() void MongoDBTest::testCountCommand() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -220,9 +233,9 @@ void MongoDBTest::testCountCommand() void MongoDBTest::testDBCountCommand() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -247,9 +260,9 @@ void MongoDBTest::testDBCountCommand() void MongoDBTest::testDBCount2Command() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -261,9 +274,9 @@ void MongoDBTest::testDBCount2Command() void MongoDBTest::testDeleteRequest() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -276,9 +289,9 @@ void MongoDBTest::testDeleteRequest() void MongoDBTest::testCursorRequest() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -325,9 +338,9 @@ void MongoDBTest::testCursorRequest() void MongoDBTest::testBuildInfo() { - if ( ! _connected ) + if (!_connected) { - std::cout << "test skipped." << std::endl; + std::cout << "Not connected, test skipped." << std::endl; return; } @@ -361,7 +374,8 @@ void MongoDBTest::testBuildInfo() void MongoDBTest::testConnectionPool() { - Poco::PoolableObjectFactory factory("localhost:27017"); + Poco::Net::SocketAddress sa(_host, _port); + Poco::PoolableObjectFactory factory(sa); Poco::ObjectPool pool(factory, 10, 15); Poco::MongoDB::PooledConnection pooledConnection(pool); @@ -385,6 +399,29 @@ void MongoDBTest::testConnectionPool() } } + +void MongoDBTest::testObjectID() +{ + std::string str; + str.append(1, (char) 0x53); + str.append(1, (char) 0x6A); + str.append(1, (char) 0xEE); + str.append(1, (char) 0xBB); + str.append(1, (char) 0xA0); + str.append(1, (char) 0x81); + str.append(1, (char) 0xDE); + str.append(1, (char) 0x68); + str.append(1, (char) 0x15); + str.append(1, (char) 0x00); + str.append(1, (char) 0x00); + str.append(1, (char) 0x02); + + ObjectId oid(str); + std::string str2 = oid.toString("%02x"); + assert(str2 == "536aeebba081de6815000002"); +} + + CppUnit::Test* MongoDBTest::suite() { CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MongoDBTest"); @@ -399,6 +436,7 @@ CppUnit::Test* MongoDBTest::suite() CppUnit_addTest(pSuite, MongoDBTest, testDeleteRequest); CppUnit_addTest(pSuite, MongoDBTest, testBuildInfo); CppUnit_addTest(pSuite, MongoDBTest, testCursorRequest); + CppUnit_addTest(pSuite, MongoDBTest, testObjectID); return pSuite; } diff --git a/MongoDB/testsuite/src/MongoDBTest.h b/MongoDB/testsuite/src/MongoDBTest.h index 2090dd2d7..7e7de188f 100755 --- a/MongoDB/testsuite/src/MongoDBTest.h +++ b/MongoDB/testsuite/src/MongoDBTest.h @@ -30,50 +30,28 @@ public: virtual ~MongoDBTest(); - void testInsertRequest(); - - void testQueryRequest(); - - void testDBQueryRequest(); - - void testCountCommand(); - - void testDBCountCommand(); - - void testDBCount2Command(); - - void testDeleteRequest(); - - void testBuildInfo(); - - void testConnectionPool(); - - void testCursorRequest(); - - + void testObjectID(); void setUp(); - - void tearDown(); - static CppUnit::Test* suite(); private: - bool _connected; - - Poco::MongoDB::Connection _mongo; + std::string _host; + unsigned _port; + static bool _connected; + static Poco::MongoDB::Connection _mongo; };