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 org.apache.maven.shared.utils.StringUtils;
23  import org.apache.maven.shared.utils.xml.pull.XmlPullParserException;
24  
25  import org.junit.Test;
26  
27  import java.io.IOException;
28  import java.io.StringReader;
29  import java.io.StringWriter;
30  
31  import static org.junit.Assert.assertEquals;
32  import static org.junit.Assert.fail;
33  
34  
35  /**
36   * @author Kristian Rosenvold
37   */
38  public class Xpp3DomBuilderTest
39  {
40  
41      private static final String LS = System.getProperty( "line.separator" );
42  
43      private static final String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
44  
45          @Test
46      public void selfClosingTag()
47          throws Exception
48      {
49  
50          // Todo:  http://stackoverflow.com/questions/12968390/detecting-self-closing-tags-in-sax
51          String domString = selfClosingTagSource();
52  
53          Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ) );
54  
55          String expected = expectedSelfClosingTag();
56          String dom1Str = dom.toString();
57          assertEquals( "check DOMs match", expected, dom1Str );
58      }
59  
60      @Test
61      public void trimming()
62          throws Exception
63      {
64          String domString = createDomString();
65  
66          Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( domString ), true );
67  
68          assertEquals( "element1value", dom.getChild( "element1" ).getValue() );
69  
70          assertEquals( "  preserve space  ", dom.getChild( "element6" ).getValue() );
71  
72          dom = Xpp3DomBuilder.build( new StringReader( domString ), false );
73  
74          assertEquals( " element1value\n ", dom.getChild( "element1" ).getValue() );
75  
76          assertEquals( "  preserve space  ", dom.getChild( "element6" ).getValue() );
77      }
78  
79      @Test(expected = XmlPullParserException.class)
80      public void malformedXml()
81      {
82          Xpp3DomBuilder.build( new StringReader( "<newRoot>" + createDomString() ) );
83          fail( "We're supposed to fail" );
84      }
85  
86      @Test
87      public void attributeEscaping()
88          throws IOException, XmlPullParserException
89      {
90          String s = getAttributeEncodedString();
91          Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( s ) );
92  
93          assertEquals( "<foo>", dom.getChild( "el" ).getAttribute( "att" ) );
94          StringWriter w = new StringWriter();
95          Xpp3DomWriter.write( w, dom );
96          String newString = w.toString();
97          assertEquals( newString, s );
98      }
99  
100     @Test
101     public void contentEscaping()
102         throws IOException, XmlPullParserException
103     {
104         Xpp3Dom dom = Xpp3DomBuilder.build( new StringReader( getEncodedString() ) );
105 
106         assertEquals( "\"msg\"", dom.getChild( "a1" ).getValue() );
107         assertEquals( "<b>\"msg\"</b>", dom.getChild( "a2" ).getValue() );
108         assertEquals( "<b>\"msg\"</b>", dom.getChild( "a3" ).getValue() );
109 
110         StringWriter w = new StringWriter();
111         Xpp3DomWriter.write( w, dom );
112         assertEquals( getExpectedString(), w.toString() );
113     }
114 
115     private static String getAttributeEncodedString()
116     {
117         StringBuilder domString = new StringBuilder();
118         domString.append( "<root>" );
119         domString.append( LS );
120         domString.append( "  <el att=\"&lt;foo&gt;\">bar</el>" );
121         domString.append( LS );
122         domString.append( "</root>" );
123 
124         return domString.toString();
125     }
126 
127     private static String getEncodedString()
128     {
129         StringBuilder domString = new StringBuilder();
130         domString.append( "<root>\n" );
131         domString.append( "  <a1>\"msg\"</a1>\n" );
132         domString.append( "  <a2><![CDATA[<b>\"msg\"</b>]]></a2>\n" );
133         domString.append( "  <a3>&lt;b&gt;&quot;msg&quot;&lt;/b&gt;</a3>\n" );
134         domString.append( "</root>" );
135 
136         return domString.toString();
137     }
138 
139     private static String getExpectedString()
140     {
141         StringBuilder domString = new StringBuilder();
142         domString.append( "<root>" );
143         domString.append( LS );
144         domString.append( "  <a1>\"msg\"</a1>" );
145         domString.append( LS );
146         domString.append( "  <a2>&lt;b&gt;\"msg\"&lt;/b&gt;</a2>" );
147         domString.append( LS );
148         domString.append( "  <a3>&lt;b&gt;\"msg\"&lt;/b&gt;</a3>" );
149         domString.append( LS );
150         domString.append( "</root>" );
151         return domString.toString();
152     }
153 
154     private static String createDomString()
155     {
156         StringBuilder buf = new StringBuilder();
157         buf.append( "<root>\n" );
158         buf.append( " <element1> element1value\n </element1>\n" );
159         buf.append( " <element2 att2='attribute2&#10;nextline'>\n" );
160         buf.append( "  <element3 att3='attribute3'>element3value</element3>\n" );
161         buf.append( " </element2>\n" );
162         buf.append( " <element4></element4>\n" );
163         buf.append( " <element5/>\n" );
164         buf.append( " <element6 xml:space=\"preserve\">  preserve space  </element6>\n" );
165         buf.append( "</root>\n" );
166 
167         return buf.toString();
168     }
169 
170     private static String selfClosingTagSource()
171     {
172         StringBuilder buf = new StringBuilder();
173         buf.append( "<root>\n" );
174         buf.append( "  <el4></el4>\n" );
175         buf.append( "  <el5></el5>\n" );
176         buf.append( "</root>" );
177         return StringUtils.unifyLineSeparators( buf.toString() );
178     }
179 
180     private static String expectedSelfClosingTag()
181     {
182         return StringUtils.unifyLineSeparators( xmlDeclaration + selfClosingTagSource() );
183     }
184 
185 }