##### Coverage Report - org.apache.maven.shared.io.logging.DefaultMessageHolder

Classes in this File | Line Coverage | Branch Coverage | Complexity
DefaultMessageHolder |
36% | 49/137
47% | 8/17
| 1.3934426229508197;1.393
DefaultMessageHolder$Message |
100% | 22/22
100% | 3/3
| 1.3934426229508197;1.393

1 |  |
     package org.apache.maven.shared.io.logging;
2 |  |
3 |  |
     import java.io.PrintWriter;
4 |  |
     import java.io.StringWriter;
5 |  |
     import java.util.ArrayList;
6 |  |
     import java.util.Iterator;
7 |  |
     import java.util.List;
8 |  |
9 |  |
     public class DefaultMessageHolder
10 |  |
         implements MessageHolder
11 |  |
     {
12 |  |
13 | 50 |
         private List messages = new ArrayList();
14 |  |
15 |  |
         private Message currentMessage;
16 |  |
17 | 50 |
         private int defaultMessageLevel = MessageLevels.LEVEL_INFO;
18 |  |
19 |  |
         private boolean[] messageLevelStates;
20 |  |
21 |  |
         private MessageSink onDemandSink;
22 |  |
23 |  |
         public DefaultMessageHolder()
24 | 50 |
         {
25 | 50 |
             this.messageLevelStates = MessageLevels.getLevelStates( MessageLevels.LEVEL_INFO );
26 | 50 |
         }
27 |  |
28 |  |
         public DefaultMessageHolder( int maxMessageLevel, int defaultMessageLevel )
29 | 0 |
         {
30 | 0 |
             this.defaultMessageLevel = defaultMessageLevel;
31 | 0 |
             this.messageLevelStates = MessageLevels.getLevelStates( maxMessageLevel );
32 | 0 |
         }
33 |  |
34 |  |
         public DefaultMessageHolder( int maxMessageLevel, int defaultMessageLevel, MessageSink onDemandSink )
35 | 0 |
         {
36 | 0 |
             this.defaultMessageLevel = defaultMessageLevel;
37 | 0 |
             this.onDemandSink = onDemandSink;
38 | 0 |
             this.messageLevelStates = MessageLevels.getLevelStates( maxMessageLevel );
39 | 0 |
         }
40 |  |
41 |  |
         public MessageHolder addMessage( CharSequence messagePart, Throwable error )
42 |  |
         {
43 | 6 |
             return addMessage( defaultMessageLevel, messagePart, error );
44 |  |
         }
45 |  |
46 |  |
         protected MessageHolder addMessage( int level, CharSequence messagePart, Throwable error )
47 |  |
         {
48 | 6 |
             newMessage( level );
49 | 6 |
             append( messagePart.toString() );
50 | 6 |
             append( error );
51 |  |
52 | 6 |
             return this;
53 |  |
         }
54 |  |
55 |  |
         public MessageHolder addMessage( CharSequence messagePart )
56 |  |
         {
57 | 54 |
             return addMessage( defaultMessageLevel, messagePart );
58 |  |
         }
59 |  |
60 |  |
         protected MessageHolder addMessage( int level, CharSequence messagePart )
61 |  |
         {
62 | 54 |
             newMessage( level );
63 | 54 |
             append( messagePart.toString() );
64 |  |
65 | 54 |
             return this;
66 |  |
         }
67 |  |
68 |  |
         public MessageHolder addMessage( Throwable error )
69 |  |
         {
70 | 1 |
             return addMessage( defaultMessageLevel, error );
71 |  |
         }
72 |  |
73 |  |
         protected MessageHolder addMessage( int level, Throwable error )
74 |  |
         {
75 | 1 |
             newMessage( level );
76 | 1 |
             append( error );
77 |  |
78 | 1 |
             return this;
79 |  |
         }
80 |  |
81 |  |
         public MessageHolder append( CharSequence messagePart )
82 |  |
         {
83 | 69 |
             if ( currentMessage == null )
84 |  |
             {
85 | 1 |
                 newMessage();
86 |  |
             }
87 |  |
88 | 69 |
             currentMessage.append( messagePart.toString() );
89 |  |
90 | 69 |
             return this;
91 |  |
         }
92 |  |
93 |  |
         public MessageHolder append( Throwable error )
94 |  |
         {
95 | 9 |
             if ( currentMessage == null )
96 |  |
             {
97 | 1 |
                 newMessage();
98 |  |
             }
99 |  |
100 | 9 |
             currentMessage.setError( error );
101 |  |
102 | 9 |
             return this;
103 |  |
         }
104 |  |
105 |  |
         public boolean isEmpty()
106 |  |
         {
107 | 3 |
             return messages.isEmpty();
108 |  |
         }
109 |  |
110 |  |
         public MessageHolder newMessage()
111 |  |
         {
112 | 9 |
             newMessage( defaultMessageLevel );
113 |  |
114 | 9 |
             return this;
115 |  |
         }
116 |  |
117 |  |
         protected void newMessage( int messageLevel )
118 |  |
         {
119 | 70 |
             if ( onDemandSink != null && currentMessage != null )
120 |  |
             {
121 | 0 |
                 renderTo( currentMessage, onDemandSink );
122 |  |
             }
123 |  |
124 | 70 |
             currentMessage = new Message( messageLevel );
125 | 70 |
             messages.add( currentMessage );
126 | 70 |
         }
127 |  |
128 |  |
         public String render()
129 |  |
         {
130 | 21 |
             StringBuffer buffer = new StringBuffer();
131 |  |
132 | 21 |
             int counter = 1;
133 | 21 |
             for ( Iterator it = messages.iterator(); it.hasNext(); )
134 |  |
             {
135 | 28 |
                 Message message = (Message) it.next();
136 |  |
137 | 28 |
                 int ml = message.getMessageLevel();
138 |  |
139 | 28 |
                 if ( ml >= messageLevelStates.length || ml < 0 )
140 |  |
                 {
141 | 0 |
                     ml = MessageLevels.LEVEL_DEBUG;
142 |  |
                 }
143 |  |
144 | 28 |
                 if ( !messageLevelStates[ml] )
145 |  |
                 {
146 | 0 |
                     continue;
147 |  |
                 }
148 |  |
149 | 28 |
                 CharSequence content = message.render();
150 | 28 |
                 String label = MessageLevels.getLevelLabel( message.getMessageLevel() );
151 |  |
152 | 28 |
                 if ( content.length() > label.length() + 3 )
153 |  |
                 {
154 | 27 |
                     buffer.append( '[' ).append( counter++ ).append( "] " );
155 | 27 |
                     buffer.append( content.toString() );
156 |  |
157 | 27 |
                     if ( it.hasNext() )
158 |  |
                     {
159 | 7 |
                         buffer.append( "\n\n" );
160 |  |
                     }
161 |  |
                 }
162 |  |
             }
163 |  |
164 | 21 |
             return buffer.toString();
165 |  |
         }
166 |  |
167 |  |
         public int size()
168 |  |
         {
169 | 35 |
             return messages.size();
170 |  |
         }
171 |  |
172 |  |
         private static final class Message
173 |  |
         {
174 | 70 |
             private StringBuffer message = new StringBuffer();
175 |  |
176 |  |
             private Throwable error;
177 |  |
178 |  |
             private final int messageLevel;
179 |  |
180 |  |
             public Message( int messageLevel )
181 | 70 |
             {
182 | 70 |
                 this.messageLevel = messageLevel;
183 | 70 |
             }
184 |  |
185 |  |
             public Message setError( Throwable error )
186 |  |
             {
187 | 9 |
                 this.error = error;
188 | 9 |
                 return this;
189 |  |
             }
190 |  |
191 |  |
             public Message append( CharSequence message )
192 |  |
             {
193 | 69 |
                 this.message.append( message.toString() );
194 | 69 |
                 return this;
195 |  |
             }
196 |  |
197 |  |
             public int getMessageLevel()
198 |  |
             {
199 | 56 |
                 return messageLevel;
200 |  |
             }
201 |  |
202 |  |
             public CharSequence render()
203 |  |
             {
204 | 28 |
                 StringBuffer buffer = new StringBuffer();
205 |  |
206 | 28 |
                 buffer.append( '[' ).append( MessageLevels.getLevelLabel( messageLevel ) ).append( "] " );
207 |  |
208 | 28 |
                 if ( message != null && message.length() > 0 )
209 |  |
                 {
210 | 25 |
                     buffer.append( message );
211 |  |
212 | 25 |
                     if ( error != null )
213 |  |
                     {
214 | 8 |
                         buffer.append( '\n' );
215 |  |
                     }
216 |  |
                 }
217 |  |
218 | 28 |
                 if ( error != null )
219 |  |
                 {
220 | 10 |
                     buffer.append( "Error:\n" );
221 |  |
222 | 10 |
                     StringWriter sw = new StringWriter();
223 | 10 |
                     PrintWriter pw = new PrintWriter( sw );
224 | 10 |
                     error.printStackTrace( pw );
225 |  |
226 | 10 |
                     buffer.append( sw.toString() );
227 |  |
                 }
228 |  |
229 | 28 |
                 return buffer;
230 |  |
             }
231 |  |
         }
232 |  |
233 |  |
         public MessageHolder addDebugMessage( CharSequence messagePart, Throwable error )
234 |  |
         {
235 | 0 |
             return addMessage( MessageLevels.LEVEL_DEBUG, messagePart, error );
236 |  |
         }
237 |  |
238 |  |
         public MessageHolder addDebugMessage( CharSequence messagePart )
239 |  |
         {
240 | 0 |
             return addMessage( MessageLevels.LEVEL_DEBUG, messagePart );
241 |  |
         }
242 |  |
243 |  |
         public MessageHolder addDebugMessage( Throwable error )
244 |  |
         {
245 | 0 |
             return addMessage( MessageLevels.LEVEL_DEBUG, error );
246 |  |
         }
247 |  |
248 |  |
         public MessageHolder addErrorMessage( CharSequence messagePart, Throwable error )
249 |  |
         {
250 | 0 |
             return addMessage( MessageLevels.LEVEL_ERROR, messagePart, error );
251 |  |
         }
252 |  |
253 |  |
         public MessageHolder addErrorMessage( CharSequence messagePart )
254 |  |
         {
255 | 0 |
             return addMessage( MessageLevels.LEVEL_ERROR, messagePart );
256 |  |
         }
257 |  |
258 |  |
         public MessageHolder addErrorMessage( Throwable error )
259 |  |
         {
260 | 0 |
             return addMessage( MessageLevels.LEVEL_ERROR, error );
261 |  |
         }
262 |  |
263 |  |
         public MessageHolder addInfoMessage( CharSequence messagePart, Throwable error )
264 |  |
         {
265 | 0 |
             return addMessage( MessageLevels.LEVEL_INFO, messagePart, error );
266 |  |
         }
267 |  |
268 |  |
         public MessageHolder addInfoMessage( CharSequence messagePart )
269 |  |
         {
270 | 0 |
             return addMessage( MessageLevels.LEVEL_INFO, messagePart );
271 |  |
         }
272 |  |
273 |  |
         public MessageHolder addInfoMessage( Throwable error )
274 |  |
         {
275 | 0 |
             return addMessage( MessageLevels.LEVEL_INFO, error );
276 |  |
         }
277 |  |
278 |  |
         public MessageHolder addSevereMessage( CharSequence messagePart, Throwable error )
279 |  |
         {
280 | 0 |
             return addMessage( MessageLevels.LEVEL_SEVERE, messagePart, error );
281 |  |
         }
282 |  |
283 |  |
         public MessageHolder addSevereMessage( CharSequence messagePart )
284 |  |
         {
285 | 0 |
             return addMessage( MessageLevels.LEVEL_SEVERE, messagePart );
286 |  |
         }
287 |  |
288 |  |
         public MessageHolder addSevereMessage( Throwable error )
289 |  |
         {
290 | 0 |
             return addMessage( MessageLevels.LEVEL_SEVERE, error );
291 |  |
         }
292 |  |
293 |  |
         public MessageHolder addWarningMessage( CharSequence messagePart, Throwable error )
294 |  |
         {
295 | 0 |
             return addMessage( MessageLevels.LEVEL_WARNING, messagePart, error );
296 |  |
         }
297 |  |
298 |  |
         public MessageHolder addWarningMessage( CharSequence messagePart )
299 |  |
         {
300 | 0 |
             return addMessage( MessageLevels.LEVEL_WARNING, messagePart );
301 |  |
         }
302 |  |
303 |  |
         public MessageHolder addWarningMessage( Throwable error )
304 |  |
         {
305 | 0 |
             return addMessage( MessageLevels.LEVEL_WARNING, error );
306 |  |
         }
307 |  |
308 |  |
         public int countDebugMessages()
309 |  |
         {
310 | 0 |
             return countMessagesOfType( MessageLevels.LEVEL_DEBUG );
311 |  |
         }
312 |  |
313 |  |
         public int countErrorMessages()
314 |  |
         {
315 | 0 |
             return countMessagesOfType( MessageLevels.LEVEL_ERROR );
316 |  |
         }
317 |  |
318 |  |
         public int countInfoMessages()
319 |  |
         {
320 | 0 |
             return countMessagesOfType( MessageLevels.LEVEL_INFO );
321 |  |
         }
322 |  |
323 |  |
         public int countMessages()
324 |  |
         {
325 | 0 |
             return size();
326 |  |
         }
327 |  |
328 |  |
         public int countSevereMessages()
329 |  |
         {
330 | 0 |
             return countMessagesOfType( MessageLevels.LEVEL_SEVERE );
331 |  |
         }
332 |  |
333 |  |
         public int countWarningMessages()
334 |  |
         {
335 | 0 |
             return countMessagesOfType( MessageLevels.LEVEL_WARNING );
336 |  |
         }
337 |  |
338 |  |
         private int countMessagesOfType( int messageLevel )
339 |  |
         {
340 | 0 |
             int count = 0;
341 |  |
342 | 0 |
             for ( Iterator it = messages.iterator(); it.hasNext(); )
343 |  |
             {
344 | 0 |
                 Message message = (Message) it.next();
345 | 0 |
                 if ( messageLevel == message.getMessageLevel() )
346 |  |
                 {
347 | 0 |
                     count++;
348 |  |
                 }
349 |  |
             }
350 |  |
351 | 0 |
             return count;
352 |  |
         }
353 |  |
354 |  |
         public boolean isDebugEnabled()
355 |  |
         {
356 | 0 |
             return messageLevelStates[MessageLevels.LEVEL_DEBUG];
357 |  |
         }
358 |  |
359 |  |
         public boolean isErrorEnabled()
360 |  |
         {
361 | 0 |
             return messageLevelStates[MessageLevels.LEVEL_ERROR];
362 |  |
         }
363 |  |
364 |  |
         public boolean isInfoEnabled()
365 |  |
         {
366 | 0 |
             return messageLevelStates[MessageLevels.LEVEL_INFO];
367 |  |
         }
368 |  |
369 |  |
         public boolean isSevereEnabled()
370 |  |
         {
371 | 0 |
             return messageLevelStates[MessageLevels.LEVEL_SEVERE];
372 |  |
         }
373 |  |
374 |  |
         public boolean isWarningEnabled()
375 |  |
         {
376 | 0 |
             return messageLevelStates[MessageLevels.LEVEL_WARNING];
377 |  |
         }
378 |  |
379 |  |
         public MessageHolder newDebugMessage()
380 |  |
         {
381 | 0 |
             if ( isDebugEnabled() )
382 |  |
             {
383 | 0 |
                 newMessage( MessageLevels.LEVEL_DEBUG );
384 |  |
             }
385 |  |
386 | 0 |
             return this;
387 |  |
         }
388 |  |
389 |  |
         public MessageHolder newErrorMessage()
390 |  |
         {
391 | 0 |
             if ( isErrorEnabled() )
392 |  |
             {
393 | 0 |
                 newMessage( MessageLevels.LEVEL_ERROR );
394 |  |
             }
395 |  |
396 | 0 |
             return this;
397 |  |
         }
398 |  |
399 |  |
         public MessageHolder newInfoMessage()
400 |  |
         {
401 | 0 |
             if ( isInfoEnabled() )
402 |  |
             {
403 | 0 |
                 newMessage( MessageLevels.LEVEL_INFO );
404 |  |
             }
405 |  |
406 | 0 |
             return this;
407 |  |
         }
408 |  |
409 |  |
         public MessageHolder newSevereMessage()
410 |  |
         {
411 | 0 |
             if ( isSevereEnabled() )
412 |  |
             {
413 | 0 |
                 newMessage( MessageLevels.LEVEL_SEVERE );
414 |  |
             }
415 |  |
416 | 0 |
             return this;
417 |  |
         }
418 |  |
419 |  |
         public MessageHolder newWarningMessage()
420 |  |
         {
421 | 0 |
             if ( isWarningEnabled() )
422 |  |
             {
423 | 0 |
                 newMessage( MessageLevels.LEVEL_WARNING );
424 |  |
             }
425 |  |
426 | 0 |
             return this;
427 |  |
         }
428 |  |
429 |  |
         public void setDebugEnabled( boolean enabled )
430 |  |
         {
431 | 0 |
             messageLevelStates[MessageLevels.LEVEL_DEBUG] = enabled;
432 | 0 |
         }
433 |  |
434 |  |
         public void setErrorEnabled( boolean enabled )
435 |  |
         {
436 | 0 |
             messageLevelStates[MessageLevels.LEVEL_ERROR] = enabled;
437 | 0 |
         }
438 |  |
439 |  |
         public void setInfoEnabled( boolean enabled )
440 |  |
         {
441 | 0 |
             messageLevelStates[MessageLevels.LEVEL_INFO] = enabled;
442 | 0 |
         }
443 |  |
444 |  |
         public void setSevereEnabled( boolean enabled )
445 |  |
         {
446 | 0 |
             messageLevelStates[MessageLevels.LEVEL_SEVERE] = enabled;
447 | 0 |
         }
448 |  |
449 |  |
         public void setWarningEnabled( boolean enabled )
450 |  |
         {
451 | 0 |
             messageLevelStates[MessageLevels.LEVEL_WARNING] = enabled;
452 | 0 |
         }
453 |  |
454 |  |
         public void flush()
455 |  |
         {
456 | 0 |
             if ( onDemandSink != null && currentMessage != null )
457 |  |
             {
458 | 0 |
                 renderTo( currentMessage, onDemandSink );
459 | 0 |
                 currentMessage = null;
460 |  |
             }
461 | 0 |
         }
462 |  |
463 |  |
         public void render( MessageSink sink )
464 |  |
         {
465 | 0 |
             for ( Iterator it = messages.iterator(); it.hasNext(); )
466 |  |
             {
467 | 0 |
                 Message message = (Message) it.next();
468 |  |
469 | 0 |
                 renderTo( message, sink );
470 |  |
             }
471 | 0 |
         }
472 |  |
473 |  |
         protected void renderTo( Message message, MessageSink sink )
474 |  |
         {
475 | 0 |
             switch( message.getMessageLevel() )
476 |  |
             {
477 |  |
             case( MessageLevels.LEVEL_SEVERE ):
478 |  |
             {
479 | 0 |
                 sink.severe( message.render().toString() );
480 | 0 |
                 break;
481 |  |
             }
482 |  |
             case( MessageLevels.LEVEL_ERROR ):
483 |  |
             {
484 | 0 |
                 sink.error( message.render().toString() );
485 | 0 |
                 break;
486 |  |
             }
487 |  |
             case( MessageLevels.LEVEL_WARNING ):
488 |  |
             {
489 | 0 |
                 sink.warning( message.render().toString() );
490 | 0 |
                 break;
491 |  |
             }
492 |  |
             case( MessageLevels.LEVEL_INFO ):
493 |  |
             {
494 | 0 |
                 sink.info( message.render().toString() );
495 | 0 |
                 break;
496 |  |
             }
497 |  |
             default:
498 |  |
             {
499 | 0 |
                 sink.debug( message.render().toString() );
500 |  |
                 break;
501 |  |
             }
502 |  |
             }
503 | 0 |
         }
504 |  |
505 |  |
     }