GPL v2.
[metaproxy-moved-to-github.git] / src / test_filter_factory.cpp
1 /* $Id: test_filter_factory.cpp,v 1.16 2007-05-09 21:23:09 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include <iostream>
23 #include <stdexcept>
24
25 #include "config.hpp"
26 #include "filter.hpp"
27 #include "package.hpp"
28 #include "factory_filter.hpp"
29
30
31 #define BOOST_AUTO_TEST_MAIN
32 #include <boost/test/auto_unit_test.hpp>
33
34 using namespace boost::unit_test;
35 namespace mp = metaproxy_1;
36
37 // XFilter
38 class XFilter: public mp::filter::Base {
39 public:
40     void process(mp::Package & package) const;
41 };
42
43 void XFilter::process(mp::Package & package) const
44 {
45     //package.data() = 1;
46 }
47
48 static mp::filter::Base* xfilter_creator(){
49     return new XFilter;
50 }
51
52 // YFilter ...
53 class YFilter: public mp::filter::Base {
54 public:
55     void process(mp::Package & package) const;
56 };
57
58 void YFilter::process(mp::Package & package) const
59 {
60     //package.data() = 2;
61 }
62
63 static mp::filter::Base* yfilter_creator(){
64     return new YFilter;
65 }
66
67 BOOST_AUTO_UNIT_TEST( test_filter_factory_1 )
68 {
69     try {
70         
71         mp::FactoryFilter  ffactory;
72         
73         XFilter xf;
74         YFilter yf;
75
76         const std::string xfid = "XFilter";
77         const std::string yfid = "YFilter";
78         
79         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
80         BOOST_CHECK(ffactory.drop_creator(xfid));
81         BOOST_CHECK(ffactory.add_creator(xfid, xfilter_creator));
82         BOOST_CHECK(ffactory.add_creator(yfid, yfilter_creator));
83         
84         mp::filter::Base* xfilter = 0;
85         xfilter = ffactory.create(xfid);
86         mp::filter::Base* yfilter = 0;
87         yfilter = ffactory.create(yfid);
88
89         BOOST_CHECK(0 != xfilter);
90         BOOST_CHECK(0 != yfilter);
91
92         mp::Package pack;
93         xfilter->process(pack);
94         //BOOST_CHECK_EQUAL(pack.data(), 1);
95
96         yfilter->process(pack);
97         //BOOST_CHECK_EQUAL(pack.data(), 2);
98     }
99     catch ( ... ) {
100         throw;
101         BOOST_CHECK (false);
102     }
103 }
104
105 #if HAVE_DL_SUPPORT
106 #if HAVE_DLFCN_H
107 BOOST_AUTO_UNIT_TEST( test_filter_factory_2 )
108 {
109     try {        
110         mp::FactoryFilter  ffactory;
111         
112         const std::string id = "dl";
113         
114         // first load
115         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
116
117         // test double load
118         BOOST_CHECK(ffactory.add_creator_dl(id, ".libs"));
119                 
120         mp::filter::Base* filter = 0;
121         filter = ffactory.create(id);
122
123         BOOST_CHECK(0 != filter);
124
125         mp::Package pack;
126         filter->process(pack);
127         //BOOST_CHECK_EQUAL(pack.data(), 42); // magic from filter_dl ..
128     }
129     catch ( ... ) {
130         throw;
131         BOOST_CHECK (false);
132     }
133 }
134 #endif
135 #endif
136
137 /*
138  * Local variables:
139  * c-basic-offset: 4
140  * indent-tabs-mode: nil
141  * c-file-style: "stroustrup"
142  * End:
143  * vim: shiftwidth=4 tabstop=8 expandtab
144  */