1 /* 2 * Copyright (C) 2017 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; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.platform.test.annotations.Presubmit; 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 /** 30 * Tests for {@link KeyValueListParser}. 31 */ 32 @RunWith(AndroidJUnit4.class) 33 @SmallTest 34 @Presubmit 35 public class KeyValueListParserTest { 36 private static final String TAG = "KeyValueListParserTest"; 37 private static final int[] DEFAULT = {1, 2, 3, 4}; 38 39 private KeyValueListParser mParser; 40 41 @Before 42 public void setUp() { 43 mParser = new KeyValueListParser(','); 44 } 45 46 @Test 47 public void testParseIntArrayNullInput() throws Exception { 48 mParser.setString(null); 49 int[] result = mParser.getIntArray("test", DEFAULT); 50 assertEquals(DEFAULT, result); 51 } 52 53 @Test 54 public void testParseIntArrayEmptyInput() throws Exception { 55 mParser.setString("test="); 56 int[] result = mParser.getIntArray("test", DEFAULT); 57 assertEquals(DEFAULT, result); 58 } 59 60 @Test 61 public void testParseIntArrayNullKey() throws Exception { 62 mParser.setString("foo=bar,test=100:200,baz=123"); 63 int[] result = mParser.getIntArray(null, DEFAULT); 64 assertEquals(DEFAULT, result); 65 } 66 67 @Test 68 public void testParseIntArrayComplexInput() throws Exception { 69 mParser.setString("foo=bar,test=100:200,baz=123"); 70 int[] result = mParser.getIntArray("test", DEFAULT); 71 assertEquals(2, result.length); 72 assertEquals(100, result[0]); // respect order 73 assertEquals(200, result[1]); 74 } 75 76 @Test 77 public void testParseIntArrayLeadingSep() throws Exception { 78 mParser.setString("test=:4:5:6"); 79 int[] result = mParser.getIntArray("test", DEFAULT); 80 assertEquals(DEFAULT, result); 81 } 82 83 @Test 84 public void testParseIntArrayEmptyItem() throws Exception { 85 mParser.setString("test=:4::6"); 86 int[] result = mParser.getIntArray("test", DEFAULT); 87 assertEquals(DEFAULT, result); 88 } 89 90 @Test 91 public void testParseIntArrayTrailingSep() throws Exception { 92 mParser.setString("test=4:5:6:"); 93 int[] result = mParser.getIntArray("test", DEFAULT); 94 assertEquals(3, result.length); 95 assertEquals(4, result[0]); // respect order 96 assertEquals(5, result[1]); 97 assertEquals(6, result[2]); 98 } 99 100 @Test 101 public void testParseIntArrayGoodData() throws Exception { 102 mParser.setString("test=4:5:6"); 103 int[] result = mParser.getIntArray("test", DEFAULT); 104 assertEquals(3, result.length); 105 assertEquals(4, result[0]); // respect order 106 assertEquals(5, result[1]); 107 assertEquals(6, result[2]); 108 } 109 } 110