[maven-doxia-sitetools] branch master updated: [DOXIA-611] Add line number to RendererException message only when available

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[maven-doxia-sitetools] branch master updated: [DOXIA-611] Add line number to RendererException message only when available

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

slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia-sitetools.git


The following commit(s) were added to refs/heads/master by this push:
     new e549258  [DOXIA-611] Add line number to RendererException message only when available
e549258 is described below

commit e5492587e8455e90037763b86356d93466c3e263
Author: Abel Salgado Romero <[hidden email]>
AuthorDate: Tue Jun 30 00:04:58 2020 +0200

    [DOXIA-611] Add line number to RendererException message only when available
---
 .../doxia/siterenderer/DefaultSiteRenderer.java    | 10 ++-
 .../siterenderer/DefaultSiteRendererTest.java      | 81 +++++++++++++++++++++-
 2 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/DefaultSiteRenderer.java b/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/DefaultSiteRenderer.java
index 13eea52..6950b60 100644
--- a/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/DefaultSiteRenderer.java
+++ b/doxia-site-renderer/src/main/java/org/apache/maven/doxia/siterenderer/DefaultSiteRenderer.java
@@ -441,8 +441,14 @@ public class DefaultSiteRenderer
         }
         catch ( ParseException e )
         {
-            throw new RendererException( "Error parsing '"
-                    + doc + "': line [" + e.getLineNumber() + "] " + e.getMessage(), e );
+            StringBuilder errorMsgBuilder = new StringBuilder();
+            errorMsgBuilder.append( "Error parsing '" ).append( doc ).append( "': " );
+            if ( e.getLineNumber() > 0 )
+            {
+                errorMsgBuilder.append( "line [" ).append( e.getLineNumber() ).append( "] " );
+            }
+            errorMsgBuilder.append( e.getMessage() );
+            throw new RendererException( errorMsgBuilder.toString(), e );
         }
         catch ( IOException e )
         {
diff --git a/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java b/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
index 137a739..9570bcb 100644
--- a/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
+++ b/doxia-site-renderer/src/test/java/org/apache/maven/doxia/siterenderer/DefaultSiteRendererTest.java
@@ -21,9 +21,7 @@ package org.apache.maven.doxia.siterenderer;
 
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.isNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
 
 import java.io.File;
 import java.io.FileOutputStream;
@@ -35,6 +33,7 @@ import java.io.Reader;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.FileSystem;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -46,6 +45,10 @@ import java.util.zip.ZipEntry;
 import org.apache.commons.io.IOUtils;
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DefaultArtifact;
+import org.apache.maven.doxia.Doxia;
+import org.apache.maven.doxia.parser.ParseException;
+import org.apache.maven.doxia.parser.Parser;
+import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.site.decoration.DecorationModel;
 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
 import org.apache.maven.doxia.siterenderer.sink.SiteRendererSink;
@@ -54,7 +57,9 @@ import org.codehaus.plexus.PlexusTestCase;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.ReaderFactory;
+import org.codehaus.plexus.util.ReflectionUtils;
 import org.codehaus.plexus.util.StringUtils;
+import org.mockito.Mockito;
 import org.xml.sax.EntityResolver;
 
 /**
@@ -150,6 +155,76 @@ public class DefaultSiteRendererTest
     /**
      * @throws Exception if something goes wrong.
      */
+    public void testRenderExceptionMessageWhenLineNumberIsNotAvailable()
+        throws Exception
+    {
+        final File testBasedir = getTestFile( "src/test/resources/site/xdoc" );
+        final String testDocumentName = "head.xml";
+        final String exceptionMessage = "parse error occurred";
+
+        Doxia doxiaInstance = (Doxia) lookup( Doxia.class );
+        Doxia doxiaSpy = spy( doxiaInstance );
+        Mockito.doThrow( new ParseException( exceptionMessage ) )
+                .when( doxiaSpy )
+                .parse( Mockito.<Reader>any(), Mockito.anyString(), Mockito.<Sink>any() );
+        Renderer renderer = (Renderer) lookup( Renderer.class );
+        ReflectionUtils.setVariableValueInObject( renderer, "doxia", doxiaSpy );
+
+        RenderingContext renderingContext = new RenderingContext( testBasedir, "", testDocumentName, "xdoc", "",
+                false );
+
+        try
+        {
+            renderer.renderDocument( null, renderingContext, new SiteRenderingContext() );
+            fail( "should fail with exception" );
+        }
+        catch ( RendererException e )
+        {
+            assertEquals(
+                    String.format( "Error parsing '%s%s%s': %s",
+                            testBasedir.getAbsolutePath(), File.separator, testDocumentName, exceptionMessage ),
+                    e.getMessage() );
+        }
+    }
+
+    /**
+     * @throws Exception if something goes wrong.
+     */
+    public void testRenderExceptionMessageWhenLineNumberIsAvailable()
+        throws Exception
+    {
+        final File testBasedir = getTestFile( "src/test/resources/site/xdoc" );
+        final String testDocumentName = "head.xml";
+        final String exceptionMessage = "parse error occurred";
+
+        Doxia doxiaInstance = (Doxia) lookup( Doxia.class );
+        Doxia doxiaSpy = spy( doxiaInstance );
+        Mockito.doThrow( new ParseException( exceptionMessage, 42, 36 ) )
+                .when( doxiaSpy )
+                .parse( Mockito.<Reader>any(), Mockito.anyString(), Mockito.<Sink>any() );
+        Renderer renderer = (Renderer) lookup( Renderer.class );
+        ReflectionUtils.setVariableValueInObject( renderer, "doxia", doxiaSpy );
+
+        RenderingContext renderingContext = new RenderingContext( testBasedir, "", testDocumentName, "xdoc", "",
+                false );
+
+        try
+        {
+            renderer.renderDocument( null, renderingContext, new SiteRenderingContext() );
+            fail( "should fail with exception" );
+        }
+        catch ( RendererException e )
+        {
+            assertEquals(
+                    String.format( "Error parsing '%s%s%s': line [42] %s",
+                            testBasedir.getAbsolutePath(), File.separator, testDocumentName, exceptionMessage ),
+                    e.getMessage() );
+        }
+    }
+
+    /**
+     * @throws Exception if something goes wrong.
+     */
     public void testRender()
         throws Exception
     {