View Javadoc
1   package org.codehaus.plexus.util.xml.pull;
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.IOException;
22  import java.io.StringReader;
23  
24  /**
25   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
26   * @version $Id$
27   */
28  public class MXParserTest
29      extends TestCase
30  {
31      public void testHexadecimalEntities()
32          throws Exception
33      {
34          MXParser parser = new MXParser();
35  
36          parser.defineEntityReplacementText( "test", "replacement" );
37  
38          String input = "<root>&#x41;</root>";
39  
40          parser.setInput( new StringReader( input ) );
41  
42          assertEquals( XmlPullParser.START_TAG, parser.next() );
43  
44          assertEquals( XmlPullParser.TEXT, parser.next() );
45  
46          assertEquals( "A", parser.getText() );
47  
48          assertEquals( XmlPullParser.END_TAG, parser.next() );
49      }
50  
51      public void testDecimalEntities()
52          throws Exception
53      {
54          MXParser parser = new MXParser();
55  
56          parser.defineEntityReplacementText( "test", "replacement" );
57  
58          String input = "<root>&#65;</root>";
59  
60          parser.setInput( new StringReader( input ) );
61  
62          assertEquals( XmlPullParser.START_TAG, parser.next() );
63  
64          assertEquals( XmlPullParser.TEXT, parser.next() );
65  
66          assertEquals( "A", parser.getText() );
67  
68          assertEquals( XmlPullParser.END_TAG, parser.next() );
69      }
70  
71      public void testPredefinedEntities()
72          throws Exception
73      {
74          MXParser parser = new MXParser();
75  
76          parser.defineEntityReplacementText( "test", "replacement" );
77  
78          String input = "<root>&lt;&gt;&amp;&apos;&quot;</root>";
79  
80          parser.setInput( new StringReader( input ) );
81  
82          assertEquals( XmlPullParser.START_TAG, parser.next() );
83  
84          assertEquals( XmlPullParser.TEXT, parser.next() );
85  
86          assertEquals( "<>&'\"", parser.getText() );
87  
88          assertEquals( XmlPullParser.END_TAG, parser.next() );
89      }
90  
91      public void testEntityReplacementMap()
92          throws XmlPullParserException, IOException
93      {
94          EntityReplacementMap erm = new EntityReplacementMap( new String[][] { { "abc", "CDE" }, { "EFG", "HIJ" } } );
95          MXParser parser = new MXParser( erm );
96  
97          String input = "<root>&EFG;</root>";
98          parser.setInput( new StringReader( input ) );
99  
100         assertEquals( XmlPullParser.START_TAG, parser.next() );
101         assertEquals( XmlPullParser.TEXT, parser.next() );
102         assertEquals( "HIJ", parser.getText() );
103         assertEquals( XmlPullParser.END_TAG, parser.next() );
104     }
105 
106     public void testCustomEntities()
107         throws Exception
108     {
109         MXParser parser = new MXParser();
110 
111         String input = "<root>&myentity;</root>";
112         parser.setInput( new StringReader( input ) );
113         parser.defineEntityReplacementText( "myentity", "replacement" );
114         assertEquals( XmlPullParser.START_TAG, parser.next() );
115         assertEquals( XmlPullParser.TEXT, parser.next() );
116         assertEquals( "replacement", parser.getText() );
117         assertEquals( XmlPullParser.END_TAG, parser.next() );
118 
119         parser = new MXParser();
120         input = "<root>&myCustom;</root>";
121         parser.setInput( new StringReader( input ) );
122         parser.defineEntityReplacementText( "fo", "&#65;" );
123         parser.defineEntityReplacementText( "myCustom", "&fo;" );
124         assertEquals( XmlPullParser.START_TAG, parser.next() );
125         assertEquals( XmlPullParser.TEXT, parser.next() );
126         assertEquals( "&#65;", parser.getText() );
127         assertEquals( XmlPullParser.END_TAG, parser.next() );
128     }
129 
130     public void testUnicodeEntities()
131         throws Exception
132     {
133         MXParser parser = new MXParser();
134         String input = "<root>&#x1d7ed;</root>";
135         parser.setInput( new StringReader( input ) );
136 
137         assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
138         assertEquals( XmlPullParser.ENTITY_REF, parser.nextToken() );
139         assertEquals( "\uD835\uDFED", parser.getText() );
140         assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
141 
142         parser = new MXParser();
143         input = "<root>&#x159;</root>";
144         parser.setInput( new StringReader( input ) );
145 
146         assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
147         assertEquals( XmlPullParser.ENTITY_REF, parser.nextToken() );
148         assertEquals( "\u0159", parser.getText() );
149         assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
150     }
151 
152     public void testProcessingInstruction()
153         throws Exception
154     {
155         String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>nnn</test>";
156 
157         MXParser parser = new MXParser();
158         parser.setInput( new StringReader( input ) );
159 
160         assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
161         assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
162         assertEquals( XmlPullParser.TEXT, parser.nextToken() );
163         assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
164     }
165 
166     public void testSubsequentProcessingInstructionShort()
167         throws Exception
168     {
169         StringBuffer sb = new StringBuffer();
170 
171         sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
172         sb.append( "<project>" );
173         sb.append( "<!-- comment -->" );
174         sb.append( "<?m2e ignore?>" );
175         sb.append( "</project>" );
176 
177         MXParser parser = new MXParser();
178         parser.setInput( new StringReader( sb.toString() ) );
179 
180         assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
181         assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
182         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
183         assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
184         assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
185     }
186 
187     public void testSubsequentProcessingInstructionMoreThan8k()
188         throws Exception
189     {
190         StringBuffer sb = new StringBuffer();
191 
192         sb.append( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" );
193         sb.append( "<project>" );
194 
195         // add ten times 1000 chars as comment
196         for ( int j = 0; j < 10; j++ )
197         {
198 
199             sb.append( "<!-- " );
200             for ( int i = 0; i < 2000; i++ )
201             {
202                 sb.append( "ten bytes " );
203             }
204             sb.append( " -->" );
205         }
206 
207         sb.append( "<?m2e ignore?>" );
208         sb.append( "</project>" );
209 
210         MXParser parser = new MXParser();
211         parser.setInput( new StringReader( sb.toString() ) );
212 
213         assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
214         assertEquals( XmlPullParser.START_TAG, parser.nextToken() );
215         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
216         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
217         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
218         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
219         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
220         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
221         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
222         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
223         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
224         assertEquals( XmlPullParser.COMMENT, parser.nextToken() );
225         assertEquals( XmlPullParser.PROCESSING_INSTRUCTION, parser.nextToken() );
226         assertEquals( XmlPullParser.END_TAG, parser.nextToken() );
227     }
228 }