@@ -44,66 +44,79 @@ public class LoggingHandlerTest {
44
44
.setSeverity (Severity .DEBUG )
45
45
.addLabel ("levelName" , "FINEST" )
46
46
.addLabel ("levelValue" , String .valueOf (Level .FINEST .intValue ()))
47
+ .setTimestamp (123456789L )
47
48
.build ();
48
49
private static final LogEntry FINER_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
49
50
.setSeverity (Severity .DEBUG )
50
51
.addLabel ("levelName" , "FINER" )
51
52
.addLabel ("levelValue" , String .valueOf (Level .FINER .intValue ()))
53
+ .setTimestamp (123456789L )
52
54
.build ();
53
55
private static final LogEntry FINE_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
54
56
.setSeverity (Severity .DEBUG )
55
57
.addLabel ("levelName" , "FINE" )
56
58
.addLabel ("levelValue" , String .valueOf (Level .FINE .intValue ()))
59
+ .setTimestamp (123456789L )
57
60
.build ();
58
61
private static final LogEntry CONFIG_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
59
62
.setSeverity (Severity .INFO )
60
63
.addLabel ("levelName" , "CONFIG" )
61
64
.addLabel ("levelValue" , String .valueOf (Level .CONFIG .intValue ()))
65
+ .setTimestamp (123456789L )
62
66
.build ();
63
67
private static final LogEntry INFO_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
64
68
.setSeverity (Severity .INFO )
65
69
.addLabel ("levelName" , "INFO" )
66
70
.addLabel ("levelValue" , String .valueOf (Level .INFO .intValue ()))
71
+ .setTimestamp (123456789L )
67
72
.build ();
68
73
private static final LogEntry WARNING_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
69
74
.setSeverity (Severity .WARNING )
70
75
.addLabel ("levelName" , "WARNING" )
71
76
.addLabel ("levelValue" , String .valueOf (Level .WARNING .intValue ()))
77
+ .setTimestamp (123456789L )
72
78
.build ();
73
79
private static final LogEntry SEVERE_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
74
80
.setSeverity (Severity .ERROR )
75
81
.addLabel ("levelName" , "SEVERE" )
76
82
.addLabel ("levelValue" , String .valueOf (Level .SEVERE .intValue ()))
83
+ .setTimestamp (123456789L )
77
84
.build ();
78
85
private static final LogEntry DEBUG_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
79
86
.setSeverity (Severity .DEBUG )
80
87
.addLabel ("levelName" , "DEBUG" )
81
88
.addLabel ("levelValue" , String .valueOf (LoggingLevel .DEBUG .intValue ()))
89
+ .setTimestamp (123456789L )
82
90
.build ();
83
91
private static final LogEntry NOTICE_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
84
92
.setSeverity (Severity .NOTICE )
85
93
.addLabel ("levelName" , "NOTICE" )
86
94
.addLabel ("levelValue" , String .valueOf (LoggingLevel .NOTICE .intValue ()))
95
+ .setTimestamp (123456789L )
87
96
.build ();
88
97
private static final LogEntry ERROR_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
89
98
.setSeverity (Severity .ERROR )
90
99
.addLabel ("levelName" , "ERROR" )
91
100
.addLabel ("levelValue" , String .valueOf (LoggingLevel .ERROR .intValue ()))
101
+ .setTimestamp (123456789L )
92
102
.build ();
93
103
private static final LogEntry CRITICAL_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
94
104
.setSeverity (Severity .CRITICAL )
95
105
.addLabel ("levelName" , "CRITICAL" )
96
106
.addLabel ("levelValue" , String .valueOf (LoggingLevel .CRITICAL .intValue ()))
107
+ .setTimestamp (123456789L )
97
108
.build ();
98
109
private static final LogEntry ALERT_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
99
110
.setSeverity (Severity .ALERT )
100
111
.addLabel ("levelName" , "ALERT" )
101
112
.addLabel ("levelValue" , String .valueOf (LoggingLevel .ALERT .intValue ()))
113
+ .setTimestamp (123456789L )
102
114
.build ();
103
115
private static final LogEntry EMERGENCY_ENTRY = LogEntry .newBuilder (StringPayload .of (MESSAGE ))
104
116
.setSeverity (Severity .EMERGENCY )
105
117
.addLabel ("levelName" , "EMERGENCY" )
106
118
.addLabel ("levelValue" , String .valueOf (LoggingLevel .EMERGENCY .intValue ()))
119
+ .setTimestamp (123456789L )
107
120
.build ();
108
121
109
122
private Logging logging ;
@@ -127,6 +140,13 @@ public void setUp() {
127
140
public void afterClass () {
128
141
EasyMock .verify (logging , options );
129
142
}
143
+
144
+
145
+ private static LogRecord newLogRecord (Level level , String message ) {
146
+ LogRecord record = new LogRecord (level , message );
147
+ record .setMillis (123456789L );
148
+ return record ;
149
+ }
130
150
131
151
@ Test
132
152
public void testPublishLevels () {
@@ -176,20 +196,20 @@ public void testPublishLevels() {
176
196
handler .setLevel (Level .ALL );
177
197
handler .setFormatter (new TestFormatter ());
178
198
// default levels
179
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
180
- handler .publish (new LogRecord (Level .FINER , MESSAGE ));
181
- handler .publish (new LogRecord (Level .FINE , MESSAGE ));
182
- handler .publish (new LogRecord (Level .CONFIG , MESSAGE ));
183
- handler .publish (new LogRecord (Level .INFO , MESSAGE ));
184
- handler .publish (new LogRecord (Level .WARNING , MESSAGE ));
185
- handler .publish (new LogRecord (Level .SEVERE , MESSAGE ));
199
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
200
+ handler .publish (newLogRecord (Level .FINER , MESSAGE ));
201
+ handler .publish (newLogRecord (Level .FINE , MESSAGE ));
202
+ handler .publish (newLogRecord (Level .CONFIG , MESSAGE ));
203
+ handler .publish (newLogRecord (Level .INFO , MESSAGE ));
204
+ handler .publish (newLogRecord (Level .WARNING , MESSAGE ));
205
+ handler .publish (newLogRecord (Level .SEVERE , MESSAGE ));
186
206
// Logging levels
187
- handler .publish (new LogRecord (LoggingLevel .DEBUG , MESSAGE ));
188
- handler .publish (new LogRecord (LoggingLevel .NOTICE , MESSAGE ));
189
- handler .publish (new LogRecord (LoggingLevel .ERROR , MESSAGE ));
190
- handler .publish (new LogRecord (LoggingLevel .CRITICAL , MESSAGE ));
191
- handler .publish (new LogRecord (LoggingLevel .ALERT , MESSAGE ));
192
- handler .publish (new LogRecord (LoggingLevel .EMERGENCY , MESSAGE ));
207
+ handler .publish (newLogRecord (LoggingLevel .DEBUG , MESSAGE ));
208
+ handler .publish (newLogRecord (LoggingLevel .NOTICE , MESSAGE ));
209
+ handler .publish (newLogRecord (LoggingLevel .ERROR , MESSAGE ));
210
+ handler .publish (newLogRecord (LoggingLevel .CRITICAL , MESSAGE ));
211
+ handler .publish (newLogRecord (LoggingLevel .ALERT , MESSAGE ));
212
+ handler .publish (newLogRecord (LoggingLevel .EMERGENCY , MESSAGE ));
193
213
}
194
214
195
215
@ Test
@@ -204,7 +224,7 @@ public void testPublishCustomResource() {
204
224
Handler handler = new LoggingHandler (LOG_NAME , options , resource );
205
225
handler .setLevel (Level .ALL );
206
226
handler .setFormatter (new TestFormatter ());
207
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
227
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
208
228
}
209
229
210
230
@ Test
@@ -224,7 +244,7 @@ public void testReportFlushError() {
224
244
handler .setLevel (Level .ALL );
225
245
handler .setErrorManager (errorManager );
226
246
handler .setFormatter (new TestFormatter ());
227
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
247
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
228
248
EasyMock .verify (errorManager );
229
249
}
230
250
@@ -237,7 +257,7 @@ public void testReportFormatError() {
237
257
ErrorManager errorManager = EasyMock .createStrictMock (ErrorManager .class );
238
258
errorManager .error (null , ex , ErrorManager .FORMAT_FAILURE );
239
259
EasyMock .expectLastCall ().once ();
240
- LogRecord record = new LogRecord (Level .FINEST , MESSAGE );
260
+ LogRecord record = newLogRecord (Level .FINEST , MESSAGE );
241
261
EasyMock .expect (formatter .format (record )).andThrow (ex );
242
262
EasyMock .replay (errorManager , formatter );
243
263
Handler handler = new LoggingHandler (LOG_NAME , options );
@@ -260,12 +280,12 @@ public void testFlushSize() {
260
280
handler .setLevel (Level .ALL );
261
281
handler .setFlushSize (6 );
262
282
handler .setFormatter (new TestFormatter ());
263
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
264
- handler .publish (new LogRecord (Level .FINER , MESSAGE ));
265
- handler .publish (new LogRecord (Level .FINE , MESSAGE ));
266
- handler .publish (new LogRecord (Level .CONFIG , MESSAGE ));
267
- handler .publish (new LogRecord (Level .INFO , MESSAGE ));
268
- handler .publish (new LogRecord (Level .WARNING , MESSAGE ));
283
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
284
+ handler .publish (newLogRecord (Level .FINER , MESSAGE ));
285
+ handler .publish (newLogRecord (Level .FINE , MESSAGE ));
286
+ handler .publish (newLogRecord (Level .CONFIG , MESSAGE ));
287
+ handler .publish (newLogRecord (Level .INFO , MESSAGE ));
288
+ handler .publish (newLogRecord (Level .WARNING , MESSAGE ));
269
289
}
270
290
271
291
@ Test
@@ -282,12 +302,12 @@ public void testFlushLevel() {
282
302
handler .setFlushSize (100 );
283
303
handler .setFlushLevel (Level .WARNING );
284
304
handler .setFormatter (new TestFormatter ());
285
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
286
- handler .publish (new LogRecord (Level .FINER , MESSAGE ));
287
- handler .publish (new LogRecord (Level .FINE , MESSAGE ));
288
- handler .publish (new LogRecord (Level .CONFIG , MESSAGE ));
289
- handler .publish (new LogRecord (Level .INFO , MESSAGE ));
290
- handler .publish (new LogRecord (Level .WARNING , MESSAGE ));
305
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
306
+ handler .publish (newLogRecord (Level .FINER , MESSAGE ));
307
+ handler .publish (newLogRecord (Level .FINE , MESSAGE ));
308
+ handler .publish (newLogRecord (Level .CONFIG , MESSAGE ));
309
+ handler .publish (newLogRecord (Level .INFO , MESSAGE ));
310
+ handler .publish (newLogRecord (Level .WARNING , MESSAGE ));
291
311
}
292
312
293
313
@ Test
@@ -298,13 +318,18 @@ public void testAddHandler() {
298
318
WriteOption .resource (DEFAULT_RESOURCE ));
299
319
EasyMock .expectLastCall ().andReturn (Futures .immediateFuture (null ));
300
320
EasyMock .replay (options , logging );
301
- LoggingHandler handler = new LoggingHandler (LOG_NAME , options );
321
+ LoggingHandler handler = new LoggingHandler (LOG_NAME , options ) {
322
+ @ Override
323
+ public void close () {
324
+ // Make close NOOP to avoid mock close exception
325
+ }
326
+ };
302
327
handler .setLevel (Level .ALL );
303
328
handler .setFormatter (new TestFormatter ());
304
329
Logger logger = Logger .getLogger (getClass ().getName ());
305
330
logger .setLevel (Level .ALL );
306
331
LoggingHandler .addHandler (logger , handler );
307
- logger .finest ( MESSAGE );
332
+ logger .log ( newLogRecord ( Level . FINEST , MESSAGE ) );
308
333
}
309
334
310
335
@ Test
@@ -320,7 +345,7 @@ public void testClose() throws Exception {
320
345
Handler handler = new LoggingHandler (LOG_NAME , options );
321
346
handler .setLevel (Level .ALL );
322
347
handler .setFormatter (new TestFormatter ());
323
- handler .publish (new LogRecord (Level .FINEST , MESSAGE ));
348
+ handler .publish (newLogRecord (Level .FINEST , MESSAGE ));
324
349
handler .close ();
325
350
handler .close ();
326
351
}
0 commit comments