2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
22 * (c) Copyright 2008 by Intra2net AG
33 #define BOOST_TEST_DYN_LINK
34 #include <boost/test/unit_test.hpp>
36 #include <i2n_global_config.hpp>
37 #include <filefunc.hxx>
38 #include <logfunc.hpp>
42 #define DOUT(msg) std::cout << msg << std::endl
44 #define DOUT(msg) do {} while (0)
50 class TestGlobalConfigFixture
53 std::set<std::string> used_check_files;
55 std::string getCheckFilepath(std::string tag)
58 result= "__unittest__" + tag + ".dat";
59 used_check_files.insert(result);
61 } // eo get_check_file_path
63 void removeCheckFiles()
65 for(std::set<std::string>::iterator it= used_check_files.begin();
66 it != used_check_files.end();
69 std::string filepath(*it);
70 if (path_exists(filepath))
75 used_check_files.clear();
76 } // eo removeCheckFiles
79 TestGlobalConfigFixture()
81 //Logger::enable_stderr_log(true);
82 //Logger::set_log_level( Logger::LogLevel::Debug );
83 used_check_files.clear();
86 ~TestGlobalConfigFixture()
89 //Logger::enable_stderr_log(false);
90 //Logger::set_log_level( Logger::LogLevel::Error );
94 BOOST_FIXTURE_TEST_SUITE(TestGlobalConfig, TestGlobalConfigFixture)
96 BOOST_AUTO_TEST_CASE(Basics)
98 std::string filename= getCheckFilepath("Basics");
100 "a path= /var/run/my.pid\n"
108 "a path= /var/run/my.pid\n"
117 bool res= write_file(filename,cfg);
118 BOOST_CHECK_EQUAL( true, res );
120 Config::Var<std::string> var_s1("sec1", "s1", "nix");
122 res= Config::set_config_file(filename);
123 BOOST_CHECK_EQUAL( true, res );
125 BOOST_CHECK_EQUAL( std::string("ein Text"), (std::string)var_s1 );
127 Config::Var<int> var_i1("sec1","i1",0);
128 BOOST_CHECK_EQUAL( 100, var_i1() );
130 Config::Var<double> var_f1("sec1","f1", 0.0);
131 BOOST_CHECK_EQUAL( 2.5, var_f1() );
133 Config::Var<const std::string> pid_path("a path", "/var/run/x.pid");
134 BOOST_CHECK_EQUAL( std::string("/var/run/my.pid"), pid_path() );
136 // try an already used var again as different type:
137 Config::Var<std::string> var_i1s("sec1","i1","0");
138 BOOST_CHECK_EQUAL( std::string("100"), var_i1s() );
141 // make a copy from a given var:
142 Config::Var< int > var_i1_2 ( var_i1 );
143 BOOST_CHECK_EQUAL( 100, var_i1_2() );
145 // now write a modified config
146 res= write_file(filename,cfg2);
147 BOOST_CHECK_EQUAL( true, res );
150 res= Config::reload();
151 BOOST_CHECK_EQUAL( true, res );
153 // check if the (right) vars changed..
155 BOOST_CHECK_EQUAL( std::string("ein Text"), (std::string)var_s1 );
156 BOOST_CHECK_EQUAL( 10, var_i1() );
157 BOOST_CHECK_EQUAL( std::string("10"), var_i1s() );
158 BOOST_CHECK_EQUAL( 3.5, var_f1() );
159 BOOST_CHECK_EQUAL( 10, var_i1_2() );
161 // check for the new vars
163 Config::Var<int, Config::AutoIntConverter<int> > var_i2("sec1","i2",0);
164 BOOST_CHECK_EQUAL( 8, var_i2() );
166 Config::Var<int, Config::AutoIntConverter<int> > var_i3("sec1","i3",0);
167 BOOST_CHECK_EQUAL( 16, var_i3() );
172 BOOST_AUTO_TEST_CASE(MultipleValues)
174 std::string filename= getCheckFilepath("MultipleValues");
193 bool res= write_file(filename,cfg);
194 BOOST_CHECK_EQUAL( true, res );
196 typedef std::list< int > IntList;
197 typedef std::vector< int > IntVector;
199 res= Config::set_config_file(filename);
200 BOOST_CHECK_EQUAL( true, res );
202 Config::Var< IntList > list1("sec1","g1", IntList());
203 Config::Var< IntList > list2("sec1","g2", IntList());
205 BOOST_CHECK_EQUAL( 4u, list1().size() );
206 BOOST_CHECK_EQUAL( 2u, list2().size() );
208 BOOST_CHECK_EQUAL( 11, list1().front() );
209 BOOST_CHECK_EQUAL( 14, list1().back() );
210 BOOST_CHECK_EQUAL( 21, list2().front() );
211 BOOST_CHECK_EQUAL( 22, list2().back() );
213 Config::Var< IntVector > vector1("sec1","g1", IntVector());
214 Config::Var< IntVector > vector2("sec1","g2", IntVector());
216 BOOST_CHECK_EQUAL( 4u, vector1().size() );
217 BOOST_CHECK_EQUAL( 2u, vector2().size() );
219 BOOST_CHECK_EQUAL( 11, vector1().front() );
220 BOOST_CHECK_EQUAL( 14, vector1().back() );
221 BOOST_CHECK_EQUAL( 21, vector2().front() );
222 BOOST_CHECK_EQUAL( 22, vector2().back() );
224 // now write a modified config
225 res= write_file(filename,cfg2);
226 BOOST_CHECK_EQUAL( true, res );
229 res= Config::reload();
230 BOOST_CHECK_EQUAL( true, res );
232 // check if the (right) vars changed..
234 BOOST_CHECK_EQUAL( 3u, list1().size() );
235 BOOST_CHECK_EQUAL( 2u, list2().size() );
237 BOOST_CHECK_EQUAL( 11, list1().front() );
238 BOOST_CHECK_EQUAL( 13, list1().back() );
239 BOOST_CHECK_EQUAL( 20, list2().front() );
240 BOOST_CHECK_EQUAL( 22, list2().back() );
242 } // eo MultipleValues
245 BOOST_AUTO_TEST_CASE(MultipleValuesVanish)
247 std::string filename= getCheckFilepath("MultipleValuesVanish");
259 bool res= write_file(filename,cfg);
260 BOOST_CHECK_EQUAL( true, res );
262 typedef std::list< int > IntList;
263 typedef std::vector< int > IntVector;
265 res= Config::set_config_file(filename);
266 BOOST_CHECK_EQUAL( true, res );
268 Config::Var< IntList > list1("sec1","g1", IntList());
269 Config::Var< IntList > list2("sec1","g2", IntList());
271 BOOST_CHECK_EQUAL( 2u, list1().size() );
272 BOOST_CHECK_EQUAL( 1u, list2().size() );
274 BOOST_CHECK_EQUAL( 11, list1().front() );
275 BOOST_CHECK_EQUAL( 12, list1().back() );
276 BOOST_CHECK_EQUAL( 21, list2().front() );
277 BOOST_CHECK_EQUAL( 21, list2().back() );
279 Config::Var< IntVector > vector1("sec1","g1", IntVector());
280 Config::Var< IntVector > vector2("sec1","g2", IntVector());
282 BOOST_CHECK_EQUAL( 2u, vector1().size() );
283 BOOST_CHECK_EQUAL( 1u, vector2().size() );
285 BOOST_CHECK_EQUAL( 11, vector1().front() );
286 BOOST_CHECK_EQUAL( 12, vector1().back() );
287 BOOST_CHECK_EQUAL( 21, vector2().front() );
288 BOOST_CHECK_EQUAL( 21, vector2().back() );
290 // now write a modified config
291 res= write_file(filename,cfg2);
292 BOOST_CHECK_EQUAL( true, res );
295 res= Config::reload();
296 BOOST_CHECK_EQUAL( true, res );
298 // check if the (right) vars changed..
300 BOOST_CHECK_EQUAL( 1u, list1().size() );
301 BOOST_CHECK_EQUAL( 0u, list2().size() );
303 BOOST_CHECK_EQUAL( 11, list1().front() );
305 } // eo MultipleValuesVanish
307 BOOST_AUTO_TEST_SUITE_END()