/
ConfigurationPrinterTest.java
348 lines (314 loc) · 14.8 KB
/
ConfigurationPrinterTest.java
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*******************************************************************************
* Copyright (c) 2021 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.util.log;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
@Category(JUnitTests.class)
public class ConfigurationPrinterTest extends Assert {
ConfigurationPrinter configurationPrinter;
Logger mockedLogger;
Logger logger;
@Before
public void setUp() {
configurationPrinter = new ConfigurationPrinter();
mockedLogger = Mockito.mock(Logger.class);
logger = LoggerFactory.getLogger(ConfigurationPrinter.class);
}
@Test
public void withParentLoggerTest() {
assertNull("Null expected!", configurationPrinter.getParentLogger());
configurationPrinter.withLogger(logger);
assertEquals("Expected and actual values should be the same.", logger, configurationPrinter.getParentLogger());
configurationPrinter.withLogger(null);
assertNull("Null expected!", configurationPrinter.getParentLogger());
}
@Test
public void withLogLevelTest() {
ConfigurationPrinter.LogLevel[] logLevels = new ConfigurationPrinter.LogLevel[]{
ConfigurationPrinter.LogLevel.DEBUG,
ConfigurationPrinter.LogLevel.ERROR,
ConfigurationPrinter.LogLevel.INFO,
ConfigurationPrinter.LogLevel.TRACE,
ConfigurationPrinter.LogLevel.WARN
};
for (ConfigurationPrinter.LogLevel logLevel : logLevels) {
configurationPrinter.withLogLevel(logLevel);
assertEquals("Expected and actual values should be the same.", logLevel,
configurationPrinter.getLogLevel());
}
}
@Test
public void withTitleTest() {
String[] specialSymbols = new String[]{"!", "\"", "#", "$", "%", "&", "'", "(", ")", "=", "/", "?", "+", "*", "<", ">", ",", ";",
".", ":", "-", "_", "⁄", "@", "‹", "›", "€", "–", "°", "·", "", "Œ", "„", "‰", "”", "’", "Ø", "∏", "{", "}", "Æ", "æ", "Ò", "", "Å",
"Í", "Î", "~", "«", "◊", "Ñ", "¯", "È", "ˇ", " "};
String[] title = new String[]{"", "a", "qwertyuiiopasdfghjklšđčćžzxcvbnm!", "1234567890", "QWERTYUIOPŠĐASDFGHJKLČĆŽZXCVBNM"};
for (String titleVal : title) {
for (String specSymVal : specialSymbols) {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitle(titleVal + specSymVal)
.printLog();
Mockito.verify(mockedLogger, Mockito.times(1)).info("=================== {} ===================", titleVal + specSymVal);
}
}
}
@Test
public void withTitleAlignmentTest() {
ConfigurationPrinter.TitleAlignment[] titleAlignments = new ConfigurationPrinter.TitleAlignment[]{
ConfigurationPrinter.TitleAlignment.CENTER,
ConfigurationPrinter.TitleAlignment.LEFT,
ConfigurationPrinter.TitleAlignment.RIGHT
};
for (ConfigurationPrinter.TitleAlignment titleAlignment : titleAlignments) {
configurationPrinter.withTitleAlignment(titleAlignment);
assertEquals("Expected and actual values should be the same.", titleAlignment, configurationPrinter.getTitleAlignment());
}
}
@Test
public void withTitleAlignmentNullTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("=================== {} ===================", "Title");
}
@Test
public void withTitleAlignmentCenterTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitleAlignment(ConfigurationPrinter.TitleAlignment.CENTER)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("=================== {} ===================", "Title");
}
@Test
public void withTitleAlignmentRightTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitleAlignment(ConfigurationPrinter.TitleAlignment.RIGHT)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("===================================== {} =", "Title");
}
@Test
public void withTitleAlignmentLeftTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitleAlignment(ConfigurationPrinter.TitleAlignment.LEFT)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("= {} =====================================", "Title");
}
@Test
public void getConfigurationsNullTest() {
assertEquals("Expected and actual values are not the same!", new ArrayList<>(), configurationPrinter.getConfigurations());
}
@Test
public void addHeaderNullTests() {
assertNotNull("Not null expected!", configurationPrinter.addHeader(null));
}
@Test
public void addHeaderTest() {
String[] specialSymbols = new String[]{"!", "\"", "#", "$", "%", "&", "'", "(", ")", "=", "/", "?", "+", "*", "<", ">", ",", ";",
".", ":", "-", "_", "⁄", "@", "‹", "›", "€", "€", "–", "°", "·", "", "Œ", "„", "‰", "”", "’", "Ø", "∏", "{", "}", "Æ", "æ", "Ò", "", "Å",
"Í", "Î", "~", "«", "◊", "Ñ", "¯", "È", "ˇ", " "};
String[] title = new String[]{"", "a", "qwertyuiiopasdfghjklšđčćžzxcvbnm!", "1234567890", "QWERTYUIOPŠĐASDFGHJKLČĆŽZXCVBNM"};
for (String titleVal : title) {
for (String specSymVal : specialSymbols) {
assertNotNull("Not null expected!", configurationPrinter.addHeader(titleVal + specSymVal));
}
}
}
@Test
public void addParameterNullTest() {
Object object = new Object();
assertNotNull("Not null expected!", configurationPrinter.addParameter(null, null));
assertNotNull("Not null expected!", configurationPrinter.addParameter("string", null));
assertNotNull("Not null expected!", configurationPrinter.addParameter(null, object));
}
@Test
public void addParameterTest() {
Object[] objects = new Object[]{"[string]", 1, 1.1234, 1123456789L, 1.123456D, 123912839F, true, false, 'a', (byte) 100, (short) 10,};
String[] stringArray = new String[]{"", "a", "abc", "ABC", "123",
"qwertyuiopšđasdfghjklčćžzxcvbnmQWERTYUIOPŠĐASDFGHJKLČĆŽZXCVBNM123457890"};
for (String stringArrayVal : stringArray) {
for (Object object : objects) {
assertNotNull("Not null expected!", configurationPrinter.addParameter(stringArrayVal, object));
}
}
}
@Test
public void increaseIndentationTest() {
for (int i = 0; i < 10; i++) {
assertNotNull("Not null expected!", configurationPrinter.increaseIndentation());
}
}
@Test
public void decreaseIndentationTest() {
for (int i = 0; i < 10; i++) {
configurationPrinter.increaseIndentation();
}
for (int i = 10; i >= 0; i--) {
assertNotNull("Not null expected!", configurationPrinter.decreaseIndentation());
}
}
@Test
public void printLogParentLoggerNullTest() {
configurationPrinter.printLog();
assertNotNull("Not null expected!", configurationPrinter.getParentLogger());
assertNotNull("Not null expected!", configurationPrinter.getParentLogger());
}
@Test
public void printLogTest() {
Logger logger = LoggerFactory.getLogger(ConfigurationPrinter.class);
Object[] objects = new Object[]{"[string]", 1, 1.1234, 1123456789L, 1.123456D, 123912839F, true, false, 'a', (byte) 100, (short) 10,};
configurationPrinter.withLogger(logger);
configurationPrinter.withTitle("Title");
configurationPrinter.addHeader("Header");
ConfigurationPrinter.LogLevel[] enumArray = new ConfigurationPrinter.LogLevel[]{
ConfigurationPrinter.LogLevel.DEBUG,
ConfigurationPrinter.LogLevel.ERROR,
ConfigurationPrinter.LogLevel.INFO,
ConfigurationPrinter.LogLevel.TRACE,
ConfigurationPrinter.LogLevel.WARN};
for (Object object : objects) {
for (ConfigurationPrinter.LogLevel enumArrayVal : enumArray) {
configurationPrinter.withLogLevel(enumArrayVal);
configurationPrinter.withTitleAlignment(ConfigurationPrinter.TitleAlignment.CENTER);
configurationPrinter.addParameter("parameter", object);
configurationPrinter.printLog();
}
}
for (Object object : objects) {
for (ConfigurationPrinter.LogLevel enumArrayVal : enumArray) {
configurationPrinter.withLogLevel(enumArrayVal);
configurationPrinter.withTitleAlignment(ConfigurationPrinter.TitleAlignment.LEFT);
configurationPrinter.addParameter("parameter", object);
configurationPrinter.printLog();
}
}
for (Object object : objects) {
for (ConfigurationPrinter.LogLevel enumArrayVal : enumArray) {
configurationPrinter.withLogLevel(enumArrayVal);
configurationPrinter.withTitleAlignment(ConfigurationPrinter.TitleAlignment.RIGHT);
configurationPrinter.addParameter("parameter", object);
configurationPrinter.printLog();
}
}
}
@Test
public void printLogLogLevelNullTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("=================== {} ===================", "Title");
}
@Test
public void printLogLogLevelDebugTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.DEBUG)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).debug("=================== {} ===================", "Title");
}
@Test
public void printLogLogLevelErrorTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.ERROR)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).error("=================== {} ===================", "Title");
}
@Test
public void printLogLogLevelInfoTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).info("=================== {} ===================", "Title");
}
@Test
public void printLogLogLevelTraceTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.TRACE)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).trace("=================== {} ===================", "Title");
}
@Test
public void printLogLogLevelWarnTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.WARN)
.withTitle("Title")
.printLog();
Mockito.verify(mockedLogger).warn("=================== {} ===================", "Title");
}
@Test
public void printLogTitleNullTest() {
ConfigurationPrinter
.create()
.withLogger(mockedLogger)
.withLogLevel(ConfigurationPrinter.LogLevel.INFO)
.printLog();
Mockito.verify(mockedLogger).info("=================== {} ===================", "Info");
}
@Test
public void createTest() {
assertThat("Instance of ConfigurationPrinter expected.", ConfigurationPrinter.create(), IsInstanceOf.instanceOf(ConfigurationPrinter.class));
}
@Test
public void logLevelEnumTest() {
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.LogLevel.DEBUG, ConfigurationPrinter.LogLevel.valueOf("DEBUG"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.LogLevel.ERROR, ConfigurationPrinter.LogLevel.valueOf("ERROR"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.LogLevel.INFO, ConfigurationPrinter.LogLevel.valueOf("INFO"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.LogLevel.TRACE, ConfigurationPrinter.LogLevel.valueOf("TRACE"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.LogLevel.WARN, ConfigurationPrinter.LogLevel.valueOf("WARN"));
}
@Test
public void titleAlignmentEnumTest() {
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.TitleAlignment.LEFT, ConfigurationPrinter.TitleAlignment.valueOf("LEFT"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.TitleAlignment.RIGHT, ConfigurationPrinter.TitleAlignment.valueOf("RIGHT"));
assertEquals("Expected and actual values should be the same!", ConfigurationPrinter.TitleAlignment.CENTER, ConfigurationPrinter.TitleAlignment.valueOf("CENTER"));
}
}