You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kr...@apache.org on 2012/10/28 15:44:18 UTC
svn commit: r1403003 - in /maven/shared/trunk/maven-shared-utils/src:
main/java/org/apache/maven/shared/utils/xml/
test/java/org/apache/maven/shared/utils/xml/
Author: krosenvold
Date: Sun Oct 28 14:44:18 2012
New Revision: 1403003
URL: http://svn.apache.org/viewvc?rev=1403003&view=rev
Log:
Added XmlWriterUtil and testcase
Original author Vincent Siveton, added with his permission.
Added:
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java
maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java
Modified:
maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java?rev=1403003&r1=1403002&r2=1403003&view=diff
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java (original)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/PrettyPrintXMLWriter.java Sun Oct 28 14:44:18 2012
@@ -236,8 +236,6 @@ public class PrettyPrintXMLWriter
completePreviouslyOpenedElement();
- newLine();
-
writer.write( markup );
}
Added: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java?rev=1403003&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/xml/XmlWriterUtil.java Sun Oct 28 14:44:18 2012
@@ -0,0 +1,343 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * 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.shared.utils.StringUtils;
+
+/**
+ * Utility class for the <code>XmlWriter</code> class.
+ *
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class XmlWriterUtil
+{
+ /** The vm line separator */
+ public static final String LS = System.getProperty( "line.separator" );
+
+ /** The default line indenter size i.e. 2. */
+ public static final int DEFAULT_INDENTATION_SIZE = 2;
+
+ /** The default column before line wrapping i.e. 80. */
+ public static final int DEFAULT_COLUMN_LINE = 80;
+
+ /**
+ * Convenience method to write one <code>CRLF</code>.
+ *
+ * @param writer not null writer
+ */
+ public static void writeLineBreak( XMLWriter writer )
+ {
+ writeLineBreak( writer, 1 );
+ }
+
+ /**
+ * Convenience method to repeat <code>CRLF</code>.
+ *
+ * @param writer not null
+ * @param repeat positive number
+ */
+ public static void writeLineBreak( XMLWriter writer, int repeat )
+ {
+ for ( int i = 0; i < repeat; i++ )
+ {
+ writer.writeMarkup( LS );
+ }
+ }
+
+ /**
+ * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>2</code>.
+ *
+ * @param writer not null
+ * @param repeat
+ * @param indent positive number
+ * @see #DEFAULT_INDENTATION_SIZE
+ * @see #writeLineBreak(XMLWriter, int, int, int)
+ */
+ public static void writeLineBreak( XMLWriter writer, int repeat, int indent )
+ {
+ writeLineBreak( writer, repeat, indent, DEFAULT_INDENTATION_SIZE );
+ }
+
+ /**
+ * Convenience method to repeat <code>CRLF</code> and to indent the writer by <code>indentSize</code>.
+ *
+ * @param writer not null
+ * @param repeat
+ * @param indent positive number
+ * @param indentSize positive number
+ */
+ public static void writeLineBreak( XMLWriter writer, int repeat, int indent, int indentSize )
+ {
+ writeLineBreak( writer, repeat );
+
+ if ( indent < 0 )
+ {
+ indent = 0;
+ }
+
+ if ( indentSize < 0 )
+ {
+ indentSize = 0;
+ }
+
+ writer.writeText( StringUtils.repeat( " ", indent * indentSize ) );
+ }
+
+ /**
+ * Convenience method to write XML comment line break. Its size is <code>80</code>.
+ *
+ * @param writer not null
+ * @see #DEFAULT_COLUMN_LINE
+ * @see #writeCommentLineBreak(XMLWriter, int)
+ */
+ public static void writeCommentLineBreak( XMLWriter writer )
+ {
+ writeCommentLineBreak( writer, DEFAULT_COLUMN_LINE );
+ }
+
+ /**
+ * Convenience method to write XML comment line break with <code>columnSize</code> as length.
+ *
+ * @param writer not null
+ * @param columnSize positive number
+ */
+ public static void writeCommentLineBreak( XMLWriter writer, int columnSize )
+ {
+ if ( columnSize < 10 )
+ {
+ columnSize = DEFAULT_COLUMN_LINE;
+ }
+
+ writer.writeMarkup( "<!-- " + StringUtils.repeat( "=", columnSize - 10 ) + " -->" + LS );
+ }
+
+ /**
+ * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code>.
+ *
+ * @param writer not null
+ * @param comment
+ * @see #DEFAULT_INDENTATION_SIZE
+ * @see #writeComment(XMLWriter, String, int, int)
+ */
+ public static void writeComment( XMLWriter writer, String comment )
+ {
+ writeComment( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
+ }
+
+ /**
+ * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code>
+ * and is indented by <code>indent</code> using <code>2</code> as indentation size.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @see #DEFAULT_INDENTATION_SIZE
+ * @see #writeComment(XMLWriter, String, int, int)
+ */
+ public static void writeComment( XMLWriter writer, String comment, int indent )
+ {
+ writeComment( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
+ }
+
+ /**
+ * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>80</code>
+ * and is indented by <code>indent</code> using <code>indentSize</code>.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @param indentSize positive number
+ * @see #DEFAULT_COLUMN_LINE
+ * @see #writeComment(XMLWriter, String, int, int, int)
+ */
+ public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize )
+ {
+ writeComment( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
+ }
+ /**
+ * Convenience method to write XML comment line. The <code>comment</code> is splitted to have a size of <code>columnSize</code>
+ * and is indented by <code>indent</code> using <code>indentSize</code>.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @param indentSize positive number
+ * @param columnSize positive number
+ */
+ public static void writeComment( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
+ {
+ if ( comment == null )
+ {
+ comment = "null";
+ }
+
+ if ( indent < 0 )
+ {
+ indent = 0;
+ }
+
+ if ( indentSize < 0 )
+ {
+ indentSize = 0;
+ }
+
+ if ( columnSize < 0 )
+ {
+ columnSize = DEFAULT_COLUMN_LINE;
+ }
+
+ String indentation = StringUtils.repeat( " ", indent * indentSize );
+ int magicNumber = indentation.length() + columnSize - "-->".length() - 1;
+ String[] sentences = StringUtils.split( comment, LS );
+
+ StringBuffer line = new StringBuffer( indentation + "<!-- " );
+ for ( int i = 0; i < sentences.length; i++ )
+ {
+ String sentence = sentences[i];
+ String[] words = StringUtils.split( sentence, " " );
+ for ( int j = 0; j < words.length; j++ )
+ {
+ StringBuffer sentenceTmp = new StringBuffer( line.toString() );
+ sentenceTmp.append( words[j] ).append( ' ' );
+ if ( sentenceTmp.length() > magicNumber )
+ {
+ if ( line.length() != indentation.length() + "<!-- ".length())
+ {
+ if ( magicNumber - line.length() > 0 )
+ {
+ line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
+ }
+
+ line.append( "-->" ).append( LS );
+ writer.writeMarkup( line.toString() );
+ }
+ line = new StringBuffer( indentation + "<!-- " );
+ line.append( words[j] ).append( ' ' );
+ }
+ else
+ {
+ line.append( words[j] ).append( ' ' );
+ }
+ }
+
+ if ( magicNumber - line.length() > 0 )
+ {
+ line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
+ }
+ }
+
+ if ( line.length() <= magicNumber )
+ {
+ line.append( StringUtils.repeat( " ", magicNumber - line.length() ) );
+ }
+
+ line.append( "-->" ).append( LS );
+
+ writer.writeMarkup( line.toString() );
+ }
+
+ /**
+ * Convenience method to write XML comments between two comments line break.
+ * The XML comment block is not indented.
+ *
+ * @param writer not null
+ * @param comment
+ * @see #DEFAULT_INDENTATION_SIZE
+ * @see #writeCommentText(XMLWriter, String, int, int)
+ */
+ public static void writeCommentText( XMLWriter writer, String comment )
+ {
+ writeCommentText( writer, comment, 0, DEFAULT_INDENTATION_SIZE );
+ }
+
+ /**
+ * Convenience method to write XML comments between two comments line break.
+ * The XML comment block is also indented by <code>indent</code> using
+ * <code>2</code> as indentation size.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @see #DEFAULT_INDENTATION_SIZE
+ * @see #writeCommentText(XMLWriter, String, int, int)
+ */
+ public static void writeCommentText( XMLWriter writer, String comment, int indent )
+ {
+ writeCommentText( writer, comment, indent, DEFAULT_INDENTATION_SIZE );
+ }
+
+ /**
+ * Convenience method to write XML comment between two comment line break.
+ * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @param indentSize positive number
+ * @see #DEFAULT_COLUMN_LINE
+ * @see #writeCommentText(XMLWriter, String, int, int, int)
+ */
+ public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize )
+ {
+ writeCommentText( writer, comment, indent, indentSize, DEFAULT_COLUMN_LINE );
+ }
+
+ /**
+ * Convenience method to write XML comments between two comments line break.
+ * The XML comment block is also indented by <code>indent</code> using <code>indentSize</code>.
+ * The column size could be also be specified.
+ *
+ * @param writer not null
+ * @param comment
+ * @param indent positive number
+ * @param indentSize positive number
+ * @param columnSize positive number
+ */
+ public static void writeCommentText( XMLWriter writer, String comment, int indent, int indentSize, int columnSize )
+ {
+ if ( indent < 0 )
+ {
+ indent = 0;
+ }
+
+ if ( indentSize < 0 )
+ {
+ indentSize = 0;
+ }
+
+ if ( columnSize < 0 )
+ {
+ columnSize = DEFAULT_COLUMN_LINE;
+ }
+
+ writeLineBreak( writer, 1 );
+
+ writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
+ writeCommentLineBreak( writer, columnSize );
+
+ writeComment( writer, comment, indent, indentSize, columnSize );
+
+ writer.writeMarkup( StringUtils.repeat( " ", indent * indentSize ) );
+ writeCommentLineBreak( writer, columnSize );
+
+ writeLineBreak( writer, 1, indent, indentSize );
+ }
+}
Added: maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java?rev=1403003&view=auto
==============================================================================
--- maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java (added)
+++ maven/shared/trunk/maven-shared-utils/src/test/java/org/apache/maven/shared/utils/xml/XmlWriterUtilTest.java Sun Oct 28 14:44:18 2012
@@ -0,0 +1,435 @@
+package org.apache.maven.shared.utils.xml;
+
+/*
+ * 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 java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import org.apache.maven.shared.utils.StringUtils;
+import org.apache.maven.shared.utils.WriterFactory;
+
+import junit.framework.TestCase;
+
+/**
+ * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
+ * @version $Id$
+ */
+public class XmlWriterUtilTest
+ extends TestCase
+{
+ private OutputStream output;
+
+ private Writer writer;
+
+ private XMLWriter xmlWriter;
+
+ /** {@inheritDoc} */
+ protected void setUp()
+ throws Exception
+ {
+ super.setUp();
+
+ output = new ByteArrayOutputStream();
+ writer = WriterFactory.newXmlWriter( output );
+ xmlWriter = new PrettyPrintXMLWriter( writer );
+ }
+
+ /** {@inheritDoc} */
+ protected void tearDown()
+ throws Exception
+ {
+ super.tearDown();
+
+ xmlWriter = null;
+ writer = null;
+ output = null;
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteLineBreakXMLWriter()
+ throws Exception
+ {
+ XmlWriterUtil.writeLineBreak( xmlWriter );
+ writer.close();
+ System.out.println( "outpur = " + output.toString() + "x");
+ assertTrue( StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) == 1 );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteLineBreakXMLWriterInt()
+ throws Exception
+ {
+ XmlWriterUtil.writeLineBreak( xmlWriter, 10 );
+ writer.close();
+ assertTrue( StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) == 10 );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteLineBreakXMLWriterIntInt()
+ throws Exception
+ {
+ XmlWriterUtil.writeLineBreak( xmlWriter, 10, 2 );
+ writer.close();
+ assertTrue( StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) == 10 );
+ assertTrue( StringUtils.countMatches( output.toString(), StringUtils
+ .repeat( " ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE ) ) == 1 );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteLineBreakXMLWriterIntIntInt()
+ throws Exception
+ {
+ XmlWriterUtil.writeLineBreak( xmlWriter, 10, 2, 4 );
+ writer.close();
+ assertTrue( StringUtils.countMatches( output.toString(), XmlWriterUtil.LS ) == 10 );
+ assertTrue( StringUtils.countMatches( output.toString(), StringUtils.repeat( " ", 2 * 4 ) ) == 1 );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentLineBreak(XMLWriter)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentLineBreakXMLWriter()
+ throws Exception
+ {
+ XmlWriterUtil.writeCommentLineBreak( xmlWriter );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentLineBreak(XMLWriter, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentLineBreakXMLWriterInt()
+ throws Exception
+ {
+ XmlWriterUtil.writeCommentLineBreak( xmlWriter, 20 );
+ writer.close();
+ assertEquals( output.toString(), "<!-- ========== -->" + XmlWriterUtil.LS );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeCommentLineBreak( xmlWriter, 10 );
+ writer.close();
+ assertEquals( output.toString(), output.toString(), "<!-- -->" + XmlWriterUtil.LS );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentXMLWriterString()
+ throws Exception
+ {
+ XmlWriterUtil.writeComment( xmlWriter, "hello" );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeComment( xmlWriter,
+ "hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( "<!-- hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo -->" )
+ .append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() >= XmlWriterUtil.DEFAULT_COLUMN_LINE );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello\nworld" );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentXMLWriterStringInt()
+ throws Exception
+ {
+ String indent = StringUtils.repeat( " ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE );
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello", 2 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( indent );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() + 2
+ * XmlWriterUtil.DEFAULT_INDENTATION_SIZE );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello\nworld", 2 );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( indent );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( indent );
+ sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) + 2 * indent.length() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentXMLWriterStringIntInt()
+ throws Exception
+ {
+ String repeat = StringUtils.repeat( " ", 2 * 4 );
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello", 2, 4 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( repeat );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() + 2 * 4 );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello\nworld", 2, 4 );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( repeat );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( repeat );
+ sb.append( "<!-- world -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 2 * ( XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + XmlWriterUtil.LS.length() ) + 2 * repeat.length() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentXMLWriterStringIntIntInt()
+ throws Exception
+ {
+ String indent = StringUtils.repeat( " ", 2 * 4 );
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello", 2, 4, 50 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( indent );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 50 - 1 + XmlWriterUtil.LS.length() + 2 * 4 );
+
+ tearDown();
+ setUp();
+
+ XmlWriterUtil.writeComment( xmlWriter, "hello", 2, 4, 10 );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( indent );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() >= 10 + 2 * 4 );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentTextXMLWriterStringInt()
+ throws Exception
+ {
+ XmlWriterUtil.writeCommentText( xmlWriter, "hello", 0 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( XmlWriterUtil.LS );
+ sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- ====================================================================== -->" ).append( XmlWriterUtil.LS );
+ sb.append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 3 * ( 80 - 1 + XmlWriterUtil.LS.length() ) + 2 * XmlWriterUtil.LS.length() );
+
+ tearDown();
+ setUp();
+
+ String indent = StringUtils.repeat( " ", 2 * 2 );
+
+ XmlWriterUtil.writeCommentText( xmlWriter, "hello world with end of line\n and "
+ + "loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong line", 2 );
+ writer.close();
+ sb = new StringBuffer();
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ====================================================================== -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- hello world with end of line -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- and -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- line -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ====================================================================== -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent );
+ assertEquals( output.toString(), sb.toString() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentTextXMLWriterStringIntInt()
+ throws Exception
+ {
+ String indent = StringUtils.repeat( " ", 2 * 4 );
+
+ XmlWriterUtil.writeCommentText( xmlWriter, "hello", 2, 4 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ====================================================================== -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- hello -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ====================================================================== -->" )
+ .append( XmlWriterUtil.LS );
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 3 * ( 80 - 1 + XmlWriterUtil.LS.length() ) + 4 * 2 * 4 + 2 * XmlWriterUtil.LS.length() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int, int, int)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentTextXMLWriterStringIntIntInt()
+ throws Exception
+ {
+ String indent = StringUtils.repeat( " ", 2 * 4 );
+
+ XmlWriterUtil.writeCommentText( xmlWriter, "hello", 2, 4, 50 );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ======================================== -->" ).append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- hello -->" ).append( XmlWriterUtil.LS );
+ sb.append( indent ).append( "<!-- ======================================== -->" ).append( XmlWriterUtil.LS );
+ sb.append( XmlWriterUtil.LS );
+ sb.append( indent );
+ assertEquals( output.toString(), sb.toString() );
+ assertTrue( output.toString().length() == 3 * ( 50 - 1 + XmlWriterUtil.LS.length() ) + 4 * 2 * 4 + 2 * XmlWriterUtil.LS.length() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentNull()
+ throws Exception
+ {
+ XmlWriterUtil.writeComment( xmlWriter, null );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( "<!-- null -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentShort()
+ throws Exception
+ {
+ XmlWriterUtil.writeComment( xmlWriter, "This is a short text" );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( "<!-- This is a short text -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ }
+
+ /**
+ * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
+ *
+ * @throws Exception if any
+ */
+ public void testWriteCommentLong()
+ throws Exception
+ {
+ XmlWriterUtil.writeComment( xmlWriter, "Maven is a software project management and comprehension tool. "
+ + "Based on the concept of a project object model (POM), Maven can manage a project's build, reporting "
+ + "and documentation from a central piece of information." );
+ writer.close();
+ StringBuffer sb = new StringBuffer();
+ sb.append( "<!-- Maven is a software project management and comprehension tool. Based -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- on the concept of a project object model (POM), Maven can manage a -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- project's build, reporting and documentation from a central piece of -->" ).append( XmlWriterUtil.LS );
+ sb.append( "<!-- information. -->" ).append( XmlWriterUtil.LS );
+ assertEquals( output.toString(), sb.toString() );
+ }
+}