Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 23rd, 2013  |  syntax: C++  |  size: 12.28 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <boost/noncopyable.hpp>
  2. #include <boost/shared_ptr.hpp>
  3. #include <boost/thread/mutex.hpp>
  4.  
  5. #include <stdint.h>
  6. #include <map>
  7. #include <exception>
  8.  
  9. class DatabaseParameters;
  10. typedef boost::shared_ptr<DatabaseParameters> DatabaseParametersPtr_t;
  11.  
  12. namespace DatabaseManufacturer {
  13. typedef enum {
  14.     MYSQL,
  15.     ORACLE,
  16.     POSTGRESQL,
  17.     ODBC,
  18.     MONGODB,
  19.     SQLITE3,
  20.     UNDEFINED
  21. } Type;
  22.  
  23. inline const char* getStringFromDbManufacturer(DatabaseManufacturer::Type argManufacturer) {
  24.     switch(argManufacturer) {
  25.         case DatabaseManufacturer::MYSQL:
  26.             return "MYSQL";
  27.             break;
  28.         case DatabaseManufacturer::ORACLE:
  29.             return "ORACLE";
  30.             break;
  31.         case DatabaseManufacturer::POSTGRESQL:
  32.             return "POSTGRESQL";
  33.             break;
  34.         case DatabaseManufacturer::ODBC:
  35.             return "ODBC";
  36.             break;
  37.         case DatabaseManufacturer::MONGODB:
  38.             return "MONGODB";
  39.             break;
  40.         case DatabaseManufacturer::SQLITE3:
  41.             return "SQLITE3";
  42.             break;
  43.         default:
  44.             break;
  45.         }
  46.         return "UNDEFINED";
  47.     }
  48.  
  49. }
  50.  
  51. class DatabaseParameters {
  52. protected:
  53.     uint8_t id_;
  54.     std::string host_;
  55.     std::string user_;
  56.     std::string passwd_;
  57.     std::string db_name_;
  58.     DatabaseManufacturer::Type manufacturer_;
  59.  
  60. public:
  61.     DatabaseParameters() :
  62.                 id_(255),
  63.                 host_(""),
  64.                 user_(""),
  65.                 passwd_(""),
  66.                 db_name_(""),
  67.                 manufacturer_(DatabaseManufacturer::UNDEFINED) {
  68.     }
  69.     DatabaseParameters(
  70.             uint8_t argId,
  71.             std::string argHost,
  72.             std::string argUser,
  73.             std::string argPasswd,
  74.             std::string argDbName,
  75.             DatabaseManufacturer::Type argManufacturer,
  76.             bool argIsDefault = false) :
  77.                 id_(argId),
  78.                 host_(argHost),
  79.                 user_(argUser),
  80.                 passwd_(argPasswd),
  81.                 db_name_(argDbName),
  82.                 manufacturer_(argManufacturer) {
  83.     }
  84.     virtual ~DatabaseParameters() {}
  85.     virtual const uint8_t& getId() const {
  86.         return id_;
  87.     }
  88.     virtual void setId(const uint8_t& argId) {
  89.         id_ = argId;
  90.     }
  91.     virtual const std::string& getHost() const {
  92.         return host_;
  93.     }
  94.     virtual void setHost(std::string argHost) {
  95.         host_ = argHost;
  96.     }
  97.     virtual const std::string& getUser() const {
  98.         return user_;
  99.     }
  100.     virtual void setUser(std::string argUser) {
  101.         user_ = argUser;
  102.     }
  103.     virtual const std::string& getPasswd() const {
  104.         return passwd_;
  105.     }
  106.     virtual void setPasswd(std::string argPasswd) {
  107.         passwd_ = argPasswd;
  108.     }
  109.     virtual const std::string& getDbName() const {
  110.         return db_name_;
  111.     }
  112.     virtual void setDbName(std::string argDbName) {
  113.         db_name_ = argDbName;
  114.     }
  115.     virtual const DatabaseManufacturer::Type& getManufacturer() const {
  116.         return manufacturer_;
  117.     }
  118.     virtual void setManufacturer(const DatabaseManufacturer::Type &argManufacturer) {
  119.         manufacturer_ = argManufacturer;
  120.     }
  121. };
  122.  
  123. class DatabaseParametersLoadingException : public std::exception {
  124. protected:
  125.     std::string loading_error_;
  126.     DatabaseParametersPtr_t db_params_;
  127.  
  128.     DatabaseParametersLoadingException();
  129.  
  130. public:
  131.     DatabaseParametersLoadingException(const std::string &argLoadingError, DatabaseParametersPtr_t argDbParams) :
  132.             db_params_(argDbParams) {
  133.         std::stringstream ss;
  134.         ss << "DatabaseParametersLoadingException: " << argLoadingError << std::endl;
  135.         if(db_params_.get() != NULL) {
  136.             ss << "Id:           " << (int)db_params_->getId() <<std::endl;
  137.             ss << "Host:         " << db_params_->getHost() <<std::endl;
  138.             ss << "User:         " << db_params_->getUser() <<std::endl;
  139.             ss << "Passwd:       " << db_params_->getPasswd() <<std::endl;
  140.             ss << "DbName:       " << db_params_->getDbName() <<std::endl;
  141.             ss << "Manufacturer: " << DatabaseManufacturer::getStringFromDbManufacturer(db_params_->getManufacturer()) <<std::endl;
  142.         }
  143.         loading_error_ = ss.str();
  144.     }
  145.     virtual ~DatabaseParametersLoadingException() throw() {}
  146.  
  147.     virtual const char* what() const throw() {
  148.         return loading_error_.c_str();
  149.     }
  150. };
  151.  
  152. class DatabaseHandler : boost::noncopyable {
  153. protected:
  154.     static std::map<uint8_t,DatabaseParametersPtr_t> db_parameters_;
  155.     static std::map<DatabaseManufacturer::Type,DatabaseParametersPtr_t> db_manufacturer_default_;
  156.     static DatabaseParametersPtr_t db_default_;
  157.     static boost::mutex monitor_;
  158.     DatabaseHandler();
  159.  
  160. public:
  161.     static bool pushDatabaseParameters(DatabaseParametersPtr_t argDbParams, bool argIsGlobalDefault = false, bool argIsManufacturerDefault = false) {
  162.         boost::mutex::scoped_lock guard(monitor_);
  163.  
  164.         if(db_parameters_.find(argDbParams->getId()) != db_parameters_.end()) {
  165.             throw DatabaseParametersLoadingException("DatabaseId already exists", argDbParams);
  166.             return false;
  167.  
  168.         } else if(argIsGlobalDefault) {
  169.  
  170.             if(db_default_.get() == NULL && db_manufacturer_default_.find(argDbParams->getManufacturer()) == db_manufacturer_default_.end()) {
  171.                 db_default_ = argDbParams;
  172.                 db_manufacturer_default_.insert(std::pair<DatabaseManufacturer::Type,DatabaseParametersPtr_t>(argDbParams->getManufacturer(), argDbParams));
  173.                 db_parameters_.insert(std::pair<uint8_t,DatabaseParametersPtr_t>(argDbParams->getId(), argDbParams));
  174.                 return true;
  175.  
  176.             } else {
  177.                 throw DatabaseParametersLoadingException("Cannot set this database as the global default one (Either one is already set, "
  178.                         "or there is already a default database for this manufacturer) ", argDbParams);
  179.                 return false;
  180.             }
  181.         } else if(argIsManufacturerDefault) {
  182.  
  183.             if(db_manufacturer_default_.find(argDbParams->getManufacturer()) == db_manufacturer_default_.end()) {
  184.                 db_manufacturer_default_.insert(std::pair<DatabaseManufacturer::Type,DatabaseParametersPtr_t>(argDbParams->getManufacturer(), argDbParams));
  185.                 db_parameters_.insert(std::pair<uint8_t,DatabaseParametersPtr_t>(argDbParams->getId(), argDbParams));
  186.                 return true;
  187.             } else {
  188.                 throw DatabaseParametersLoadingException("Cannot set this database as the global default one", argDbParams);
  189.                 return false;
  190.             }
  191.         }
  192.         db_parameters_.insert(std::pair<uint8_t,DatabaseParametersPtr_t>(argDbParams->getManufacturer(), argDbParams));
  193.         return true;
  194.     }
  195.     static DatabaseParametersPtr_t getGlobalDefaultDatabase() {
  196.         return db_default_;
  197.     }
  198.     static DatabaseParametersPtr_t getManufacturerDefaultDatabase(DatabaseManufacturer::Type argManufacturer) {
  199.         std::map<DatabaseManufacturer::Type,DatabaseParametersPtr_t>::iterator it = db_manufacturer_default_.find(argManufacturer);
  200.         if(it != db_manufacturer_default_.end())
  201.             return (*it).second;
  202.  
  203.         return DatabaseParametersPtr_t();
  204.     }
  205.     static DatabaseParametersPtr_t getDatabaseParametersById(uint8_t argId) {
  206.         std::map<uint8_t,DatabaseParametersPtr_t>::iterator it = db_parameters_.find(argId);
  207.         if(it != db_parameters_.end())
  208.             return (*it).second;
  209.  
  210.         return DatabaseParametersPtr_t();
  211.     }
  212.     static const std::map<uint8_t,DatabaseParametersPtr_t>& getDatabaseParameters() {
  213.         return db_parameters_;
  214.     }
  215. };
  216.  
  217. std::map<uint8_t,DatabaseParametersPtr_t> DatabaseHandler::db_parameters_;
  218. std::map<DatabaseManufacturer::Type,DatabaseParametersPtr_t> DatabaseHandler::db_manufacturer_default_;
  219. DatabaseParametersPtr_t DatabaseHandler::db_default_ = DatabaseParametersPtr_t();
  220. boost::mutex DatabaseHandler::monitor_;
  221.  
  222. #define BOOST_LIB_DIAGNOSTIC  // diagnostic linker activity
  223. #define BOOST_TEST_DYN_LINK
  224. #define BOOST_TEST_MODULE "DMC Common API database Unit tests"
  225.  
  226. #include <boost/test/unit_test.hpp>
  227.  
  228. #include <exception>
  229. #include <sstream>
  230.  
  231. BOOST_AUTO_TEST_SUITE(common_database)
  232.  
  233. BOOST_AUTO_TEST_CASE(DatabaseHandlerTest) {
  234.  
  235.     DatabaseParametersPtr_t db_param1(new DatabaseParameters()),
  236.             db_param2(new DatabaseParameters()),
  237.             db_param3(new DatabaseParameters()),
  238.             db_param4;
  239.     bool value1 = false;
  240. //  bool value2 = true;
  241.     std::stringstream ss;
  242.  
  243.     db_param1->setId(1);
  244.     db_param1->setHost("sql1");
  245.     db_param1->setUser("root");
  246.     db_param1->setPasswd("testpwd");
  247.     db_param1->setDbName("db_test");
  248.     db_param1->setManufacturer(DatabaseManufacturer::MYSQL);
  249.  
  250.     db_param2->setId(1);
  251.     db_param2->setHost("sql2");
  252.     db_param2->setUser("root");
  253.     db_param2->setPasswd("testpwd");
  254.     db_param2->setDbName("db_test");
  255.     db_param2->setManufacturer(DatabaseManufacturer::MYSQL);
  256.  
  257.     db_param3->setId(3);
  258.     db_param3->setHost("sql3");
  259.     db_param3->setUser("root");
  260.     db_param3->setPasswd("testpwd3");
  261.     db_param3->setDbName("db_test3");
  262.     db_param3->setManufacturer(DatabaseManufacturer::ODBC);
  263.  
  264.  
  265.     try {
  266.         value1 = DatabaseHandler::pushDatabaseParameters(db_param1, true);
  267.     } catch(DatabaseParametersLoadingException &e) {
  268. //      ss << "No exception should have been raised: " << e.what();
  269.         std::cout << "No exception should have been raised: " << e.what();
  270.         BOOST_ERROR(ss.str());
  271.         ss.str("");
  272.     }
  273.  
  274.     if(!value1)
  275.         BOOST_ERROR("Error while loading global default database");
  276.  
  277.     value1 = false;
  278.  
  279. //  std::cout << "About to push param2" << std::endl;
  280.     try {
  281.         DatabaseHandler::pushDatabaseParameters(db_param2, true);
  282.     } catch(DatabaseParametersLoadingException &e) {
  283.         std::cout << "Exception raised: "<< e.what();
  284.         value1 = true;
  285.     }
  286. //  std::cout << "Pushed param2" << std::endl;
  287.  
  288.     if(!value1)
  289.         BOOST_ERROR("Exception should have been raised when "
  290.                 "pushing a database of the same id than the previous one!");
  291.  
  292.     db_param2->setId(2);
  293.     value1 = false;
  294.  
  295.     try {
  296.         DatabaseHandler::pushDatabaseParameters(db_param2, true);
  297.     } catch(DatabaseParametersLoadingException &e) {
  298.         value1 = true;
  299.     }
  300.  
  301.     if(!value1)
  302.         BOOST_ERROR("Exception should have been raised when pushing a global default "
  303.                 "database when one has already been pushed for the same!");
  304.  
  305.     db_param2->setManufacturer(DatabaseManufacturer::SQLITE3);
  306.     value1 = false;
  307.  
  308.     try {
  309.         BOOST_CHECK(DatabaseHandler::pushDatabaseParameters(db_param2, false, true) == true);
  310.     } catch(DatabaseParametersLoadingException &e) {
  311.         ss << "No exception should have been raised: " << e.what();
  312.         BOOST_ERROR(ss.str());
  313.         ss.str("");
  314.     }
  315.  
  316.     try {
  317.         BOOST_CHECK(DatabaseHandler::pushDatabaseParameters(db_param3) == true);
  318.     } catch(DatabaseParametersLoadingException &e) {
  319.         ss << "No exception should have been raised: " << e.what();
  320.         BOOST_ERROR(ss.str());
  321.         ss.str("");
  322.     }
  323.  
  324.     db_param4 = DatabaseHandler::getGlobalDefaultDatabase();
  325.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param1.get());
  326.  
  327.     db_param4 = DatabaseHandler::getManufacturerDefaultDatabase(db_param1->getManufacturer());
  328.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param1.get());
  329.  
  330.     db_param4 = DatabaseHandler::getManufacturerDefaultDatabase(db_param2->getManufacturer());
  331.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param2.get());
  332.  
  333.     db_param4 = DatabaseHandler::getDatabaseParametersById(db_param1->getId());
  334.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param1.get());
  335.  
  336.     db_param4 = DatabaseHandler::getDatabaseParametersById(db_param2->getId());
  337.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param2.get());
  338.  
  339.     db_param4 = DatabaseHandler::getDatabaseParametersById(db_param3->getId());
  340.     BOOST_CHECK(db_param4.get() != NULL && db_param4.get() == db_param3.get());
  341.  
  342.     std::cout << "OK" << std::endl;
  343. }
  344.  
  345. BOOST_AUTO_TEST_SUITE_END()