Make public yp2_filter_struct non-const. If not, the linker symbol
[metaproxy-moved-to-github.git] / src / test_filter_factory.cpp
1 /* $Id: test_filter_factory.cpp,v 1.9 2006-01-04 11:55:32 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_DL_SUPPORT
91 #if HAVE_DLFCN_H
92 BOOST_AUTO_UNIT_TEST( test_filter_factory_2 )
93 {
94     try {        
95         yp2::FilterFactory  ffactory;
96         
97         const std::string id = "dl";
98         
99         // first load
100         BOOST_CHECK(ffactory.add_creator_dyn(id, ".libs"));
101
102         // test double load
103         BOOST_CHECK(ffactory.add_creator_dyn(id, ".libs"));
104                 
105         yp2::filter::Base* filter = 0;
106         filter = ffactory.create(id);
107
108         BOOST_CHECK(0 != filter);
109
110         yp2::Package pack;
111         filter->process(pack);
112         BOOST_CHECK_EQUAL(pack.data(), 42); // magic from filter_dl ..
113     }
114     catch ( ... ) {
115         throw;
116         BOOST_CHECK (false);
117     }
118 }
119 #endif
120 #endif
121
122 /*
123  * Local variables:
124  * c-basic-offset: 4
125  * indent-tabs-mode: nil
126  * c-file-style: "stroustrup"
127  * End:
128  * vim: shiftwidth=4 tabstop=8 expandtab
129  */