|
1 | 1 | package com.baeldung.java9.varhandles; |
2 | 2 |
|
3 | | -import org.junit.Assert; |
4 | 3 | import org.junit.Test; |
5 | 4 |
|
6 | 5 | import java.lang.invoke.MethodHandles; |
7 | 6 | import java.lang.invoke.VarHandle; |
8 | 7 |
|
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
9 | 10 | public class VariableHandlesTest { |
| 11 | + |
10 | 12 | public int publicTestVariable = 1; |
11 | 13 | private int privateTestVariable = 1; |
12 | 14 | public int variableToSet = 1; |
13 | 15 | public int variableToCompareAndSet = 1; |
14 | 16 | public int variableToGetAndAdd = 0; |
15 | 17 | public byte variableToBitwiseOr = 0; |
| 18 | + |
16 | 19 | @Test |
17 | | - public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() |
18 | | - throws NoSuchFieldException, IllegalAccessException { |
19 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
20 | | - .in(VariableHandlesTest.class) |
21 | | - .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); |
22 | | - Assert.assertEquals(1, publicIntHandle.coordinateTypes().size()); |
23 | | - Assert.assertEquals(VariableHandlesTest.class, publicIntHandle.coordinateTypes().get(0)); |
| 20 | + public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { |
| 21 | + VarHandle publicIntHandle = MethodHandles |
| 22 | + .lookup() |
| 23 | + .in(VariableHandlesTest.class) |
| 24 | + .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); |
| 25 | + |
| 26 | + assertThat(publicIntHandle.coordinateTypes().size() == 1); |
| 27 | + assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandles.class); |
24 | 28 |
|
25 | 29 | } |
26 | 30 |
|
27 | 31 | @Test |
28 | | - public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() |
29 | | - throws NoSuchFieldException, IllegalAccessException { |
30 | | - VarHandle privateIntHandle = MethodHandles.privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) |
31 | | - .findVarHandle(VariableHandlesTest.class,"privateTestVariable", int.class); |
32 | | - Assert.assertNotNull(privateIntHandle); |
33 | | - Assert.assertEquals(1, privateIntHandle.coordinateTypes().size()); |
34 | | - Assert.assertEquals(VariableHandlesTest.class, privateIntHandle.coordinateTypes().get(0)); |
| 32 | + public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { |
| 33 | + VarHandle privateIntHandle = MethodHandles |
| 34 | + .privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) |
| 35 | + .findVarHandle(VariableHandlesTest.class, "privateTestVariable", int.class); |
| 36 | + |
| 37 | + assertThat(privateIntHandle.coordinateTypes().size() == 1); |
| 38 | + assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesTest.class); |
35 | 39 |
|
36 | 40 | } |
37 | 41 |
|
38 | 42 | @Test |
39 | | - public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() |
40 | | - throws NoSuchFieldException, IllegalAccessException { |
41 | | - VarHandle arrayVarHandle = MethodHandles.arrayElementVarHandle(int[].class); |
42 | | - Assert.assertEquals(2, arrayVarHandle.coordinateTypes().size()); |
43 | | - Assert.assertEquals(int[].class, arrayVarHandle.coordinateTypes().get(0)); |
| 43 | + public void whenVariableHandleForArrayVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { |
| 44 | + VarHandle arrayVarHandle = MethodHandles |
| 45 | + .arrayElementVarHandle(int[].class); |
| 46 | + |
| 47 | + assertThat(arrayVarHandle.coordinateTypes().size() == 2); |
| 48 | + assertThat(arrayVarHandle.coordinateTypes().get(0) == int[].class); |
44 | 49 | } |
45 | 50 |
|
46 | 51 | @Test |
47 | 52 | public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException { |
48 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
49 | | - .in(VariableHandlesTest.class) |
50 | | - .findVarHandle(VariableHandlesTest.class,"publicTestVariable", int.class); |
51 | | - Assert.assertEquals(1, publicIntHandle.get(this)); |
| 53 | + VarHandle publicIntHandle = MethodHandles |
| 54 | + .lookup() |
| 55 | + .in(VariableHandlesTest.class) |
| 56 | + .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); |
| 57 | + |
| 58 | + assertThat((int) publicIntHandle.get(this) == 1); |
52 | 59 | } |
53 | 60 |
|
54 | 61 | @Test |
55 | 62 | public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
56 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
57 | | - .in(VariableHandlesTest.class) |
58 | | - .findVarHandle(VariableHandlesTest.class,"variableToSet", int.class); |
| 63 | + VarHandle publicIntHandle = MethodHandles |
| 64 | + .lookup() |
| 65 | + .in(VariableHandlesTest.class) |
| 66 | + .findVarHandle(VariableHandlesTest.class, "variableToSet", int.class); |
59 | 67 | publicIntHandle.set(this, 15); |
60 | | - Assert.assertEquals(15, publicIntHandle.get(this)); |
| 68 | + |
| 69 | + assertThat((int) publicIntHandle.get(this) == 15); |
61 | 70 | } |
62 | 71 |
|
63 | 72 | @Test |
64 | 73 | public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
65 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
66 | | - .in(VariableHandlesTest.class) |
67 | | - .findVarHandle(VariableHandlesTest.class,"variableToCompareAndSet", int.class); |
68 | | - publicIntHandle.compareAndSet(this, 1,100); |
69 | | - Assert.assertEquals(100, publicIntHandle.get(this)); |
| 74 | + VarHandle publicIntHandle = MethodHandles |
| 75 | + .lookup() |
| 76 | + .in(VariableHandlesTest.class) |
| 77 | + .findVarHandle(VariableHandlesTest.class, "variableToCompareAndSet", int.class); |
| 78 | + publicIntHandle.compareAndSet(this, 1, 100); |
| 79 | + |
| 80 | + assertThat((int) publicIntHandle.get(this) == 100); |
70 | 81 | } |
71 | 82 |
|
72 | 83 | @Test |
73 | 84 | public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
74 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
75 | | - .in(VariableHandlesTest.class) |
76 | | - .findVarHandle(VariableHandlesTest.class,"variableToGetAndAdd", int.class); |
| 85 | + VarHandle publicIntHandle = MethodHandles |
| 86 | + .lookup() |
| 87 | + .in(VariableHandlesTest.class) |
| 88 | + .findVarHandle(VariableHandlesTest.class, "variableToGetAndAdd", int.class); |
77 | 89 | int before = (int) publicIntHandle.getAndAdd(this, 200); |
78 | | - Assert.assertEquals(0, before); |
79 | | - Assert.assertEquals(200, publicIntHandle.get(this)); |
| 90 | + |
| 91 | + assertThat(before == 0); |
| 92 | + assertThat((int) publicIntHandle.get(this) == 200); |
80 | 93 | } |
81 | 94 |
|
82 | 95 | @Test |
83 | 96 | public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { |
84 | | - VarHandle publicIntHandle = MethodHandles.lookup() |
85 | | - .in(VariableHandlesTest.class) |
86 | | - .findVarHandle(VariableHandlesTest.class,"variableToBitwiseOr", byte.class); |
| 97 | + VarHandle publicIntHandle = MethodHandles |
| 98 | + .lookup() |
| 99 | + .in(VariableHandlesTest.class) |
| 100 | + .findVarHandle(VariableHandlesTest.class, "variableToBitwiseOr", byte.class); |
87 | 101 | byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127); |
88 | | - Assert.assertEquals(0, before); |
89 | | - Assert.assertEquals(127, variableToBitwiseOr); |
| 102 | + |
| 103 | + assertThat(before == 0); |
| 104 | + assertThat(variableToBitwiseOr == 127); |
90 | 105 | } |
91 | 106 | } |
0 commit comments