message_initializer_1_unittest.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2012 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 <log/message_dictionary.h>
  15. #include <log/message_initializer.h>
  16. #include <boost/lexical_cast.hpp>
  17. #include <gtest/gtest.h>
  18. #include <string>
  19. using namespace isc;
  20. using namespace isc::log;
  21. using namespace std;
  22. // Declare a set of messages to go into the global dictionary.
  23. namespace {
  24. const char* values1[] = {
  25. "GLOBAL1", "global message one",
  26. "GLOBAL2", "global message two",
  27. NULL
  28. };
  29. const char* values2[] = {
  30. "GLOBAL3", "global message three",
  31. "GLOBAL4", "global message four",
  32. NULL
  33. };
  34. }
  35. // Statically initialize the global dictionary with those messages. Three sets
  36. // are used to check that the declaration of separate initializer objects
  37. // really does combine the messages. (The third set - declaring message IDs
  38. // GLOBAL5 and GLOBAL6) is declared in the separately-compiled file,
  39. // message_identifier_initializer_1a_unittest.cc.)
  40. const MessageInitializer init_message_initializer_unittest_1(values1);
  41. const MessageInitializer init_message_initializer_unittest_2(values2);
  42. // Check that the global dictionary is initialized with the specified
  43. // messages.
  44. TEST(MessageInitializerTest1, MessageTest) {
  45. MessageDictionary& global = MessageDictionary::globalDictionary();
  46. // Pointers to the message arrays should have been stored, but none of the
  47. // messages should yet be in the dictionary.
  48. for (int i = 1; i <= 6; ++i) {
  49. string symbol = string("GLOBAL") + boost::lexical_cast<std::string>(i);
  50. EXPECT_EQ(string(""), global.getText(symbol));
  51. }
  52. // Load the dictionary - this should clear the message array pending count.
  53. // (N.B. We do not check for a known value before the call, only that the
  54. // value is not zero. This is because libraries against which the test
  55. // is linked may have registered their own message arrays.)
  56. EXPECT_NE(0, MessageInitializer::getPendingCount());
  57. MessageInitializer::loadDictionary();
  58. EXPECT_EQ(0, MessageInitializer::getPendingCount());
  59. // ... and check the messages loaded.
  60. EXPECT_EQ(string("global message one"), global.getText("GLOBAL1"));
  61. EXPECT_EQ(string("global message two"), global.getText("GLOBAL2"));
  62. EXPECT_EQ(string("global message three"), global.getText("GLOBAL3"));
  63. EXPECT_EQ(string("global message four"), global.getText("GLOBAL4"));
  64. EXPECT_EQ(string("global message five"), global.getText("GLOBAL5"));
  65. EXPECT_EQ(string("global message six"), global.getText("GLOBAL6"));
  66. }
  67. TEST(MessageInitializerTest1, Duplicates) {
  68. // Original set should not have dupes
  69. ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
  70. // This just defines 1, but we'll add it a number of times
  71. const char* dupe[] = {
  72. "DUPE", "dupe",
  73. NULL
  74. };
  75. const MessageInitializer init_message_initializer_unittest_1(dupe);
  76. const MessageInitializer init_message_initializer_unittest_2(dupe);
  77. MessageInitializer::loadDictionary();
  78. // Should be a dupe now
  79. ASSERT_EQ(1, MessageInitializer::getDuplicates().size());
  80. // clear them
  81. MessageInitializer::clearDuplicates();
  82. ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
  83. // Do it again to make sure, let's explicitely provide false now
  84. const MessageInitializer init_message_initializer_unittest_3(dupe);
  85. MessageInitializer::loadDictionary(false);
  86. ASSERT_EQ(1, MessageInitializer::getDuplicates().size());
  87. // Loading with ignore_duplicates=true should result in no (reported)
  88. // dupes
  89. MessageInitializer::clearDuplicates();
  90. ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
  91. const MessageInitializer init_message_initializer_unittest_4(dupe);
  92. MessageInitializer::loadDictionary(true);
  93. ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
  94. }