1 | // |
2 | // LoggingFactoryTest.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 "LoggingFactoryTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/LoggingFactory.h" |
15 | #include "Poco/Instantiator.h" |
16 | #include "Poco/Channel.h" |
17 | #include "Poco/ConsoleChannel.h" |
18 | #if defined(_WIN32) |
19 | #include "Poco/WindowsConsoleChannel.h" |
20 | #endif |
21 | #ifndef POCO_NO_FILECHANNEL |
22 | #include "Poco/FileChannel.h" |
23 | #include "Poco/SimpleFileChannel.h" |
24 | #endif |
25 | #include "Poco/SplitterChannel.h" |
26 | #include "Poco/Formatter.h" |
27 | #include "Poco/PatternFormatter.h" |
28 | #include "Poco/Message.h" |
29 | #include "Poco/AutoPtr.h" |
30 | #include "Poco/Exception.h" |
31 | #include <memory> |
32 | |
33 | |
34 | using Poco::LoggingFactory; |
35 | using Poco::Channel; |
36 | using Poco::ConsoleChannel; |
37 | #ifndef POCO_NO_FILECHANNEL |
38 | using Poco::FileChannel; |
39 | using Poco::SimpleFileChannel; |
40 | #endif |
41 | using Poco::SplitterChannel; |
42 | using Poco::Formatter; |
43 | using Poco::PatternFormatter; |
44 | using Poco::Message; |
45 | using Poco::AutoPtr; |
46 | using Poco::Instantiator; |
47 | |
48 | |
49 | namespace |
50 | { |
51 | class CustomChannel: public Channel |
52 | { |
53 | public: |
54 | void log(const Message& msg) |
55 | { |
56 | } |
57 | }; |
58 | |
59 | class CustomFormatter: public Formatter |
60 | { |
61 | void (const Message& msg, std::string& text) |
62 | { |
63 | } |
64 | }; |
65 | } |
66 | |
67 | |
68 | LoggingFactoryTest::LoggingFactoryTest(const std::string& rName): CppUnit::TestCase(rName) |
69 | { |
70 | } |
71 | |
72 | |
73 | LoggingFactoryTest::~LoggingFactoryTest() |
74 | { |
75 | } |
76 | |
77 | |
78 | void LoggingFactoryTest::testBuiltins() |
79 | { |
80 | LoggingFactory& fact = LoggingFactory::defaultFactory(); |
81 | |
82 | Channel::Ptr pConsoleChannel = fact.createChannel("ConsoleChannel" ); |
83 | #if defined(_WIN32) && !defined(_WIN32_WCE) |
84 | assertTrue (!pConsoleChannel.cast<Poco::WindowsConsoleChannel>().isNull()); |
85 | #else |
86 | assertTrue (!pConsoleChannel.cast<ConsoleChannel>().isNull()); |
87 | #endif |
88 | |
89 | #ifndef POCO_NO_FILECHANNEL |
90 | Channel::Ptr pFileChannel = fact.createChannel("FileChannel" ); |
91 | assertTrue (!pFileChannel.cast<FileChannel>().isNull()); |
92 | |
93 | Channel::Ptr pSimpleFileChannel = fact.createChannel("SimpleFileChannel" ); |
94 | assertTrue(!pSimpleFileChannel.cast<SimpleFileChannel>().isNull()); |
95 | #endif |
96 | |
97 | Channel::Ptr pSplitterChannel = fact.createChannel("SplitterChannel" ); |
98 | assertTrue (!pSplitterChannel.cast<SplitterChannel>().isNull()); |
99 | |
100 | try |
101 | { |
102 | Channel::Ptr pUnknownChannel = fact.createChannel("UnknownChannel" ); |
103 | fail("unknown class - must throw" ); |
104 | } |
105 | catch (Poco::NotFoundException&) |
106 | { |
107 | } |
108 | |
109 | Formatter::Ptr pPatternFormatter = fact.createFormatter("PatternFormatter" ); |
110 | assertTrue (!pPatternFormatter.isNull()); |
111 | |
112 | try |
113 | { |
114 | Formatter::Ptr pUnknownFormatter = fact.createFormatter("UnknownFormatter" ); |
115 | fail("unknown class - must throw" ); |
116 | } |
117 | catch (Poco::NotFoundException&) |
118 | { |
119 | } |
120 | } |
121 | |
122 | |
123 | void LoggingFactoryTest::testCustom() |
124 | { |
125 | std::unique_ptr<LoggingFactory> fact(new LoggingFactory); |
126 | |
127 | fact->registerChannelClass("CustomChannel" , new Instantiator<CustomChannel, Channel>); |
128 | fact->registerFormatterClass("CustomFormatter" , new Instantiator<CustomFormatter, Formatter>); |
129 | |
130 | Channel::Ptr pCustomChannel = fact->createChannel("CustomChannel" ); |
131 | assertTrue (dynamic_cast<CustomChannel*>(pCustomChannel.get()) != 0); |
132 | |
133 | Formatter::Ptr pCustomFormatter = fact->createFormatter("CustomFormatter" ); |
134 | assertTrue (!pCustomFormatter.isNull()); |
135 | } |
136 | |
137 | |
138 | void LoggingFactoryTest::setUp() |
139 | { |
140 | } |
141 | |
142 | |
143 | void LoggingFactoryTest::tearDown() |
144 | { |
145 | } |
146 | |
147 | |
148 | CppUnit::Test* LoggingFactoryTest::suite() |
149 | { |
150 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("LoggingFactoryTest" ); |
151 | |
152 | CppUnit_addTest(pSuite, LoggingFactoryTest, testBuiltins); |
153 | CppUnit_addTest(pSuite, LoggingFactoryTest, testCustom); |
154 | |
155 | return pSuite; |
156 | } |
157 | |