Narayan Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 1 | /** |
| 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 | |
| 17 | package com.android.internal.util; |
| 18 | |
Roshan Pius | 08a61aa | 2019-08-02 10:07:40 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertArrayEquals; |
| 20 | |
| 21 | import android.util.Xml; |
| 22 | |
Narayan Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 23 | import java.io.ByteArrayInputStream; |
| 24 | import java.io.ByteArrayOutputStream; |
| 25 | import java.io.InputStream; |
Roshan Pius | 08a61aa | 2019-08-02 10:07:40 -0700 | [diff] [blame] | 26 | import java.nio.charset.StandardCharsets; |
Narayan Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
Roshan Pius | 08a61aa | 2019-08-02 10:07:40 -0700 | [diff] [blame] | 29 | |
Narayan Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 30 | import junit.framework.TestCase; |
| 31 | |
Roshan Pius | 08a61aa | 2019-08-02 10:07:40 -0700 | [diff] [blame] | 32 | import org.xmlpull.v1.XmlPullParser; |
| 33 | import org.xmlpull.v1.XmlSerializer; |
| 34 | |
Narayan Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 35 | public 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 Pius | 08a61aa | 2019-08-02 10:07:40 -0700 | [diff] [blame] | 50 | |
| 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 Kamath | f748257 | 2013-12-18 15:19:17 +0000 | [diff] [blame] | 69 | } |