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 junit.framework.TestCase;
20  
21  import java.io.StringWriter;
22  
23  /**
24   * @author Edwin Punzalan
25   */
26  public class Xpp3DomWriterTest
27      extends TestCase
28  {
29      private static final String LS = System.getProperty( "line.separator" );
30  
31      public void testWriter()
32      {
33          StringWriter writer = new StringWriter();
34  
35          Xpp3DomWriter.write( writer, createXpp3Dom() );
36  
37          assertEquals( "Check if output matches", createExpectedXML( true ), writer.toString() );
38      }
39  
40      public void testWriterNoEscape()
41      {
42          StringWriter writer = new StringWriter();
43  
44          Xpp3DomWriter.write( new PrettyPrintXMLWriter( writer ), createXpp3Dom(), false );
45  
46          assertEquals( "Check if output matches", createExpectedXML( false ), writer.toString() );
47      }
48  
49      private String createExpectedXML( boolean escape )
50      {
51          StringBuilder buf = new StringBuilder();
52          buf.append( "<root>" );
53          buf.append( LS );
54          buf.append( "  <el1>element1</el1>" );
55          buf.append( LS );
56          buf.append( "  <el2 att2=\"attribute2&#10;nextline\">" );
57          buf.append( LS );
58          buf.append( "    <el3 att3=\"attribute3\">element3</el3>" );
59          buf.append( LS );
60          buf.append( "  </el2>" );
61          buf.append( LS );
62          buf.append( "  <el4></el4>" );
63          buf.append( LS );
64          buf.append( "  <el5/>" );
65          buf.append( LS );
66          buf.append( "  <el6 att6=\"attribute6&#10;&amp;&quot;&apos;&lt;&gt;\">" );
67          buf.append( LS );
68          if ( escape )
69          {
70              buf.append( "    <el7>element7" ).append( LS ).append( "&amp;&quot;&apos;&lt;&gt;</el7>" );
71          }
72          else
73          {
74              buf.append( "    <el7>element7" ).append( LS ).append( "&\"\'<></el7>" );
75          }
76          buf.append( LS );
77          buf.append( "  </el6>" );
78          buf.append( LS );
79          buf.append( "</root>" );
80  
81          return buf.toString();
82      }
83  
84      private Xpp3Dom createXpp3Dom()
85      {
86          Xpp3Dom dom = new Xpp3Dom( "root" );
87  
88          Xpp3Dom el1 = new Xpp3Dom( "el1" );
89          el1.setValue( "element1" );
90          dom.addChild( el1 );
91  
92          Xpp3Dom el2 = new Xpp3Dom( "el2" );
93          el2.setAttribute( "att2", "attribute2\nnextline" );
94          dom.addChild( el2 );
95  
96          Xpp3Dom el3 = new Xpp3Dom( "el3" );
97          el3.setAttribute( "att3", "attribute3" );
98          el3.setValue( "element3" );
99          el2.addChild( el3 );
100 
101         Xpp3Dom el4 = new Xpp3Dom( "el4" );
102         el4.setValue( "" );
103         dom.addChild( el4 );
104 
105         Xpp3Dom el5 = new Xpp3Dom( "el5" );
106         dom.addChild( el5 );
107 
108         // test escaping
109         Xpp3Dom el6 = new Xpp3Dom( "el6" );
110         el6.setAttribute( "att6", "attribute6\n&\"'<>" );
111         dom.addChild( el6 );
112 
113         Xpp3Dom el7 = new Xpp3Dom( "el7" );
114         el7.setValue( "element7\n&\"\'<>" );
115         el6.addChild( el7 );
116 
117         return dom;
118     }
119 }