2ecba64a3a3e3d34d9b10fc6dc23007b2e044d3d
[metaproxy-moved-to-github.git] / src / test_filter_factory.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2011 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 <iostream>
20 #include <stdexcept>
21
22 #include "config.hpp"
23 #include <metaproxy/filter.hpp>
24 #include <metaproxy/package.hpp>
25 #include "factory_filter.hpp"
26
27
28 #define BOOST_AUTO_TEST_MAIN
29 #define BOOST_TEST_DYN_LINK
30 #include <boost/test/auto_unit_test.hpp>
31
32 using namespace boost::unit_test;
33 namespace mp = metaproxy_1;
34
35 // XFilter
36 class XFilter: public mp::filter::Base {
37 public:
38     void process(mp::Package & package) const;
39 };
40
41 void XFilter::process(mp::Package & package) const
42 {
43     //package.data() = 1;
44 }
45
46 static mp::filter::Base* xfilter_creator(){
47     return new XFilter;
48 }
49
50 // YFilter ...
51 class YFilter: public mp::filter::Base {
52 public:
53     void process(mp::Package & package) const;
54 };
55
56 void YFilter::process(mp::Package & package) const
57 {
58     //package.data() = 2;
59 }
60
61 static mp::filter::Base* yfilter_creator(){
62     return new YFilter;
63 }
64
65 BOOST_AUTO_TEST_CASE( test_filter_factory_1 )
66 {
67     try {
68         
69         mp::FactoryFilter  ffactory;
70         
71         XFilter xf;
72         YFilter yf;
73
74         const std::string xfid = "XFilter";
75         const std::string yfid = "YFilter";
76         
77         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
78         BOOST_CHECK(ffactory.drop_creator(xfid));
79         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
80         BOOST_CHECK(ffactory.add_creator(yfid, yfilter_creator));
81         
82         mp::filter::Base* xfilter = 0;
83         xfilter = ffactory.create(xfid);
84         mp::filter::Base* yfilter = 0;
85         yfilter = ffactory.create(yfid);
86
87         BOOST_CHECK(0 != xfilter);
88         BOOST_CHECK(0 != yfilter);
89
90         mp::Package pack;
91         xfilter->process(pack);
92         //BOOST_CHECK_EQUAL(pack.data(), 1);
93
94         yfilter->process(pack);
95         //BOOST_CHECK_EQUAL(pack.data(), 2);
96     }
97     catch ( ... ) {
98         throw;
99         BOOST_CHECK (false);
100     }
101 }
102
103 #if HAVE_DL_SUPPORT
104 #if HAVE_DLFCN_H
105 BOOST_AUTO_TEST_CASE( test_filter_factory_2 )
106 {
107     try {        
108         mp::FactoryFilter  ffactory;
109         
110         const std::string id = "dl";
111         
112         // first load
113         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
114
115         // test double load
116         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
117                 
118         mp::filter::Base* filter = 0;
119         filter = ffactory.create(id);
120
121         BOOST_CHECK(0 != filter);
122
123         mp::Package pack;
124         filter->process(pack);
125         //BOOST_CHECK_EQUAL(pack.data(), 42); // magic from filter_dl ..
126     }
127     catch ( ... ) {
128         throw;
129         BOOST_CHECK (false);
130     }
131 }
132 #endif
133 #endif
134
135 /*
136  * Local variables:
137  * c-basic-offset: 4
138  * c-file-style: "Stroustrup"
139  * indent-tabs-mode: nil
140  * End:
141  * vim: shiftwidth=4 tabstop=8 expandtab
142  */
143