You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by mt...@apache.org on 2022/02/16 21:12:44 UTC

[maven] branch master updated: [MNG-7406] Do not include formatted details in Exception message

This is an automated email from the ASF dual-hosted git repository.

mthmulders pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 0be5e40  [MNG-7406] Do not include formatted details in Exception message
0be5e40 is described below

commit 0be5e406d78062b56b32644fefce2642f5eab650
Author: Maarten Mulders <mt...@apache.org>
AuthorDate: Tue Feb 1 16:50:37 2022 +0100

    [MNG-7406] Do not include formatted details in Exception message
    
    Closes #672
---
 .../main/java/org/apache/maven/DefaultMaven.java   |  4 +-
 .../maven/project/ProjectBuildingException.java    | 39 +---------
 .../project/DefaultMavenProjectBuilderTest.java    | 10 ++-
 .../apache/maven/project/ProjectBuilderTest.java   | 11 ++-
 .../ProjectBuildingResultWithLocationMatcher.java  | 91 ++++++++++++++++++++++
 ...ectBuildingResultWithProblemMessageMatcher.java | 84 ++++++++++++++++++++
 6 files changed, 191 insertions(+), 48 deletions(-)

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 54ef5a0..56a42b7 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 @@ public class DefaultMaven
         {
             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 3ace7a4..148d215 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 @@ package org.apache.maven.project;
  * 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 @@ public class ProjectBuildingException
 
     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 @@ public class ProjectBuildingException
         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();
-    }
-
 }
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 a2ca999..10bda67 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 @@ package org.apache.maven.project;
  * 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 class DefaultMavenProjectBuilderTest
                 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 class DefaultMavenProjectBuilderTest
                 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 class DefaultMavenProjectBuilderTest
                 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 class DefaultMavenProjectBuilderTest
                 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 53b0af8..5590b9f 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.model.building.ModelSource;
 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 class ProjectBuilderTest
 
         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 class ProjectBuilderTest
         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 0000000..fafcb42
--- /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<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 );
+    }
+}
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 0000000..0ae052a
--- /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<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 );
+    }
+}