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