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 java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.io.Writer;
25  
26  import junit.framework.TestCase;
27  import org.apache.maven.shared.utils.StringUtils;
28  import org.apache.maven.shared.utils.WriterFactory;
29  
30  /**
31   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
32   *
33   */
34  public class XmlWriterUtilTest extends TestCase {
35      private OutputStream output;
36  
37      private Writer writer;
38  
39      private XMLWriter xmlWriter;
40  
41      /** {@inheritDoc} */
42      protected void setUp() throws Exception {
43          super.setUp();
44  
45          output = new ByteArrayOutputStream();
46          writer = WriterFactory.newXmlWriter(output);
47          xmlWriter = new PrettyPrintXMLWriter(writer);
48      }
49  
50      /**
51       * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter)}.
52       *
53       * @throws Exception if any
54       */
55      public void testWriteLineBreakXMLWriter() throws Exception {
56          XmlWriterUtil.writeLineBreak(xmlWriter);
57          writer.close();
58          assertEquals(1, StringUtils.countMatches(output.toString(), "\r\n"));
59      }
60  
61      /**
62       * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int)}.
63       *
64       * @throws Exception if any
65       */
66      public void testWriteLineBreakXMLWriterInt() throws Exception {
67          XmlWriterUtil.writeLineBreak(xmlWriter, 10);
68          writer.close();
69          assertEquals(10, StringUtils.countMatches(output.toString(), "\r\n"));
70      }
71  
72      /**
73       * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int, int)}.
74       *
75       * @throws Exception if any
76       */
77      public void testWriteLineBreakXMLWriterIntInt() throws Exception {
78          XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2);
79          writer.close();
80          assertEquals(10, StringUtils.countMatches(output.toString(), "\r\n"));
81          assertEquals(
82                  1,
83                  StringUtils.countMatches(
84                          output.toString(), StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE)));
85      }
86  
87      /**
88       * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeLineBreak(XMLWriter, int, int, int)}.
89       *
90       * @throws Exception if any
91       */
92      public void testWriteLineBreakXMLWriterIntIntInt() throws Exception {
93          XmlWriterUtil.writeLineBreak(xmlWriter, 10, 2, 4);
94          writer.close();
95          assertEquals(10, StringUtils.countMatches(output.toString(), "\r\n"));
96          assertEquals(1, StringUtils.countMatches(output.toString(), StringUtils.repeat(" ", 2 * 4)));
97      }
98  
99      /**
100      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentLineBreak(XMLWriter)}.
101      *
102      * @throws Exception if any
103      */
104     public void testWriteCommentLineBreakXMLWriter() throws Exception {
105         XmlWriterUtil.writeCommentLineBreak(xmlWriter);
106         writer.close();
107         StringBuilder sb = new StringBuilder();
108         sb.append("<!-- ====================================================================== -->")
109                 .append("\r\n");
110         assertEquals(output.toString(), sb.toString());
111         assertEquals(output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length());
112     }
113 
114     /**
115      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentLineBreak(XMLWriter, int)}.
116      *
117      * @throws Exception if any
118      */
119     public void testWriteCommentLineBreakXMLWriterInt() throws Exception {
120         XmlWriterUtil.writeCommentLineBreak(xmlWriter, 20);
121         writer.close();
122         assertEquals(output.toString(), "<!-- ========== -->" + "\r\n");
123     }
124 
125     public void testWriteCommentLineBreak() throws IOException {
126         XmlWriterUtil.writeCommentLineBreak(xmlWriter, 10);
127         writer.close();
128         assertEquals(output.toString(), output.toString(), "<!--  -->" + "\r\n");
129     }
130 
131     /**
132      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
133      *
134      * @throws Exception if any
135      */
136     public void testWriteCommentXMLWriterString() throws Exception {
137         XmlWriterUtil.writeComment(xmlWriter, "hello");
138         writer.close();
139         StringBuffer sb = new StringBuffer();
140         sb.append("<!-- hello                                                                  -->")
141                 .append("\r\n");
142         assertEquals(output.toString(), sb.toString());
143         assertEquals(output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length());
144     }
145 
146     public void testWriteComment() throws IOException {
147         XmlWriterUtil.writeComment(
148                 xmlWriter, "hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
149         writer.close();
150         StringBuffer sb = new StringBuffer();
151         sb.append("<!-- hellooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo -->")
152                 .append("\r\n");
153         assertEquals(output.toString(), sb.toString());
154         assertTrue(output.toString().length() >= XmlWriterUtil.DEFAULT_COLUMN_LINE);
155     }
156 
157     public void testWriteComment_2() throws IOException {
158         XmlWriterUtil.writeComment(xmlWriter, "hello\nworld");
159         writer.close();
160         StringBuffer sb = new StringBuffer();
161         sb.append("<!-- hello                                                                  -->")
162                 .append("\r\n");
163         sb.append("<!-- world                                                                  -->")
164                 .append("\r\n");
165         assertEquals(output.toString(), sb.toString());
166         assertEquals(output.toString().length(), 2 * (XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length()));
167     }
168 
169     /**
170      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int)}.
171      *
172      * @throws Exception if any
173      */
174     public void testWriteCommentXMLWriterStringInt() throws Exception {
175         String indent = StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE);
176 
177         XmlWriterUtil.writeComment(xmlWriter, "hello", 2);
178         writer.close();
179         StringBuffer sb = new StringBuffer();
180         sb.append(indent);
181         sb.append("<!-- hello                                                                  -->")
182                 .append("\r\n");
183         assertEquals(output.toString(), sb.toString());
184         assertEquals(
185                 output.toString().length(),
186                 XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() + 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE);
187     }
188 
189     public void testWriteComment_3() throws IOException {
190         String indent = StringUtils.repeat(" ", 2 * XmlWriterUtil.DEFAULT_INDENTATION_SIZE);
191         XmlWriterUtil.writeComment(xmlWriter, "hello\nworld", 2);
192         writer.close();
193         StringBuffer sb = new StringBuffer();
194         sb.append(indent);
195         sb.append("<!-- hello                                                                  -->")
196                 .append("\r\n");
197         sb.append(indent);
198         sb.append("<!-- world                                                                  -->")
199                 .append("\r\n");
200         assertEquals(output.toString(), sb.toString());
201         assertEquals(
202                 output.toString().length(),
203                 2 * (XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length()) + 2 * indent.length());
204     }
205 
206     /**
207      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int, int)}.
208      *
209      * @throws Exception if any
210      */
211     public void testWriteCommentXMLWriterStringIntInt() throws Exception {
212         String repeat = StringUtils.repeat(" ", 2 * 4);
213 
214         XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4);
215         writer.close();
216         StringBuffer sb = new StringBuffer();
217         sb.append(repeat);
218         sb.append("<!-- hello                                                                  -->")
219                 .append("\r\n");
220         assertEquals(output.toString(), sb.toString());
221         assertEquals(output.toString().length(), XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length() + 2 * 4);
222     }
223 
224     public void testWriteCommentXMLWriterStringIntInt_2() throws IOException {
225         String repeat = StringUtils.repeat(" ", 2 * 4);
226         XmlWriterUtil.writeComment(xmlWriter, "hello\nworld", 2, 4);
227         writer.close();
228         StringBuffer sb = new StringBuffer();
229         sb.append(repeat);
230         sb.append("<!-- hello                                                                  -->")
231                 .append("\r\n");
232         sb.append(repeat);
233         sb.append("<!-- world                                                                  -->")
234                 .append("\r\n");
235         assertEquals(output.toString(), sb.toString());
236         assertTrue(output.toString().length()
237                 == 2 * (XmlWriterUtil.DEFAULT_COLUMN_LINE - 1 + "\r\n".length()) + 2 * repeat.length());
238     }
239 
240     /**
241      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String, int, int, int)}.
242      *
243      * @throws Exception if any
244      */
245     public void testWriteCommentXMLWriterStringIntIntInt() throws Exception {
246         String indent = StringUtils.repeat(" ", 2 * 4);
247 
248         XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4, 50);
249         writer.close();
250         StringBuffer sb = new StringBuffer();
251         sb.append(indent);
252         sb.append("<!-- hello                                    -->").append("\r\n");
253         assertEquals(output.toString(), sb.toString());
254         assertTrue(output.toString().length() == 50 - 1 + "\r\n".length() + 2 * 4);
255     }
256 
257     public void testWriteCommentXMLWriterStringIntIntInt_2() throws IOException {
258         String indent = StringUtils.repeat(" ", 2 * 4);
259         XmlWriterUtil.writeComment(xmlWriter, "hello", 2, 4, 10);
260         writer.close();
261         StringBuffer sb = new StringBuffer();
262         sb.append(indent);
263         sb.append("<!-- hello -->").append("\r\n");
264         assertEquals(output.toString(), sb.toString());
265         assertTrue(output.toString().length() >= 10 + 2 * 4);
266     }
267 
268     /**
269      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int)}.
270      *
271      * @throws Exception if any
272      */
273     public void testWriteCommentTextXMLWriterStringInt() throws Exception {
274         XmlWriterUtil.writeCommentText(xmlWriter, "hello", 0);
275         writer.close();
276         StringBuffer sb = new StringBuffer();
277         sb.append("\r\n");
278         sb.append("<!-- ====================================================================== -->")
279                 .append("\r\n");
280         sb.append("<!-- hello                                                                  -->")
281                 .append("\r\n");
282         sb.append("<!-- ====================================================================== -->")
283                 .append("\r\n");
284         sb.append("\r\n");
285         assertEquals(output.toString(), sb.toString());
286         assertEquals(output.toString().length(), 3 * (80 - 1 + "\r\n".length()) + 2 * "\r\n".length());
287     }
288 
289     public void testWriteCommentTextXMLWriterStringInt_2() throws IOException {
290         String indent = StringUtils.repeat(" ", 2 * 2);
291 
292         XmlWriterUtil.writeCommentText(
293                 xmlWriter,
294                 "hello world with end of line\n and "
295                         + "loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong line",
296                 2);
297         writer.close();
298         StringBuffer sb = new StringBuffer();
299         sb.append("\r\n");
300         sb.append(indent)
301                 .append("<!-- ====================================================================== -->")
302                 .append("\r\n");
303         sb.append(indent)
304                 .append("<!-- hello world with end of line                                           -->")
305                 .append("\r\n");
306         sb.append(indent)
307                 .append("<!-- and                                                                    -->")
308                 .append("\r\n");
309         sb.append(indent)
310                 .append("<!-- loooooooooooooooooooooooooooooooooooooooooooooooooooooonnnnnnnnnnong   -->")
311                 .append("\r\n");
312         sb.append(indent)
313                 .append("<!-- line                                                                   -->")
314                 .append("\r\n");
315         sb.append(indent)
316                 .append("<!-- ====================================================================== -->")
317                 .append("\r\n");
318         sb.append("\r\n");
319         sb.append(indent);
320         assertEquals(output.toString(), sb.toString());
321     }
322 
323     /**
324      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int, int)}.
325      *
326      * @throws Exception if any
327      */
328     public void testWriteCommentTextXMLWriterStringIntInt() throws Exception {
329         String indent = StringUtils.repeat(" ", 2 * 4);
330 
331         XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4);
332         writer.close();
333         StringBuilder sb = new StringBuilder();
334         sb.append("\r\n");
335         sb.append(indent)
336                 .append("<!-- ====================================================================== -->")
337                 .append("\r\n");
338         sb.append(indent)
339                 .append("<!-- hello                                                                  -->")
340                 .append("\r\n");
341         sb.append(indent)
342                 .append("<!-- ====================================================================== -->")
343                 .append("\r\n");
344         sb.append("\r\n");
345         sb.append(indent);
346         assertEquals(output.toString(), sb.toString());
347         assertEquals(output.toString().length(), 3 * (80 - 1 + "\r\n".length()) + 4 * 2 * 4 + 2 * "\r\n".length());
348     }
349 
350     /**
351      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeCommentText(XMLWriter, java.lang.String, int, int, int)}.
352      *
353      * @throws Exception if any
354      */
355     public void testWriteCommentTextXMLWriterStringIntIntInt() throws Exception {
356         String indent = StringUtils.repeat(" ", 2 * 4);
357 
358         XmlWriterUtil.writeCommentText(xmlWriter, "hello", 2, 4, 50);
359         writer.close();
360         StringBuilder sb = new StringBuilder();
361         sb.append("\r\n");
362         sb.append(indent)
363                 .append("<!-- ======================================== -->")
364                 .append("\r\n");
365         sb.append(indent)
366                 .append("<!-- hello                                    -->")
367                 .append("\r\n");
368         sb.append(indent)
369                 .append("<!-- ======================================== -->")
370                 .append("\r\n");
371         sb.append("\r\n");
372         sb.append(indent);
373         assertEquals(output.toString(), sb.toString());
374         assertEquals(output.toString().length(), 3 * (50 - 1 + "\r\n".length()) + 4 * 2 * 4 + 2 * "\r\n".length());
375     }
376 
377     /**
378      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
379      *
380      * @throws Exception if any
381      */
382     public void testWriteCommentNull() throws Exception {
383         XmlWriterUtil.writeComment(xmlWriter, null);
384         writer.close();
385         StringBuilder sb = new StringBuilder();
386         sb.append("<!-- null                                                                   -->")
387                 .append("\r\n");
388         assertEquals(output.toString(), sb.toString());
389     }
390 
391     /**
392      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
393      *
394      * @throws Exception if any
395      */
396     public void testWriteCommentShort() throws Exception {
397         XmlWriterUtil.writeComment(xmlWriter, "This is a short text");
398         writer.close();
399         StringBuilder sb = new StringBuilder();
400         sb.append("<!-- This is a short text                                                   -->")
401                 .append("\r\n");
402         assertEquals(output.toString(), sb.toString());
403     }
404 
405     /**
406      * Test method for {@link org.apache.maven.shared.utils.xml.XmlWriterUtil#writeComment(XMLWriter, java.lang.String)}.
407      *
408      * @throws Exception if any
409      */
410     public void testWriteCommentLong() throws Exception {
411         XmlWriterUtil.writeComment(
412                 xmlWriter,
413                 "Maven is a software project management and comprehension tool. "
414                         + "Based on the concept of a project object model (POM), Maven can manage a project's build, reporting "
415                         + "and documentation from a central piece of information.");
416         writer.close();
417         StringBuilder sb = new StringBuilder();
418         sb.append("<!-- Maven is a software project management and comprehension tool. Based   -->")
419                 .append("\r\n");
420         sb.append("<!-- on the concept of a project object model (POM), Maven can manage a     -->")
421                 .append("\r\n");
422         sb.append("<!-- project's build, reporting and documentation from a central piece of   -->")
423                 .append("\r\n");
424         sb.append("<!-- information.                                                           -->")
425                 .append("\r\n");
426         assertEquals(output.toString(), sb.toString());
427     }
428 }