/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /** @file * * (c) Copyright 2008 by Intra2net AG */ #include #include #include #include #include #include #include #define BOOST_TEST_DYN_LINK #include #include #include #include #ifdef NOISEDEBUG #define DOUT(msg) std::cout << msg << std::endl #else #define DOUT(msg) do {} while (0) #endif using namespace I2n; class TestGlobalConfigFixture { protected: std::set used_check_files; std::string getCheckFilepath(std::string tag) { std::string result; result= "__unittest__" + tag + ".dat"; used_check_files.insert(result); return result; } // eo get_check_file_path void removeCheckFiles() { for(std::set::iterator it= used_check_files.begin(); it != used_check_files.end(); ++it) { std::string filepath(*it); if (path_exists(filepath)) { unlink(filepath); } } used_check_files.clear(); } // eo removeCheckFiles public: TestGlobalConfigFixture() { //Logger::enable_stderr_log(true); //Logger::set_log_level( Logger::LogLevel::Debug ); used_check_files.clear(); } ~TestGlobalConfigFixture() { removeCheckFiles(); //Logger::enable_stderr_log(false); //Logger::set_log_level( Logger::LogLevel::Error ); } }; BOOST_FIXTURE_TEST_SUITE(TestGlobalConfig, TestGlobalConfigFixture) BOOST_AUTO_TEST_CASE(Basics) { std::string filename= getCheckFilepath("Basics"); std::string cfg( "a path= /var/run/my.pid\n" "\n" "[sec1]\n" "s1 = ein Text\n" "i1 = 100\n" "f1 = 2.5\n" ); std::string cfg2( "a path= /var/run/my.pid\n" "\n" "[sec1]\n" "s1 = ein Text\n" "i1 = 10\n" "f1 = 3.5\n" "i2 = 010\n" "i3 = 0x10\n" ); bool res= write_file(filename,cfg); BOOST_CHECK_EQUAL( true, res ); Config::Var var_s1("sec1", "s1", "nix"); res= Config::set_config_file(filename); BOOST_CHECK_EQUAL( true, res ); BOOST_CHECK_EQUAL( std::string("ein Text"), (std::string)var_s1 ); Config::Var var_i1("sec1","i1",0); BOOST_CHECK_EQUAL( 100, var_i1() ); Config::Var var_f1("sec1","f1", 0.0); BOOST_CHECK_EQUAL( 2.5, var_f1() ); Config::Var pid_path("a path", "/var/run/x.pid"); BOOST_CHECK_EQUAL( std::string("/var/run/my.pid"), pid_path() ); // try an already used var again as different type: Config::Var var_i1s("sec1","i1","0"); BOOST_CHECK_EQUAL( std::string("100"), var_i1s() ); // make a copy from a given var: Config::Var< int > var_i1_2 ( var_i1 ); BOOST_CHECK_EQUAL( 100, var_i1_2() ); // now write a modified config res= write_file(filename,cfg2); BOOST_CHECK_EQUAL( true, res ); // .. and reload. res= Config::reload(); BOOST_CHECK_EQUAL( true, res ); // check if the (right) vars changed.. BOOST_CHECK_EQUAL( std::string("ein Text"), (std::string)var_s1 ); BOOST_CHECK_EQUAL( 10, var_i1() ); BOOST_CHECK_EQUAL( std::string("10"), var_i1s() ); BOOST_CHECK_EQUAL( 3.5, var_f1() ); BOOST_CHECK_EQUAL( 10, var_i1_2() ); // check for the new vars Config::Var > var_i2("sec1","i2",0); BOOST_CHECK_EQUAL( 8, var_i2() ); Config::Var > var_i3("sec1","i3",0); BOOST_CHECK_EQUAL( 16, var_i3() ); } // eo Basics BOOST_AUTO_TEST_CASE(MultipleValues) { std::string filename= getCheckFilepath("MultipleValues"); std::string cfg( "[sec1]\n" "g1 = 11\n" "g1 = 12\n" "g2 = 21\n" "g2 = 22\n" "g1 = 13\n" "g1 = 14\n" ); std::string cfg2( "[sec1]\n" "g1 = 11\n" "g1 = 12\n" "g2 = 20\n" "g2 = 22\n" "g1 = 13\n" ); bool res= write_file(filename,cfg); BOOST_CHECK_EQUAL( true, res ); typedef std::list< int > IntList; typedef std::vector< int > IntVector; res= Config::set_config_file(filename); BOOST_CHECK_EQUAL( true, res ); Config::Var< IntList > list1("sec1","g1", IntList()); Config::Var< IntList > list2("sec1","g2", IntList()); BOOST_CHECK_EQUAL( 4u, list1().size() ); BOOST_CHECK_EQUAL( 2u, list2().size() ); BOOST_CHECK_EQUAL( 11, list1().front() ); BOOST_CHECK_EQUAL( 14, list1().back() ); BOOST_CHECK_EQUAL( 21, list2().front() ); BOOST_CHECK_EQUAL( 22, list2().back() ); Config::Var< IntVector > vector1("sec1","g1", IntVector()); Config::Var< IntVector > vector2("sec1","g2", IntVector()); BOOST_CHECK_EQUAL( 4u, vector1().size() ); BOOST_CHECK_EQUAL( 2u, vector2().size() ); BOOST_CHECK_EQUAL( 11, vector1().front() ); BOOST_CHECK_EQUAL( 14, vector1().back() ); BOOST_CHECK_EQUAL( 21, vector2().front() ); BOOST_CHECK_EQUAL( 22, vector2().back() ); // now write a modified config res= write_file(filename,cfg2); BOOST_CHECK_EQUAL( true, res ); // .. and reload. res= Config::reload(); BOOST_CHECK_EQUAL( true, res ); // check if the (right) vars changed.. BOOST_CHECK_EQUAL( 3u, list1().size() ); BOOST_CHECK_EQUAL( 2u, list2().size() ); BOOST_CHECK_EQUAL( 11, list1().front() ); BOOST_CHECK_EQUAL( 13, list1().back() ); BOOST_CHECK_EQUAL( 20, list2().front() ); BOOST_CHECK_EQUAL( 22, list2().back() ); } // eo MultipleValues BOOST_AUTO_TEST_CASE(MultipleValuesVanish) { std::string filename= getCheckFilepath("MultipleValuesVanish"); std::string cfg( "[sec1]\n" "g1 = 11\n" "g1 = 12\n" "g2 = 21\n" ); std::string cfg2( "[sec1]\n" "g1 = 11\n" ); bool res= write_file(filename,cfg); BOOST_CHECK_EQUAL( true, res ); typedef std::list< int > IntList; typedef std::vector< int > IntVector; res= Config::set_config_file(filename); BOOST_CHECK_EQUAL( true, res ); Config::Var< IntList > list1("sec1","g1", IntList()); Config::Var< IntList > list2("sec1","g2", IntList()); BOOST_CHECK_EQUAL( 2u, list1().size() ); BOOST_CHECK_EQUAL( 1u, list2().size() ); BOOST_CHECK_EQUAL( 11, list1().front() ); BOOST_CHECK_EQUAL( 12, list1().back() ); BOOST_CHECK_EQUAL( 21, list2().front() ); BOOST_CHECK_EQUAL( 21, list2().back() ); Config::Var< IntVector > vector1("sec1","g1", IntVector()); Config::Var< IntVector > vector2("sec1","g2", IntVector()); BOOST_CHECK_EQUAL( 2u, vector1().size() ); BOOST_CHECK_EQUAL( 1u, vector2().size() ); BOOST_CHECK_EQUAL( 11, vector1().front() ); BOOST_CHECK_EQUAL( 12, vector1().back() ); BOOST_CHECK_EQUAL( 21, vector2().front() ); BOOST_CHECK_EQUAL( 21, vector2().back() ); // now write a modified config res= write_file(filename,cfg2); BOOST_CHECK_EQUAL( true, res ); // .. and reload. res= Config::reload(); BOOST_CHECK_EQUAL( true, res ); // check if the (right) vars changed.. BOOST_CHECK_EQUAL( 1u, list1().size() ); BOOST_CHECK_EQUAL( 0u, list2().size() ); BOOST_CHECK_EQUAL( 11, list1().front() ); } // eo MultipleValuesVanish BOOST_AUTO_TEST_SUITE_END()