Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/SwitchPointTest.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.SwitchPoint;2930/**31* @author mesbah32* This class contains tests for SwitchPoint API33*/34public class SwitchPointTest {3536/**37* Tests SwitchPoint invalidation in a single threaded environment38*/39@Test(groups = { "level.extended" })40public void testSwitchPoint_Invalidation_Basic() {41SwitchPoint spt = new SwitchPoint();4243assert(!spt.hasBeenInvalidated());4445SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });4647assert(spt.hasBeenInvalidated());48}4950/**51* Creates a target and a fallback MethodHandle, initializes a SwitchPoint using them and validates invocation of target52* while the SwitchPoint is valid and invocation of the fallback when SwitchPoint has been invalidated.53* @throws Throwable54*/55@Test(groups = { "level.extended" })56public void testSwitchPoint_guardWithTest_Basic() throws Throwable {57MethodHandle target = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));58MethodHandle fallback = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnTwo", MethodType.methodType(String.class));59SwitchPoint spt = new SwitchPoint();6061MethodHandle guardedDelegator = spt.guardWithTest(target, fallback);6263AssertJUnit.assertEquals("1", (String) guardedDelegator.invokeExact());6465SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });6667AssertJUnit.assertEquals("2", (String) guardedDelegator.invokeExact());68}6970/**71* The test creates and combines 2 guarded pairs of method handles into two guarded delegators and combines the delegators72* into a single SwitchPoint. Then assertions are performed based on valid state of the SwitchPoint and invalid state.73*/74@Test(groups = { "level.extended" })75public void testSwitchPoint_guardWithTest_Chained() throws Throwable {76SwitchPoint spt = new SwitchPoint();77assert(!spt.hasBeenInvalidated());7879MethodHandle target1 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));80MethodHandle fallback1 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnTwo", MethodType.methodType(String.class));81MethodHandle guardedDelegator1 = spt.guardWithTest(target1, fallback1);8283MethodHandle target2 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnThree", MethodType.methodType(String.class));84MethodHandle guardedDelegator2 = spt.guardWithTest(target2, guardedDelegator1);8586AssertJUnit.assertEquals("3", (String) guardedDelegator2.invokeExact());8788SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });8990assert(spt.hasBeenInvalidated());9192AssertJUnit.assertEquals("2", (String) guardedDelegator2.invokeExact());93}94}959697