Added support for dynamic load support for RouterFlexXML. The filter path
[metaproxy-moved-to-github.git] / src / test_filter_factory.cpp
1 /* $Id: test_filter_factory.cpp,v 1.11 2006-01-19 09:41:01 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5 */
6
7 #include <iostream>
8 #include <stdexcept>
9
10 #include "config.hpp"
11 #include "filter.hpp"
12 #include "package.hpp"
13 #include "factory_filter.hpp"
14
15
16 #define BOOST_AUTO_TEST_MAIN
17 #include <boost/test/auto_unit_test.hpp>
18
19 using namespace boost::unit_test;
20
21 // XFilter
22 class XFilter: public yp2::filter::Base {
23 public:
24     void process(yp2::Package & package) const;
25 };
26
27 void XFilter::process(yp2::Package & package) const
28 {
29     package.data() = 1;
30 }
31
32 static yp2::filter::Base* xfilter_creator(){
33     return new XFilter;
34 }
35
36 // YFilter ...
37 class YFilter: public yp2::filter::Base {
38 public:
39     void process(yp2::Package & package) const;
40 };
41
42 void YFilter::process(yp2::Package & package) const
43 {
44     package.data() = 2;
45 }
46
47 static yp2::filter::Base* yfilter_creator(){
48     return new YFilter;
49 }
50
51 BOOST_AUTO_UNIT_TEST( test_filter_factory_1 )
52 {
53     try {
54         
55         yp2::FactoryFilter  ffactory;
56         
57         XFilter xf;
58         YFilter yf;
59
60         const std::string xfid = "XFilter";
61         const std::string yfid = "YFilter";
62         
63         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
64         BOOST_CHECK(ffactory.drop_creator(xfid));
65         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
66         BOOST_CHECK(ffactory.add_creator(yfid, yfilter_creator));
67         
68         yp2::filter::Base* xfilter = 0;
69         xfilter = ffactory.create(xfid);
70         yp2::filter::Base* yfilter = 0;
71         yfilter = ffactory.create(yfid);
72
73         BOOST_CHECK(0 != xfilter);
74         BOOST_CHECK(0 != yfilter);
75
76         yp2::Package pack;
77         xfilter->process(pack);
78         BOOST_CHECK_EQUAL(pack.data(), 1);
79
80         yfilter->process(pack);
81         BOOST_CHECK_EQUAL(pack.data(), 2);
82     }
83     catch ( ... ) {
84         throw;
85         BOOST_CHECK (false);
86     }
87 }
88
89 #if HAVE_DL_SUPPORT
90 #if HAVE_DLFCN_H
91 BOOST_AUTO_UNIT_TEST( test_filter_factory_2 )
92 {
93     try {        
94         yp2::FactoryFilter  ffactory;
95         
96         const std::string id = "dl";
97         
98         // first load
99         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
100
101         // test double load
102         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
103                 
104         yp2::filter::Base* filter = 0;
105         filter = ffactory.create(id);
106
107         BOOST_CHECK(0 != filter);
108
109         yp2::Package pack;
110         filter->process(pack);
111         BOOST_CHECK_EQUAL(pack.data(), 42); // magic from filter_dl ..
112     }
113     catch ( ... ) {
114         throw;
115         BOOST_CHECK (false);
116     }
117 }
118 #endif
119 #endif
120
121 /*
122  * Local variables:
123  * c-basic-offset: 4
124  * indent-tabs-mode: nil
125  * c-file-style: "stroustrup"
126  * End:
127  * vim: shiftwidth=4 tabstop=8 expandtab
128  */