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