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