Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / test_filter2.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
23
24 #include <metaproxy/filter.hpp>
25 #include <metaproxy/router_chain.hpp>
26 #include <metaproxy/package.hpp>
27
28 #include <iostream>
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 class FilterConstant: public mp::filter::Base {
39 public:
40     FilterConstant() : m_constant(1234) { };
41     void process(mp::Package & package) const {
42         package.move();
43     };
44     void configure(const xmlNode* ptr, bool test_only, const char *path);
45     int get_constant() const { return m_constant; };
46 private:
47     const xmlNode *m_ptr;
48     int m_constant;
49 };
50
51
52 void FilterConstant::configure(const xmlNode* ptr, bool test_only,
53                                const char *path)
54 {
55     m_ptr = ptr;
56
57     BOOST_CHECK_EQUAL (ptr->type, XML_ELEMENT_NODE);
58     BOOST_CHECK_EQUAL(std::string((const char *) ptr->name), "filter");
59
60     const struct _xmlAttr *attr;
61
62     for (attr = ptr->properties; attr; attr = attr->next)
63     {
64         BOOST_CHECK_EQUAL( std::string((const char *)attr->name), "type");
65         const xmlNode *val = attr->children;
66         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
67         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "constant");
68     }
69     const xmlNode *p = ptr->children;
70     for (; p; p = p->next)
71     {
72         if (p->type != XML_ELEMENT_NODE)
73             continue;
74
75         BOOST_CHECK_EQUAL (p->type, XML_ELEMENT_NODE);
76         BOOST_CHECK_EQUAL(std::string((const char *) p->name), "value");
77
78         const xmlNode *val = p->children;
79         BOOST_CHECK(val);
80         if (!val)
81             continue;
82
83         BOOST_CHECK_EQUAL(val->type, XML_TEXT_NODE);
84         BOOST_CHECK_EQUAL(std::string((const char *)val->content), "2");
85
86         m_constant = atoi((const char *) val->content);
87     }
88 }
89
90 // This filter dose not have a configure function
91
92 class FilterDouble: public mp::filter::Base {
93 public:
94     void process(mp::Package & package) const {
95         package.move();
96     };
97     void configure(const xmlNode * ptr, bool test_only,
98                    const char *path) { };
99 };
100
101
102 BOOST_AUTO_TEST_CASE( testfilter2_1 )
103 {
104     try {
105         FilterConstant fc;
106         FilterDouble fd;
107
108         {
109             mp::RouterChain router1;
110
111             // test filter set/get/exception
112             router1.append(fc);
113
114             router1.append(fd);
115
116             mp::Session session;
117             mp::Origin origin;
118             mp::Package pack(session, origin);
119
120             pack.router(router1).move();
121
122             //BOOST_CHECK_EQUAL(pack.data(), 2468);
123
124         }
125
126         {
127             mp::RouterChain router2;
128
129             router2.append(fd);
130             router2.append(fc);
131
132             mp::Session session;
133             mp::Origin origin;
134             mp::Package pack(session, origin);
135
136             pack.router(router2).move();
137
138             //BOOST_CHECK_EQUAL(pack.data(), 1234);
139
140         }
141
142     }
143     catch (std::exception &e) {
144         std::cout << e.what() << "\n";
145         BOOST_CHECK (false);
146     }
147     catch ( ...) {
148         BOOST_CHECK (false);
149     }
150
151 }
152
153 BOOST_AUTO_TEST_CASE( testfilter2_2 )
154 {
155     try {
156         FilterConstant fc;
157         BOOST_CHECK_EQUAL(fc.get_constant(), 1234);
158
159         mp::filter::Base *base = &fc;
160
161         std::string some_xml = "<?xml version=\"1.0\"?>\n"
162             "<filter type=\"constant\">\n"
163             " <value>2</value>\n"
164             "</filter>";
165
166         // std::cout << some_xml  << std::endl;
167
168         xmlDocPtr doc = xmlParseMemory(some_xml.c_str(), some_xml.size());
169
170         BOOST_CHECK(doc);
171
172         if (doc)
173         {
174             xmlNodePtr root_element = xmlDocGetRootElement(doc);
175
176             base->configure(root_element, true, 0);
177
178             xmlFreeDoc(doc);
179         }
180
181         BOOST_CHECK_EQUAL(fc.get_constant(), 2);
182     }
183     catch (std::exception &e) {
184         std::cout << e.what() << "\n";
185         BOOST_CHECK (false);
186     }
187     catch ( ...) {
188         BOOST_CHECK (false);
189     }
190
191 }
192
193 /*
194  * Local variables:
195  * c-basic-offset: 4
196  * c-file-style: "Stroustrup"
197  * indent-tabs-mode: nil
198  * End:
199  * vim: shiftwidth=4 tabstop=8 expandtab
200  */
201