1 /* 2 * Copyright (C) 2012 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 package com.android.tradefed.util; 17 18 import junit.framework.TestCase; 19 20 /** 21 * Unit tests for {@link NullUtil} 22 */ 23 public class NullUtilTest extends TestCase { 24 /** 25 * Make sure countNulls works as expected 26 */ 27 public void testCountNulls() throws Exception { 28 assertEquals(0, NullUtil.countNulls()); 29 assertEquals(0, NullUtil.countNulls("hi")); 30 31 assertEquals(1, NullUtil.countNulls((Object)null)); 32 assertEquals(1, NullUtil.countNulls(null, "hi")); 33 assertEquals(1, NullUtil.countNulls("hi", null)); 34 35 assertEquals(2, NullUtil.countNulls(null, null)); 36 } 37 38 /** 39 * Make sure countNonNulls works as expected 40 */ 41 public void testCountNonNulls() throws Exception { 42 assertEquals(0, NullUtil.countNonNulls()); 43 assertEquals(0, NullUtil.countNonNulls((Object)null)); 44 45 assertEquals(1, NullUtil.countNonNulls("hi")); 46 assertEquals(1, NullUtil.countNonNulls(null, "hi")); 47 assertEquals(1, NullUtil.countNonNulls("hi", null)); 48 49 assertEquals(2, NullUtil.countNonNulls("hi", "hi")); 50 } 51 52 /** 53 * Make sure allNull works as expected 54 */ 55 public void testAllNull() throws Exception { 56 assertTrue(NullUtil.allNull()); 57 assertTrue(NullUtil.allNull((Object)null)); 58 assertTrue(NullUtil.allNull(null, null)); 59 60 assertFalse(NullUtil.allNull(1)); 61 assertFalse(NullUtil.allNull(1, null, null)); 62 assertFalse(NullUtil.allNull(null, null, 1)); 63 } 64 65 /** 66 * Ensure that we return true only when exactly one passed value is non-null 67 */ 68 public void testSingleNonNull() throws Exception { 69 assertFalse(NullUtil.singleNonNull()); 70 71 assertFalse(NullUtil.singleNonNull(null, null, null)); 72 assertTrue(NullUtil.singleNonNull(1, null, null)); 73 assertTrue(NullUtil.singleNonNull(null, null, 1)); 74 assertFalse(NullUtil.singleNonNull(1, null, 1)); 75 assertFalse(NullUtil.singleNonNull(1, 1, 1)); 76 } 77 78 /** 79 * Ensure that we return true only when it is not the case that a null object is simultaneously 80 * present with a non-null object 81 */ 82 public void testHomogeneousSet() throws Exception { 83 assertTrue(NullUtil.isHomogeneousSet()); 84 85 assertTrue(NullUtil.isHomogeneousSet(1)); 86 assertTrue(NullUtil.isHomogeneousSet((Object)null)); 87 88 assertTrue(NullUtil.isHomogeneousSet(1, 1)); 89 assertTrue(NullUtil.isHomogeneousSet(null, null)); 90 91 assertFalse(NullUtil.isHomogeneousSet(1, null)); 92 assertFalse(NullUtil.isHomogeneousSet(null, 1)); 93 } 94 } 95 96