1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2012 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
23 #include <metaproxy/filter.hpp>
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--; };
41 void configure(const xmlNode* ptr, bool test_only, const char *path) {};
44 static mp::filter::Base* filter_creator()
49 // Pass well-formed XML and valid configuration to it (implicit NS)
50 BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
54 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
55 "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
57 " <start route=\"start\"/>\n"
59 " <filter id=\"front_default\" type=\"frontend_net\">\n"
60 " <port>@:210</port>\n"
62 " <filter id=\"log_cout1\" type=\"log\">\n"
63 " <message>my msg</message>\n"
65 " <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
66 " <filter id=\"log_cout2\" type=\"log\">\n"
67 " <message>other</message>\n"
71 " <route id=\"start\">\n"
72 " <filter refid=\"front_default\"/>\n"
73 " <filter refid=\"log_cout1\"/>\n"
74 " <filter type=\"tfilter\">\n"
76 " <filter type=\"z3950_client\">\n"
82 mp::FactoryStatic factory;
83 factory.add_creator("tfilter", filter_creator);
84 mp::RouterFleXML rflexml(xmlconf, factory, true);
85 BOOST_CHECK_EQUAL(tfilter_ref, 2);
87 catch ( std::runtime_error &e) {
88 std::cout << "std::runtime error: " << e.what() << "\n";
94 BOOST_CHECK_EQUAL(tfilter_ref, 0);
97 // Pass non-wellformed XML
98 BOOST_AUTO_TEST_CASE( test_router_flexml_2 )
100 bool got_error_as_expected = false;
103 std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
104 "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
105 " <start route=\"start\"/>\n"
107 " <filter id=\"front_default\" type=\"frontend_net\">\n"
108 " <port>@:210</port>\n";
110 mp::FactoryFilter factory;
111 mp::RouterFleXML rflexml(xmlconf_invalid, factory, true);
113 catch ( mp::XMLError &e) {
114 std::cout << "XMLError: " << e.what() << "\n";
115 got_error_as_expected = true;
117 catch ( std::runtime_error &e) {
118 std::cout << "std::runtime error: " << e.what() << "\n";
123 BOOST_CHECK(got_error_as_expected);
126 // Pass well-formed XML with explicit NS
127 BOOST_AUTO_TEST_CASE( test_router_flexml_3 )
131 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
132 "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
133 " version=\"1.0\">\n"
134 " <mp:start route=\"start\"/>\n"
136 " <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
137 " <port>@:210</port>\n"
139 " <mp:filter id=\"log_cout\" type=\"log\">\n"
140 " <message>my msg</message>\n"
144 " <mp:route id=\"start\">\n"
145 " <mp:filter refid=\"front_default\"/>\n"
146 " <mp:filter refid=\"log_cout\"/>\n"
151 mp::FactoryStatic factory;
152 mp::RouterFleXML rflexml(xmlconf, factory, true);
154 catch ( std::runtime_error &e) {
155 std::cout << "std::runtime error: " << e.what() << "\n";
163 // Pass well-formed XML but bad filter type
164 BOOST_AUTO_TEST_CASE( test_router_flexml_4 )
166 bool got_error_as_expected = false;
169 std::string xmlconf = "<?xml version=\"1.0\"?>\n"
170 "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
171 " version=\"1.0\">\n"
172 " <start route=\"start\"/>\n"
174 " <filter id=\"front_default\" type=\"notknown\">\n"
175 " <port>@:210</port>\n"
179 " <route id=\"start\">\n"
180 " <filter refid=\"front_default\"/>\n"
185 mp::FactoryStatic factory;
186 factory.add_creator("tfilter", filter_creator);
187 mp::RouterFleXML rflexml(xmlconf, factory, true);
189 catch ( mp::FactoryFilter::NotFound &e) {
190 std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
191 got_error_as_expected = true;
193 catch ( std::runtime_error &e) {
194 std::cout << "std::runtime error: " << e.what() << "\n";
196 BOOST_CHECK(got_error_as_expected);
203 * c-file-style: "Stroustrup"
204 * indent-tabs-mode: nil
206 * vim: shiftwidth=4 tabstop=8 expandtab