Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/VolatileCallSiteTest.java
6007 views
/*******************************************************************************1* Copyright (c) 2001, 2018 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21package com.ibm.j9.jsr292;2223import org.testng.annotations.Test;24import org.testng.AssertJUnit;25import java.lang.invoke.MethodHandle;26import java.lang.invoke.MethodHandles;27import java.lang.invoke.MethodType;28import java.lang.invoke.VolatileCallSite;29import java.lang.invoke.WrongMethodTypeException;3031/**32* @author mesbah33* This class contains tests for VolatileCallSite API34*/35public class VolatileCallSiteTest {36/**37* Test for VolatileCallSite.type()38* @throws Throwable39*/40@Test(groups = { "level.extended" })41public void testType_VolatileCallSite() throws Throwable {42MethodType mt = MethodType.methodType(String.class);43MethodHandle mh = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne",mt);44VolatileCallSite vcs = new VolatileCallSite(mh);4546AssertJUnit.assertEquals(mt,vcs.type());47}4849/**50* Test for validating default MethodHandle behavior of VolatileCallSite51* using VolatileCallSite.dynamicInvoker.invoke()52* @throws Throwable53*/54@Test(groups = { "level.extended" })55public void testIllegalState_dynamicInvoker_VolatileCallSite() throws Throwable {56MethodType mt = MethodType.methodType(void.class);57VolatileCallSite vcs = new VolatileCallSite(mt);58boolean iseHit = false;5960try {61vcs.dynamicInvoker().invoke();62}63catch ( IllegalStateException e ) {64iseHit = true;65}6667AssertJUnit.assertTrue(iseHit);68}6970/**71* Test for validating default MethodHandle behavior of VolatileCallSite72* using VolatileCallSite.getTarget().invoke()73* @throws Throwable74*/75@Test(groups = { "level.extended" })76public void testIllegalState_getTarget_VolatileCallSite() throws Throwable {77MethodType mt = MethodType.methodType(void.class);78VolatileCallSite vcs = new VolatileCallSite(mt);79boolean iseHit = false;8081try {82vcs.getTarget().invoke();83}84catch ( IllegalStateException e ) {85iseHit = true;86}8788AssertJUnit.assertTrue(iseHit);89}9091/**92* Basic sanity test for VolatileCallSite93* @throws Throwable94*/95@Test(groups = { "level.extended" })96public void testBasic_VolatileCallSite() throws Throwable {97MethodType mt = MethodType.methodType(double.class,String.class);98VolatileCallSite vcs = new VolatileCallSite(mt);99MethodHandle mh = MethodHandles.lookup().findStatic(Double.class, "parseDouble",vcs.type());100vcs.setTarget(mh);101double d = (double)vcs.dynamicInvoker().invokeExact("1.1");102103AssertJUnit.assertEquals(1.1,d);104}105106/**107* Basic negative test for VolatileCallSite108* @throws Throwable109*/110@Test(groups = { "level.extended" })111public void testBasicNegative_VolatileCallSite() throws Throwable {112MethodHandle mh = MethodHandles.lookup().findStatic(Math.class, "sin",MethodType.methodType(double.class,double.class));113VolatileCallSite vcs = new VolatileCallSite(mh);114115boolean wmtThrown = false;116117try {118String s = (String)vcs.dynamicInvoker().invokeExact(0.0);119}120catch ( WrongMethodTypeException e ) {121wmtThrown = true;122}123124AssertJUnit.assertTrue(wmtThrown);125}126}127128129