message_dictionary_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright (C) 2010 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 <cstddef>
  15. #include <string>
  16. #include <gtest/gtest.h>
  17. #include <log/message_dictionary.h>
  18. #include <log/message_types.h>
  19. using namespace isc;
  20. using namespace isc::log;
  21. using namespace std;
  22. class MessageDictionaryTest : public ::testing::Test {
  23. protected:
  24. MessageDictionaryTest() :
  25. alpha_id("ALPHA"), alpha_text("This is alpha"),
  26. beta_id("BETA"), beta_text("This is beta"),
  27. gamma_id("GAMMA"), gamma_text("This is gamma")
  28. {
  29. }
  30. MessageID alpha_id;
  31. std::string alpha_text;
  32. MessageID beta_id;
  33. std::string beta_text;
  34. MessageID gamma_id;
  35. std::string gamma_text;
  36. };
  37. // Check that the global dictionary is a singleton.
  38. TEST_F(MessageDictionaryTest, GlobalTest) {
  39. MessageDictionary* global = MessageDictionary::globalDictionary();
  40. EXPECT_FALSE(NULL == global);
  41. MessageDictionary* global2 = MessageDictionary::globalDictionary();
  42. EXPECT_EQ(global2, global);
  43. }
  44. // Check that adding messages works
  45. TEST_F(MessageDictionaryTest, Add) {
  46. MessageDictionary dictionary;
  47. EXPECT_EQ(0, dictionary.size());
  48. // Add a few messages and check that we can look them up and that there is
  49. // nothing in the overflow vector.
  50. EXPECT_TRUE(dictionary.add(alpha_id, alpha_text));
  51. EXPECT_TRUE(dictionary.add(beta_id, beta_text));
  52. EXPECT_EQ(2, dictionary.size());
  53. EXPECT_EQ(alpha_text, dictionary.getText(alpha_id));
  54. EXPECT_EQ(beta_text, dictionary.getText(beta_id));
  55. EXPECT_EQ(string(""), dictionary.getText(gamma_id));
  56. // Try adding a duplicate with different text. It should not replace the
  57. // current text and the ID should be in the overflow section.
  58. EXPECT_FALSE(dictionary.add(alpha_id, gamma_text));
  59. EXPECT_EQ(2, dictionary.size());
  60. }
  61. // Check that replacing messages works.
  62. TEST_F(MessageDictionaryTest, Replace) {
  63. MessageDictionary dictionary;
  64. EXPECT_EQ(0, dictionary.size());
  65. // Try to replace a non-existent message
  66. EXPECT_FALSE(dictionary.replace(alpha_id, alpha_text));
  67. EXPECT_EQ(0, dictionary.size());
  68. // Add a couple of messages.
  69. EXPECT_TRUE(dictionary.add(alpha_id, alpha_text));
  70. EXPECT_TRUE(dictionary.add(beta_id, beta_text));
  71. EXPECT_EQ(2, dictionary.size());
  72. // Replace an existing message
  73. EXPECT_TRUE(dictionary.replace(alpha_id, gamma_text));
  74. EXPECT_EQ(2, dictionary.size());
  75. EXPECT_EQ(gamma_text, dictionary.getText(alpha_id));
  76. // ... and replace non-existent message (but now the dictionary has some
  77. // items in it).
  78. EXPECT_FALSE(dictionary.replace(gamma_id, alpha_text));
  79. EXPECT_EQ(2, dictionary.size());
  80. EXPECT_EQ(string(""), dictionary.getText(gamma_id));
  81. }
  82. // Load test
  83. TEST_F(MessageDictionaryTest, LoadTest) {
  84. static const char* data1[] = {
  85. "ALPHA", "This is alpha",
  86. "BETA", "This is beta",
  87. "GAMMA", "This is gamma",
  88. NULL
  89. };
  90. static const char* data2[] = {
  91. "DELTA", "This is delta",
  92. "EPSILON", "This is epsilon",
  93. "ETA", NULL
  94. };
  95. MessageDictionary dictionary1;
  96. EXPECT_EQ(0, dictionary1.size());
  97. // Load a dictionary1.
  98. vector<string> duplicates = dictionary1.load(data1);
  99. EXPECT_EQ(3, dictionary1.size());
  100. EXPECT_EQ(string(data1[1]), dictionary1.getText(data1[0]));
  101. EXPECT_EQ(string(data1[3]), dictionary1.getText(data1[2]));
  102. EXPECT_EQ(string(data1[5]), dictionary1.getText(data1[4]));
  103. EXPECT_EQ(0, duplicates.size());
  104. // Attempt an overwrite
  105. duplicates = dictionary1.load(data1);
  106. EXPECT_EQ(3, dictionary1.size());
  107. EXPECT_EQ(3, duplicates.size());
  108. // Try a new dictionary but with an incorrect number of elements
  109. MessageDictionary dictionary2;
  110. EXPECT_EQ(0, dictionary2.size());
  111. duplicates = dictionary2.load(data2);
  112. EXPECT_EQ(2, dictionary2.size());
  113. EXPECT_EQ(string(data2[1]), dictionary2.getText(data2[0]));
  114. EXPECT_EQ(string(data2[3]), dictionary2.getText(data2[2]));
  115. EXPECT_EQ(string(""), dictionary2.getText(data2[4]));
  116. EXPECT_EQ(0, duplicates.size());
  117. }
  118. // Check for some non-existent items
  119. TEST_F(MessageDictionaryTest, Lookups) {
  120. static const char* data[] = {
  121. "ALPHA", "This is alpha",
  122. "BETA", "This is beta",
  123. "GAMMA", "This is gamma",
  124. NULL
  125. };
  126. MessageDictionary dictionary;
  127. vector<string> duplicates = dictionary.load(data);
  128. EXPECT_EQ(3, dictionary.size());
  129. EXPECT_EQ(0, duplicates.size());
  130. // Valid lookups
  131. EXPECT_EQ(string("This is alpha"), dictionary.getText("ALPHA"));
  132. EXPECT_EQ(string("This is beta"), dictionary.getText("BETA"));
  133. EXPECT_EQ(string("This is gamma"), dictionary.getText("GAMMA"));
  134. // ... and invalid ones
  135. EXPECT_EQ(string(""), dictionary.getText("XYZZY"));
  136. EXPECT_EQ(string(""), dictionary.getText(""));
  137. EXPECT_EQ(string(""), dictionary.getText("\n\n\n"));
  138. }