0a8a299f845ba111da033114ea7bb11aea78c765
[senf.git] / senf / Utils / Logger / Target.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Target unit tests */
25
26 //#include "Target.test.hh"
27 //#include "Target.test.ih"
28
29 // Custom includes
30 #include <sstream>
31 #include "main.test.hh"
32
33 #include <senf/Utils/auto_unit_test.hh>
34 #include <boost/test/test_tools.hpp>
35
36 #define prefix_
37 //-/////////////////////////////////////////////////////////////////////////////////////////////////
38
39 namespace {
40
41     struct RouteCheck
42     {
43         typedef std::string result_type;
44         std::string operator()(senf::log::Target::RoutingEntry const & entry) const
45             {
46                 static char const * levels[] = {
47                         "NONE", "VERBOSE", "NOTICE", "MESSAGE", "IMPORTANT", "CRITICAL", "FATAL", "DISABLED" };
48                 static char const * actions[] = { "ACCEPT", "REJECT" };
49                 std::stringstream s;
50                 s << entry.stream() << "-" << entry.area() << "-" << levels[entry.level()] << "-"
51                   << actions[entry.action()];
52                 return s.str();
53             }
54     };
55
56 }
57
58 SENF_AUTO_UNIT_TEST(target)
59 {
60     senf::log::StringTarget target;
61
62     BOOST_CHECK_THROW( target.route("senf::log::test::myStream", "invalid_area"),
63             senf::log::Target::InvalidAreaException );
64     BOOST_CHECK_THROW( target.route("invalid_stream", ""),
65             senf::log::Target::InvalidStreamException );
66     BOOST_CHECK_THROW( target.unroute("senf::log::test::myStream", "invalid_area"),
67             senf::log::Target::InvalidAreaException );
68     BOOST_CHECK_THROW( target.unroute("invalid_stream", ""),
69             senf::log::Target::InvalidStreamException );
70
71     target.route<senf::log::Debug>();
72     target.route<senf::log::test::myStream, senf::log::DefaultArea>(senf::log::Target::REJECT);
73     target.route<senf::log::test::myStream, senf::log::VERBOSE>(senf::log::Target::ACCEPT, 0);
74     target.route<senf::log::test::myStream, senf::log::test::Foo, senf::log::VERBOSE>(
75         senf::log::Target::ACCEPT, 2);
76     target.route("senf::log::test::myStream", "", senf::log::IMPORTANT::value,
77                  senf::log::Target::REJECT, 4);
78     target.route("senf::log::Debug", "senf::log::test::Foo", senf::log::VERBOSE::value,
79                  senf::log::Target::REJECT, -5);
80     target.route("senf::log::Debug", "", senf::log::MESSAGE::value,
81                  senf::log::Target::ACCEPT, -7);
82
83     typedef boost::transform_iterator<RouteCheck, senf::log::Target::iterator> iterator;
84     iterator i (boost::make_transform_iterator(target.begin(), RouteCheck()));
85     iterator const i_end (boost::make_transform_iterator(target.end(), RouteCheck()));
86
87     char const * data[] = {
88         "senf::log::Debug--MESSAGE-ACCEPT",
89         "senf::log::test::myStream--VERBOSE-ACCEPT",
90         "senf::log::Debug-senf::log::test::Foo-VERBOSE-REJECT",
91         "senf::log::Debug--NONE-ACCEPT",
92         "senf::log::test::myStream-senf::log::test::Foo-VERBOSE-ACCEPT",
93         "senf::log::test::myStream-senf::log::DefaultArea-NONE-REJECT",
94         "senf::log::test::myStream--IMPORTANT-REJECT",
95     };
96
97     BOOST_CHECK_EQUAL_COLLECTIONS( i, i_end, data, data + sizeof(data)/sizeof(data[0]) );
98     BOOST_CHECK( *target.begin() == target[0] );
99
100     target.unroute<senf::log::Debug>();
101     target.unroute<senf::log::test::myStream, senf::log::VERBOSE>();
102     target.unroute<senf::log::test::myStream, senf::log::DefaultArea>(senf::log::Target::REJECT);
103     target.unroute("senf::log::test::myStream", "", senf::log::IMPORTANT::value,
104                    senf::log::Target::REJECT);
105     target.unroute(1);
106     target.flush();
107
108     BOOST_CHECK( target.begin() == target.end() );
109     BOOST_CHECK( target.empty() );
110     BOOST_CHECK_EQUAL( target.size(), 0u );
111 }
112
113 //-/////////////////////////////////////////////////////////////////////////////////////////////////
114 #undef prefix_
115
116 \f
117 // Local Variables:
118 // mode: c++
119 // fill-column: 100
120 // comment-column: 40
121 // c-file-style: "senf"
122 // indent-tabs-mode: nil
123 // ispell-local-dictionary: "american"
124 // compile-command: "scons -u test"
125 // End: