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