1 /* 2 * Copyright (C) 2014 The Guava Authors 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.google.common.base; 18 19 import static com.google.common.base.ReflectionFreeAssertThrows.assertThrows; 20 21 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.annotations.GwtIncompatible; 23 import com.google.common.annotations.J2ktIncompatible; 24 import com.google.common.testing.NullPointerTester; 25 import junit.framework.TestCase; 26 27 /** Tests for {@link MoreObjects}. */ 28 @GwtCompatible(emulated = true) 29 public class MoreObjectsTest extends TestCase { testFirstNonNull_withNonNull()30 public void testFirstNonNull_withNonNull() { 31 String s1 = "foo"; 32 String s2 = MoreObjects.firstNonNull(s1, "bar"); 33 assertSame(s1, s2); 34 35 Long n1 = 42L; 36 Long n2 = MoreObjects.firstNonNull(null, n1); 37 assertSame(n1, n2); 38 39 Boolean b1 = true; 40 Boolean b2 = MoreObjects.firstNonNull(b1, null); 41 assertSame(b1, b2); 42 } 43 testFirstNonNull_throwsNullPointerException()44 public void testFirstNonNull_throwsNullPointerException() { 45 assertThrows(NullPointerException.class, () -> MoreObjects.firstNonNull(null, null)); 46 } 47 48 // ToStringHelper's tests are in ToStringHelperTest 49 50 @J2ktIncompatible 51 @GwtIncompatible("NullPointerTester") testNulls()52 public void testNulls() throws Exception { 53 NullPointerTester tester = new NullPointerTester(); 54 tester.ignore(MoreObjects.class.getMethod("firstNonNull", Object.class, Object.class)); 55 tester.testAllPublicStaticMethods(MoreObjects.class); 56 tester.testAllPublicInstanceMethods(MoreObjects.toStringHelper(new TestClass())); 57 } 58 59 /** Test class for testing formatting of inner classes. */ 60 private static class TestClass {} 61 } 62