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 testEscapeXmlAttributeUnix()
130     {
131         // Unix
132         writer.startElement( HTML.Tag.DIV.toString() );
133         writer.addAttribute( "class", "sect\nion" );
134         writer.endElement(); // Tag.DIV
135         Assert.assertEquals( "<div class=\"sect&#10;ion\"/>", w.toString() );
136     }
137 
138     private void writeXhtmlHead( XMLWriter writer )
139     {
140         writer.startElement( HTML.Tag.HEAD.toString() );
141         writer.startElement( HTML.Tag.TITLE.toString() );
142         writer.writeText( "title" );
143         writer.endElement(); // Tag.TITLE
144         writer.startElement( HTML.Tag.META.toString() );
145         writer.addAttribute( "name", "author" );
146         writer.addAttribute( "content", "Author" );
147         writer.endElement(); // Tag.META
148         writer.startElement( HTML.Tag.META.toString() );
149         writer.addAttribute( "name", "date" );
150         writer.addAttribute( "content", "Date" );
151         writer.endElement(); // Tag.META
152         writer.endElement(); // Tag.HEAD
153     }
154 
155     private void writeXhtmlBody( XMLWriter writer )
156     {
157         writer.startElement( HTML.Tag.BODY.toString() );
158         writer.startElement( HTML.Tag.P.toString() );
159         writer.writeText( "Paragraph 1, line 1. Paragraph 1, line 2." );
160         writer.endElement(); // Tag.P
161         writer.startElement( HTML.Tag.DIV.toString() );
162         writer.addAttribute( "class", "section" );
163         writer.startElement( HTML.Tag.H2.toString() );
164         writer.writeText( "Section title" );
165         writer.endElement(); // Tag.H2
166         writer.endElement(); // Tag.DIV
167         writer.endElement(); // Tag.BODY
168     }
169 
170     private String expectedResult( String lineSeparator )
171     {
172         return expectedResult( "  ", lineSeparator );
173     }
174 
175     private String expectedResult( String lineIndenter, String lineSeparator )
176     {
177         StringBuffer expected = new StringBuffer();
178 
179         expected.append( "<html>" ).append( lineSeparator );
180         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<head>" ).append( lineSeparator );
181         expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<title>title</title>" )
182                 .append( lineSeparator );
183         expected.append( StringUtils.repeat( lineIndenter, 2 ) )
184                 .append( "<meta name=\"author\" content=\"Author\"/>" ).append( lineSeparator );
185         expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<meta name=\"date\" content=\"Date\"/>" )
186                 .append( lineSeparator );
187         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</head>" ).append( lineSeparator );
188         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "<body>" ).append( lineSeparator );
189         expected.append( StringUtils.repeat( lineIndenter, 2 ) )
190                 .append( "<p>Paragraph 1, line 1. Paragraph 1, line 2.</p>" ).append( lineSeparator );
191         expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "<div class=\"section\">" )
192                 .append( lineSeparator );
193         expected.append( StringUtils.repeat( lineIndenter, 3 ) ).append( "<h2>Section title</h2>" )
194                 .append( lineSeparator );
195         expected.append( StringUtils.repeat( lineIndenter, 2 ) ).append( "</div>" ).append( lineSeparator );
196         expected.append( StringUtils.repeat( lineIndenter, 1 ) ).append( "</body>" ).append( lineSeparator );
197         expected.append( "</html>" );
198 
199         return expected.toString();
200     }
201 }