test_util.cpp
Go to the documentation of this file.
1 /*
2  * Unit tests for XmlRpc++
3  *
4  * Copyright (C) 2017, Zoox Inc
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Author: Austin Hendrix <austin@zoox.com>
21  *
22  */
23 
24 #include "xmlrpcpp/XmlRpcUtil.h"
25 
26 #include <gtest/gtest.h>
27 
28 using namespace XmlRpc;
29 
31 public:
32  FakeLogHandler() : last_level(-1), last_msg(""){};
33 
34  virtual void log(int level, const char* msg) {
35  last_level = level;
36  last_msg = msg;
37  }
38 
40  std::string last_msg;
41 };
42 
43 TEST(XmlRpc, Log) {
44  FakeLogHandler fakelog;
45 
46  // Check that setting log handler is reflected in getLogHandler().
48  ASSERT_EQ(&fakelog, XmlRpcLogHandler::getLogHandler());
49 
50  // Check default verbosity.
51  ASSERT_EQ(0, XmlRpcLogHandler::getVerbosity());
52  EXPECT_EQ(0, XmlRpc::getVerbosity());
53 
54  // Test all messages masked at default verbosity.
55  for (int i = 1; i < 6; i++) {
56  XmlRpcUtil::log(i, "Hello");
57  ASSERT_EQ(-1, fakelog.last_level);
58  ASSERT_EQ("", fakelog.last_msg);
59  }
60 
61  // Test masking at levels below maximum verbosity.
62  for (int i = 1; i < 5; i++) {
64 
65  for (int j = 1; j <= i; j++) {
66  XmlRpcUtil::log(j, "Hello1");
67  EXPECT_EQ(j, fakelog.last_level);
68  EXPECT_EQ("Hello1", fakelog.last_msg);
69 
70  fakelog.last_level = -1;
71  fakelog.last_msg = "";
72  }
73 
74  XmlRpcUtil::log(i + 1, "Hello2");
75  ASSERT_EQ(-1, fakelog.last_level);
76  ASSERT_EQ("", fakelog.last_msg);
77  }
78 
79  // Test no messages masked at max verbosity.
81  for (int i = 1; i < 5; i++) {
82  XmlRpcUtil::log(i, "Hello3");
83  EXPECT_EQ(i, fakelog.last_level);
84  EXPECT_EQ("Hello3", fakelog.last_msg);
85 
86  fakelog.last_level = -1;
87  fakelog.last_msg = "";
88  }
89 
90  // Basic formatting test.
91  XmlRpcUtil::log(2, "Hello %d", 42);
92  EXPECT_EQ(2, fakelog.last_level);
93  EXPECT_EQ("Hello 42", fakelog.last_msg);
94 }
95 
97 public:
98  FakeErrorHandler() : last_msg(""){};
99 
100  virtual void error(const char* msg) {
101  last_msg = msg;
102  }
103 
104  std::string last_msg;
105 };
106 
107 TEST(XmlRpc, error) {
108  FakeErrorHandler errors;
109 
110  // Check that setErrorHandler is reflected in getErrorHandler.
112  EXPECT_EQ(&errors, XmlRpcErrorHandler::getErrorHandler());
113 
114  // Basic error check.
115  XmlRpcUtil::error("Error!");
116  EXPECT_EQ("Error!", errors.last_msg);
117  errors.last_msg = "";
118 
119  // Error check with formatting.
120  XmlRpcUtil::error("%d: I'm a teapot", 408);
121  EXPECT_EQ("408: I'm a teapot", errors.last_msg);
122 }
123 
124 int main(int argc, char **argv)
125 {
126  ::testing::InitGoogleTest(&argc, argv);
127  return RUN_ALL_TESTS();
128 }
FakeLogHandler::FakeLogHandler
FakeLogHandler()
Definition: test_util.cpp:32
XmlRpc::setVerbosity
XMLRPCPP_DECL void setVerbosity(int level)
Sets log message verbosity. This is short for XmlRpcLogHandler::setVerbosity(level)
Definition: XmlRpcUtil.cpp:77
XmlRpc::XmlRpcLogHandler
An interface allowing custom handling of informational message reporting.
Definition: XmlRpcUtil.h:47
FakeErrorHandler::error
virtual void error(const char *msg)
Report an error. Custom error handlers should define this method.
Definition: test_util.cpp:100
FakeErrorHandler::last_msg
std::string last_msg
Definition: test_util.cpp:104
XmlRpc::XmlRpcUtil::log
static void log(int level, const char *fmt,...)
Dump messages somewhere.
Definition: XmlRpcUtil.cpp:81
FakeLogHandler::log
virtual void log(int level, const char *msg)
Output a message. Custom error handlers should define this method.
Definition: test_util.cpp:34
XmlRpc::getVerbosity
XMLRPCPP_DECL int getVerbosity()
Returns log message verbosity. This is short for XmlRpcLogHandler::getVerbosity()
Definition: XmlRpcUtil.cpp:76
FakeErrorHandler
Definition: test_util.cpp:96
XmlRpc
Definition: XmlRpcClient.h:20
XmlRpc::XmlRpcLogHandler::getLogHandler
static XmlRpcLogHandler * getLogHandler()
Returns a pointer to the currently installed message reporting object.
Definition: XmlRpcUtil.h:52
XmlRpc::XmlRpcErrorHandler::setErrorHandler
static void setErrorHandler(XmlRpcErrorHandler *eh)
Specifies the error handler.
Definition: XmlRpcUtil.h:36
XmlRpc::XmlRpcLogHandler::setLogHandler
static void setLogHandler(XmlRpcLogHandler *lh)
Specifies the message handler.
Definition: XmlRpcUtil.h:56
XmlRpc::XmlRpcErrorHandler
An interface allowing custom handling of error message reporting.
Definition: XmlRpcUtil.h:27
XmlRpc::TEST
TEST(xmlrpcvalue_base64, empty_string)
Definition: xmlrpcvalue_base64.cpp:52
FakeErrorHandler::FakeErrorHandler
FakeErrorHandler()
Definition: test_util.cpp:98
XmlRpc::XmlRpcUtil::error
static void error(const char *fmt,...)
Dump error messages somewhere.
Definition: XmlRpcUtil.cpp:96
FakeLogHandler::last_level
int last_level
Definition: test_util.cpp:39
FakeLogHandler::last_msg
std::string last_msg
Definition: test_util.cpp:40
XmlRpc::XmlRpcErrorHandler::getErrorHandler
static XmlRpcErrorHandler * getErrorHandler()
Returns a pointer to the currently installed error handling object.
Definition: XmlRpcUtil.h:32
XmlRpcUtil.h
FakeLogHandler
Definition: test_util.cpp:30
main
int main(int argc, char **argv)
Definition: test_util.cpp:124
XmlRpc::XmlRpcLogHandler::getVerbosity
static int getVerbosity()
Returns the level of verbosity of informational messages. 0 is no output, 5 is very verbose.
Definition: XmlRpcUtil.h:60


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:32