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  import org.codehaus.plexus.util.xml.pull.MXParser;
21  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
22  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
23  
24  import java.io.IOException;
25  import java.io.StringReader;
26  import java.io.StringWriter;
27  
28  /**
29   * Test the Xpp3DomBuilder.
30   *
31   * @author <a href="mailto:brett@codehaus.org">Brett Porter</a>
32   * @version $Id$
33   */
34  public class Xpp3DomBuilderTest
35      extends TestCase
36  {
37  
38      private static final String LS = System.getProperty( "line.separator" );
39  
40      public void testBuildFromReader()
41          throws Exception
42      {
43          String domString = createDomString();
44  
45          Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ) );
46  
47          Xpp3Dom expectedDom = createExpectedDom();
48  
49          assertEquals( "check DOMs match", expectedDom, dom );
50      }
51  
52      public void testBuildTrimming()
53          throws Exception
54      {
55          String domString = createDomString();
56  
57          Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ), true );
58  
59          assertEquals( "test with trimming on", "element1", dom.getChild( "el1" ).getValue() );
60  
61          dom = Xpp3DomBuilder.build( new StringReader( domString ), false );
62  
63          assertEquals( "test with trimming off", " element1\n ", dom.getChild( "el1" ).getValue() );
64      }
65  
66      public void testBuildFromXpp3Dom()
67          throws Exception
68      {
69          Xpp3Dom expectedDom = createExpectedDom();
70          Xpp3Dom dom = null;
71  
72          XmlPullParser parser = new MXParser();
73  
74          String domString = "<newRoot><configuration>" + createDomString() + "</configuration></newRoot>";
75          parser.setInput( new StringReader( domString ) );
76  
77          int eventType = parser.getEventType();
78  
79          boolean configurationClosed = false;
80          boolean newRootClosed = false;
81          boolean rootClosed = false;
82  
83          while ( eventType != XmlPullParser.END_DOCUMENT )
84          {
85              if ( eventType == XmlPullParser.START_TAG )
86              {
87                  String rawName = parser.getName();
88  
89                  if ( "root".equals( rawName ) )
90                  {
91                      dom = Xpp3DomBuilder.build( parser );
92                  }
93              }
94              else if ( eventType == XmlPullParser.END_TAG )
95              {
96                  String rawName = parser.getName();
97  
98                  if ( "configuration".equals( rawName ) )
99                  {
100                     configurationClosed = true;
101                 }
102                 else if ( "newRoot".equals( rawName ) )
103                 {
104                     newRootClosed = true;
105                 }
106                 else if ( "root".equals( rawName ) )
107                 {
108                     rootClosed = true;
109                 }
110             }
111             eventType = parser.next();
112         }
113 
114         assertEquals( "Check DOM matches", expectedDom, dom );
115         assertFalse( "Check closing root was consumed", rootClosed );
116         assertTrue( "Check continued to parse configuration", configurationClosed );
117         assertTrue( "Check continued to parse newRoot", newRootClosed );
118     }
119 
120     /**
121      * Test we get an error from the parser, and don't hit the IllegalStateException.
122      */
123     public void testUnclosedXml()
124     {
125         String domString = "<newRoot>" + createDomString();
126         try
127         {
128             Xpp3DomBuilder.build( new StringReader( domString ) );
129         }
130         catch ( XmlPullParserException expected )
131         {
132             // correct
133             assertTrue( true );
134         }
135         catch ( IOException expected )
136         {
137             // this will do too
138             assertTrue( true );
139         }
140     }
141 
142     public void testEscapingInContent()
143         throws IOException, XmlPullParserException
144     {
145         Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( getEncodedString() ) );
146 
147         assertEquals( "Check content value", "\"text\"", dom.getChild( "el" ).getValue() );
148         assertEquals( "Check content value", "<b>\"text\"</b>", dom.getChild( "ela" ).getValue() );
149         assertEquals( "Check content value", "<b>\"text\"</b>", dom.getChild( "elb" ).getValue() );
150 
151         StringWriter w = new StringWriter();
152         Xpp3DomWriter.write( w, dom );
153         assertEquals( "Compare stringified DOMs", getExpectedString(), w.toString() );
154     }
155 
156     public void testEscapingInAttributes()
157         throws IOException, XmlPullParserException
158     {
159         String s = getAttributeEncodedString();
160         Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( s ) );
161 
162         assertEquals( "Check attribute value", "<foo>", dom.getChild( "el" ).getAttribute( "att" ) );
163 
164         StringWriter w = new StringWriter();
165         Xpp3DomWriter.write( w, dom );
166         String newString = w.toString();
167         assertEquals( "Compare stringified DOMs", newString, s );
168     }
169 
170     private static String getAttributeEncodedString()
171     {
172         StringBuilder domString = new StringBuilder();
173         domString.append( "<root>" );
174         domString.append( LS );
175         domString.append( "  <el att=\"&lt;foo&gt;\">bar</el>" );
176         domString.append( LS );
177         domString.append( "</root>" );
178 
179         return domString.toString();
180     }
181 
182     private static String getEncodedString()
183     {
184         StringBuilder domString = new StringBuilder();
185         domString.append( "<root>\n" );
186         domString.append( "  <el>\"text\"</el>\n" );
187         domString.append( "  <ela><![CDATA[<b>\"text\"</b>]]></ela>\n" );
188         domString.append( "  <elb>&lt;b&gt;&quot;text&quot;&lt;/b&gt;</elb>\n" );
189         domString.append( "</root>" );
190 
191         return domString.toString();
192     }
193 
194     private static String getExpectedString()
195     {
196         StringBuilder domString = new StringBuilder();
197         domString.append( "<root>" );
198         domString.append( LS );
199         domString.append( "  <el>&quot;text&quot;</el>" );
200         domString.append( LS );
201         domString.append( "  <ela>&lt;b&gt;&quot;text&quot;&lt;/b&gt;</ela>" );
202         domString.append( LS );
203         domString.append( "  <elb>&lt;b&gt;&quot;text&quot;&lt;/b&gt;</elb>" );
204         domString.append( LS );
205         domString.append( "</root>" );
206 
207         return domString.toString();
208     }
209 
210     //
211     // HELPER METHODS
212     //
213 
214     private static String createDomString()
215     {
216         StringBuilder buf = new StringBuilder();
217         buf.append( "<root>\n" );
218         buf.append( " <el1> element1\n </el1>\n" );
219         buf.append( " <el2 att2='attribute2&#10;nextline'>\n" );
220         buf.append( "  <el3 att3='attribute3'>element3</el3>\n" );
221         buf.append( " </el2>\n" );
222         buf.append( " <el4></el4>\n" );
223         buf.append( " <el5/>\n" );
224         buf.append( " <el6 xml:space=\"preserve\">  do not trim  </el6>\n" );
225         buf.append( "</root>\n" );
226 
227         return buf.toString();
228     }
229 
230     private static Xpp3Dom createExpectedDom()
231     {
232         Xpp3Dom expectedDom = new Xpp3Dom( "root" );
233         Xpp3Dom el1 = new Xpp3Dom( "el1" );
234         el1.setValue( "element1" );
235         expectedDom.addChild( el1 );
236         Xpp3Dom el2 = new Xpp3Dom( "el2" );
237         el2.setAttribute( "att2", "attribute2\nnextline" );
238         expectedDom.addChild( el2 );
239         Xpp3Dom el3 = new Xpp3Dom( "el3" );
240         el3.setAttribute( "att3", "attribute3" );
241         el3.setValue( "element3" );
242         el2.addChild( el3 );
243         Xpp3Dom el4 = new Xpp3Dom( "el4" );
244         el4.setValue( "" );
245         expectedDom.addChild( el4 );
246         Xpp3Dom el5 = new Xpp3Dom( "el5" );
247         expectedDom.addChild( el5 );
248         Xpp3Dom el6 = new Xpp3Dom( "el6" );
249         el6.setAttribute( "xml:space", "preserve" );
250         el6.setValue( "  do not trim  " );
251         expectedDom.addChild( el6 );
252         return expectedDom;
253     }
254 }