1 /*
   2  * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package test.sun.invoke.util;
  25 
  26 import sun.invoke.util.ValueConversions;
  27 import sun.invoke.util.Wrapper;
  28 import java.lang.invoke.MethodHandles;
  29 import java.lang.invoke.MethodType;
  30 import java.lang.invoke.MethodHandle;
  31 import java.io.Serializable;
  32 import java.util.Arrays;
  33 import org.junit.Test;
  34 import static org.junit.Assert.*;
  35 
  36 /* @test
  37  * @summary unit tests for making sure Wrapper.zero doesn't leak
  38  * @modules java.base/sun.invoke.util
  39  * @compile -XDignore.symbol.file WrapperTest.java
  40  * @run junit/othervm test.sun.invoke.util.WrapperTest
  41  */
  42 
  43 public class WrapperTest {
  44 
  45     @Test
  46     public void testShortZeroConversion() throws Throwable {
  47         MethodHandle h1 = MethodHandles.constant(Short.class, (short)42);
  48         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
  49         MethodHandle h3 = h2.asType(MethodType.methodType(short.class));  // add 0
  50         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
  51 
  52         Object x = h4.invokeExact();
  53         assertEquals(x, (short)0);
  54         assertTrue(x == Short.valueOf((short)0));
  55         assertFalse(x == Wrapper.SHORT.zero());
  56     }
  57 
  58     @Test
  59     public void testIntZeroConversion() throws Throwable {
  60         MethodHandle h1 = MethodHandles.constant(Integer.class, 42);
  61         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
  62         MethodHandle h3 = h2.asType(MethodType.methodType(int.class));  // add 0
  63         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
  64 
  65         Object x = h4.invokeExact();
  66         assertEquals(x, 0);
  67         assertTrue(x == Integer.valueOf(0));
  68         assertFalse(x == Wrapper.INT.zero());
  69     }
  70 
  71     @Test
  72     public void testLongZeroConversion() throws Throwable {
  73         MethodHandle h1 = MethodHandles.constant(Long.class, 42L);
  74         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
  75         MethodHandle h3 = h2.asType(MethodType.methodType(long.class));  // add 0
  76         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
  77 
  78         Object x = h4.invokeExact();
  79         assertEquals(x, 0L);
  80         assertTrue(x == Long.valueOf(0));
  81         assertFalse(x == Wrapper.LONG.zero());
  82     }
  83 
  84     @Test
  85     public void testByteZeroConversion() throws Throwable {
  86         MethodHandle h1 = MethodHandles.constant(Byte.class, (byte)42);
  87         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
  88         MethodHandle h3 = h2.asType(MethodType.methodType(Byte.class));  // add 0
  89         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
  90 
  91         Object x = h4.invokeExact();
  92         assertEquals(x, null);
  93     }
  94 
  95     @Test
  96     public void testCharacterZeroConversion() throws Throwable {
  97         MethodHandle h1 = MethodHandles.constant(Character.class, (char)42);
  98         MethodHandle h2 = h1.asType(MethodType.methodType(void.class));  // drop 42
  99         MethodHandle h3 = h2.asType(MethodType.methodType(Character.class));  // add 0
 100         MethodHandle h4 = h3.asType(MethodType.methodType(Object.class));  // box
 101 
 102         Object x = h4.invokeExact();
 103         assertEquals(x, null);
 104     }
 105 }