--- /dev/null
+/** @file
+ *
+ * (c) Copyright 2008 by Intra2net AG
+ *
+ * info@intra2net.com
+ */
+
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <iomanip>
+#include <list>
+#include <vector>
+
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <i2n_global_config.hpp>
+#include <filefunc.hxx>
+#include <logfunc.hpp>
+
+
+#ifdef NOISEDEBUG
+#define DOUT(msg) std::cout << msg << std::endl
+#else
+#define DOUT(msg) do {} while (0)
+#endif
+
+
+using namespace I2n;
+
+
+using namespace CppUnit;
+
+
+class TestGlobalConfig : public TestFixture
+{
+ CPPUNIT_TEST_SUITE(TestGlobalConfig);
+
+ CPPUNIT_TEST(Basics);
+ CPPUNIT_TEST(MultipleValues);
+
+ CPPUNIT_TEST_SUITE_END();
+
+ protected:
+
+ std::set<std::string> 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<std::string>::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:
+
+ void setUp()
+ {
+ //Logger::enable_stderr_log(true);
+ //Logger::set_log_level( Logger::LogLevel::Debug );
+ used_check_files.clear();
+ } // eo setUp
+
+
+ void tearDown()
+ {
+ removeCheckFiles();
+ //Logger::enable_stderr_log(false);
+ //Logger::set_log_level( Logger::LogLevel::Error );
+ } // eo tearDown
+
+
+ /*
+ * the tests:
+ */
+
+
+
+ void 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);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ Config::Var<std::string> var_s1("sec1", "s1", "nix");
+
+ res= Config::set_config_file(filename);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ CPPUNIT_ASSERT_EQUAL( std::string("ein Text"), (std::string)var_s1 );
+
+ Config::Var<int> var_i1("sec1","i1",0);
+ CPPUNIT_ASSERT_EQUAL( 100, var_i1() );
+
+ Config::Var<double> var_f1("sec1","f1", 0.0);
+ CPPUNIT_ASSERT_EQUAL( 2.5, var_f1() );
+
+ Config::Var<const std::string> pid_path("a path", "/var/run/x.pid");
+ CPPUNIT_ASSERT_EQUAL( std::string("/var/run/my.pid"), pid_path() );
+
+ // try an already used var again as different type:
+ Config::Var<std::string> var_i1s("sec1","i1","0");
+ CPPUNIT_ASSERT_EQUAL( std::string("100"), var_i1s() );
+
+
+ // make a copy from a given var:
+ Config::Var< int > var_i1_2 ( var_i1 );
+ CPPUNIT_ASSERT_EQUAL( 100, var_i1_2() );
+
+ // now write a modified config
+ res= write_file(filename,cfg2);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ // .. and reload.
+ res= Config::reload();
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ // check if the (right) vars changed..
+
+ CPPUNIT_ASSERT_EQUAL( std::string("ein Text"), (std::string)var_s1 );
+ CPPUNIT_ASSERT_EQUAL( 10, var_i1() );
+ CPPUNIT_ASSERT_EQUAL( std::string("10"), var_i1s() );
+ CPPUNIT_ASSERT_EQUAL( 3.5, var_f1() );
+ CPPUNIT_ASSERT_EQUAL( 10, var_i1_2() );
+
+ // check for the new vars
+
+ Config::Var<int, Config::AutoIntConverter<int> > var_i2("sec1","i2",0);
+ CPPUNIT_ASSERT_EQUAL( 8, var_i2() );
+
+ Config::Var<int, Config::AutoIntConverter<int> > var_i3("sec1","i3",0);
+ CPPUNIT_ASSERT_EQUAL( 16, var_i3() );
+ } // eo Basics
+
+
+
+ void 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);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ typedef std::list< int > IntList;
+ typedef std::vector< int > IntVector;
+
+ res= Config::set_config_file(filename);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ Config::Var< IntList > list1("sec1","g1", IntList());
+ Config::Var< IntList > list2("sec1","g2", IntList());
+
+ CPPUNIT_ASSERT_EQUAL( 4u, list1().size() );
+ CPPUNIT_ASSERT_EQUAL( 2u, list2().size() );
+
+ CPPUNIT_ASSERT_EQUAL( 11, list1().front() );
+ CPPUNIT_ASSERT_EQUAL( 14, list1().back() );
+ CPPUNIT_ASSERT_EQUAL( 21, list2().front() );
+ CPPUNIT_ASSERT_EQUAL( 22, list2().back() );
+
+ Config::Var< IntVector > vector1("sec1","g1", IntVector());
+ Config::Var< IntVector > vector2("sec1","g2", IntVector());
+
+ CPPUNIT_ASSERT_EQUAL( 4u, vector1().size() );
+ CPPUNIT_ASSERT_EQUAL( 2u, vector2().size() );
+
+ CPPUNIT_ASSERT_EQUAL( 11, vector1().front() );
+ CPPUNIT_ASSERT_EQUAL( 14, vector1().back() );
+ CPPUNIT_ASSERT_EQUAL( 21, vector2().front() );
+ CPPUNIT_ASSERT_EQUAL( 22, vector2().back() );
+
+ // now write a modified config
+ res= write_file(filename,cfg2);
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ // .. and reload.
+ res= Config::reload();
+ CPPUNIT_ASSERT_EQUAL( true, res );
+
+ // check if the (right) vars changed..
+
+ CPPUNIT_ASSERT_EQUAL( 3u, list1().size() );
+ CPPUNIT_ASSERT_EQUAL( 2u, list2().size() );
+
+ CPPUNIT_ASSERT_EQUAL( 11, list1().front() );
+ CPPUNIT_ASSERT_EQUAL( 13, list1().back() );
+ CPPUNIT_ASSERT_EQUAL( 20, list2().front() );
+ CPPUNIT_ASSERT_EQUAL( 22, list2().back() );
+
+ } // eo MultipleValues
+
+
+}; // eo class TestGlobalConfig
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestGlobalConfig);