1 /***************************************************************************
2 * Copyright (C) 2004 by Intra2net AG *
5 ***************************************************************************/
18 #include <cppunit/extensions/TestFactoryRegistry.h>
19 #include <cppunit/ui/text/TestRunner.h>
20 #include <cppunit/extensions/HelperMacros.h>
22 #include <boost/archive/binary_oarchive.hpp>
23 #include <boost/archive/binary_iarchive.hpp>
24 #include <boost/archive/xml_oarchive.hpp>
25 #include <boost/archive/xml_iarchive.hpp>
26 #include <boost/serialization/serialization.hpp>
27 #include <boost/serialization/export.hpp>
29 #include <container.hxx>
30 #include <socket_client.hxx>
31 #include <socket_server.hxx>
32 #include <command_client.hxx>
33 #include <command_server.hxx>
36 using namespace CppUnit;
37 using namespace libt2n;
39 string testfunc4(const string& str)
42 throw libt2n::t2n_runtime_error("throw me around");
44 ret+=", testfunc() was here";
48 class testfunc4_res : public libt2n::result
53 friend class boost::serialization::access;
54 template<class Archive>
55 void serialize(Archive & ar, const unsigned int version)
57 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::result);
58 ar & BOOST_SERIALIZATION_NVP(res);
65 testfunc4_res(const string& str)
76 class cmd_group_a : public command
79 friend class boost::serialization::access;
80 template<class Archive>
81 void serialize(Archive & ar, const unsigned int version)
83 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
87 class cmd_group_b : public command
90 friend class boost::serialization::access;
91 template<class Archive>
92 void serialize(Archive & ar, const unsigned int version)
94 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(libt2n::command);
98 class testfunc4a_cmd : public cmd_group_a
103 friend class boost::serialization::access;
104 template<class Archive>
105 void serialize(Archive & ar, const unsigned int version)
107 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_a);
108 ar & BOOST_SERIALIZATION_NVP(param);
115 testfunc4a_cmd(const string& str)
120 libt2n::result* operator()()
122 return new testfunc4_res(testfunc4(param));
126 class testfunc4b_cmd : public cmd_group_b
131 friend class boost::serialization::access;
132 template<class Archive>
133 void serialize(Archive & ar, const unsigned int version)
135 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(cmd_group_b);
136 ar & BOOST_SERIALIZATION_NVP(param);
143 testfunc4b_cmd(const string& str)
148 libt2n::result* operator()()
150 return new testfunc4_res(testfunc4(param));
155 BOOST_CLASS_EXPORT(testfunc4_res)
156 BOOST_CLASS_EXPORT(cmd_group_a)
157 BOOST_CLASS_EXPORT(cmd_group_b)
158 BOOST_CLASS_EXPORT(testfunc4a_cmd)
159 BOOST_CLASS_EXPORT(testfunc4b_cmd)
161 class test_cmdgroup : public TestFixture
163 CPPUNIT_TEST_SUITE(test_cmdgroup);
165 CPPUNIT_TEST(GroupOk);
166 CPPUNIT_TEST(WrongGroup);
168 CPPUNIT_TEST_SUITE_END();
179 // make sure the server-child is dead before the next test runs
180 kill(child_pid,SIGKILL);
186 switch(child_pid=fork())
190 CPPUNIT_FAIL("fork error");
196 socket_server ss("./socket");
197 group_command_server<cmd_group_a> cs(ss);
200 for (int i=0; i < 10; i++)
203 // don't call atexit and stuff
210 // wait till server is up
212 socket_client_connection sc("./socket");
213 command_client cc(&sc);
216 cc.send_command(new testfunc4a_cmd("hello"),rc);
218 string ret=dynamic_cast<testfunc4_res*>(rc.get_result())->get_data();
220 CPPUNIT_ASSERT_EQUAL(string("hello, testfunc() was here"),ret);
227 switch(child_pid=fork())
231 CPPUNIT_FAIL("fork error");
237 socket_server ss("./socket");
238 group_command_server<cmd_group_b> cs(ss);
241 for (int i=0; i < 10; i++)
244 // don't call atexit and stuff
251 // wait till server is up
253 socket_client_connection sc("./socket");
254 command_client cc(&sc);
257 cc.send_command(new testfunc4a_cmd("hello"),rc);
263 ret=dynamic_cast<testfunc4_res*>(rc.get_result())->get_data();
265 catch(t2n_command_error &e)
270 string expected_what="illegal command of type ";
272 CPPUNIT_ASSERT_EQUAL(expected_what,ret.substr(0,expected_what.size()));
279 CPPUNIT_TEST_SUITE_REGISTRATION(test_cmdgroup);