Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/test/functional/Jsr292/src/com/ibm/j9/jsr292/SwitchPointTest.java
6007 views
1
/*******************************************************************************
2
* Copyright (c) 2001, 2018 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* 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-exception
21
*******************************************************************************/
22
package com.ibm.j9.jsr292;
23
24
import org.testng.annotations.Test;
25
import org.testng.AssertJUnit;
26
import java.lang.invoke.MethodHandle;
27
import java.lang.invoke.MethodHandles;
28
import java.lang.invoke.MethodType;
29
import java.lang.invoke.SwitchPoint;
30
31
/**
32
* @author mesbah
33
* This class contains tests for SwitchPoint API
34
*/
35
public class SwitchPointTest {
36
37
/**
38
* Tests SwitchPoint invalidation in a single threaded environment
39
*/
40
@Test(groups = { "level.extended" })
41
public void testSwitchPoint_Invalidation_Basic() {
42
SwitchPoint spt = new SwitchPoint();
43
44
assert(!spt.hasBeenInvalidated());
45
46
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
47
48
assert(spt.hasBeenInvalidated());
49
}
50
51
/**
52
* Creates a target and a fallback MethodHandle, initializes a SwitchPoint using them and validates invocation of target
53
* while the SwitchPoint is valid and invocation of the fallback when SwitchPoint has been invalidated.
54
* @throws Throwable
55
*/
56
@Test(groups = { "level.extended" })
57
public void testSwitchPoint_guardWithTest_Basic() throws Throwable {
58
MethodHandle target = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));
59
MethodHandle fallback = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnTwo", MethodType.methodType(String.class));
60
SwitchPoint spt = new SwitchPoint();
61
62
MethodHandle guardedDelegator = spt.guardWithTest(target, fallback);
63
64
AssertJUnit.assertEquals("1", (String) guardedDelegator.invokeExact());
65
66
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
67
68
AssertJUnit.assertEquals("2", (String) guardedDelegator.invokeExact());
69
}
70
71
/**
72
* The test creates and combines 2 guarded pairs of method handles into two guarded delegators and combines the delegators
73
* into a single SwitchPoint. Then assertions are performed based on valid state of the SwitchPoint and invalid state.
74
*/
75
@Test(groups = { "level.extended" })
76
public void testSwitchPoint_guardWithTest_Chained() throws Throwable {
77
SwitchPoint spt = new SwitchPoint();
78
assert(!spt.hasBeenInvalidated());
79
80
MethodHandle target1 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnOne", MethodType.methodType(String.class));
81
MethodHandle fallback1 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnTwo", MethodType.methodType(String.class));
82
MethodHandle guardedDelegator1 = spt.guardWithTest(target1, fallback1);
83
84
MethodHandle target2 = MethodHandles.lookup().findStatic(SamePackageExample.class, "returnThree", MethodType.methodType(String.class));
85
MethodHandle guardedDelegator2 = spt.guardWithTest(target2, guardedDelegator1);
86
87
AssertJUnit.assertEquals("3", (String) guardedDelegator2.invokeExact());
88
89
SwitchPoint.invalidateAll(new SwitchPoint[]{ spt });
90
91
assert(spt.hasBeenInvalidated());
92
93
AssertJUnit.assertEquals("2", (String) guardedDelegator2.invokeExact());
94
}
95
}
96
97