1 /* $Id: test_router_flexml.cpp,v 1.15 2006-01-11 14:58:28 adam Exp $
2 Copyright (c) 2005, Index Data.
12 #include "router_flexml.hpp"
13 #include "factory_static.hpp"
15 #define BOOST_AUTO_TEST_MAIN
16 #include <boost/test/auto_unit_test.hpp>
18 using namespace boost::unit_test;
20 static int tfilter_ref = 0;
21 class TFilter: public yp2::filter::Base {
23 void process(yp2::Package & package) const {};
24 TFilter() { tfilter_ref++; };
25 ~TFilter() { tfilter_ref--; };
28 static yp2::filter::Base* filter_creator()
33 // Pass well-formed XML and valid configuration to it (implicit NS)
34 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
38 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
39 "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
40 " <start route=\"start\"/>\n"
42 " <filter id=\"front_default\" type=\"frontend_net\">\n"
43 " <port>@:210</port>\n"
45 " <filter id=\"log_cout1\" type=\"log\">\n"
46 " <message>my msg</message>\n"
48 " <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
49 " <filter id=\"log_cout2\" type=\"log\">\n"
50 " <message>other</message>\n"
54 " <route id=\"start\">\n"
55 " <filter refid=\"front_default\"/>\n"
56 " <filter refid=\"log_cout1\"/>\n"
57 " <filter type=\"tfilter\">\n"
59 " <filter type=\"z3950_client\">\n"
65 yp2::FactoryStatic factory;
66 factory.add_creator("tfilter", filter_creator);
67 yp2::RouterFleXML rflexml(xmlconf, factory);
68 BOOST_CHECK_EQUAL(tfilter_ref, 2);
70 catch ( std::runtime_error &e) {
71 std::cout << "std::runtime error: " << e.what() << "\n";
77 BOOST_CHECK_EQUAL(tfilter_ref, 0);
80 // Pass non-wellformed XML
81 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
83 bool got_error_as_expected = false;
86 std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
87 "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
88 " <start route=\"start\"/>\n"
90 " <filter id=\"front_default\" type=\"frontend_net\">\n"
91 " <port>@:210</port>\n";
93 yp2::FactoryFilter factory;
94 yp2::RouterFleXML rflexml(xmlconf_invalid, factory);
96 catch ( yp2::XMLError &e) {
97 std::cout << "XMLError: " << e.what() << "\n";
98 got_error_as_expected = true;
100 catch ( std::runtime_error &e) {
101 std::cout << "std::runtime error: " << e.what() << "\n";
106 BOOST_CHECK(got_error_as_expected);
109 // Pass well-formed XML with explicit NS
110 BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
114 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
115 "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
116 " <y:start route=\"start\"/>\n"
118 " <y:filter id=\"front_default\" type=\"frontend_net\">\n"
119 " <port>@:210</port>\n"
121 " <y:filter id=\"log_cout\" type=\"log\">\n"
122 " <message>my msg</message>\n"
126 " <y:route id=\"start\">\n"
127 " <y:filter refid=\"front_default\"/>\n"
128 " <y:filter refid=\"log_cout\"/>\n"
133 yp2::FactoryStatic factory;
134 yp2::RouterFleXML rflexml(xmlconf, factory);
136 catch ( std::runtime_error &e) {
137 std::cout << "std::runtime error: " << e.what() << "\n";
145 // Pass well-formed XML but bad filter type
146 BOOST_AUTO_UNIT_TEST( test_router_flexml_4 )
148 bool got_error_as_expected = false;
151 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
152 "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
153 " <start route=\"start\"/>\n"
155 " <filter id=\"front_default\" type=\"notknown\">\n"
156 " <port>@:210</port>\n"
160 " <route id=\"start\">\n"
161 " <filter refid=\"front_default\"/>\n"
166 yp2::FactoryStatic factory;
167 factory.add_creator("tfilter", filter_creator);
168 yp2::RouterFleXML rflexml(xmlconf, factory);
170 catch ( yp2::FactoryFilter::NotFound &e) {
171 std::cout << "yp2::FactoryFilter::NotFound: " << e.what() << "\n";
172 got_error_as_expected = true;
174 catch ( std::runtime_error &e) {
175 std::cout << "std::runtime error: " << e.what() << "\n";
177 BOOST_CHECK(got_error_as_expected);
184 * indent-tabs-mode: nil
185 * c-file-style: "stroustrup"
187 * vim: shiftwidth=4 tabstop=8 expandtab