1 | // |
2 | // ChannelTest.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 "ChannelTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SplitterChannel.h" |
15 | #include "Poco/AsyncChannel.h" |
16 | #include "Poco/AutoPtr.h" |
17 | #include "Poco/Message.h" |
18 | #include "Poco/Formatter.h" |
19 | #include "Poco/FormattingChannel.h" |
20 | #include "Poco/ConsoleChannel.h" |
21 | #include "Poco/StreamChannel.h" |
22 | #include "TestChannel.h" |
23 | #include <sstream> |
24 | |
25 | |
26 | using Poco::SplitterChannel; |
27 | using Poco::AsyncChannel; |
28 | using Poco::FormattingChannel; |
29 | using Poco::ConsoleChannel; |
30 | using Poco::StreamChannel; |
31 | using Poco::Formatter; |
32 | using Poco::Message; |
33 | using Poco::AutoPtr; |
34 | |
35 | |
36 | class SimpleFormatter: public Formatter |
37 | { |
38 | public: |
39 | void (const Message& msg, std::string& text) |
40 | { |
41 | text = msg.getSource(); |
42 | text.append(": " ); |
43 | text.append(msg.getText()); |
44 | } |
45 | }; |
46 | |
47 | |
48 | ChannelTest::ChannelTest(const std::string& rName): CppUnit::TestCase(rName) |
49 | { |
50 | } |
51 | |
52 | |
53 | ChannelTest::~ChannelTest() |
54 | { |
55 | } |
56 | |
57 | |
58 | void ChannelTest::testSplitter() |
59 | { |
60 | AutoPtr<TestChannel> pChannel = new TestChannel; |
61 | AutoPtr<SplitterChannel> pSplitter = new SplitterChannel; |
62 | pSplitter->addChannel(pChannel); |
63 | pSplitter->addChannel(pChannel); |
64 | Message msg; |
65 | pSplitter->log(msg); |
66 | assertTrue (pChannel->list().size() == 2); |
67 | } |
68 | |
69 | |
70 | void ChannelTest::testAsync() |
71 | { |
72 | AutoPtr<TestChannel> pChannel = new TestChannel; |
73 | #ifndef POCO_OS_FAMILY_WINDOWS |
74 | AutoPtr<AsyncChannel> pAsync = new AsyncChannel(pChannel); // FIXME It is crashing here on Windows! |
75 | pAsync->open(); |
76 | Message msg; |
77 | pAsync->log(msg); |
78 | pAsync->log(msg); |
79 | pAsync->close(); |
80 | assertTrue (pChannel->list().size() == 2); |
81 | #endif |
82 | } |
83 | |
84 | |
85 | void ChannelTest::testFormatting() |
86 | { |
87 | AutoPtr<TestChannel> pChannel = new TestChannel; |
88 | AutoPtr<Formatter> pFormatter = new SimpleFormatter; |
89 | AutoPtr<FormattingChannel> pFormatterChannel = new FormattingChannel(pFormatter, pChannel); |
90 | Message msg("Source" , "Text" , Message::PRIO_INFORMATION); |
91 | pFormatterChannel->log(msg); |
92 | assertTrue (pChannel->list().size() == 1); |
93 | assertTrue (pChannel->list().begin()->getText() == "Source: Text" ); |
94 | } |
95 | |
96 | |
97 | void ChannelTest::testConsole() |
98 | { |
99 | AutoPtr<ConsoleChannel> pChannel = new ConsoleChannel; |
100 | AutoPtr<Formatter> pFormatter = new SimpleFormatter; |
101 | AutoPtr<FormattingChannel> pFormatterChannel = new FormattingChannel(pFormatter, pChannel); |
102 | Message msg("Source" , "Text" , Message::PRIO_INFORMATION); |
103 | pFormatterChannel->log(msg); |
104 | } |
105 | |
106 | |
107 | void ChannelTest::testStream() |
108 | { |
109 | std::ostringstream str; |
110 | AutoPtr<StreamChannel> pChannel = new StreamChannel(str); |
111 | AutoPtr<Formatter> pFormatter = new SimpleFormatter; |
112 | AutoPtr<FormattingChannel> pFormatterChannel = new FormattingChannel(pFormatter, pChannel); |
113 | Message msg("Source" , "Text" , Message::PRIO_INFORMATION); |
114 | pFormatterChannel->log(msg); |
115 | assertTrue (str.str().find("Source: Text" ) == 0); |
116 | } |
117 | |
118 | |
119 | void ChannelTest::setUp() |
120 | { |
121 | } |
122 | |
123 | |
124 | void ChannelTest::tearDown() |
125 | { |
126 | } |
127 | |
128 | |
129 | CppUnit::Test* ChannelTest::suite() |
130 | { |
131 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ChannelTest" ); |
132 | |
133 | CppUnit_addTest(pSuite, ChannelTest, testSplitter); |
134 | CppUnit_addTest(pSuite, ChannelTest, testAsync); |
135 | CppUnit_addTest(pSuite, ChannelTest, testFormatting); |
136 | CppUnit_addTest(pSuite, ChannelTest, testConsole); |
137 | CppUnit_addTest(pSuite, ChannelTest, testStream); |
138 | |
139 | return pSuite; |
140 | } |
141 | |