Skip to content

Commit 4690b1b

Browse files
committed
clean up, refactor, restructure, and fix unit tests
1 parent 908fb9e commit 4690b1b

20 files changed

+185
-236
lines changed

build.gradle

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def deriveJosmCompileVersion(releases) {
6767
? project.property("plugin.josmCompileVersion")
6868
: null
6969
def josmCompileVersion
70+
//noinspection GroovyFallthrough
7071
switch (value) {
7172
case null:
7273
josmCompileVersion = "latest"
@@ -159,11 +160,13 @@ File josmHomeForTests() {
159160
return new File(project.projectDir, "build/josm.home")
160161
}
161162

163+
/**
164+
* Run unit tests that only depend on the JDK, the plugin classes,
165+
* and Mozilla Rhino. Runs on the current stock Java VM and JDK.
166+
*/
162167
test {
163168
useJUnitPlatform()
164169
environment "JOSM_SCRIPTING_PLUGIN_HOME", project.projectDir
165-
environment "TEST_COMMONJS_MODULE_REPO",
166-
"${project.projectDir}/src/test/resources/require"
167170
environment "BUILTIN_COMMONJS_MODULE_REPO",
168171
"${project.projectDir}/src/main/javascript/v2"
169172
systemProperty "josm.home", josmHomeForTests().absolutePath
@@ -175,6 +178,10 @@ test {
175178
testLogging.events TestLogEvent.FAILED, TestLogEvent.PASSED
176179
}
177180

181+
/**
182+
* Runs the API V1 JavaScript unit tests with JOSM and Mozilla
183+
* Rhino present.
184+
*/
178185
task testScriptApiV1 (type: Test) {
179186
useJUnitPlatform()
180187
environment "JOSM_SCRIPTING_PLUGIN_HOME", project.projectDir
@@ -192,30 +199,39 @@ def getGraalVMHome() {
192199
}
193200
final version = project.property("graalvm.version")
194201
if (!project.hasProperty("graalvm.jdk")) {
195-
throw new GradleException("Missing mandatory project property 'jdk'")
202+
throw new GradleException("Missing mandatory project property 'graalvm.jdk'")
196203
}
197204
final jdk = project.property("graalvm.jdk")
198205
return new File(project.projectDir, "software/graalvm-ce-$jdk-$version")
199206
}
200207

208+
/**
209+
* Runs the API V2 JavaScript unit tests with JOSM and GraalJS
210+
* present. The tests are run with with a GraalVM 'java' binary.
211+
*/
201212
task testScriptApiV2(type: Test) {
202213
dependsOn "downloadGraalVM"
203214
useJUnitPlatform()
204-
testLogging {
205-
outputs.upToDateWhen {false}
206-
showStandardStreams = true
207-
}
208215
environment "JOSM_SCRIPTING_PLUGIN_HOME", project.projectDir
216+
environment "TEST_COMMONJS_MODULE_REPO",
217+
"${project.projectDir}/src/test/resources/require/modules"
209218
executable new File(getGraalVMHome(), "bin/java").absolutePath
210219
environment "JAVA_HOME", getGraalVMHome().absolutePath
211220
scanForTestClasses= false
212221
include "org/openstreetmap/josm/plugins/scripting/graalvm/api/*.class"
213222
testLogging {
223+
outputs.upToDateWhen {false}
224+
showStandardStreams = true
214225
events TestLogEvent.FAILED, TestLogEvent.PASSED
215226
exceptionFormat = TestExceptionFormat.FULL
216227
}
217228
}
218229

230+
/**
231+
* Runs tests for functionality that tests whether GraalJS is
232+
* present or not. Runs with the current stock Java VM and JDK and makes
233+
* sure that GraalJS/GraalVM isn't present on the classpath.
234+
*/
219235
task testGraalVMSupportNotPresent(type: Test) {
220236
// remove GraalVM from classpath under test
221237
classpath = classpath.filter {! it.toString().toLowerCase().contains("graalvm")}
@@ -228,54 +244,47 @@ task testGraalVMSupportNotPresent(type: Test) {
228244
}
229245
}
230246

247+
/**
248+
* Run unit tests for GraalJS functionality. Run it with the GraalVM java
249+
* executable.
250+
*/
231251
import java.nio.file.Paths
232252
import java.nio.file.StandardCopyOption
233-
task testGraalVMSupportPresent(type: Test) {
253+
task testWithGraalVMJDK(type: Test) {
254+
dependsOn "downloadGraalVM"
234255
doFirst {
235256
// prepare a suitable 'preferences.xml' for the test cases in this
236257
// executed by this task
237258
def josmHome = josmHomeForTests()
238259
josmHome.mkdirs()
239260
Files.copy(
240261
Paths.get("${project.projectDir}/src/test/resources/josm-test-environments/env01/preferences.xml"),
241-
Paths.get("${josmHome.absolutePath}preferences.xml"),
262+
Paths.get("${josmHome.absolutePath}/preferences.xml"),
242263
StandardCopyOption.REPLACE_EXISTING
243264
)
244-
println("testGraalVMSupportPresent: prepared preferences.xml")
245265
}
246266
useJUnitPlatform()
247-
environment "JOSM_SCRIPTING_PLUGIN_HOME",project.projectDir
267+
environment "JOSM_SCRIPTING_PLUGIN_HOME", project.projectDir
268+
environment "TEST_COMMONJS_MODULE_REPO",
269+
"${project.projectDir}/src/test/resources/require/modules"
248270
systemProperty "josm.home", josmHomeForTests().absolutePath
249-
scanForTestClasses= false
250-
include "**/graalvm/GraalVMPresentTest.class"
251-
include "**/graalvm/GraalVMEvalTest.class"
252-
include "**/graalvm/GraalVMEmbeddedInJOSMTest.class"
253-
include "**/graalvm/GraalVMAndRequireTest.class"
254-
testLogging {
255-
events TestLogEvent.FAILED, TestLogEvent.PASSED
256-
exceptionFormat = TestExceptionFormat.FULL
257-
}
258-
}
259271

260-
task testWithGraalVMJDK(type: Test) {
261-
dependsOn "downloadGraalVM"
262-
useJUnitPlatform()
263-
environment "JOSM_SCRIPTING_PLUGIN_HOME", project.projectDir
264272
scanForTestClasses= false
273+
// configuration to use GraalVM
265274
executable new File(getGraalVMHome(), "bin/java").absolutePath
266275
environment "JAVA_HOME", getGraalVMHome()
267-
268-
include "**/graalvm/GraalVMPresentTest.class"
269-
include "**/graalvm/GraalVMEvalTest.class"
270-
include "**/graalvm/GraalVMEmbeddedInJOSMTest.class"
271-
include "**/graalvm/GraalVMAndRequireTest.class"
272-
include "**/graalvm/ScriptingAPIV2Test.class"
276+
scanForTestClasses= false
277+
include "org/openstreetmap/josm/plugins/scripting/graalvm/with_graalvm/*.class"
273278
testLogging {
274279
events TestLogEvent.FAILED, TestLogEvent.PASSED
275280
exceptionFormat = TestExceptionFormat.FULL
276281
}
277282
}
278283

284+
/**
285+
* Run tests on a stock JDK with the groovy and python scripting
286+
* engines present.
287+
*/
279288
task testJSR223EnginePresent(type: Test) {
280289
useJUnitPlatform()
281290

@@ -291,7 +300,6 @@ task testJSR223EnginePresent(type: Test) {
291300
tasks.check.dependsOn(tasks.testScriptApiV1)
292301
tasks.check.dependsOn(tasks.testScriptApiV2)
293302
tasks.check.dependsOn(tasks.testGraalVMSupportNotPresent)
294-
tasks.check.dependsOn(tasks.testGraalVMSupportPresent)
295303
tasks.check.dependsOn(tasks.testJSR223EnginePresent)
296304
tasks.check.dependsOn(tasks.testWithGraalVMJDK)
297305

src/main/java/org/openstreetmap/josm/plugins/scripting/graalvm/CommonJSModuleRepositoryRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ public void clear() {
199199
}
200200

201201
private void logFine(Supplier<String> messageBuilder) {
202-
if (logger.isLoggable(Level.FINE)) {
202+
if (logger.isLoggable(Level.INFO)) {
203203
final String message = messageBuilder.get();
204-
logger.log(Level.FINE, message);
204+
logger.log(Level.INFO, message);
205205
}
206206
}
207207

src/main/java/org/openstreetmap/josm/plugins/scripting/graalvm/FileSystemJSModuleRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected boolean isRepoFile(String repoPath) {
3838
* @param baseDir the base directory. Must not be null. Must not
3939
* be empty.
4040
*/
41-
FileSystemJSModuleRepository(@NotNull String baseDir) {
41+
public FileSystemJSModuleRepository(@NotNull String baseDir) {
4242
Objects.requireNonNull(baseDir);
4343
this.baseDir = new File(baseDir).getAbsoluteFile();
4444
}
@@ -49,7 +49,7 @@ protected boolean isRepoFile(String repoPath) {
4949
*
5050
* @param baseDir the base directory. Must not be null.
5151
*/
52-
FileSystemJSModuleRepository(@NotNull File baseDir) {
52+
public FileSystemJSModuleRepository(@NotNull File baseDir) {
5353
Objects.requireNonNull(baseDir);
5454
this.baseDir = baseDir.getAbsoluteFile();
5555
}

src/main/java/org/openstreetmap/josm/plugins/scripting/graalvm/GraalVMFacade.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,16 @@ public class GraalVMFacade implements IGraalVMFacade {
2727

2828
private Context context;
2929

30-
private void populateContext(final Context context) {
30+
/**
31+
* Initializes a GraalVM context with the standard bindings.
32+
*
33+
* Can be used in test cases to properly initialize a
34+
* GraalVM context for testing.
35+
*
36+
* @param context the context
37+
*/
38+
static public void populateContext(@NotNull final Context context) {
39+
Objects.requireNonNull(context);
3140
final Value bindings = context.getBindings("js");
3241

3342
// populate the context with the require function
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.message = "hello-world"

src/test/unit/groovy/org/openstreetmap/josm/plugins/scripting/graalvm/AbstractGraalVMBasedTest.groovy

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openstreetmap.josm.plugins.scripting.graalvm
22

3-
3+
import org.junit.jupiter.api.AfterEach
4+
import org.junit.jupiter.api.BeforeAll
45
import org.junit.jupiter.api.BeforeEach
56
import org.openstreetmap.josm.plugins.PluginException
67
import org.openstreetmap.josm.plugins.PluginInformation
@@ -17,18 +18,37 @@ class AbstractGraalVMBasedTest extends JOSMFixtureBasedTest {
1718
desc.getLanguageName().filter{it == "JavaScript"}
1819
}
1920

20-
@BeforeEach
21-
void setup() throws PluginException, IOException {
22-
def projectDir = getProjectHome()
21+
static ICommonJSModuleRepository moduleRepo
22+
23+
@BeforeAll
24+
static void readEnvironmentVariables() {
25+
final moduleRepoPath = System.getenv("TEST_COMMONJS_MODULE_REPO")
26+
if (moduleRepoPath == null) {
27+
fail("environment variable TEST_COMMONJS_MODULE_REPO not set")
28+
}
29+
final dir = new File(moduleRepoPath)
30+
if (!dir.isDirectory() || !dir.canRead()) {
31+
fail("directory '$dir.absolutePath' with CommonJS modules doesn't exist or isn't readable")
32+
}
33+
moduleRepo = new FileSystemJSModuleRepository(moduleRepoPath)
34+
}
2335

36+
@BeforeEach
37+
void resetRepositoryRegistry() {
2438
// Initialize the CommonJS module repositories
2539
def registry = CommonJSModuleRepositoryRegistry.getInstance()
2640
registry.setBuiltInRepository( new JarJSModuleRepository(scriptingJarFile, "/js/v2"))
2741
registry.addUserDefinedRepository(
28-
new FileSystemJSModuleRepository(
29-
new File(projectDir, "src/test/unit/javascript/v2")
30-
)
42+
new FileSystemJSModuleRepository(
43+
new File(getProjectHome(), "src/test/unit/javascript/v2")
44+
)
3145
)
46+
CommonJSModuleRepositoryRegistry.instance.clear()
47+
CommonJSModuleRepositoryRegistry.instance.addUserDefinedRepository(moduleRepo)
48+
}
49+
50+
@BeforeEach
51+
void setup() throws PluginException, IOException {
3252
//noinspection GroovyResultOfObjectAllocationIgnored
3353
new ScriptingPlugin(new PluginInformation(scriptingJarFile), true /* in test environment */)
3454

@@ -38,4 +58,18 @@ class AbstractGraalVMBasedTest extends JOSMFixtureBasedTest {
3858
Logging.getLogger().setFilter(
3959
record -> record.getLevel().intValue() >= Level.WARNING.intValue())
4060
}
61+
62+
protected IGraalVMFacade facade
63+
64+
@BeforeEach
65+
void initGraalVMFacade() {
66+
facade = GraalVMFacadeFactory.getOrCreateGraalVMFacade()
67+
}
68+
69+
@AfterEach
70+
void resetGraalVMFacade() {
71+
if (facade != null) {
72+
facade.resetContext()
73+
}
74+
}
4175
}

src/test/unit/groovy/org/openstreetmap/josm/plugins/scripting/graalvm/CommonJSModuleCacheTest.groovy

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.openstreetmap.josm.plugins.scripting.graalvm
22

3+
import groovy.test.GroovyTestCase
34
import org.graalvm.polyglot.Context
45
import org.graalvm.polyglot.Value
56
import org.junit.jupiter.api.Test
67

7-
import static org.junit.Assert.assertNotNull
8-
import static org.junit.Assert.assertTrue
9-
import static org.junit.Assert.assertFalse
10-
import static org.junit.Assert.assertEquals
11-
12-
class CommonJSModuleCacheTest {
8+
class CommonJSModuleCacheTest extends GroovyTestCase {
139

1410
@Test
1511
void "singleton should be available"() {

src/test/unit/groovy/org/openstreetmap/josm/plugins/scripting/graalvm/CommonJSModuleRepositoryFactoryTest.groovy

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.openstreetmap.josm.plugins.scripting.graalvm
22

3+
import groovy.test.GroovyTestCase
34
import org.junit.jupiter.api.BeforeAll
45
import org.junit.jupiter.api.Test
56

6-
import static org.junit.Assert.assertTrue
7-
8-
class CommonJSModuleRepositoryFactoryTest {
7+
class CommonJSModuleRepositoryFactoryTest extends GroovyTestCase{
98

109
static def projectHome
1110

src/test/unit/groovy/org/openstreetmap/josm/plugins/scripting/graalvm/GraalVMAndRequireTest.groovy

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/test/unit/groovy/org/openstreetmap/josm/plugins/scripting/graalvm/GraalVMEvalTest.groovy

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)