The configure method takes test_only flag so we can avoid
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* $Id: test_router_flexml.cpp,v 1.22 2008-02-20 15:07:53 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "filter.hpp"
27 #include "router_flexml.hpp"
28 #include "factory_static.hpp"
29
30 #define BOOST_AUTO_TEST_MAIN
31 #define BOOST_TEST_DYN_LINK
32 #include <boost/test/auto_unit_test.hpp>
33
34 using namespace boost::unit_test;
35
36 namespace mp = metaproxy_1;
37
38 static int tfilter_ref = 0;
39 class TFilter: public mp::filter::Base {
40 public:
41     void process(mp::Package & package) const {};
42     TFilter() { tfilter_ref++; };
43     ~TFilter() { tfilter_ref--; };
44 };
45
46 static mp::filter::Base* filter_creator()
47 {
48     return new TFilter;
49 }
50
51 // Pass well-formed XML and valid configuration to it (implicit NS)
52 BOOST_AUTO_TEST_CASE( test_router_flexml_1 )
53 {
54     try
55     {
56         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
57             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
58             " version=\"1.0\">\n"
59             "  <start route=\"start\"/>\n"
60             "  <filters>\n"
61             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
62             "      <port>@:210</port>\n"
63             "    </filter>\n"
64             "    <filter id=\"log_cout1\" type=\"log\">\n"
65             "      <message>my msg</message>\n"
66             "    </filter>\n"
67             "    <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
68             "    <filter id=\"log_cout2\" type=\"log\">\n"
69             "      <message>other</message>\n"
70             "    </filter>\n"
71             "  </filters>\n"
72             "  <routes>\n"  
73             "    <route id=\"start\">\n"
74             "      <filter refid=\"front_default\"/>\n"
75             "      <filter refid=\"log_cout1\"/>\n"
76             "      <filter type=\"tfilter\">\n"
77             "      </filter>\n"
78             "      <filter type=\"z3950_client\">\n"
79             "      </filter>\n"
80             "    </route>\n"
81             "  </routes>\n"
82             "</metaproxy>\n";
83
84         mp::FactoryStatic factory;
85         factory.add_creator("tfilter", filter_creator);
86         mp::RouterFleXML rflexml(xmlconf, factory, true);
87         BOOST_CHECK_EQUAL(tfilter_ref, 2);
88     }
89     catch ( std::runtime_error &e) {
90         std::cout << "std::runtime error: " << e.what() << "\n";
91         BOOST_CHECK (false);
92     }
93     catch ( ... ) {
94         BOOST_CHECK (false);
95     }
96     BOOST_CHECK_EQUAL(tfilter_ref, 0);
97 }
98
99 // Pass non-wellformed XML
100 BOOST_AUTO_TEST_CASE( test_router_flexml_2 )
101 {
102     bool got_error_as_expected = false;
103     try
104     {
105         std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
106             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\" version=\"1.0\">\n"
107             "  <start route=\"start\"/>\n"
108             "  <filters>\n"
109             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
110             "      <port>@:210</port>\n";
111         
112         mp::FactoryFilter factory;
113         mp::RouterFleXML rflexml(xmlconf_invalid, factory, true);
114     }
115     catch ( mp::XMLError &e) {
116         std::cout << "XMLError: " << e.what() << "\n";
117         got_error_as_expected = true;
118     }
119     catch ( std::runtime_error &e) {
120         std::cout << "std::runtime error: " << e.what() << "\n";
121     }
122     catch ( ... ) {
123         ;
124     }
125     BOOST_CHECK(got_error_as_expected);
126 }
127
128 // Pass well-formed XML with explicit NS
129 BOOST_AUTO_TEST_CASE( test_router_flexml_3 )
130 {
131     try
132     {
133         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
134             "<mp:metaproxy xmlns:mp=\"http://indexdata.com/metaproxy\""
135             "  version=\"1.0\">\n"
136             "  <mp:start route=\"start\"/>\n"
137             "  <mp:filters>\n"
138             "    <mp:filter id=\"front_default\" type=\"frontend_net\">\n"
139             "      <port>@:210</port>\n"
140             "    </mp:filter>\n"
141             "    <mp:filter id=\"log_cout\" type=\"log\">\n"
142             "      <message>my msg</message>\n"
143             "    </mp:filter>\n"
144             "  </mp:filters>\n"
145             "  <mp:routes>\n"  
146             "    <mp:route id=\"start\">\n"
147             "      <mp:filter refid=\"front_default\"/>\n"
148             "      <mp:filter refid=\"log_cout\"/>\n"
149             "    </mp:route>\n"
150             "  </mp:routes>\n"
151             "</mp:metaproxy>\n";
152        
153         mp::FactoryStatic factory;
154         mp::RouterFleXML rflexml(xmlconf, factory, true);
155     }
156     catch ( std::runtime_error &e) {
157         std::cout << "std::runtime error: " << e.what() << "\n";
158         BOOST_CHECK (false);
159     }
160     catch ( ... ) {
161         BOOST_CHECK (false);
162     }
163 }
164
165 // Pass well-formed XML but bad filter type
166 BOOST_AUTO_TEST_CASE( test_router_flexml_4 )
167 {
168     bool got_error_as_expected = false;
169     try
170     {
171         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
172             "<metaproxy xmlns=\"http://indexdata.com/metaproxy\""
173             " version=\"1.0\">\n"
174             "  <start route=\"start\"/>\n" 
175             "  <filters>\n"
176             "    <filter id=\"front_default\" type=\"notknown\">\n"
177             "      <port>@:210</port>\n"
178             "    </filter>\n"
179             "  </filters>\n"
180             "  <routes>\n"  
181             "    <route id=\"start\">\n"
182             "      <filter refid=\"front_default\"/>\n"
183             "    </route>\n"
184             "  </routes>\n"
185             "</metaproxy>\n";
186
187         mp::FactoryStatic factory;
188         factory.add_creator("tfilter", filter_creator);
189         mp::RouterFleXML rflexml(xmlconf, factory, true);
190     }
191     catch ( mp::FactoryFilter::NotFound &e) {
192         std::cout << "mp::FactoryFilter::NotFound: " << e.what() << "\n";
193         got_error_as_expected = true;
194     }
195     catch ( std::runtime_error &e) {
196         std::cout << "std::runtime error: " << e.what() << "\n";
197     }
198     BOOST_CHECK(got_error_as_expected);
199 }
200
201
202 /*
203  * Local variables:
204  * c-basic-offset: 4
205  * indent-tabs-mode: nil
206  * c-file-style: "stroustrup"
207  * End:
208  * vim: shiftwidth=4 tabstop=8 expandtab
209  */