1 /* 2 * Copyright (C) 2007 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.primitives; 18 19 import static com.google.common.truth.Truth.assertThat; 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 java.util.Set; 26 import junit.framework.TestCase; 27 28 /** 29 * Unit test for {@link Primitives}. 30 * 31 * @author Kevin Bourrillion 32 */ 33 @GwtCompatible(emulated = true) 34 public class PrimitivesTest extends TestCase { testIsWrapperType()35 public void testIsWrapperType() { 36 assertThat(Primitives.isWrapperType(Void.class)).isTrue(); 37 assertThat(Primitives.isWrapperType(void.class)).isFalse(); 38 } 39 testWrap()40 public void testWrap() { 41 assertThat(Primitives.wrap(int.class)).isSameInstanceAs(Integer.class); 42 assertThat(Primitives.wrap(Integer.class)).isSameInstanceAs(Integer.class); 43 assertThat(Primitives.wrap(String.class)).isSameInstanceAs(String.class); 44 } 45 testUnwrap()46 public void testUnwrap() { 47 assertThat(Primitives.unwrap(Integer.class)).isSameInstanceAs(int.class); 48 assertThat(Primitives.unwrap(int.class)).isSameInstanceAs(int.class); 49 assertThat(Primitives.unwrap(String.class)).isSameInstanceAs(String.class); 50 } 51 testAllPrimitiveTypes()52 public void testAllPrimitiveTypes() { 53 Set<Class<?>> primitives = Primitives.allPrimitiveTypes(); 54 assertThat(primitives) 55 .containsExactly( 56 boolean.class, 57 byte.class, 58 char.class, 59 double.class, 60 float.class, 61 int.class, 62 long.class, 63 short.class, 64 void.class); 65 66 try { 67 primitives.remove(boolean.class); 68 fail(); 69 } catch (UnsupportedOperationException expected) { 70 } 71 } 72 testAllWrapperTypes()73 public void testAllWrapperTypes() { 74 Set<Class<?>> wrappers = Primitives.allWrapperTypes(); 75 assertThat(wrappers) 76 .containsExactly( 77 Boolean.class, 78 Byte.class, 79 Character.class, 80 Double.class, 81 Float.class, 82 Integer.class, 83 Long.class, 84 Short.class, 85 Void.class); 86 87 try { 88 wrappers.remove(Boolean.class); 89 fail(); 90 } catch (UnsupportedOperationException expected) { 91 } 92 } 93 94 @GwtIncompatible 95 @J2ktIncompatible testNullPointerExceptions()96 public void testNullPointerExceptions() { 97 NullPointerTester tester = new NullPointerTester(); 98 tester.testAllPublicStaticMethods(Primitives.class); 99 } 100 } 101