Rename yp2::FilterFactory to yp2::FactoryFilter
[metaproxy-moved-to-github.git] / src / test_router_flexml.cpp
1 /* $Id: test_router_flexml.cpp,v 1.11 2006-01-04 14:30:51 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 bool tfilter_destroyed = false;
21 class TFilter: public yp2::filter::Base {
22 public:
23     void process(yp2::Package & package) const {};
24     ~TFilter() { tfilter_destroyed = true; };
25 };
26
27 static yp2::filter::Base* filter_creator()
28 {
29     return new TFilter;
30 }
31
32 BOOST_AUTO_UNIT_TEST( test_router_flexml_1 )
33 {
34     try
35     {
36         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
37             "<yp2 xmlns=\"http://indexdata.dk/yp2/config/1\">\n"
38             "  <start route=\"start\"/>\n"
39             "  <filters>\n"
40             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
41             "      <port>210</port>\n"
42             "    </filter>\n"
43             "    <filter id=\"log_cout1\" type=\"log\">\n"
44             "      <logfile>mylog1.log</logfile>\n"
45             "    </filter>\n"
46             "    <filter id=\"tfilter_id\" type=\"tfilter\">\n"
47             "      <someelement/>\n"
48             "    </filter>\n"
49             "    <filter id=\"log_cout2\" type=\"log\">\n"
50             "      <logfile>mylog2.log</logfile>\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_cout\"/>\n"
57             "    </route>\n"
58             "  </routes>\n"
59             "</yp2>\n";
60
61         yp2::FactoryStatic factory;
62         factory.add_creator("tfilter", filter_creator);
63         yp2::RouterFleXML rflexml(xmlconf, factory);
64     }
65     catch ( std::runtime_error &e) {
66         std::cout << "std::runtime error: " << e.what() << "\n";
67         BOOST_CHECK (false);
68     }
69     catch ( ... ) {
70         BOOST_CHECK (false);
71     }
72     BOOST_CHECK(tfilter_destroyed == true);
73 }
74
75 BOOST_AUTO_UNIT_TEST( test_router_flexml_2 )
76 {
77     bool got_xml_error = false;
78     try
79     {
80         std::string xmlconf_invalid = "<?xml version=\"1.0\"?>\n"
81             "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
82             "  <start route=\"start\"/>\n"
83             "  <filters>\n"
84             "    <filter id=\"front_default\" type=\"frontend_net\">\n"
85             "      <port>210</port>\n";
86         
87         yp2::FactoryFilter factory;
88         yp2::RouterFleXML rflexml(xmlconf_invalid, factory);
89     }
90     catch ( yp2::RouterFleXML::XMLError &e) {
91         got_xml_error = true;
92     }
93     catch ( std::runtime_error &e) {
94         std::cout << "std::runtime error: " << e.what() << "\n";
95         BOOST_CHECK (false);
96     }
97     catch ( ... ) {
98         ;
99     }
100     BOOST_CHECK(got_xml_error);
101 }
102
103 BOOST_AUTO_UNIT_TEST( test_router_flexml_3 )
104 {
105     try
106     {
107         std::string xmlconf = "<?xml version=\"1.0\"?>\n"
108             "<y:yp2 xmlns:y=\"http://indexdata.dk/yp2/config/1\">\n"
109             "  <y:start route=\"start\"/>\n"
110             "  <y:filters>\n"
111             "    <y:filter id=\"front_default\" type=\"frontend_net\">\n"
112             "      <port>210</port>\n"
113             "    </y:filter>\n"
114             "    <y:filter id=\"log_cout\" type=\"log\">\n"
115             "      <logfile>mylog.log</logfile>\n"
116             "    </y:filter>\n"
117             "  </y:filters>\n"
118             "  <y:routes>\n"  
119             "    <y:route id=\"start\">\n"
120             "      <y:filter refid=\"front_default\"/>\n"
121             "      <y:filter refid=\"log_cout\"/>\n"
122             "    </y:route>\n"
123             "  </y:routes>\n"
124             "</y:yp2>\n";
125        
126         yp2::FactoryStatic factory;
127         yp2::RouterFleXML rflexml(xmlconf, factory);
128     }
129     catch ( std::runtime_error &e) {
130         std::cout << "std::runtime error: " << e.what() << "\n";
131         BOOST_CHECK (false);
132     }
133     catch ( ... ) {
134         BOOST_CHECK (false);
135     }
136 }
137
138
139 /*
140  * Local variables:
141  * c-basic-offset: 4
142  * indent-tabs-mode: nil
143  * c-file-style: "stroustrup"
144  * End:
145  * vim: shiftwidth=4 tabstop=8 expandtab
146  */