1 /* $Id: test_router_flexml.cpp,v 1.16 2006-03-16 10:40:59 adam Exp $
2 Copyright (c) 2005-2006, 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 namespace mp = metaproxy_1;
22 static int tfilter_ref = 0;
23 class TFilter: public mp::filter::Base {
25 void process(mp::Package & package) const {};
26 TFilter() { tfilter_ref++; };
27 ~TFilter() { tfilter_ref--; };
30 static mp::filter::Base* filter_creator()
35 // Pass well-formed XML and valid configuration to it (implicit NS)
36 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
40 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
41 "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
42 " <start route=\"start\"/>\n"
44 " <filter id=\"front_default\" type=\"frontend_net\">\n"
45 " <port>@:210</port>\n"
47 " <filter id=\"log_cout1\" type=\"log\">\n"
48 " <message>my msg</message>\n"
50 " <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
51 " <filter id=\"log_cout2\" type=\"log\">\n"
52 " <message>other</message>\n"
56 " <route id=\"start\">\n"
57 " <filter refid=\"front_default\"/>\n"
58 " <filter refid=\"log_cout1\"/>\n"
59 " <filter type=\"tfilter\">\n"
61 " <filter type=\"z3950_client\">\n"
67 mp::FactoryStatic factory;
68 factory.add_creator("tfilter", filter_creator);
69 mp::RouterFleXML rflexml(xmlconf, factory);
70 BOOST_CHECK_EQUAL(tfilter_ref, 2);
72 catch ( std::runtime_error &e) {
73 std::cout << "std::runtime error: " << e.what() << "\n";
79 BOOST_CHECK_EQUAL(tfilter_ref, 0);
82 // Pass non-wellformed XML
83 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
85 bool got_error_as_expected = false;
88 std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
89 "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
90 " <start route=\"start\"/>\n"
92 " <filter id=\"front_default\" type=\"frontend_net\">\n"
93 " <port>@:210</port>\n";
95 mp::FactoryFilter factory;
96 mp::RouterFleXML rflexml(xmlconf_invalid, factory);
98 catch ( mp::XMLError &e) {
99 std::cout << "XMLError: " << e.what() << "\n";
100 got_error_as_expected = true;
102 catch ( std::runtime_error &e) {
103 std::cout << "std::runtime error: " << e.what() << "\n";
108 BOOST_CHECK(got_error_as_expected);
111 // Pass well-formed XML with explicit NS
112 BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
116 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
117 "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
118 " <y:start route=\"start\"/>\n"
120 " <y:filter id=\"front_default\" type=\"frontend_net\">\n"
121 " <port>@:210</port>\n"
123 " <y:filter id=\"log_cout\" type=\"log\">\n"
124 " <message>my msg</message>\n"
128 " <y:route id=\"start\">\n"
129 " <y:filter refid=\"front_default\"/>\n"
130 " <y:filter refid=\"log_cout\"/>\n"
135 mp::FactoryStatic factory;
136 mp::RouterFleXML rflexml(xmlconf, factory);
138 catch ( std::runtime_error &e) {
139 std::cout << "std::runtime error: " << e.what() << "\n";
147 // Pass well-formed XML but bad filter type
148 BOOST_AUTO_UNIT_TEST( test_router_flexml_4 )
150 bool got_error_as_expected = false;
153 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
154 "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
155 " <start route=\"start\"/>\n"
157 " <filter id=\"front_default\" type=\"notknown\">\n"
158 " <port>@:210</port>\n"
162 " <route id=\"start\">\n"
163 " <filter refid=\"front_default\"/>\n"
168 mp::FactoryStatic factory;
169 factory.add_creator("tfilter", filter_creator);
170 mp::RouterFleXML rflexml(xmlconf, factory);
172 catch ( mp::FactoryFilter::NotFound &e) {
173 std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
174 got_error_as_expected = true;
176 catch ( std::runtime_error &e) {
177 std::cout << "std::runtime error: " << e.what() << "\n";
179 BOOST_CHECK(got_error_as_expected);
186 * indent-tabs-mode: nil
187 * c-file-style: "stroustrup"
189 * vim: shiftwidth=4 tabstop=8 expandtab