blob: 27f3596f239ba0cb5d112da5716435f735c6c4ee [file] [log] [blame]
Narayan Kamathf7482572013-12-18 15:19:17 +00001/**
2 * Copyright (C) 2013 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
17package com.android.internal.util;
18
Roshan Pius08a61aa2019-08-02 10:07:40 -070019import static org.junit.Assert.assertArrayEquals;
20
21import android.util.Xml;
22
Narayan Kamathf7482572013-12-18 15:19:17 +000023import java.io.ByteArrayInputStream;
24import java.io.ByteArrayOutputStream;
25import java.io.InputStream;
Roshan Pius08a61aa2019-08-02 10:07:40 -070026import java.nio.charset.StandardCharsets;
Narayan Kamathf7482572013-12-18 15:19:17 +000027import java.util.HashMap;
28import java.util.Map;
Roshan Pius08a61aa2019-08-02 10:07:40 -070029
Narayan Kamathf7482572013-12-18 15:19:17 +000030import junit.framework.TestCase;
31
Roshan Pius08a61aa2019-08-02 10:07:40 -070032import org.xmlpull.v1.XmlPullParser;
33import org.xmlpull.v1.XmlSerializer;
34
Narayan Kamathf7482572013-12-18 15:19:17 +000035public class XmlUtilsTest extends TestCase {
36
37 // https://code.google.com/p/android/issues/detail?id=63717
38 public void testMapWithNullKeys() throws Exception {
39 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
40 Map<String, Object> map = new HashMap<String, Object>();
41 map.put(null, "nullValue");
42 map.put("foo", "fooValue");
43 XmlUtils.writeMapXml(map, baos);
44
45 InputStream mapInput = new ByteArrayInputStream(baos.toByteArray());
46 HashMap<String, ?> deserialized = XmlUtils.readMapXml(mapInput);
47 assertEquals("nullValue", deserialized.get(null));
48 assertEquals("fooValue", deserialized.get("foo"));
49 }
Roshan Pius08a61aa2019-08-02 10:07:40 -070050
51 public void testreadWriteXmlByteArrayValue() throws Exception {
52 byte[] testByteArray = {0x1 , 0xa, 0xb, 0x9, 0x34, (byte) 0xaa, (byte) 0xba, (byte) 0x99};
53
54 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
55 XmlSerializer serializer = new FastXmlSerializer();
56 serializer.setOutput(baos, StandardCharsets.UTF_8.name());
57 serializer.startDocument(null, true);
58 XmlUtils.writeValueXml(testByteArray, "testByteArray", serializer);
59 serializer.endDocument();
60
61 InputStream bais = new ByteArrayInputStream(baos.toByteArray());
62 XmlPullParser pullParser = Xml.newPullParser();
63 pullParser.setInput(bais, StandardCharsets.UTF_8.name());
64 String[] name = new String[1];
65 byte[] testByteArrayDeserialized = (byte[]) XmlUtils.readValueXml(pullParser, name);
66 assertEquals("testByteArray", name[0]);
67 assertArrayEquals(testByteArray, testByteArrayDeserialized);
68 }
Narayan Kamathf7482572013-12-18 15:19:17 +000069}