1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2009 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "router_flexml.hpp"
25 #include "factory_static.hpp"
27 #define BOOST_AUTO_TEST_MAIN
28 #define BOOST_TEST_DYN_LINK
29 #include <boost/test/auto_unit_test.hpp>
31 using namespace boost::unit_test;
33 namespace mp = metaproxy_1;
35 static int tfilter_ref = 0;
36 class TFilter: public mp::filter::Base {
38 void process(mp::Package & package) const {};
39 TFilter() { tfilter_ref++; };
40 ~TFilter() { tfilter_ref--; };
43 static mp::filter::Base* filter_creator()
48 // Pass well-formed XML and valid configuration to it (implicit NS)
49 BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
53 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
54 "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
56 " <start route=\"start\"/>\n"
58 " <filter id=\"front_default\" type=\"frontend_net\">\n"
59 " <port>@:210</port>\n"
61 " <filter id=\"log_cout1\" type=\"log\">\n"
62 " <message>my msg</message>\n"
64 " <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
65 " <filter id=\"log_cout2\" type=\"log\">\n"
66 " <message>other</message>\n"
70 " <route id=\"start\">\n"
71 " <filter refid=\"front_default\"/>\n"
72 " <filter refid=\"log_cout1\"/>\n"
73 " <filter type=\"tfilter\">\n"
75 " <filter type=\"z3950_client\">\n"
81 mp::FactoryStatic factory;
82 factory.add_creator("tfilter", filter_creator);
83 mp::RouterFleXML rflexml(xmlconf, factory, true);
84 BOOST_CHECK_EQUAL(tfilter_ref, 2);
86 catch ( std::runtime_error &e) {
87 std::cout << "std::runtime error: " << e.what() << "\n";
93 BOOST_CHECK_EQUAL(tfilter_ref, 0);
96 // Pass non-wellformed XML
97 BOOST_AUTO_TEST_CASE( test_router_flexml_2 )
99 bool got_error_as_expected = false;
102 std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
103 "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
104 " <start route=\"start\"/>\n"
106 " <filter id=\"front_default\" type=\"frontend_net\">\n"
107 " <port>@:210</port>\n";
109 mp::FactoryFilter factory;
110 mp::RouterFleXML rflexml(xmlconf_invalid, factory, true);
112 catch ( mp::XMLError &e) {
113 std::cout << "XMLError: " << e.what() << "\n";
114 got_error_as_expected = true;
116 catch ( std::runtime_error &e) {
117 std::cout << "std::runtime error: " << e.what() << "\n";
122 BOOST_CHECK(got_error_as_expected);
125 // Pass well-formed XML with explicit NS
126 BOOST_AUTO_TEST_CASE( test_router_flexml_3 )
130 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
131 "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
132 " version=\"1.0\">\n"
133 " <mp:start route=\"start\"/>\n"
135 " <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
136 " <port>@:210</port>\n"
138 " <mp:filter id=\"log_cout\" type=\"log\">\n"
139 " <message>my msg</message>\n"
143 " <mp:route id=\"start\">\n"
144 " <mp:filter refid=\"front_default\"/>\n"
145 " <mp:filter refid=\"log_cout\"/>\n"
150 mp::FactoryStatic factory;
151 mp::RouterFleXML rflexml(xmlconf, factory, true);
153 catch ( std::runtime_error &e) {
154 std::cout << "std::runtime error: " << e.what() << "\n";
162 // Pass well-formed XML but bad filter type
163 BOOST_AUTO_TEST_CASE( test_router_flexml_4 )
165 bool got_error_as_expected = false;
168 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
169 "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
170 " version=\"1.0\">\n"
171 " <start route=\"start\"/>\n"
173 " <filter id=\"front_default\" type=\"notknown\">\n"
174 " <port>@:210</port>\n"
178 " <route id=\"start\">\n"
179 " <filter refid=\"front_default\"/>\n"
184 mp::FactoryStatic factory;
185 factory.add_creator("tfilter", filter_creator);
186 mp::RouterFleXML rflexml(xmlconf, factory, true);
188 catch ( mp::FactoryFilter::NotFound &e) {
189 std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
190 got_error_as_expected = true;
192 catch ( std::runtime_error &e) {
193 std::cout << "std::runtime error: " << e.what() << "\n";
195 BOOST_CHECK(got_error_as_expected);
202 * c-file-style: "Stroustrup"
203 * indent-tabs-mode: nil
205 * vim: shiftwidth=4 tabstop=8 expandtab