|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package org.mozilla.javascript.tests.es2021; |
| 6 | + |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertNotNull; |
| 9 | +import static org.junit.Assert.assertTrue; |
| 10 | + |
| 11 | +import java.lang.reflect.Field; |
| 12 | +import java.lang.reflect.Method; |
| 13 | +import java.util.concurrent.ConcurrentHashMap; |
| 14 | +import org.junit.Test; |
| 15 | +import org.mozilla.javascript.Context; |
| 16 | +import org.mozilla.javascript.Function; |
| 17 | +import org.mozilla.javascript.NativeFinalizationRegistry; |
| 18 | +import org.mozilla.javascript.Scriptable; |
| 19 | +import org.mozilla.javascript.ScriptableObject; |
| 20 | +import org.mozilla.javascript.Undefined; |
| 21 | + |
| 22 | +/** |
| 23 | + * Internal tests for FinalizationRegistry using reflection to test private methods. These are |
| 24 | + * "white box" tests as requested by reviewer. |
| 25 | + */ |
| 26 | +public class FinalizationRegistryInternalTest { |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testProcessPendingCleanupsMethod() throws Exception { |
| 30 | + try (Context cx = Context.enter()) { |
| 31 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 32 | + Scriptable scope = cx.initStandardObjects(); |
| 33 | + |
| 34 | + // Create a FinalizationRegistry |
| 35 | + String script = "new FinalizationRegistry(function(heldValue) {})"; |
| 36 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 37 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 38 | + |
| 39 | + // Use reflection to access processPendingCleanups method |
| 40 | + Method method = |
| 41 | + NativeFinalizationRegistry.class.getDeclaredMethod("processPendingCleanups"); |
| 42 | + method.setAccessible(true); |
| 43 | + |
| 44 | + // Should not throw exception |
| 45 | + method.invoke(registry); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void testRegistrationsMapInitialization() throws Exception { |
| 51 | + try (Context cx = Context.enter()) { |
| 52 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 53 | + Scriptable scope = cx.initStandardObjects(); |
| 54 | + |
| 55 | + // Create a FinalizationRegistry |
| 56 | + String script = "new FinalizationRegistry(function(heldValue) {})"; |
| 57 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 58 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 59 | + |
| 60 | + // Use reflection to access registrations field |
| 61 | + Field field = NativeFinalizationRegistry.class.getDeclaredField("registrations"); |
| 62 | + field.setAccessible(true); |
| 63 | + Object registrations = field.get(registry); |
| 64 | + |
| 65 | + assertNotNull(registrations); |
| 66 | + assertTrue(registrations instanceof ConcurrentHashMap); |
| 67 | + assertEquals(0, ((ConcurrentHashMap<?, ?>) registrations).size()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testTokenMapInitialization() throws Exception { |
| 73 | + try (Context cx = Context.enter()) { |
| 74 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 75 | + Scriptable scope = cx.initStandardObjects(); |
| 76 | + |
| 77 | + // Create a FinalizationRegistry |
| 78 | + String script = "new FinalizationRegistry(function(heldValue) {})"; |
| 79 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 80 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 81 | + |
| 82 | + // Use reflection to access tokenMap field |
| 83 | + Field field = NativeFinalizationRegistry.class.getDeclaredField("tokenMap"); |
| 84 | + field.setAccessible(true); |
| 85 | + Object tokenMap = field.get(registry); |
| 86 | + |
| 87 | + assertNotNull(tokenMap); |
| 88 | + assertTrue(tokenMap instanceof ConcurrentHashMap); |
| 89 | + assertEquals(0, ((ConcurrentHashMap<?, ?>) tokenMap).size()); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testCleanupCallbackStorage() throws Exception { |
| 95 | + try (Context cx = Context.enter()) { |
| 96 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 97 | + Scriptable scope = cx.initStandardObjects(); |
| 98 | + |
| 99 | + // Create a FinalizationRegistry |
| 100 | + String script = |
| 101 | + "var callback = function(heldValue) {}; new FinalizationRegistry(callback)"; |
| 102 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 103 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 104 | + |
| 105 | + // Use reflection to access cleanupCallback field |
| 106 | + Field field = NativeFinalizationRegistry.class.getDeclaredField("cleanupCallback"); |
| 107 | + field.setAccessible(true); |
| 108 | + Object cleanupCallback = field.get(registry); |
| 109 | + |
| 110 | + assertNotNull(cleanupCallback); |
| 111 | + assertTrue(cleanupCallback instanceof Function); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testExecuteCleanupCallbackMethod() throws Exception { |
| 117 | + try (Context cx = Context.enter()) { |
| 118 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 119 | + Scriptable scope = cx.initStandardObjects(); |
| 120 | + |
| 121 | + // Create a FinalizationRegistry with a callback that sets a flag |
| 122 | + String script = |
| 123 | + "var called = false;" |
| 124 | + + "var callback = function(heldValue) { called = true; };" |
| 125 | + + "new FinalizationRegistry(callback)"; |
| 126 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 127 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 128 | + |
| 129 | + // Use reflection to call executeCleanupCallback |
| 130 | + Method method = |
| 131 | + NativeFinalizationRegistry.class.getDeclaredMethod( |
| 132 | + "executeCleanupCallback", Object.class); |
| 133 | + method.setAccessible(true); |
| 134 | + method.invoke(registry, "test value"); |
| 135 | + |
| 136 | + // Check if callback was called |
| 137 | + Object called = ScriptableObject.getProperty(scope, "called"); |
| 138 | + assertEquals(Boolean.TRUE, called); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testPerformCleanupSomeMethod() throws Exception { |
| 144 | + try (Context cx = Context.enter()) { |
| 145 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 146 | + Scriptable scope = cx.initStandardObjects(); |
| 147 | + |
| 148 | + // Create a FinalizationRegistry |
| 149 | + String script = "new FinalizationRegistry(function(heldValue) {})"; |
| 150 | + Object registry = cx.evaluateString(scope, script, "test", 1, null); |
| 151 | + assertTrue(registry instanceof NativeFinalizationRegistry); |
| 152 | + |
| 153 | + // Use reflection to access performCleanupSome method |
| 154 | + Method method = |
| 155 | + NativeFinalizationRegistry.class.getDeclaredMethod( |
| 156 | + "performCleanupSome", Context.class, Function.class); |
| 157 | + method.setAccessible(true); |
| 158 | + |
| 159 | + // Should not throw exception |
| 160 | + method.invoke(registry, cx, null); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void testIsValidTargetMethod() throws Exception { |
| 166 | + // Use reflection to test the static isValidTarget method |
| 167 | + Method method = |
| 168 | + NativeFinalizationRegistry.class.getDeclaredMethod("isValidTarget", Object.class); |
| 169 | + method.setAccessible(true); |
| 170 | + |
| 171 | + try (Context cx = Context.enter()) { |
| 172 | + cx.setLanguageVersion(Context.VERSION_ES6); |
| 173 | + Scriptable scope = cx.initStandardObjects(); |
| 174 | + |
| 175 | + // Test with valid object |
| 176 | + Object obj = cx.evaluateString(scope, "({})", "test", 1, null); |
| 177 | + assertTrue((Boolean) method.invoke(null, obj)); |
| 178 | + |
| 179 | + // Test with undefined |
| 180 | + assertEquals(false, method.invoke(null, Undefined.instance)); |
| 181 | + |
| 182 | + // Test with null |
| 183 | + assertEquals(false, method.invoke(null, new Object[] {null})); |
| 184 | + |
| 185 | + // Test with primitive wrapped as Object |
| 186 | + assertEquals(false, method.invoke(null, "string")); |
| 187 | + assertEquals(false, method.invoke(null, 42)); |
| 188 | + assertEquals(false, method.invoke(null, true)); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments