Implemented yp2::xml::check_empty that serves as default
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* $Id: test_router_flexml.cpp,v 1.15 2006-01-11 14:58:28 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6
7 #include "config.hpp"
8 #include <iostream>
9 #include <stdexcept>
10
11 #include "filter.hpp"
12 #include "router_flexml.hpp"
13 #include "factory_static.hpp"
14
15 #define BOOST_AUTO_TEST_MAIN
16 #include <boost/test/auto_unit_test.hpp>
17
18 using namespace boost::unit_test;
19
20 static int tfilter_ref = 0;
21 class TFilter: public yp2::filter::Base {
22 public:
23     void process(yp2::Package & package) const {};
24     TFilter() { tfilter_ref++; };
25     ~TFilter() { tfilter_ref--; };
26 };
27
28 static yp2::filter::Base* filter_creator()
29 {
30     return new TFilter;
31 }
32
33 // Pass well-formed XML and valid configuration to it (implicit NS)
34 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
35 {
36     try
37     {
38         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
39             "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
40             "  <start route=\"start\"/>\n"
41             "  <filters>\n"
42             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
43             "      <port>@:210</port>\n"
44             "    </filter>\n"
45             "    <filter id=\"log_cout1\" type=\"log\">\n"
46             "      <message>my msg</message>\n"
47             "    </filter>\n"
48             "    <filter id=\"tfilter_id\" type=\"tfilter\"/>\n"
49             "    <filter id=\"log_cout2\" type=\"log\">\n"
50             "      <message>other</message>\n"
51             "    </filter>\n"
52             "  </filters>\n"
53             "  <routes>\n"  
54             "    <route id=\"start\">\n"
55             "      <filter refid=\"front_default\"/>\n"
56             "      <filter refid=\"log_cout1\"/>\n"
57             "      <filter type=\"tfilter\">\n"
58             "      </filter>\n"
59             "      <filter type=\"z3950_client\">\n"
60             "      </filter>\n"
61             "    </route>\n"
62             "  </routes>\n"
63             "</yp2>\n";
64
65         yp2::FactoryStatic factory;
66         factory.add_creator("tfilter", filter_creator);
67         yp2::RouterFleXML rflexml(xmlconf, factory);
68         BOOST_CHECK_EQUAL(tfilter_ref, 2);
69     }
70     catch ( std::runtime_error &e) {
71         std::cout << "std::runtime error: " << e.what() << "\n";
72         BOOST_CHECK (false);
73     }
74     catch ( ... ) {
75         BOOST_CHECK (false);
76     }
77     BOOST_CHECK_EQUAL(tfilter_ref, 0);
78 }
79
80 // Pass non-wellformed XML
81 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
82 {
83     bool got_error_as_expected = false;
84     try
85     {
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"
89             "  <filters>\n"
90             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
91             "      <port>@:210</port>\n";
92         
93         yp2::FactoryFilter factory;
94         yp2::RouterFleXML rflexml(xmlconf_invalid, factory);
95     }
96     catch ( yp2::XMLError &e) {
97         std::cout << "XMLError: " << e.what() << "\n";
98         got_error_as_expected = true;
99     }
100     catch ( std::runtime_error &e) {
101         std::cout << "std::runtime error: " << e.what() << "\n";
102     }
103     catch ( ... ) {
104         ;
105     }
106     BOOST_CHECK(got_error_as_expected);
107 }
108
109 // Pass well-formed XML with explicit NS
110 BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
111 {
112     try
113     {
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"
117             "  <y:filters>\n"
118             "    <y:filter id=\"front_default\" type=\"frontend_net\">\n"
119             "      <port>@:210</port>\n"
120             "    </y:filter>\n"
121             "    <y:filter id=\"log_cout\" type=\"log\">\n"
122             "      <message>my msg</message>\n"
123             "    </y:filter>\n"
124             "  </y:filters>\n"
125             "  <y:routes>\n"  
126             "    <y:route id=\"start\">\n"
127             "      <y:filter refid=\"front_default\"/>\n"
128             "      <y:filter refid=\"log_cout\"/>\n"
129             "    </y:route>\n"
130             "  </y:routes>\n"
131             "</y:yp2>\n";
132        
133         yp2::FactoryStatic factory;
134         yp2::RouterFleXML rflexml(xmlconf, factory);
135     }
136     catch ( std::runtime_error &e) {
137         std::cout << "std::runtime error: " << e.what() << "\n";
138         BOOST_CHECK (false);
139     }
140     catch ( ... ) {
141         BOOST_CHECK (false);
142     }
143 }
144
145 // Pass well-formed XML but bad filter type
146 BOOST_AUTO_UNIT_TEST( test_router_flexml_4 )
147 {
148     bool got_error_as_expected = false;
149     try
150     {
151         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
152             "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
153             "  <start route=\"start\"/>\n"
154             "  <filters>\n"
155             "    <filter id=\"front_default\" type=\"notknown\">\n"
156             "      <port>@:210</port>\n"
157             "    </filter>\n"
158             "  </filters>\n"
159             "  <routes>\n"  
160             "    <route id=\"start\">\n"
161             "      <filter refid=\"front_default\"/>\n"
162             "    </route>\n"
163             "  </routes>\n"
164             "</yp2>\n";
165
166         yp2::FactoryStatic factory;
167         factory.add_creator("tfilter", filter_creator);
168         yp2::RouterFleXML rflexml(xmlconf, factory);
169     }
170     catch ( yp2::FactoryFilter::NotFound &e) {
171         std::cout << "yp2::FactoryFilter::NotFound: " << e.what() << "\n";
172         got_error_as_expected = true;
173     }
174     catch ( std::runtime_error &e) {
175         std::cout << "std::runtime error: " << e.what() << "\n";
176     }
177     BOOST_CHECK(got_error_as_expected);
178 }
179
180
181 /*
182  * Local variables:
183  * c-basic-offset: 4
184  * indent-tabs-mode: nil
185  * c-file-style: "stroustrup"
186  * End:
187  * vim: shiftwidth=4 tabstop=8 expandtab
188  */