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