| 1 | // |
| 2 | // PatternFormatterTest.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 "PatternFormatterTest.h" |
| 12 | #include "Poco/CppUnit/TestCaller.h" |
| 13 | #include "Poco/CppUnit/TestSuite.h" |
| 14 | #include "Poco/PatternFormatter.h" |
| 15 | #include "Poco/Message.h" |
| 16 | #include "Poco/DateTime.h" |
| 17 | #include "Poco/Exception.h" |
| 18 | |
| 19 | |
| 20 | using Poco::PatternFormatter; |
| 21 | using Poco::Message; |
| 22 | using Poco::DateTime; |
| 23 | |
| 24 | |
| 25 | PatternFormatterTest::PatternFormatterTest(const std::string& rName): CppUnit::TestCase(rName) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | |
| 30 | PatternFormatterTest::~PatternFormatterTest() |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | void PatternFormatterTest::testPatternFormatter() |
| 36 | { |
| 37 | Message msg; |
| 38 | PatternFormatter fmt; |
| 39 | msg.setSource("TestSource" ); |
| 40 | msg.setText("Test message text" ); |
| 41 | msg.setPid(1234); |
| 42 | msg.setTid(1); |
| 43 | msg.setThread("TestThread" ); |
| 44 | msg.setPriority(Message::PRIO_ERROR); |
| 45 | msg.setTime(DateTime(2005, 1, 1, 14, 30, 15, 500).timestamp()); |
| 46 | msg["testParam" ] = "Test Parameter" ; |
| 47 | |
| 48 | std::string result; |
| 49 | fmt.setProperty("pattern" , "%Y-%m-%dT%H:%M:%S [%s] %p: %t" ); |
| 50 | fmt.format(msg, result); |
| 51 | assertTrue (result == "2005-01-01T14:30:15 [TestSource] Error: Test message text" ); |
| 52 | |
| 53 | result.clear(); |
| 54 | fmt.setProperty("pattern" , "%w, %e %b %y %H:%M:%S.%i [%s:%I:%T] %q: %t" ); |
| 55 | fmt.format(msg, result); |
| 56 | assertTrue (result == "Sat, 1 Jan 05 14:30:15.500 [TestSource:1:TestThread] E: Test message text" ); |
| 57 | |
| 58 | result.clear(); |
| 59 | fmt.setProperty("pattern" , "%Y-%m-%d %H:%M:%S [%N:%P:%s]%l-%t" ); |
| 60 | fmt.format(msg, result); |
| 61 | assertTrue (result.find("2005-01-01 14:30:15 [" ) == 0); |
| 62 | assertTrue (result.find(":TestSource]3-Test message text" ) != std::string::npos); |
| 63 | |
| 64 | result.clear(); |
| 65 | assertTrue (fmt.getProperty("times" ) == "UTC" ); |
| 66 | fmt.setProperty("times" , "local" ); |
| 67 | fmt.format(msg, result); |
| 68 | assertTrue (result.find("2005-01-01 " ) == 0); |
| 69 | assertTrue (result.find(":TestSource]3-Test message text" ) != std::string::npos); |
| 70 | |
| 71 | result.clear(); |
| 72 | fmt.setProperty("pattern" , "%[testParam]" ); |
| 73 | fmt.format(msg, result); |
| 74 | assertTrue (result == "Test Parameter" ); |
| 75 | |
| 76 | result.clear(); |
| 77 | fmt.setProperty("pattern" , "%[testParam] %p" ); |
| 78 | fmt.format(msg, result); |
| 79 | assertTrue (result == "Test Parameter Error" ); |
| 80 | |
| 81 | result.clear(); |
| 82 | fmt.setProperty("pattern" , "start %v[10] end" ); |
| 83 | fmt.format(msg, result); |
| 84 | assertTrue (result == "start TestSource end" ); |
| 85 | |
| 86 | result.clear(); |
| 87 | fmt.setProperty("pattern" , "start %v[12] end" ); |
| 88 | fmt.format(msg, result); |
| 89 | assertTrue (result == "start TestSource end" ); |
| 90 | |
| 91 | result.clear(); |
| 92 | fmt.setProperty("pattern" , "start %v[8] end" ); |
| 93 | fmt.format(msg, result); |
| 94 | assertTrue (result == "start stSource end" ); |
| 95 | |
| 96 | result.clear(); |
| 97 | fmt.setProperty("pattern" , "%p" ); |
| 98 | fmt.setProperty("priorityNames" , "FAT, CRI, ERR, WRN, NTC, INF, DBG, TRC" ); |
| 99 | fmt.format(msg, result); |
| 100 | assertTrue (result == "ERR" ); |
| 101 | |
| 102 | try |
| 103 | { |
| 104 | fmt.setProperty("priorityNames" , "FAT, CRI," ); |
| 105 | fail("invalid value, must throw" ); |
| 106 | } |
| 107 | catch (Poco::SyntaxException&) |
| 108 | { |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void PatternFormatterTest::setUp() |
| 114 | { |
| 115 | } |
| 116 | |
| 117 | |
| 118 | void PatternFormatterTest::tearDown() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | |
| 123 | CppUnit::Test* PatternFormatterTest::suite() |
| 124 | { |
| 125 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("PatternFormatterTest" ); |
| 126 | |
| 127 | CppUnit_addTest(pSuite, PatternFormatterTest, testPatternFormatter); |
| 128 | |
| 129 | return pSuite; |
| 130 | } |
| 131 | |