View Javadoc

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