You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2021/04/21 11:46:59 UTC

[maven-shared-utils] branch master updated: [SUREFIRE-1556] fail fast on empty element names (#11)

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

elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-shared-utils.git


The following commit(s) were added to refs/heads/master by this push:
     new 4e93c56  [SUREFIRE-1556] fail fast on empty element names (#11)
4e93c56 is described below

commit 4e93c5621ef45c7cf6a03d96ca7a3c45ac4e2062
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Wed Apr 21 11:46:52 2021 +0000

    [SUREFIRE-1556] fail fast on empty element names (#11)
    
    * fail fast on empty element names
---
 .../shared/utils/xml/PrettyPrintXMLWriter.java     | 52 ++++++++++++----------
 .../apache/maven/shared/utils/xml/XMLWriter.java   | 26 ++++++-----
 .../shared/utils/xml/PrettyPrintXmlWriterTest.java | 19 ++++++--
 3 files changed, 59 insertions(+), 38 deletions(-)

diff --git a/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java b/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
index b0d1a7d..f38c1ab 100644
--- a/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
+++ b/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
@@ -60,7 +60,7 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param lineIndent could be null, but the normal way is some spaces.
+     * @param lineIndent can be null, but the normal way is some spaces
      */
     public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent )
     {
@@ -69,7 +69,7 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param lineIndent could be null, but the normal way is some spaces.
+     * @param lineIndent can be null, but the normal way is some spaces.
      */
     public PrettyPrintXMLWriter( Writer writer, String lineIndent )
     {
@@ -94,9 +94,9 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param lineIndent could be null, but the normal way is some spaces.
-     * @param encoding could be null or invalid.
-     * @param doctype could be null.
+     * @param lineIndent can be null, but the normal way is some spaces
+     * @param encoding can be null or invalid
+     * @param doctype can be null
      */
     public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String encoding, String doctype )
     {
@@ -105,9 +105,9 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param lineIndent could be null, but the normal way is some spaces.
-     * @param encoding could be null or invalid.
-     * @param doctype could be null.
+     * @param lineIndent can be null, but the normal way is some spaces
+     * @param encoding can be null or invalid
+     * @param doctype can be null
      */
     public PrettyPrintXMLWriter( Writer writer, String lineIndent, String encoding, String doctype )
     {
@@ -116,8 +116,8 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param encoding could be null or invalid.
-     * @param doctype could be null.
+     * @param encoding can be null or invalid
+     * @param doctype can be null
      */
     public PrettyPrintXMLWriter( PrintWriter writer, String encoding, String doctype )
     {
@@ -126,8 +126,8 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param encoding could be null or invalid.
-     * @param doctype could be null.
+     * @param encoding can be null or invalid
+     * @param doctype can be null
      */
     public PrettyPrintXMLWriter( Writer writer, String encoding, String doctype )
     {
@@ -136,10 +136,10 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer not null
-     * @param lineIndent could be null, but the normal way is some spaces.
-     * @param lineSeparator could be null, but the normal way is valid line separator
-     * @param encoding could be null or the encoding to use.
-     * @param doctype could be null.
+     * @param lineIndent can be null, but the normal way is some spaces.
+     * @param lineSeparator can be null, but the normal way is valid line separator
+     * @param encoding can be null or the encoding to use.
+     * @param doctype can be null
      */
     public PrettyPrintXMLWriter( PrintWriter writer, String lineIndent, String lineSeparator, String encoding,
                                  String doctype )
@@ -149,10 +149,10 @@ public class PrettyPrintXMLWriter
 
     /**
      * @param writer        not null
-     * @param lineIndent    could be null, but the normal way is some spaces.
-     * @param lineSeparator could be null, but the normal way is valid line separator
-     * @param encoding      could be null or the encoding to use.
-     * @param doctype       could be null.
+     * @param lineIndent    can be null, but the normal way is some spaces
+     * @param lineSeparator can be null, but the normal way is valid line separator
+     * @param encoding      can be null or the encoding to use
+     * @param doctype       can be null
      */
     private PrettyPrintXMLWriter( PrintWriter writer, char[] lineIndent, char[] lineSeparator, String encoding,
                                   String doctype )
@@ -211,7 +211,7 @@ public class PrettyPrintXMLWriter
     }
 
     /**
-     * @param lineSeparator The line separator to be used.
+     * @param lineSeparator the line separator to be output
      */
     public void setLineSeparator( String lineSeparator )
     {
@@ -224,7 +224,7 @@ public class PrettyPrintXMLWriter
     }
 
     /**
-     * @param lineIndentParameter The line indent parameter.
+     * @param lineIndentParameter the line indent parameter
      */
     public void setLineIndenter( String lineIndentParameter )
     {
@@ -239,6 +239,12 @@ public class PrettyPrintXMLWriter
     /** {@inheritDoc} */
     public void startElement( String elementName ) throws IOException
     {
+        
+        if ( elementName.isEmpty() )
+        {
+            throw new IllegalArgumentException( "Element name cannot be empty" );
+        }
+        
         boolean firstLine = ensureDocumentStarted();
 
         completePreviouslyOpenedElement();
@@ -325,7 +331,7 @@ public class PrettyPrintXMLWriter
     }
 
     /**
-     * Write the documents if not already done.
+     * Write the document if not already done.
      *
      * @return <code>true</code> if the document headers have freshly been written.
      */
diff --git a/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java b/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java
index 0daad8b..32a56d2 100644
--- a/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java
+++ b/src/main/java/org/apache/maven/shared/utils/xml/XMLWriter.java
@@ -30,7 +30,7 @@ public interface XMLWriter
 
     /**
      * Sets the encoding of the document.
-     * If not set, UTF-8 is being used
+     * If not set, UTF-8 is used.
      *
      * @param encoding the encoding
      * @throws IllegalStateException if the generation of the document has already started
@@ -38,7 +38,7 @@ public interface XMLWriter
     void setEncoding( String encoding );
 
     /**
-     * Sets the docType of the document.
+     * Sets the DOCTYPE of the document.
      *
      * @param docType the docType
      * @throws IllegalStateException if the generation of the document has already started
@@ -48,15 +48,17 @@ public interface XMLWriter
 
     /**
      * Start an XML Element tag.
-     * @param name The name of the tag.
-     * @throws IOException if starting the element fails.
+     * 
+     * @param name the name of the tag
+     * @throws IOException if starting the element fails
      */
     void startElement( String name ) throws IOException;
 
 
     /**
      * Add a XML attribute to the current XML Element.
-     * This method must get called immediately after {@link #startElement(String)}
+     * This method must get called immediately after {@link #startElement(String)}.
+     * 
      * @param key The key of the attribute.
      * @param value The value of the attribute.
      * @throws IllegalStateException if no element tag is currently in process
@@ -65,8 +67,9 @@ public interface XMLWriter
     void addAttribute( String key, String value ) throws IOException;
 
     /**
-     * Add a value text to the current element tag
-     * This will perform XML escaping to guarantee valid content
+     * Add text to the current element tag.
+     * This performs XML escaping to guarantee well-formed content.
+     * 
      * @param text The text which should be written.
      * @throws IllegalStateException if no element tag got started yet
      * @throws IOException if writing the text fails.
@@ -74,10 +77,11 @@ public interface XMLWriter
     void writeText( String text ) throws IOException;
 
     /**
-     * Add a preformatted markup to the current element tag
-     * @param text The text which should be written.
-     * @throws IllegalStateException if no element tag got started yet
-     * @throws IOException if writing the markup fails.
+     * Add preformatted markup to the current element tag.
+     * 
+     * @param text the text which should be written
+     * @throws IllegalStateException if no element tag is started yet
+     * @throws IOException if writing the markup fails
      */
     void writeMarkup( String text ) throws IOException;
 
diff --git a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java
index 4830f43..d061111 100644
--- a/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java
+++ b/src/test/java/org/apache/maven/shared/utils/xml/PrettyPrintXmlWriterTest.java
@@ -20,9 +20,8 @@ package org.apache.maven.shared.utils.xml;
  */
 
 import java.io.IOException;
-
-import javax.swing.text.html.HTML;
 import java.io.StringWriter;
+import javax.swing.text.html.HTML;
 
 import org.apache.maven.shared.utils.StringUtils;
 import org.junit.Assert;
@@ -32,14 +31,26 @@ import org.junit.Test;
  * Test of {@link PrettyPrintXMLWriter}
  *
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
- *
  */
 public class PrettyPrintXmlWriterTest
 {
-    private StringWriter w = new StringWriter();
+    private StringWriter w = new StringWriter();;
     private PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( w );
     
     @Test
+    public void testNoStartTag() throws IOException
+    {
+        
+        try {
+            writer.startElement( "" );
+            Assert.fail( "allowed empty name" );
+        } catch ( IllegalArgumentException ex ) {
+            Assert.assertEquals( "Element name cannot be empty", ex.getMessage() );
+        }
+        
+    }
+    
+    @Test
     public void testDefaultPrettyPrintXMLWriter() throws IOException
     {
         writer.startElement( HTML.Tag.HTML.toString() );