[groovyscripting] Update Groovy to 4.0.9 and support slurpers (#14499)

Updates Groovy from 4.0.7 to 4.0.9.

For release notes, see:

https://groovy-lang.org/changelogs/changelog-4.0.8.html
https://groovy-lang.org/changelogs/changelog-4.0.9.html

Adds dependencies and service loader config so JSON, XML and YAML can be more easily parsed using the JsonSlurper, XmlSlurper and YamlSlurper.

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2023-03-11 21:15:06 +01:00
committed by GitHub
parent ac7993d329
commit 59b3ed33df
11 changed files with 292 additions and 4 deletions

View File

@@ -0,0 +1,67 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.automation.groovyscripting;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openhab.core.automation.module.script.ScriptEngineContainer;
import org.openhab.core.automation.module.script.ScriptEngineManager;
import org.openhab.core.test.java.JavaOSGiTest;
/**
* This tests the JSON, XML and YAML slurpers using the Groovy scripting engine.
*
* @author Wouter Born - Initial contribution
*/
@NonNullByDefault
public class SlurperOSGiTest extends JavaOSGiTest {
private @NonNullByDefault({}) ScriptEngine engine;
private final String path = "OH-INF/automation/jsr223/";
@BeforeEach
public void init() {
ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
ScriptEngineContainer container = scriptManager.createScriptEngine("groovy", "myGroovyEngine");
engine = container.getScriptEngine();
}
private void evalScript(String fileName) throws ScriptException, IOException {
URL url = bundleContext.getBundle().getResource(path + fileName);
engine.eval(new InputStreamReader(url.openStream()));
}
@Test
public void jsonSlurper() throws ScriptException, IOException {
evalScript("json-slurper.groovy");
}
@Test
public void xmlSlurper() throws ScriptException, IOException {
evalScript("xml-slurper.groovy");
}
@Test
public void yamlSlurper() throws ScriptException, IOException {
evalScript("yaml-slurper.groovy");
}
}

View File

@@ -0,0 +1,22 @@
import static org.hamcrest.CoreMatchers.*
import static org.hamcrest.MatcherAssert.assertThat
import groovy.json.JsonSlurper
def json = '''\
{
"person": {
"name": "John",
"age": 10,
"pets": ["cat", "dog"]
}
}
'''
def result = new JsonSlurper().parseText(json)
assertThat(result.person.name, is("John"))
assertThat(result.person.age, is(10))
assertThat(result.person.pets.size(), is(2))
assertThat(result.person.pets[0], is("cat"))
assertThat(result.person.pets[1], is("dog"))

View File

@@ -0,0 +1,23 @@
import static org.hamcrest.CoreMatchers.*
import static org.hamcrest.MatcherAssert.assertThat
import groovy.xml.XmlSlurper
def xml = '''\
<root>
<person>
<name>Brigitte</name>
<age>34</age>
<pets>bird</pets>
<pets>fish</pets>
</person>
</root>
'''
def result = new XmlSlurper().parseText(xml)
assertThat(result.person.name, is("Brigitte"))
assertThat(result.person.age, is(34))
assertThat(result.person.pets.size(), is(2))
assertThat(result.person.pets[0], is("bird"))
assertThat(result.person.pets[1], is("fish"))

View File

@@ -0,0 +1,21 @@
import static org.hamcrest.CoreMatchers.*
import static org.hamcrest.MatcherAssert.assertThat
import groovy.yaml.YamlSlurper
def yaml = '''\
person:
name: "Itsuki"
age: 78
pets:
- rabbit
- snake
'''
def result = new YamlSlurper().parseText(yaml)
assertThat(result.person.name, is("Itsuki"))
assertThat(result.person.age, is(78))
assertThat(result.person.pets.size(), is(2))
assertThat(result.person.pets[0], is("rabbit"))
assertThat(result.person.pets[1], is("snake"))