switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Logger / Target.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Target unit tests */
30
31 //#include "Target.test.hh"
32 //#include "Target.test.ih"
33
34 // Custom includes
35 #include <sstream>
36 #include "main.test.hh"
37
38 #include <senf/Utils/auto_unit_test.hh>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 //-/////////////////////////////////////////////////////////////////////////////////////////////////
43
44 namespace {
45
46     struct RouteCheck
47     {
48         typedef std::string result_type;
49         std::string operator()(senf::log::Target::RoutingEntry const & entry) const
50             {
51                 static char const * levels[] = {
52                         "NONE", "VERBOSE", "NOTICE", "MESSAGE", "IMPORTANT", "CRITICAL", "FATAL", "DISABLED" };
53                 static char const * actions[] = { "ACCEPT", "REJECT" };
54                 std::stringstream s;
55                 s << entry.stream() << "-" << entry.area() << "-" << levels[entry.level()] << "-"
56                   << actions[entry.action()];
57                 return s.str();
58             }
59     };
60
61 }
62
63 SENF_AUTO_UNIT_TEST(target)
64 {
65     senf::log::StringTarget target;
66
67     BOOST_CHECK_THROW( target.route("senf::log::test::myStream", "invalid_area"),
68             senf::log::Target::InvalidAreaException );
69     BOOST_CHECK_THROW( target.route("invalid_stream", ""),
70             senf::log::Target::InvalidStreamException );
71     BOOST_CHECK_THROW( target.unroute("senf::log::test::myStream", "invalid_area"),
72             senf::log::Target::InvalidAreaException );
73     BOOST_CHECK_THROW( target.unroute("invalid_stream", ""),
74             senf::log::Target::InvalidStreamException );
75
76     target.route<senf::log::Debug>();
77     target.route<senf::log::test::myStream, senf::log::DefaultArea>(senf::log::Target::REJECT);
78     target.route<senf::log::test::myStream, senf::log::VERBOSE>(senf::log::Target::ACCEPT, 0);
79     target.route<senf::log::test::myStream, senf::log::test::Foo, senf::log::VERBOSE>(
80         senf::log::Target::ACCEPT, 2);
81     target.route("senf::log::test::myStream", "", senf::log::IMPORTANT::value,
82                  senf::log::Target::REJECT, 4);
83     target.route("senf::log::Debug", "senf::log::test::Foo", senf::log::VERBOSE::value,
84                  senf::log::Target::REJECT, -5);
85     target.route("senf::log::Debug", "", senf::log::MESSAGE::value,
86                  senf::log::Target::ACCEPT, -7);
87
88     typedef boost::transform_iterator<RouteCheck, senf::log::Target::iterator> iterator;
89     iterator i (boost::make_transform_iterator(target.begin(), RouteCheck()));
90     iterator const i_end (boost::make_transform_iterator(target.end(), RouteCheck()));
91
92     char const * data[] = {
93         "senf::log::Debug--MESSAGE-ACCEPT",
94         "senf::log::test::myStream--VERBOSE-ACCEPT",
95         "senf::log::Debug-senf::log::test::Foo-VERBOSE-REJECT",
96         "senf::log::Debug--NONE-ACCEPT",
97         "senf::log::test::myStream-senf::log::test::Foo-VERBOSE-ACCEPT",
98         "senf::log::test::myStream-senf::log::DefaultArea-NONE-REJECT",
99         "senf::log::test::myStream--IMPORTANT-REJECT",
100     };
101
102     BOOST_CHECK_EQUAL_COLLECTIONS( i, i_end, data, data + sizeof(data)/sizeof(data[0]) );
103     BOOST_CHECK( *target.begin() == target[0] );
104
105     target.unroute<senf::log::Debug>();
106     target.unroute<senf::log::test::myStream, senf::log::VERBOSE>();
107     target.unroute<senf::log::test::myStream, senf::log::DefaultArea>(senf::log::Target::REJECT);
108     target.unroute("senf::log::test::myStream", "", senf::log::IMPORTANT::value,
109                    senf::log::Target::REJECT);
110     target.unroute(1);
111     target.flush();
112
113     BOOST_CHECK( target.begin() == target.end() );
114     BOOST_CHECK( target.empty() );
115     BOOST_CHECK_EQUAL( target.size(), 0u );
116 }
117
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 #undef prefix_
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: