@@ -2,6 +2,7 @@ package com.worksap.nlp.tools
22
33import org.gradle.api.Plugin
44import org.gradle.api.Project
5+ import org.gradle.api.Action
56import org.gradle.api.Task
67import org.gradle.api.Transformer
78import org.gradle.api.model.ObjectFactory
@@ -49,67 +50,6 @@ class PluginDescriptor {
4950 }
5051}
5152
52- class StringProvider implements Provider<String > , Serializable {
53- private static final long serialVersionUID = 42L
54- String value
55-
56- @Override
57- String get () {
58- return value
59- }
60-
61- @Override
62- String getOrNull () {
63- return value
64- }
65-
66- @Override
67- String getOrElse (String defaultValue ) {
68- if (value == null ) return defaultValue else return value
69- }
70-
71- @Override
72- def <S> Provider<S> map (Transformer<? extends S, ? super String > transformer ) {
73- throw new IllegalStateException (" not implemented" )
74- }
75-
76- @Override
77- def <S> Provider<S> flatMap (Transformer<? extends Provider<? extends S> , ? super String > transformer ) {
78- throw new IllegalStateException (" not implemented" )
79- }
80-
81- @Override
82- boolean isPresent () {
83- return value != null
84- }
85-
86- @Override
87- Provider<String > orElse (String value ) {
88- return this
89- }
90-
91- @Override
92- Provider<String > orElse (Provider<? extends String > provider ) {
93- if (value == null ) return provider else return this
94- }
95-
96- @Override
97- Provider<String > forUseAtConfigurationTime () {
98- return this
99- }
100-
101- @Override
102- def <U, R> Provider<R> zip (Provider<U> right , BiFunction<? super String , ? super U, ? extends R> combiner ) {
103- throw new IllegalStateException (" not implemented" )
104- }
105-
106-
107- @Override
108- String toString () {
109- return value;
110- }
111- }
112-
11353class EsTestEnvPlugin implements Plugin<Project > {
11454
11555 private final ObjectFactory objectFactory
@@ -124,14 +64,28 @@ class EsTestEnvPlugin implements Plugin<Project> {
12464 EsTestEnvExtension ext = new EsTestEnvExtension ()
12565 target. extensions. add(EsTestEnvExtension . class, " esTestEnv" , ext)
12666 target. tasks. named(" test" ). configure { Test task ->
127- Provider<String > envRoot = new StringProvider ()
67+ // Determine the final, unique path for this test run during the configuration phase.
68+ // This is required because the test JVM is forked before the task's actions run,
69+ // and it needs java.io.tmpdir to exist on startup. A newer/faster JDK can expose
70+ // this timing issue.
71+ def formatter = DateTimeFormatter . ofPattern(" yyyyMMdd-HH-mm-ss-SSS" , Locale . ROOT )
72+ def now = Instant . now(). atZone(ZoneId . of(" UTC" ))
73+ def timepart = formatter. format(now)
12874 Path esHomePath = target. buildDir. toPath(). resolve(" es-env" )
129- envRoot. setValue(esHomePath. toString())
130- task. systemProperty(" sudachi.es.root" , envRoot)
75+ Path finalEnvRootPath = esHomePath. resolve(timepart)
76+
77+ // Create the directory during configuration. This is generally an anti-pattern,
78+ // but necessary to prevent the "java.io.tmpdir directory does not exist" warning from the forked JVM.
79+ Files . createDirectories(finalEnvRootPath)
80+
81+ task. systemProperty(" sudachi.es.root" , finalEnvRootPath. toString())
82+ task. systemProperty(" java.io.tmpdir" , finalEnvRootPath. toString())
83+
13184 task. doFirst {
132- envRoot. setValue(prepareEnvironment(target, task, esHomePath, ext). toString())
85+ // The root directory is already created, just populate it with files.
86+ populateEnvironment(target, task, finalEnvRootPath, ext)
13387 }
134- task. doLast { cleanupEnvironment(esHomePath) }
88+ task. doLast { cleanupEnvironment(target, esHomePath) }
13589 ext. additionalPlugins. forEach {
13690 if (it. value instanceof TaskProvider || it. value instanceof Task ) {
13791 dependsOn(it. value)
@@ -147,7 +101,18 @@ class EsTestEnvPlugin implements Plugin<Project> {
147101 task. systemProperty(" tests.task" , task. getPath())
148102 task. systemProperty(" gradle.dist.lib" , gradleRtDir. resolve(" lib" ). toString())
149103 task. systemProperty(" gradle.worker.jar" , gradleCacheDir. resolve(" workerMain/gradle-worker.jar" ). toString())
150- task. systemProperty(" java.io.tmpdir" , envRoot)
104+
105+ // OpenSearch test framework requires reflective access to JDK internals, which is restricted on Java 17+.
106+ // It also uses a Security Manager, which is disabled by default in Java 17+.
107+ // We need to add JVM arguments to re-enable these features for the tests to run.
108+ def esExtension = target. extensions. findByName(" sudachiEs" )
109+ if (esExtension != null && esExtension. kind. get(). engine == EngineType.OpenSearch ) {
110+ task. jvmArgs(
111+ ' --add-opens=java.base/java.lang=ALL-UNNAMED'
112+ )
113+ // Allow the test framework to set a Security Manager
114+ task. systemProperty(" java.security.manager" , " allow" )
115+ }
151116 }
152117
153118 target. gradle. taskGraph. whenReady {
@@ -172,12 +137,8 @@ class EsTestEnvPlugin implements Plugin<Project> {
172137 }
173138 }
174139
175- private Path prepareEnvironment (Project project , Test testTask , Path basePath , EsTestEnvExtension ext ) {
176- def formatter = DateTimeFormatter . ofPattern(" yyyyMMdd-HH-mm-ss" , Locale . ROOT )
177- def now = Instant . now(). atZone(ZoneId . of(" UTC" ))
178- def timepart = formatter. format(now)
179- def rootPath = basePath. resolve(timepart)
180-
140+ /* * Populates the pre-created environment directory with necessary files for the test run. */
141+ private void populateEnvironment (Project project , Test testTask , Path rootPath , EsTestEnvExtension ext ) {
181142 def pluginDir = rootPath. resolve(" plugins" )
182143 def configPath = rootPath. resolve(" config" )
183144
@@ -204,12 +165,10 @@ class EsTestEnvPlugin implements Plugin<Project> {
204165 Files . createDirectories(sudachiConfigDir)
205166 Files . copy(ext. systemDic, sudachiConfigDir. resolve(" system_core.dic" ))
206167 Files . copy(ext. configFile, sudachiConfigDir. resolve(" sudachi.json" ))
207-
208- return rootPath
209168 }
210169
211- private void cleanupEnvironment (Path envPath ) {
212-
170+ private void cleanupEnvironment (Project project , Path envPath ) {
171+ // project.delete(envPath)
213172 }
214173
215174
@@ -273,5 +232,3 @@ class EsTestEnvPlugin implements Plugin<Project> {
273232 .writeTo(outputStream. newWriter(' utf-8' ))
274233 }
275234}
276-
277-
0 commit comments