Skip to content

Commit 25425c3

Browse files
committed
[SUREFIRE-2052] Handles internal exceptions do not have suppressed exceptions in ThreadedStreamConsumer
1 parent 32f7dd3 commit 25425c3

2 files changed

Lines changed: 70 additions & 19 deletions

File tree

maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/MultipleFailureException.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,43 @@
2020
*/
2121

2222
import java.io.IOException;
23-
import java.util.Queue;
24-
import java.util.concurrent.ConcurrentLinkedQueue;
23+
import java.util.function.Function;
2524

2625
final class MultipleFailureException
2726
extends IOException
2827
{
29-
private final Queue<Throwable> exceptions = new ConcurrentLinkedQueue<>();
30-
3128
void addException( Throwable exception )
3229
{
33-
exceptions.add( exception );
30+
addSuppressed( exception );
3431
}
3532

3633
boolean hasNestedExceptions()
3734
{
38-
return !exceptions.isEmpty();
35+
return getSuppressed().length != 0;
3936
}
4037

4138
@Override
4239
public String getLocalizedMessage()
4340
{
44-
StringBuilder messages = new StringBuilder();
45-
for ( Throwable exception : exceptions )
46-
{
47-
if ( messages.length() != 0 )
48-
{
49-
messages.append( '\n' );
50-
}
51-
String message = exception.getLocalizedMessage();
52-
messages.append( message == null ? exception.toString() : message );
53-
}
54-
return messages.toString();
41+
return toMessage( Throwable::getLocalizedMessage );
5542
}
5643

5744
@Override
5845
public String getMessage()
46+
{
47+
return toMessage( Throwable::getMessage );
48+
}
49+
50+
private String toMessage( Function<Throwable, String> msg )
5951
{
6052
StringBuilder messages = new StringBuilder();
61-
for ( Throwable exception : exceptions )
53+
for ( Throwable exception : getSuppressed() )
6254
{
6355
if ( messages.length() != 0 )
6456
{
6557
messages.append( '\n' );
6658
}
67-
String message = exception.getMessage();
59+
String message = msg.apply( exception );
6860
messages.append( message == null ? exception.toString() : message );
6961
}
7062
return messages.toString();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.apache.maven.plugin.surefire.booterclient.output;
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
import java.io.IOException;
23+
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@code MultipleFailureException}.
30+
*/
31+
public class MultipleFailureExceptionTest
32+
{
33+
@Test
34+
public void test()
35+
{
36+
MultipleFailureException e = new MultipleFailureException();
37+
NullPointerException suppressed1 = new NullPointerException( "field is null" );
38+
IOException suppressed2 = new IOException( "read error" );
39+
e.addException( suppressed1 );
40+
e.addException( suppressed2 );
41+
42+
assertThat( e.getMessage() )
43+
.contains( "field is null" )
44+
.contains( "read error" );
45+
46+
assertThat( e.getLocalizedMessage() )
47+
.contains( "field is null" )
48+
.contains( "read error" );
49+
50+
assertThat( e.getSuppressed() )
51+
.hasSize( 2 );
52+
53+
assertThat( e.getSuppressed() )
54+
.contains( suppressed1, suppressed2 );
55+
56+
assertThat( e.hasNestedExceptions() )
57+
.isTrue();
58+
}
59+
}

0 commit comments

Comments
 (0)