diff --git a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java index 54ef5a01ba..56a42b7244 100644 --- a/maven-core/src/main/java/org/apache/maven/DefaultMaven.java +++ b/maven-core/src/main/java/org/apache/maven/DefaultMaven.java @@ -709,11 +709,11 @@ private Result 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() ); } } diff --git a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java index 3ace7a489a..148d215862 100644 --- a/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java +++ b/maven-core/src/main/java/org/apache/maven/project/ProjectBuildingException.java @@ -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 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 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(); - } - } diff --git a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java index a2ca9998e7..10bda67d0a 100644 --- a/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/DefaultMavenProjectBuilderTest.java @@ -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") ) ); } /** diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java index 53b0af88b0..5590b9f728 100644 --- a/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuilderTest.java @@ -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 diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithLocationMatcher.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithLocationMatcher.java new file mode 100644 index 0000000000..fafcb42d7c --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithLocationMatcher.java @@ -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 +{ + 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 projectBuildingResultWithLocation( int columnNumber, int lineNumber ) + { + return new ProjectBuildingResultWithLocationMatcher( columnNumber, lineNumber ); + } +} diff --git a/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithProblemMessageMatcher.java b/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithProblemMessageMatcher.java new file mode 100644 index 0000000000..0ae052a030 --- /dev/null +++ b/maven-core/src/test/java/org/apache/maven/project/ProjectBuildingResultWithProblemMessageMatcher.java @@ -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 +{ + 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 projectBuildingResultWithProblemMessage( String message ) + { + return new ProjectBuildingResultWithProblemMessageMatcher( message ); + } +}