[MNG-7406] Do not include formatted details in Exception message

Closes #672
This commit is contained in:
Maarten Mulders 2022-02-01 16:50:37 +01:00
parent cc51006f29
commit 0be5e406d7
6 changed files with 191 additions and 48 deletions

View File

@ -709,11 +709,11 @@ private Result<? extends ProjectDependencyGraph> buildGraph( MavenSession sessio
{
if ( problem.getSeverity() == ModelProblem.Severity.WARNING )
{
logger.warn( problem.toString() );
logger.warn( problem.getMessage() );
}
else
{
logger.error( problem.toString() );
logger.error( problem.getMessage() );
}
}

View File

@ -19,12 +19,7 @@
* under the License.
*/
import org.apache.maven.model.building.ModelProblem;
import org.apache.maven.model.building.ModelProblemUtils;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
/**
@ -72,7 +67,7 @@ protected ProjectBuildingException( String projectId, String message, File pomFi
public ProjectBuildingException( List<ProjectBuildingResult> results )
{
super( createMessage( results ) );
super( "Some problems were encountered while processing the POMs" );
this.projectId = "";
this.results = results;
}
@ -119,36 +114,4 @@ private static String createMessage( String message, String projectId, File pomF
return buffer.toString();
}
private static String createMessage( List<ProjectBuildingResult> results )
{
StringWriter buffer = new StringWriter( 1024 );
PrintWriter writer = new PrintWriter( buffer );
writer.println( "Some problems were encountered while processing the POMs:" );
try
{
for ( ProjectBuildingResult result : results )
{
for ( ModelProblem problem : result.getProblems() )
{
writer.print( "[" );
writer.print( problem.getSeverity() );
writer.print( "] " );
writer.print( problem.getMessage() );
String location = ModelProblemUtils.formatLocation( problem, result.getProjectId() );
if ( !location.isEmpty() )
{
writer.print( " @ " );
writer.println( location );
}
}
}
}
finally
{
writer.close();
}
return buffer.toString();
}
}

View File

@ -19,8 +19,10 @@
* under the License.
*/
import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -241,7 +243,7 @@ public void testBuildParentVersionRangeLocallyWithoutChildVersion() throws Excep
ProjectBuildingException.class,
() -> getProject( f1 ),
"Expected 'ProjectBuildingException' not thrown." );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
assertThat( e.getResults(), contains( projectBuildingResultWithProblemMessage( "Version must be a constant" ) ) );
}
/**
@ -260,7 +262,7 @@ public void testBuildParentVersionRangeLocallyWithChildProjectVersionExpression(
ProjectBuildingException.class,
() -> getProject( f1 ),
"Expected 'ProjectBuildingException' not thrown." );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
assertThat( e.getResults(), contains( projectBuildingResultWithProblemMessage( "Version must be a constant" ) ) );
}
/**
@ -339,7 +341,7 @@ public void testBuildParentVersionRangeExternallyWithoutChildVersion() throws Ex
ProjectBuildingException.class,
() -> getProjectFromRemoteRepository( f1 ),
"Expected 'ProjectBuildingException' not thrown." );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
assertThat( e.getResults(), contains( projectBuildingResultWithProblemMessage( "Version must be a constant" ) ) );
}
/**
@ -358,7 +360,7 @@ public void testBuildParentVersionRangeExternallyWithChildProjectVersionExpressi
ProjectBuildingException.class,
() -> getProjectFromRemoteRepository( f1 ),
"Expected 'ProjectBuildingException' not thrown." );
assertThat( e.getMessage(), containsString( "Version must be a constant" ) );
assertThat( e.getResults(), contains( projectBuildingResultWithProblemMessage( "Version must be a constant") ) );
}
/**

View File

@ -38,7 +38,10 @@
import org.apache.maven.shared.utils.io.FileUtils;
import org.junit.jupiter.api.Test;
import static org.apache.maven.project.ProjectBuildingResultWithLocationMatcher.projectBuildingResultWithLocation;
import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.greaterThan;
@ -103,9 +106,9 @@ public void testVersionlessManagedDependency()
ProjectBuildingException e = assertThrows( ProjectBuildingException.class,
() -> getContainer().lookup( org.apache.maven.project.ProjectBuilder.class ).build( pomFile, configuration ) );
assertThat( e.getMessage(),
containsString( "[ERROR] 'dependencies.dependency.version' for org.apache.maven.its:a:jar is missing. "
+ "@ line 9, column 17" ) );
assertThat( e.getResults(), contains( projectBuildingResultWithProblemMessage(
"'dependencies.dependency.version' for org.apache.maven.its:a:jar is missing" ) ) );
assertThat( e.getResults(), contains( projectBuildingResultWithLocation( 17, 9 ) ) );
}
@Test
@ -251,7 +254,7 @@ public void testReadInvalidPom()
assertEquals( 1, pex.getResults().size() );
assertNotNull( pex.getResults().get( 0 ).getPomFile() );
assertThat( pex.getResults().get( 0 ).getProblems().size(), greaterThan( 0 ) );
assertThat( pex.getMessage(), containsString( "expected START_TAG or END_TAG not TEXT" ) );
assertThat( pex.getResults(), contains( projectBuildingResultWithProblemMessage( "expected START_TAG or END_TAG not TEXT" ) ) );
}
@Test

View File

@ -0,0 +1,91 @@
package org.apache.maven.project;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.building.ModelProblem;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static java.util.stream.Collectors.joining;
/**
* Hamcrest matcher to help create fluent assertions about {@link ProjectBuildingResult} instances.
*/
class ProjectBuildingResultWithLocationMatcher extends BaseMatcher<ProjectBuildingResult>
{
private final int columnNumber;
private final int lineNumber;
ProjectBuildingResultWithLocationMatcher( int columnNumber, int lineNumber )
{
this.columnNumber = columnNumber;
this.lineNumber = lineNumber;
}
@Override
public boolean matches( Object o )
{
if ( !( o instanceof ProjectBuildingResult ) )
{
return false;
}
final ProjectBuildingResult r = (ProjectBuildingResult) o;
return r.getProblems().stream()
.anyMatch( p -> p.getLineNumber() == lineNumber && p.getColumnNumber() == columnNumber );
}
@Override
public void describeTo( Description description )
{
description.appendText( "a ProjectBuildingResult with location " )
.appendText( formatLocation( columnNumber, lineNumber ) );
}
private String formatLocation( int columnNumber, int lineNumber )
{
return String.format( "line %d, column %d", lineNumber, columnNumber );
}
@Override
public void describeMismatch(final Object o, final Description description)
{
if ( !( o instanceof ProjectBuildingResult ) )
{
super.describeMismatch( o, description );
}
else
{
final ProjectBuildingResult r = (ProjectBuildingResult) o;
description.appendText( "was a ProjectBuildingResult with locations " );
String messages = r.getProblems().stream()
.map( p -> formatLocation( p.getColumnNumber(), p.getLineNumber() ) )
.collect( joining( ", ") );
description.appendText( messages );
}
}
static Matcher<ProjectBuildingResult> projectBuildingResultWithLocation( int columnNumber, int lineNumber )
{
return new ProjectBuildingResultWithLocationMatcher( columnNumber, lineNumber );
}
}

View File

@ -0,0 +1,84 @@
package org.apache.maven.project;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.apache.maven.model.building.ModelProblem;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import static java.util.stream.Collectors.joining;
/**
* Hamcrest matcher to help create fluent assertions about {@link ProjectBuildingResult} instances.
*/
class ProjectBuildingResultWithProblemMessageMatcher extends BaseMatcher<ProjectBuildingResult>
{
private final String problemMessage;
ProjectBuildingResultWithProblemMessageMatcher( String problemMessage ) {
this.problemMessage = problemMessage;
}
@Override
public boolean matches( Object o )
{
if ( !( o instanceof ProjectBuildingResult ) )
{
return false;
}
final ProjectBuildingResult r = (ProjectBuildingResult) o;
return r.getProblems().stream()
.anyMatch( p -> p.getMessage().contains( problemMessage ) );
}
@Override
public void describeTo( Description description )
{
description.appendText( "a ProjectBuildingResult with message " )
.appendValue(problemMessage);
}
@Override
public void describeMismatch(final Object o, final Description description)
{
if ( !( o instanceof ProjectBuildingResult ) )
{
super.describeMismatch( o, description );
}
else
{
final ProjectBuildingResult r = (ProjectBuildingResult) o;
description.appendText( "was a ProjectBuildingResult with messages " );
String messages = r.getProblems().stream()
.map( ModelProblem::getMessage )
.map( m -> "\"" + m + "\"" + System.lineSeparator() )
.collect( joining( ", ") );
description.appendText( messages );
}
}
static Matcher<ProjectBuildingResult> projectBuildingResultWithProblemMessage( String message )
{
return new ProjectBuildingResultWithProblemMessageMatcher( message );
}
}