xdebuglevel_unittest.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <iostream>
  15. #include <string>
  16. #include <gtest/gtest.h>
  17. #include <log4cxx/level.h>
  18. #include <log/xdebuglevel.h>
  19. #include <log/debug_levels.h>
  20. /// \brief XDebugLevel (Debug Extension to Level Class)
  21. ///
  22. /// The class is an extension of the log4cxx Level class; this set of tests
  23. /// only test the extensions, they do not test the underlying Level class
  24. /// itself.
  25. using namespace log4cxx;
  26. class XDebugLevelTest : public ::testing::Test {
  27. protected:
  28. XDebugLevelTest()
  29. {
  30. }
  31. };
  32. // Check a basic assertion about the numeric values of the debug levels
  33. TEST_F(XDebugLevelTest, NumericValues) {
  34. EXPECT_EQ(XDebugLevel::XDEBUG_MIN_LEVEL_INT, Level::DEBUG_INT);
  35. EXPECT_EQ(XDebugLevel::XDEBUG_MAX_LEVEL_INT,
  36. Level::DEBUG_INT - MAX_DEBUG_LEVEL);
  37. // ... and check that assumptions used below - that the debug levels
  38. // range from 0 to 99 - are valid.
  39. EXPECT_EQ(0, MIN_DEBUG_LEVEL);
  40. EXPECT_EQ(99, MAX_DEBUG_LEVEL);
  41. }
  42. // Checks that the main function for generating logging level objects from
  43. // debug levels is working.
  44. TEST_F(XDebugLevelTest, GetExtendedDebug) {
  45. // Get a debug level of 0. This should be the same as the main DEBUG
  46. // level.
  47. LevelPtr debug0 = XDebugLevel::getExtendedDebug(0);
  48. EXPECT_EQ(std::string("DEBUG"), debug0->toString());
  49. EXPECT_EQ(Level::DEBUG_INT, debug0->toInt());
  50. EXPECT_TRUE(*Level::getDebug() == *debug0);
  51. // Get an arbitrary debug level in the allowed range.
  52. LevelPtr debug32 = XDebugLevel::getExtendedDebug(32);
  53. EXPECT_EQ(std::string("DEBUG32"), debug32->toString());
  54. EXPECT_TRUE((XDebugLevel::XDEBUG_MIN_LEVEL_INT - 32) == debug32->toInt());
  55. // Check that a value outside the range gives the nearest level.
  56. LevelPtr debug_more = XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL + 1);
  57. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) == *debug_more);
  58. LevelPtr debug_less = XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL - 1);
  59. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) == *debug_less);
  60. }
  61. // Creation of a level from an int - should return the default debug level
  62. // if outside the range.
  63. TEST_F(XDebugLevelTest, FromIntOneArg) {
  64. // Check that a valid debug level is as expected
  65. LevelPtr debug42 = XDebugLevel::toLevel(
  66. XDebugLevel::XDEBUG_MIN_LEVEL_INT - 42);
  67. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(42) == *debug42);
  68. // ... and that an invalid one returns an object of type debug.
  69. LevelPtr debug_invalid = XDebugLevel::toLevel(Level::getInfo()->toInt());
  70. EXPECT_TRUE(*Level::getDebug() == *debug_invalid);
  71. }
  72. // Creation of a level from an int - should return the default level
  73. // if outside the range.
  74. TEST_F(XDebugLevelTest, FromIntTwoArg) {
  75. // Check that a valid debug level is as expected
  76. LevelPtr debug42 = XDebugLevel::toLevel(
  77. (XDebugLevel::XDEBUG_MIN_LEVEL_INT - 42), Level::getFatal());
  78. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(42) == *debug42);
  79. // ... and that an invalid one returns an object of type debug.
  80. LevelPtr debug_invalid = XDebugLevel::toLevel(
  81. Level::getInfo()->toInt(), Level::getFatal());
  82. EXPECT_TRUE(*Level::getFatal() == *debug_invalid);
  83. }
  84. // Creation of a level from a string - should return the default debug level
  85. // if outside the range.
  86. TEST_F(XDebugLevelTest, FromStringOneArg) {
  87. // Check that a valid debug levels are as expected
  88. LevelPtr debug85 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG85"));
  89. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(85) == *debug85);
  90. LevelPtr debug92 = XDebugLevel::toLevelLS(LOG4CXX_STR("debug92"));
  91. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(92) == *debug92);
  92. LevelPtr debug27 = XDebugLevel::toLevelLS(LOG4CXX_STR("Debug27"));
  93. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(27) == *debug27);
  94. LevelPtr debug0 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG"));
  95. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug0);
  96. // ... and that an invalid one returns an object of type debug (which is
  97. // the equivalent of a debug level 0 object).
  98. LevelPtr debug_invalid1 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBU"));
  99. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid1);
  100. LevelPtr debug_invalid2 = XDebugLevel::toLevelLS(LOG4CXX_STR("EBU"));
  101. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid2);
  102. LevelPtr debug_invalid3 = XDebugLevel::toLevelLS(LOG4CXX_STR(""));
  103. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid3);
  104. LevelPtr debug_invalid4 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUGTEN"));
  105. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug_invalid4);
  106. LevelPtr debug_invalid5 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG105"));
  107. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) ==
  108. *debug_invalid5);
  109. LevelPtr debug_invalid6 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG-7"));
  110. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) ==
  111. *debug_invalid6);
  112. }
  113. // Creation of a level from a string - should return the default level
  114. // if outside the range.
  115. TEST_F(XDebugLevelTest, FromStringTwoArg) {
  116. // Check that a valid debug levels are as expected
  117. LevelPtr debug85 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG85"),
  118. Level::getFatal());
  119. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(85) == *debug85);
  120. LevelPtr debug92 = XDebugLevel::toLevelLS(LOG4CXX_STR("debug92"),
  121. Level::getFatal());
  122. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(92) == *debug92);
  123. LevelPtr debug27 = XDebugLevel::toLevelLS(LOG4CXX_STR("Debug27"),
  124. Level::getFatal());
  125. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(27) == *debug27);
  126. LevelPtr debug0 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG"),
  127. Level::getFatal());
  128. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(0) == *debug0);
  129. // ... and that an invalid one returns an object of type debug (which is
  130. // the equivalent of a debug level 0 object).
  131. LevelPtr debug_invalid1 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBU"),
  132. Level::getFatal());
  133. EXPECT_TRUE(*Level::getFatal() == *debug_invalid1);
  134. LevelPtr debug_invalid2 = XDebugLevel::toLevelLS(LOG4CXX_STR("EBU"),
  135. Level::getFatal());
  136. EXPECT_TRUE(*Level::getFatal() == *debug_invalid2);
  137. LevelPtr debug_invalid3 = XDebugLevel::toLevelLS(LOG4CXX_STR(""),
  138. Level::getFatal());
  139. EXPECT_TRUE(*Level::getFatal() == *debug_invalid3);
  140. LevelPtr debug_invalid4 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUGTEN"),
  141. Level::getFatal());
  142. EXPECT_TRUE(*Level::getFatal() == *debug_invalid4);
  143. LevelPtr debug_invalid5 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG105"),
  144. Level::getFatal());
  145. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MAX_DEBUG_LEVEL) ==
  146. *debug_invalid5);
  147. LevelPtr debug_invalid6 = XDebugLevel::toLevelLS(LOG4CXX_STR("DEBUG-7"),
  148. Level::getFatal());
  149. EXPECT_TRUE(*XDebugLevel::getExtendedDebug(MIN_DEBUG_LEVEL) ==
  150. *debug_invalid6);
  151. }