From 49f35aa5eabddd2702483b95c8752139b7b7cda9 Mon Sep 17 00:00:00 2001 From: Balwant Kothari Date: Mon, 15 Jan 2024 15:26:10 +0530 Subject: [PATCH 1/3] ENT-11113 Updating test case for accessing modifier as per JDK17 compatibility --- node/build.gradle | 1 + .../net/corda/node/services/config/ConfigHelperTests.kt | 9 ++++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/node/build.gradle b/node/build.gradle index 4759f663f9..d942b3bbb8 100644 --- a/node/build.gradle +++ b/node/build.gradle @@ -330,6 +330,7 @@ jar { tasks.named('test', Test) { maxHeapSize = "3g" maxParallelForks = (System.env.CORDA_NODE_TESTING_FORKS == null) ? 1 : "$System.env.CORDA_NODE_TESTING_FORKS".toInteger() + jvmArgs(['--add-opens', 'java.base/java.lang.reflect=ALL-UNNAMED', '--add-opens', 'java.base/java.util=ALL-UNNAMED']) } publishing { diff --git a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt index 3996d0965a..60deb1f936 100644 --- a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt @@ -7,12 +7,12 @@ import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.After import org.junit.Assert import org.junit.Before -import org.junit.Ignore import org.junit.Test import org.mockito.ArgumentMatchers.contains import org.mockito.kotlin.spy import org.mockito.kotlin.verify import org.slf4j.Logger +import java.lang.invoke.MethodHandles import java.lang.reflect.Field import java.lang.reflect.Modifier import java.nio.file.Files @@ -71,13 +71,12 @@ class ConfigHelperTests { } @Test(timeout = 300_000) - @Ignore("TODO JDK17: Modifiers no longer supported") fun `bad keys are ignored and warned for`() { val loggerField = Node::class.java.getDeclaredField("staticLog") loggerField.isAccessible = true - val modifiersField = Field::class.java.getDeclaredField("modifiers") - modifiersField.isAccessible = true - modifiersField.setInt(loggerField, loggerField.modifiers and Modifier.FINAL.inv()) + val fieldLookup = MethodHandles.privateLookupIn(Field::class.java, MethodHandles.lookup()); + val modifiersField = fieldLookup.findVarHandle(Field::class.java, "modifiers", Int::class.javaPrimitiveType) + modifiersField.set(loggerField, loggerField.modifiers and Modifier.FINAL.inv()) val originalLogger = loggerField.get(null) as Logger val spyLogger = spy(originalLogger) loggerField.set(null, spyLogger) From 13e13fd236a8bcc4ce604acf5de3dadce80c508e Mon Sep 17 00:00:00 2001 From: Balwant Kothari Date: Thu, 18 Jan 2024 00:15:46 +0530 Subject: [PATCH 2/3] ENT-11113 Updating test case to user overrridden Sysout instead of mock --- .../node/services/config/ConfigHelperTests.kt | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt index 60deb1f936..24d19a2209 100644 --- a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt @@ -2,24 +2,19 @@ package net.corda.node.services.config import com.typesafe.config.Config import com.typesafe.config.ConfigFactory -import net.corda.node.internal.Node import org.assertj.core.api.Assertions.assertThatExceptionOfType import org.junit.After import org.junit.Assert import org.junit.Before import org.junit.Test -import org.mockito.ArgumentMatchers.contains -import org.mockito.kotlin.spy -import org.mockito.kotlin.verify -import org.slf4j.Logger -import java.lang.invoke.MethodHandles -import java.lang.reflect.Field -import java.lang.reflect.Modifier +import java.io.ByteArrayOutputStream +import java.io.PrintStream import java.nio.file.Files import java.nio.file.Path import kotlin.io.path.deleteExisting import kotlin.io.path.div import kotlin.test.assertFalse +import kotlin.test.assertTrue class ConfigHelperTests { private var baseDir: Path? = null @@ -70,23 +65,20 @@ class ConfigHelperTests { } } + @Test(timeout = 300_000) fun `bad keys are ignored and warned for`() { - val loggerField = Node::class.java.getDeclaredField("staticLog") - loggerField.isAccessible = true - val fieldLookup = MethodHandles.privateLookupIn(Field::class.java, MethodHandles.lookup()); - val modifiersField = fieldLookup.findVarHandle(Field::class.java, "modifiers", Int::class.javaPrimitiveType) - modifiersField.set(loggerField, loggerField.modifiers and Modifier.FINAL.inv()) - val originalLogger = loggerField.get(null) as Logger - val spyLogger = spy(originalLogger) - loggerField.set(null, spyLogger) - + val outContent = ByteArrayOutputStream() + val errContent = ByteArrayOutputStream() + val originalOut = System.out + val originalErr = System.err + System.setOut(PrintStream(outContent)); + System.setErr(PrintStream(errContent)); val config = loadConfig("corda_bad_key" to "2077") - - verify(spyLogger).warn(contains("(property or environment variable) cannot be mapped to an existing Corda")) + assertTrue(outContent.toString().contains("(property or environment variable) cannot be mapped to an existing Corda")) assertFalse(config?.hasPath("corda_bad_key") ?: true) - - loggerField.set(null, originalLogger) + System.setOut(originalOut); + System.setErr(originalErr); } /** From 795e61807d35c24ce423f0a247b296b2c75b4335 Mon Sep 17 00:00:00 2001 From: Balwant Kothari Date: Thu, 18 Jan 2024 00:34:59 +0530 Subject: [PATCH 3/3] ENT-11113 Fixed review comments --- node/build.gradle | 1 - .../node/services/config/ConfigHelperTests.kt | 19 +++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/node/build.gradle b/node/build.gradle index d942b3bbb8..4759f663f9 100644 --- a/node/build.gradle +++ b/node/build.gradle @@ -330,7 +330,6 @@ jar { tasks.named('test', Test) { maxHeapSize = "3g" maxParallelForks = (System.env.CORDA_NODE_TESTING_FORKS == null) ? 1 : "$System.env.CORDA_NODE_TESTING_FORKS".toInteger() - jvmArgs(['--add-opens', 'java.base/java.lang.reflect=ALL-UNNAMED', '--add-opens', 'java.base/java.util=ALL-UNNAMED']) } publishing { diff --git a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt index 24d19a2209..11d77da6aa 100644 --- a/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt +++ b/node/src/test/kotlin/net/corda/node/services/config/ConfigHelperTests.kt @@ -68,17 +68,16 @@ class ConfigHelperTests { @Test(timeout = 300_000) fun `bad keys are ignored and warned for`() { - val outContent = ByteArrayOutputStream() - val errContent = ByteArrayOutputStream() val originalOut = System.out - val originalErr = System.err - System.setOut(PrintStream(outContent)); - System.setErr(PrintStream(errContent)); - val config = loadConfig("corda_bad_key" to "2077") - assertTrue(outContent.toString().contains("(property or environment variable) cannot be mapped to an existing Corda")) - assertFalse(config?.hasPath("corda_bad_key") ?: true) - System.setOut(originalOut); - System.setErr(originalErr); + try { + val outContent = ByteArrayOutputStream() + System.setOut(PrintStream(outContent)); + val config = loadConfig("corda_bad_key" to "2077") + assertTrue(outContent.toString().contains("(property or environment variable) cannot be mapped to an existing Corda")) + assertFalse(config?.hasPath("corda_bad_key") ?: true) + } finally { + System.setOut(originalOut); + } } /**