summaryrefslogtreecommitdiffstats
path: root/tests/tst_qlogsystem/tst_qlogsystem.cpp
blob: 92fbe0a2d1b5d18ff93ad38db7ba0379c43d5fe3 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QObject>
#include <QTest>
#include <QFile>

#include <qloggers.h>

//TESTED_CLASS= LogSystem
//TESTED_FILES=src/libraries/qtopiamail/support/qlogsystem.cpp

/*
    This class primarily tests that LogSystem correctly handles e-mail messages.
*/
class tst_QLogSystem : public QObject
{
    Q_OBJECT

public:
    tst_QLogSystem();
    virtual ~tst_QLogSystem();

private slots:
    virtual void initTestCase();
    virtual void cleanupTestCase();
    virtual void init();
    virtual void cleanup();

    void loggerConstruction();
    void minLogLvl();

    void loggerOutput_data();
    void loggerOutput();

    void qDebugOutput_data();
    void qDebugOutput();

    void qWarningOutput_data();
    void qWarningOutput();

    void sysLoggerOutput();
};

QTEST_MAIN(tst_QLogSystem)

#include "tst_qlogsystem.moc"


tst_QLogSystem::tst_QLogSystem()
{
}

tst_QLogSystem::~tst_QLogSystem()
{
}

void tst_QLogSystem::initTestCase()
{ // this file will be regenerated in each test
    if(QFile::exists("./LoggersTest.log"))
        QVERIFY2(QFile::remove("./LoggersTest.log"), "could not delete log file");
}


void tst_QLogSystem::cleanupTestCase()
{
}

void tst_QLogSystem::init()
{
}

void tst_QLogSystem::cleanup()
{
    if(QFile::exists("./LoggersTest.log"))
        QVERIFY2(QFile::remove("./LoggersTest.log"), "could not delete log file");
}
Q_DECLARE_METATYPE(LogLevel)

void tst_QLogSystem::loggerConstruction()
{
    FileLogger<> l_name1("NO-DIRECTORY/file-should-not-be-opened");
    QString err_msg;
    QVERIFY2(!l_name1.isReady(err_msg), "File logger is ready with invalid dir.");
    QVERIFY2(!err_msg.isEmpty(), "File logger has not obtained error message");

    err_msg = QString("");

    FileLogger<> l_name2("./LoggersTest.log");
    QVERIFY2(l_name2.isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");
    QVERIFY2(QFile::remove("./LoggersTest.log"), "could not delete log file");

    FileLogger<> l_name3(stdout);
    QVERIFY2(l_name3.isReady(err_msg), "File logger is not ready with stdout.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with stdout.");

    FileLogger<> l_name4(stderr);
    QVERIFY2(l_name4.isReady(err_msg), "File logger is not ready with stderr.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with stderr.");
}

void tst_QLogSystem::minLogLvl()
{

    FileLogger<LvlLogPrefix>* fileLogger = new FileLogger<LvlLogPrefix>("./LoggersTest.log");
    char forbidden[] = "should not be shown!";

    QString err_msg;
    QVERIFY2(fileLogger->isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");

    fileLogger->setMinLogLvl(LlWarning);

    LogSystem::getInstance().addLogger(fileLogger);
    LogSystem::getInstance().log(LlInfo, forbidden);
    LogSystem::getInstance().clear();

    QFile file("./LoggersTest.log");
    QVERIFY2(file.open(QFile::ReadOnly), "Could not open log file for reading");
    QByteArray dataBuf= file.readLine();
    file.close();

    QVERIFY2(dataBuf.isEmpty(), "The logger has written record with invalid Log Level");
}

void tst_QLogSystem::loggerOutput_data()
{
    QTest::addColumn<LogLevel>("lvl");
    QTest::addColumn<QString>("fstr");
    QTest::addColumn<int>("intIn");
    QTest::addColumn<QString>("chIn");
    QTest::addColumn<double>("dlIn");
    QTest::addColumn<QString>("expected");

    QTest::newRow("test 'log' method dbg level")<<LlDbg<<"Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Debug] Test: '12' 'string' '12.25'\n";
    QTest::newRow("test 'log' method info level")<<LlInfo<<"Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Info] Test: '12' 'string' '12.25'\n";
    QTest::newRow("test 'log' method warn level")<<LlWarning<< "Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Warning] Test: '12' 'string' '12.25'\n";
    QTest::newRow("test 'log' method err level")<<LlError<< "Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Error] Test: '12' 'string' '12.25'\n";
    QTest::newRow("test 'log' method err level")<<LlCritical<< "Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Critical] Test: '12' 'string' '12.25'\n";
}


void tst_QLogSystem::loggerOutput()
{
    QFETCH(LogLevel, lvl);
    QFETCH(QString, fstr);
    QFETCH(int, intIn);
    QFETCH(QString, chIn);
    QFETCH(double, dlIn);
    QFETCH(QString, expected);

    FileLogger<LvlLogPrefix>* fileLogger = new FileLogger<LvlLogPrefix>("./LoggersTest.log");
    fileLogger->setMinLogLvl(LlDbg);

    QString err_msg;
    QVERIFY2(fileLogger->isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");

    LogSystem::getInstance().addLogger(fileLogger);
    LogSystem::getInstance().log(lvl, qPrintable(fstr), intIn,  qPrintable(chIn), dlIn);
    LogSystem::getInstance().clear();

    QFile file("./LoggersTest.log");
    QVERIFY2(file.open(QFile::ReadOnly), "Could not open log file for reading");
    QByteArray dataBuf= file.readLine();

    QCOMPARE( QString(dataBuf.data()), expected);

    file.close();
}

void tst_QLogSystem::qDebugOutput_data()
{
    QTest::addColumn<QString>("fstr");
    QTest::addColumn<int>("intIn");
    QTest::addColumn<QString>("chIn");
    QTest::addColumn<double>("dlIn");
    QTest::addColumn<QString>("expected");

    QTest::newRow("test 'log' method dbg level")<<"Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Debug] Test: '12' 'string' '12.25'\n";
}


void tst_QLogSystem::qDebugOutput()
{
    QFETCH(QString, fstr);
    QFETCH(int, intIn);
    QFETCH(QString, chIn);
    QFETCH(double, dlIn);
    QFETCH(QString, expected);

    FileLogger<LvlLogPrefix>* fileLogger = new FileLogger<LvlLogPrefix>("./LoggersTest.log");
    fileLogger->setMinLogLvl(LlDbg);

    QString err_msg;
    QVERIFY2(fileLogger->isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");

    LogSystem::getInstance().addLogger(fileLogger);
    qDebug(qPrintable(fstr), intIn,  qPrintable(chIn), dlIn);
    LogSystem::getInstance().clear();

    QFile file("./LoggersTest.log");
    QVERIFY2(file.open(QFile::ReadOnly), "Could not open log file for reading");
    QByteArray dataBuf= file.readLine();

    QCOMPARE( QString(dataBuf.data()), expected);

    file.close();
    QVERIFY2(QFile::remove("./LoggersTest.log"), "could not delete log file");
}

void tst_QLogSystem::qWarningOutput_data()
{
    QTest::addColumn<QString>("fstr");
    QTest::addColumn<int>("intIn");
    QTest::addColumn<QString>("chIn");
    QTest::addColumn<double>("dlIn");
    QTest::addColumn<QString>("expected");

    QTest::newRow("test 'log' method warn level")<< "Test: '%d' '%s' '%.2f'"<<12<<"string"<<12.25<<"[Warning] Test: '12' 'string' '12.25'\n";
}

void tst_QLogSystem::qWarningOutput()
{
    QFETCH(QString, fstr);
    QFETCH(int, intIn);
    QFETCH(QString, chIn);
    QFETCH(double, dlIn);
    QFETCH(QString, expected);

    FileLogger<LvlLogPrefix>* fileLogger = new FileLogger<LvlLogPrefix>("./LoggersTest.log");
    fileLogger->setMinLogLvl(LlDbg);

    QString err_msg;
    QVERIFY2(fileLogger->isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");

    LogSystem::getInstance().addLogger(fileLogger);
    qWarning(qPrintable(fstr), intIn,  qPrintable(chIn), dlIn);
    LogSystem::getInstance().clear();

    QFile file("./LoggersTest.log");
    QVERIFY2(file.open(QFile::ReadOnly), "Could not open log file for reading");
    QByteArray dataBuf= file.readLine();

    QCOMPARE(QString(dataBuf.data()), expected);

    file.close();
}

void tst_QLogSystem::sysLoggerOutput()
{
    QString expected = "[Debug] Test: '12' 'string' '12.25'\n";

    SysLogger<LvlLogPrefix>* sysLogger = new SysLogger<LvlLogPrefix>("QMail logger test", LOG_PID, LOG_LOCAL7);
    sysLogger->setMinLogLvl(LlDbg);

    QString err_msg;
    QVERIFY2(sysLogger->isReady(err_msg), "File logger is not ready with local dir.");
    QVERIFY2(err_msg.isEmpty(), "File logger has obtained error message with local dir");

    LogSystem::getInstance().addLogger(sysLogger);
    LogSystem::getInstance().log(LlDbg, "Test: '%d' '%s' '%.2f'", 12,  "string", 12.25);
    LogSystem::getInstance().clear();

    QWARN (qPrintable(tr("Now You should see the string") + expected + tr("at syslog output LOG_LOCAL7, LOG_INFO")));
}