1 | // |
2 | // LoggingRegistryTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "LoggingRegistryTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/LoggingRegistry.h" |
15 | #include "Poco/ConsoleChannel.h" |
16 | #include "Poco/PatternFormatter.h" |
17 | #include "Poco/AutoPtr.h" |
18 | |
19 | |
20 | using Poco::LoggingRegistry; |
21 | using Poco::Channel; |
22 | using Poco::ConsoleChannel; |
23 | using Poco::Formatter; |
24 | using Poco::PatternFormatter; |
25 | using Poco::AutoPtr; |
26 | |
27 | |
28 | LoggingRegistryTest::LoggingRegistryTest(const std::string& rName): CppUnit::TestCase(rName) |
29 | { |
30 | } |
31 | |
32 | |
33 | LoggingRegistryTest::~LoggingRegistryTest() |
34 | { |
35 | } |
36 | |
37 | |
38 | void LoggingRegistryTest::testRegister() |
39 | { |
40 | LoggingRegistry& reg = LoggingRegistry::defaultRegistry(); |
41 | |
42 | reg.clear(); |
43 | |
44 | Channel::Ptr pC1 = new ConsoleChannel(); |
45 | Channel::Ptr pC2 = new ConsoleChannel(); |
46 | Formatter::Ptr pF1 = new PatternFormatter("" ); |
47 | Formatter::Ptr pF2 = new PatternFormatter("" ); |
48 | |
49 | reg.registerChannel("c1" , pC1); |
50 | reg.registerChannel("c2" , pC2); |
51 | reg.registerFormatter("f1" , pF1); |
52 | reg.registerFormatter("f2" , pF2); |
53 | |
54 | Channel::Ptr pC = reg.channelForName("c1" ); |
55 | assertTrue (pC1 == pC); |
56 | pC = reg.channelForName("c2" ); |
57 | assertTrue (pC2 == pC); |
58 | |
59 | Formatter* pF = reg.formatterForName("f1" ); |
60 | assertTrue (pF1 == pF); |
61 | pF = reg.formatterForName("f2" ); |
62 | assertTrue (pF2 == pF); |
63 | |
64 | try |
65 | { |
66 | pC = reg.channelForName("c3" ); |
67 | fail("not found - must throw" ); |
68 | } |
69 | catch (Poco::NotFoundException&) |
70 | { |
71 | } |
72 | } |
73 | |
74 | |
75 | void LoggingRegistryTest::testReregister() |
76 | { |
77 | LoggingRegistry& reg = LoggingRegistry::defaultRegistry(); |
78 | |
79 | reg.clear(); |
80 | |
81 | Channel::Ptr pC1 = new ConsoleChannel(); |
82 | Channel::Ptr pC2 = new ConsoleChannel(); |
83 | Channel::Ptr pC1b = new ConsoleChannel(); |
84 | AutoPtr<Formatter> pF1 = new PatternFormatter("" ); |
85 | AutoPtr<Formatter> pF2 = new PatternFormatter("" ); |
86 | AutoPtr<Formatter> pF1b = new PatternFormatter("" ); |
87 | |
88 | reg.registerChannel("c1" , pC1); |
89 | reg.registerChannel("c2" , pC2); |
90 | reg.registerFormatter("f1" , pF1); |
91 | reg.registerFormatter("f2" , pF2); |
92 | |
93 | reg.registerChannel("c1" , pC1b); |
94 | |
95 | Channel::Ptr pC = reg.channelForName("c1" ); |
96 | assertTrue (pC1b == pC); |
97 | |
98 | pC = reg.channelForName("c2" ); |
99 | assertTrue (pC2 == pC); |
100 | |
101 | reg.registerFormatter("f1" , pF1b); |
102 | |
103 | Formatter::Ptr pF = reg.formatterForName("f1" ); |
104 | assertTrue (pF1b == pF); |
105 | pF = reg.formatterForName("f2" ); |
106 | assertTrue (pF2 == pF); |
107 | } |
108 | |
109 | |
110 | void LoggingRegistryTest::testUnregister() |
111 | { |
112 | LoggingRegistry& reg = LoggingRegistry::defaultRegistry(); |
113 | |
114 | reg.clear(); |
115 | |
116 | Channel::Ptr pC1 = new ConsoleChannel(); |
117 | Channel::Ptr pC2 = new ConsoleChannel(); |
118 | AutoPtr<Formatter> pF1 = new PatternFormatter("" ); |
119 | AutoPtr<Formatter> pF2 = new PatternFormatter("" ); |
120 | |
121 | reg.registerChannel("c1" , pC1); |
122 | reg.registerChannel("c2" , pC2); |
123 | reg.registerFormatter("f1" , pF1); |
124 | reg.registerFormatter("f2" , pF2); |
125 | |
126 | reg.unregisterChannel("c1" ); |
127 | reg.unregisterFormatter("f2" ); |
128 | |
129 | try |
130 | { |
131 | Channel::Ptr pC = reg.channelForName("c1" ); |
132 | fail("unregistered - must throw" ); |
133 | } |
134 | catch (Poco::NotFoundException&) |
135 | { |
136 | } |
137 | |
138 | try |
139 | { |
140 | Formatter::Ptr pF = reg.formatterForName("f2" ); |
141 | fail("unregistered - must throw" ); |
142 | } |
143 | catch (Poco::NotFoundException&) |
144 | { |
145 | } |
146 | } |
147 | |
148 | |
149 | void LoggingRegistryTest::setUp() |
150 | { |
151 | } |
152 | |
153 | |
154 | void LoggingRegistryTest::tearDown() |
155 | { |
156 | } |
157 | |
158 | |
159 | CppUnit::Test* LoggingRegistryTest::suite() |
160 | { |
161 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LoggingRegistryTest" ); |
162 | |
163 | CppUnit_addTest(pSuite, LoggingRegistryTest, testRegister); |
164 | CppUnit_addTest(pSuite, LoggingRegistryTest, testReregister); |
165 | CppUnit_addTest(pSuite, LoggingRegistryTest, testUnregister); |
166 | |
167 | return pSuite; |
168 | } |
169 | |