summaryrefslogtreecommitdiffstats
path: root/src/lib/log/tests/message_initializer_1_unittest.cc
blob: cb28cf236177b813450432ba37fd6aad9a423d29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (C) 2012-2015 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <log/message_dictionary.h>
#include <log/message_initializer.h>
#include <boost/lexical_cast.hpp>
#include <boost/scoped_ptr.hpp>
#include <gtest/gtest.h>
#include <string>

using namespace isc;
using namespace isc::log;
using namespace std;

// Declare a set of messages to go into the global dictionary.

namespace {
const char* values1[] = {
    "GLOBAL1", "global message one",
    "GLOBAL2", "global message two",
    NULL
};

const char* values2[] = {
    "GLOBAL3", "global message three",
    "GLOBAL4", "global message four",
    NULL
};

const char* values3[] = {
    "GLOBAL7", "global message seven",
    "GLOBAL8", "global message eight",
    NULL
};

const char* values4[] = {
    "GLOBAL8", "global message eight",
    "GLOBAL9", "global message nine",
    NULL
};

/// @brief Scoped pointer to the @c MessageInitializer object.
typedef boost::scoped_ptr<MessageInitializer> MessageInitializerPtr;

}

// Statically initialize the global dictionary with those messages.  Three sets
// are used to check that the declaration of separate initializer objects
// really does combine the messages. (The third set - declaring message IDs
// GLOBAL5 and GLOBAL6) is declared in the separately-compiled file,
// message_identifier_initializer_1a_unittest.cc.)

const MessageInitializer init_message_initializer_unittest_1(values1);
const MessageInitializer init_message_initializer_unittest_2(values2);

// Check that the global dictionary is initialized with the specified
// messages.

TEST(MessageInitializerTest1, messageTest) {
    const MessageDictionaryPtr& global = MessageDictionary::globalDictionary();

    // Pointers to the message arrays should have been stored, but none of the
    // messages should yet be in the dictionary.
    for (int i = 1; i <= 6; ++i) {
        string symbol = string("GLOBAL") + boost::lexical_cast<std::string>(i);
        EXPECT_EQ(string(""), global->getText(symbol));
    }

    // Load the dictionary - this should clear the message array pending count.
    // (N.B. We do not check for a known value before the call, only that the
    // value is not zero.  This is because libraries against which the test
    // is linked may have registered their own message arrays.)
    EXPECT_NE(0, MessageInitializer::getPendingCount());
    MessageInitializer::loadDictionary();
    EXPECT_EQ(0, MessageInitializer::getPendingCount());

    // ... and check the messages loaded.
    EXPECT_EQ(string("global message one"), global->getText("GLOBAL1"));
    EXPECT_EQ(string("global message two"), global->getText("GLOBAL2"));
    EXPECT_EQ(string("global message three"), global->getText("GLOBAL3"));
    EXPECT_EQ(string("global message four"), global->getText("GLOBAL4"));
    EXPECT_EQ(string("global message five"), global->getText("GLOBAL5"));
    EXPECT_EQ(string("global message six"), global->getText("GLOBAL6"));
}

// Check that destroying the MessageInitializer causes the relevant
// messages to be removed from the dictionary.

TEST(MessageInitializerTest1, dynamicLoadUnload) {
    // Obtain the instance of the global dictionary.
    const MessageDictionaryPtr& global = MessageDictionary::globalDictionary();

    // Dynamically create the first initializer.
    MessageInitializerPtr init1(new MessageInitializer(values3));
    EXPECT_EQ(1, MessageInitializer::getPendingCount());

    // Dynamically create the second initializer.
    MessageInitializerPtr init2(new MessageInitializer(values4));
    EXPECT_EQ(2, MessageInitializer::getPendingCount());

    // Load messages from both initializers to the global dictionary.
    MessageInitializer::loadDictionary();
    // There should be no pending messages.
    EXPECT_EQ(0, MessageInitializer::getPendingCount());

    // Make sure that the messages have been loaded.
    EXPECT_EQ("global message seven", global->getText("GLOBAL7"));
    EXPECT_EQ("global message eight", global->getText("GLOBAL8"));
    EXPECT_EQ("global message nine", global->getText("GLOBAL9"));

    // Destroy the first initializer. The first message should be removed.
    // The second message should not be removed because it is also held
    // by another object.
    init1.reset();
    EXPECT_TRUE(global->getText("GLOBAL7").empty());
    EXPECT_EQ("global message eight", global->getText("GLOBAL8"));
    EXPECT_EQ("global message nine", global->getText("GLOBAL9"));

    // Destroy the second initializer. Now, all messages should be
    // unregistered.
    init2.reset();
    EXPECT_TRUE(global->getText("GLOBAL7").empty());
    EXPECT_TRUE(global->getText("GLOBAL8").empty());
    EXPECT_TRUE(global->getText("GLOBAL9").empty());
}

// Check that destroying the MessageInitializer removes pending messages.

TEST(MessageInitializerTest1, dynamicUnloadPending) {
    // Obtain the instance of the global dictionary.
    const MessageDictionaryPtr& global = MessageDictionary::globalDictionary();

    // Dynamically create the first initializer.
    MessageInitializerPtr init1(new MessageInitializer(values3));
    ASSERT_EQ(1, MessageInitializer::getPendingCount());

    // Create second initializer without committing the first set
    // of messages to the dictionary.
    MessageInitializerPtr init2(new MessageInitializer(values4));
    ASSERT_EQ(2, MessageInitializer::getPendingCount());

    // Destroy the first initializer and make sure that the number of
    // pending message sets drops to 1.
    init1.reset();
    ASSERT_EQ(1, MessageInitializer::getPendingCount());

    // Now destroy the second initializer and make sure that there are
    // no pending messages.
    init2.reset();
    ASSERT_EQ(0, MessageInitializer::getPendingCount());

    init1.reset(new MessageInitializer(values3));
    ASSERT_EQ(1, MessageInitializer::getPendingCount());

    // Load the messages to the dictionary and make sure there are no pending
    // messages.
    MessageInitializer::loadDictionary();
    EXPECT_EQ(0, MessageInitializer::getPendingCount());

    // Create the second initializer. There should be one pending set of
    // messages.
    init2.reset(new MessageInitializer(values4));
    ASSERT_EQ(1, MessageInitializer::getPendingCount());

    // Make sure that the messages defined by the first initializer
    // are in the dictionary.
    ASSERT_EQ("global message seven", global->getText("GLOBAL7"));
    ASSERT_EQ("global message eight", global->getText("GLOBAL8"));
    ASSERT_TRUE(global->getText("GLOBAL9").empty());

    // Destroy the second initializer. There should be no pending messages
    // now.
    init2.reset();
    ASSERT_EQ(0, MessageInitializer::getPendingCount());

    // Loading the messages should be no-op.
    MessageInitializer::loadDictionary();
    ASSERT_EQ(0, MessageInitializer::getPendingCount());

    // Make sure that the messages loaded from the first initializer
    // are not affected.
    ASSERT_EQ("global message seven", global->getText("GLOBAL7"));
    ASSERT_EQ("global message eight", global->getText("GLOBAL8"));
    ASSERT_TRUE(global->getText("GLOBAL9").empty());

    // And remove them.
    init1.reset();
    EXPECT_TRUE(global->getText("GLOBAL7").empty());
    EXPECT_TRUE(global->getText("GLOBAL8").empty());
    EXPECT_TRUE(global->getText("GLOBAL9").empty());
}

TEST(MessageInitializerTest1, duplicates) {
    // Original set should not have dupes
    ASSERT_EQ(0, MessageInitializer::getDuplicates().size());

    // This just defines 1, but we'll add it a number of times
    const char* dupe[] = {
        "DUPE", "dupe",
        NULL
    };
    const MessageInitializer init_message_initializer_unittest_1(dupe);
    const MessageInitializer init_message_initializer_unittest_2(dupe);

    MessageInitializer::loadDictionary();
    // Should be a dupe now
    ASSERT_EQ(1, MessageInitializer::getDuplicates().size());

    // clear them
    MessageInitializer::clearDuplicates();
    ASSERT_EQ(0, MessageInitializer::getDuplicates().size());

    // Do it again to make sure, let's explicitly provide false now
    const MessageInitializer init_message_initializer_unittest_3(dupe);
    MessageInitializer::loadDictionary(false);
    ASSERT_EQ(1, MessageInitializer::getDuplicates().size());

    // Loading with ignore_duplicates=true should result in no (reported)
    // dupes
    MessageInitializer::clearDuplicates();
    ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
    const MessageInitializer init_message_initializer_unittest_4(dupe);
    MessageInitializer::loadDictionary(true);
    ASSERT_EQ(0, MessageInitializer::getDuplicates().size());
}