View Javadoc
1   package org.codehaus.plexus.util.xml;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.StringWriter;
20  
21  import javax.swing.text.html.HTML.Tag;
22  
23  import org.codehaus.plexus.util.StringUtils;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * Test of {@link PrettyPrintXMLWriter}
29   *
30   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
31   * @version $Id$
32   */
33  public class PrettyPrintXMLWriterTest
34      extends TestCase
35  {
36      StringWriter w;
37  
38      PrettyPrintXMLWriter writer;
39  
40      /** {@inheritDoc} */
41      protected void setUp()
42          throws Exception
43      {
44          super.setUp();
45  
46          initWriter();
47      }
48  
49      /** {@inheritDoc} */
50      protected void tearDown()
51          throws Exception
52      {
53          super.tearDown();
54  
55          writer = null;
56          w = null;
57      }
58  
59      private void initWriter()
60      {
61          w = new StringWriter();
62          writer = new PrettyPrintXMLWriter( w );
63      }
64  
65      public void testDefaultPrettyPrintXMLWriter()
66      {
67          writer.startElement( Tag.HTML.toString() );
68  
69          writeXhtmlHead( writer );
70  
71          writeXhtmlBody( writer );
72  
73          writer.endElement(); // Tag.HTML
74  
75          assertEquals( expectedResult( PrettyPrintXMLWriter.LS ), w.toString() );
76      }
77  
78      public void testPrettyPrintXMLWriterWithGivenLineSeparator()
79      {
80          writer.setLineSeparator( "\n" );
81  
82          writer.startElement( Tag.HTML.toString() );
83  
84          writeXhtmlHead( writer );
85  
86          writeXhtmlBody( writer );
87  
88          writer.endElement(); // Tag.HTML
89  
90          assertEquals( expectedResult( "\n" ), w.toString() );
91      }
92  
93      public void testPrettyPrintXMLWriterWithGivenLineIndenter()
94      {
95          writer.setLineIndenter( "    " );
96  
97          writer.startElement( Tag.HTML.toString() );
98  
99          writeXhtmlHead( writer );
100 
101         writeXhtmlBody( writer );
102 
103         writer.endElement(); // Tag.HTML
104 
105         assertEquals( expectedResult( "    ", PrettyPrintXMLWriter.LS ), w.toString() );
106     }
107 
108     public void testEscapeXmlAttribute()
109     {
110         // Windows
111         writer.startElement( Tag.DIV.toString() );
112         writer.addAttribute( "class", "sect\r\nion" );
113         writer.endElement(); // Tag.DIV
114         assertEquals( "<div class=\"sect&#10;ion\"/>", w.toString() );
115 
116         // Mac
117         initWriter();
118         writer.startElement( Tag.DIV.toString() );
119         writer.addAttribute( "class", "sect\rion" );
120         writer.endElement(); // Tag.DIV
121         assertEquals( "<div class=\"sect&#13;ion\"/>", w.toString() );
122 
123         // Unix
124         initWriter();
125         writer.startElement( Tag.DIV.toString() );
126         writer.addAttribute( "class", "sect\nion" );
127         writer.endElement(); // Tag.DIV
128         assertEquals( "<div class=\"sect&#10;ion\"/>", w.toString() );
129     }
130 
131     private void writeXhtmlHead( XMLWriter writer )
132     {
133         writer.startElement( Tag.HEAD.toString() );
134         writer.startElement( Tag.TITLE.toString() );
135         writer.writeText( "title" );
136         writer.endElement(); // Tag.TITLE
137         writer.startElement( Tag.META.toString() );
138         writer.addAttribute( "name", "author" );
139         writer.addAttribute( "content", "Author" );
140         writer.endElement(); // Tag.META
141         writer.startElement( Tag.META.toString() );
142         writer.addAttribute( "name", "date" );
143         writer.addAttribute( "content", "Date" );
144         writer.endElement(); // Tag.META
145         writer.endElement(); // Tag.HEAD
146     }
147 
148     private void writeXhtmlBody( XMLWriter writer )
149     {
150         writer.startElement( Tag.BODY.toString() );
151         writer.startElement( Tag.P.toString() );
152         writer.writeText( "Paragraph 1, line 1. Paragraph 1, line 2." );
153         writer.endElement(); // Tag.P
154         writer.startElement( Tag.DIV.toString() );
155         writer.addAttribute( "class", "section" );
156         writer.startElement( Tag.H2.toString() );
157         writer.writeText( "Section title" );
158         writer.endElement(); // Tag.H2
159         writer.endElement(); // Tag.DIV
160         writer.endElement(); // Tag.BODY
161     }
162 
163     private String expectedResult( String lineSeparator )
164     {
165         return expectedResult( "  ", lineSeparator );
166     }
167 
168     private String expectedResult( String lineIndenter, String lineSeparator )
169     {
170         StringBuilder expected = new StringBuilder();
171 
172         expected.append( "<html>" ).append( lineSeparator );
173         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<head>" ).append( lineSeparator );
174         expected.append( StringUtils.repeat( lineIndenter,
175                                              2 ) ).append( "<title>title</title>" ).append( lineSeparator );
176         expected.append( StringUtils.repeat( lineIndenter,
177                                              2 ) ).append( "<meta name=\"author\" content=\"Author\"/>" ).append( lineSeparator );
178         expected.append( StringUtils.repeat( lineIndenter,
179                                              2 ) ).append( "<meta name=\"date\" content=\"Date\"/>" ).append( lineSeparator );
180         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</head>" ).append( lineSeparator );
181         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<body>" ).append( lineSeparator );
182         expected.append( StringUtils.repeat( lineIndenter,
183                                              2 ) ).append( "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" ).append( lineSeparator );
184         expected.append( StringUtils.repeat( lineIndenter,
185                                              2 ) ).append( "<div class=\"section\">" ).append( lineSeparator );
186         expected.append( StringUtils.repeat( lineIndenter,
187                                              3 ) ).append( "<h2>Section title</h2>" ).append( lineSeparator );
188         expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "</div>" ).append( lineSeparator );
189         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</body>" ).append( lineSeparator );
190         expected.append( "</html>" );
191 
192         return expected.toString();
193     }
194 }