1 /*
2  * Copyright (C) 2014 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.inputmethod.latin.utils;
18 
19 import android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 import com.android.inputmethod.latin.common.CollectionUtils;
23 
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 
31 /**
32  * Tests for {@link CollectionUtils}.
33  */
34 @SmallTest
35 public class CollectionUtilsTests extends AndroidTestCase {
36     /**
37      * Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} fails as expected
38      * with some invalid inputs.
39      */
testArrayAsListFailure()40     public void testArrayAsListFailure() {
41         final String[] array = { "0", "1" };
42         // Negative start
43         try {
44             CollectionUtils.arrayAsList(array, -1, 1);
45             fail("Failed to catch start < 0");
46         } catch (final IllegalArgumentException e) {
47             assertEquals("Invalid start: -1 end: 1 with array.length: 2", e.getMessage());
48         }
49         // start > end
50         try {
51             CollectionUtils.arrayAsList(array, 1, -1);
52             fail("Failed to catch start > end");
53         } catch (final IllegalArgumentException e) {
54             assertEquals("Invalid start: 1 end: -1 with array.length: 2", e.getMessage());
55         }
56         // end > array.length
57         try {
58             CollectionUtils.arrayAsList(array, 1, 3);
59             fail("Failed to catch end > array.length");
60         } catch (final IllegalArgumentException e) {
61             assertEquals("Invalid start: 1 end: 3 with array.length: 2", e.getMessage());
62         }
63     }
64 
65     /**
66      * Tests that {@link CollectionUtils#arrayAsList(Object[],int,int)} gives the expected
67      * results for a few valid inputs.
68      */
testArrayAsList()69     public void testArrayAsList() {
70         final ArrayList<String> empty = new ArrayList<>();
71         assertEquals(empty, CollectionUtils.arrayAsList(new String[] {}, 0, 0));
72         final String[] array = { "0", "1", "2", "3", "4" };
73         assertEquals(empty, CollectionUtils.arrayAsList(array, 0, 0));
74         assertEquals(empty, CollectionUtils.arrayAsList(array, 1, 1));
75         assertEquals(empty, CollectionUtils.arrayAsList(array, array.length, array.length));
76         final ArrayList<String> expected123 = new ArrayList<>(Arrays.asList("1", "2", "3"));
77         assertEquals(expected123, CollectionUtils.arrayAsList(array, 1, 4));
78     }
79 
80     /**
81      * Tests that {@link CollectionUtils#isNullOrEmpty(java.util.Collection)} gives the expected
82      * results for a few cases.
83      */
testIsNullOrEmpty()84     public void testIsNullOrEmpty() {
85         assertTrue(CollectionUtils.isNullOrEmpty((List<String>) null));
86         assertTrue(CollectionUtils.isNullOrEmpty((Map<String, String>) null));
87         assertTrue(CollectionUtils.isNullOrEmpty(new ArrayList<String>()));
88         assertTrue(CollectionUtils.isNullOrEmpty(new HashMap<String, String>()));
89         assertFalse(CollectionUtils.isNullOrEmpty(Collections.singletonList("Not empty")));
90         assertFalse(CollectionUtils.isNullOrEmpty(Collections.singletonMap("Not", "empty")));
91     }
92 }
93