1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.util.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.fail;
21 
22 import android.util.Xml;
23 
24 import androidx.test.filters.SmallTest;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.xml.sax.Attributes;
30 import org.xml.sax.ContentHandler;
31 import org.xml.sax.Locator;
32 import org.xml.sax.SAXException;
33 
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.util.Vector;
37 
38 /**
39  * TestCases for android.util.Xml.Encoding.
40  */
41 //FIXME: This is a duplicated testcase. Need to improve the coverage tool in future.
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public class XmlEncodingTest {
45 
46     private static final String STR_ISO_8859_1 = "ISO-8859-1";
47     private static final String STR_US_ASCII = "US-ASCII";
48     private static final String STR_UTF_16 = "UTF-16";
49     private static final String STR_UTF_8 = "UTF-8";
50     private static final String STR_END_DOCUMENT = "endDocument";
51     private static final String STR_1_NUMBER = "1";
52     private static final String STR_0_NUMBER = "0";
53     private static final String STR_EMPTY = "";
54     private static final String STR_END_ELEMENT = "endElement";
55     private static final String STR_ABC = "abc";
56     private static final String ATT_NAME = "name";
57     private static final String TAG_SON = "Son";
58     private static final String TAG_TEST = "Test";
59     private static final String STR_START_ELEMENT = "startElement:";
60     private static final String STR_START_DOCUMENT = "startDocument";
61     private static final String STR_SET_DOCUMENT_LOCATOR = "setDocumentLocator:Locator[publicId: "
62                + "null, systemId: null, line: 1, column: 0]";
63 
64     final String sourceStr = "<" + TAG_TEST + "><" + TAG_SON + " " + ATT_NAME + "=\"" + STR_ABC
65             + "\"/></" + TAG_TEST + ">";
66 
67     // used for ContentHandler to pause XML.
68     private static final String STR_START_PREFIX_MAPPING_TAG = "startPrefixMapping:";
69     private static final String STR_SKIPPED_ENTITY_TAG = "skippedEntity:";
70     private static final String STR_SET_DOCUMENT_LOCATOR_TAG = "setDocumentLocator:";
71     private static final String STR_PROCESSING_INSTRUCTION_TAG = "processingInstruction:";
72     private static final String STR_IGNORABLE_WHITESPACE_TAG = "ignorableWhitespace:";
73     private static final String STR_END_PREFIX_MAPPING_TAG = "endPrefixMapping:";
74     private static final String STR_LENGTH_TAG = "length:";
75     private static final String STR_START_TAG = "start:";
76     private static final String STR_CHARACTERS_TAG = "characters:";
77 
78     @Test
testValueOf()79     public void testValueOf() {
80         // test US-ASCII
81         DefaultContentHandler dc = new DefaultContentHandler();
82         try {
83             Xml.parse(new ByteArrayInputStream(sourceStr.getBytes(STR_US_ASCII)),
84                             Xml.Encoding.US_ASCII, dc);
85             assertEquals(STR_SET_DOCUMENT_LOCATOR, dc.mVec.elementAt(0));
86             assertEquals(STR_START_DOCUMENT, dc.mVec.elementAt(1));
87             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(2));
88             assertEquals(TAG_TEST, dc.mVec.elementAt(3));
89             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(4));
90             assertEquals(STR_0_NUMBER, dc.mVec.elementAt(5));
91             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(6));
92             assertEquals(TAG_SON, dc.mVec.elementAt(7));
93             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(8));
94             assertEquals(STR_1_NUMBER, dc.mVec.elementAt(9));
95             assertEquals(ATT_NAME, dc.mVec.elementAt(10));
96             assertEquals(STR_ABC, dc.mVec.elementAt(11));
97             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(12));
98             assertEquals(STR_EMPTY, dc.mVec.elementAt(13));
99             assertEquals(TAG_SON, dc.mVec.elementAt(14));
100             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(15));
101             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(16));
102             assertEquals(STR_EMPTY, dc.mVec.elementAt(17));
103             assertEquals(TAG_TEST, dc.mVec.elementAt(18));
104             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(19));
105             assertEquals(STR_END_DOCUMENT, dc.mVec.elementAt(20));
106         } catch (SAXException e) {
107             fail(e.getMessage());
108         } catch (IOException e) {
109             fail(e.getMessage());
110         }
111 
112         // test UTF-8
113         dc = new DefaultContentHandler();
114         try {
115             Xml.parse(new ByteArrayInputStream(sourceStr.getBytes(STR_UTF_8)),
116                             Xml.Encoding.UTF_8, dc);
117             assertEquals(STR_SET_DOCUMENT_LOCATOR, dc.mVec.elementAt(0));
118             assertEquals(STR_START_DOCUMENT, dc.mVec.elementAt(1));
119             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(2));
120             assertEquals(TAG_TEST, dc.mVec.elementAt(3));
121             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(4));
122             assertEquals(STR_0_NUMBER, dc.mVec.elementAt(5));
123             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(6));
124             assertEquals(TAG_SON, dc.mVec.elementAt(7));
125             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(8));
126             assertEquals(STR_1_NUMBER, dc.mVec.elementAt(9));
127             assertEquals(ATT_NAME, dc.mVec.elementAt(10));
128             assertEquals(STR_ABC, dc.mVec.elementAt(11));
129             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(12));
130             assertEquals(STR_EMPTY, dc.mVec.elementAt(13));
131             assertEquals(TAG_SON, dc.mVec.elementAt(14));
132             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(15));
133             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(16));
134             assertEquals(STR_EMPTY, dc.mVec.elementAt(17));
135             assertEquals(TAG_TEST, dc.mVec.elementAt(18));
136             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(19));
137             assertEquals(STR_END_DOCUMENT, dc.mVec.elementAt(20));
138         } catch (SAXException e) {
139             fail(e.getMessage());
140         } catch (IOException e) {
141             fail(e.getMessage());
142         }
143 
144         // test UTF-16
145         dc = new DefaultContentHandler();
146         try {
147             Xml.parse(new ByteArrayInputStream(sourceStr.getBytes(STR_UTF_16)),
148                             Xml.Encoding.UTF_16, dc);
149             assertEquals(STR_SET_DOCUMENT_LOCATOR, dc.mVec.elementAt(0));
150             assertEquals(STR_START_DOCUMENT, dc.mVec.elementAt(1));
151             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(2));
152             assertEquals(TAG_TEST, dc.mVec.elementAt(3));
153             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(4));
154             assertEquals(STR_0_NUMBER, dc.mVec.elementAt(5));
155             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(6));
156             assertEquals(TAG_SON, dc.mVec.elementAt(7));
157             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(8));
158             assertEquals(STR_1_NUMBER, dc.mVec.elementAt(9));
159             assertEquals(ATT_NAME, dc.mVec.elementAt(10));
160             assertEquals(STR_ABC, dc.mVec.elementAt(11));
161             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(12));
162             assertEquals(STR_EMPTY, dc.mVec.elementAt(13));
163             assertEquals(TAG_SON, dc.mVec.elementAt(14));
164             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(15));
165             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(16));
166             assertEquals(STR_EMPTY, dc.mVec.elementAt(17));
167             assertEquals(TAG_TEST, dc.mVec.elementAt(18));
168             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(19));
169             assertEquals(STR_END_DOCUMENT, dc.mVec.elementAt(20));
170         } catch (SAXException e) {
171             fail(e.getMessage());
172         } catch (IOException e) {
173             fail(e.getMessage());
174         }
175 
176         // test ISO-8859-1
177         dc = new DefaultContentHandler();
178         try {
179             Xml.parse(new ByteArrayInputStream(sourceStr.getBytes(STR_ISO_8859_1)),
180                             Xml.Encoding.ISO_8859_1, dc);
181             assertEquals(STR_SET_DOCUMENT_LOCATOR, dc.mVec.elementAt(0));
182             assertEquals(STR_START_DOCUMENT, dc.mVec.elementAt(1));
183             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(2));
184             assertEquals(TAG_TEST, dc.mVec.elementAt(3));
185             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(4));
186             assertEquals(STR_0_NUMBER, dc.mVec.elementAt(5));
187             assertEquals(STR_START_ELEMENT, dc.mVec.elementAt(6));
188             assertEquals(TAG_SON, dc.mVec.elementAt(7));
189             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(8));
190             assertEquals(STR_1_NUMBER, dc.mVec.elementAt(9));
191             assertEquals(ATT_NAME, dc.mVec.elementAt(10));
192             assertEquals(STR_ABC, dc.mVec.elementAt(11));
193             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(12));
194             assertEquals(STR_EMPTY, dc.mVec.elementAt(13));
195             assertEquals(TAG_SON, dc.mVec.elementAt(14));
196             assertEqualsOrEmpty(TAG_SON, dc.mVec.elementAt(15));
197             assertEquals(STR_END_ELEMENT, dc.mVec.elementAt(16));
198             assertEquals(STR_EMPTY, dc.mVec.elementAt(17));
199             assertEquals(TAG_TEST, dc.mVec.elementAt(18));
200             assertEqualsOrEmpty(TAG_TEST, dc.mVec.elementAt(19));
201             assertEquals(STR_END_DOCUMENT, dc.mVec.elementAt(20));
202         } catch (SAXException e) {
203             fail(e.getMessage());
204         } catch (IOException e) {
205             fail(e.getMessage());
206         }
207     }
208 
209     class DefaultContentHandler implements ContentHandler {
210 
211         public Vector<String> mVec = new Vector<>();
212 
characters(char[] ch, int start, int length)213         public void characters(char[] ch, int start, int length) throws SAXException {
214             mVec.add(STR_CHARACTERS_TAG + new String(ch));
215             mVec.add(STR_START_TAG + start);
216             mVec.add(STR_LENGTH_TAG + length);
217         }
218 
endDocument()219         public void endDocument() throws SAXException {
220             mVec.add(STR_END_DOCUMENT);
221         }
222 
endElement(String uri, String localName, String name)223         public void endElement(String uri, String localName, String name) throws SAXException {
224             mVec.add(STR_END_ELEMENT);
225             mVec.add(uri);
226             mVec.add(localName);
227             mVec.add(name);
228         }
229 
endPrefixMapping(String prefix)230         public void endPrefixMapping(String prefix) throws SAXException {
231             mVec.add(STR_END_PREFIX_MAPPING_TAG + prefix);
232         }
233 
ignorableWhitespace(char[] ch, int start, int length)234         public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
235             mVec.add(STR_IGNORABLE_WHITESPACE_TAG + new String(ch));
236             mVec.add(STR_START_TAG + start);
237             mVec.add(STR_LENGTH_TAG + length);
238         }
239 
processingInstruction(String target, String data)240         public void processingInstruction(String target, String data) throws SAXException {
241             mVec.add(STR_PROCESSING_INSTRUCTION_TAG + target);
242             mVec.add(data);
243         }
244 
setDocumentLocator(Locator locator)245         public void setDocumentLocator(Locator locator) {
246             mVec.add(STR_SET_DOCUMENT_LOCATOR_TAG + locator);
247         }
248 
skippedEntity(String name)249         public void skippedEntity(String name) throws SAXException {
250             mVec.add(STR_SKIPPED_ENTITY_TAG + name);
251         }
252 
startDocument()253         public void startDocument() throws SAXException {
254             mVec.add(STR_START_DOCUMENT);
255         }
256 
startElement(String uri, String localName, String name, Attributes atts)257         public void startElement(String uri, String localName, String name, Attributes atts)
258                 throws SAXException {
259             mVec.add(STR_START_ELEMENT + uri);
260             mVec.add(localName);
261             mVec.add(name);
262             mVec.add(atts.getLength() + STR_EMPTY);
263             for (int i = 0; i < atts.getLength(); i++) {
264                 mVec.add(atts.getLocalName(i));
265                 mVec.add(atts.getValue(i));
266             }
267         }
268 
startPrefixMapping(String prefix, String uri)269         public void startPrefixMapping(String prefix, String uri) throws SAXException {
270             mVec.add(STR_START_PREFIX_MAPPING_TAG + prefix);
271             mVec.add(uri);
272         }
273 
274     }
275 
assertEqualsOrEmpty(String expected, String actual)276     private void assertEqualsOrEmpty(String expected, String actual) {
277         if (actual.length() != 0) {
278             assertEquals(expected, actual);
279         }
280     }
281 }
282