2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.automation.groovyscripting;
15 import java.io.IOException;
16 import java.io.InputStreamReader;
19 import javax.script.ScriptEngine;
20 import javax.script.ScriptException;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.core.automation.module.script.ScriptEngineContainer;
26 import org.openhab.core.automation.module.script.ScriptEngineManager;
27 import org.openhab.core.test.java.JavaOSGiTest;
30 * This tests the JSON, XML and YAML slurpers using the Groovy scripting engine.
32 * @author Wouter Born - Initial contribution
35 public class SlurperOSGiTest extends JavaOSGiTest {
37 private @NonNullByDefault({}) ScriptEngine engine;
39 private final String path = "OH-INF/automation/jsr223/";
43 ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
44 ScriptEngineContainer container = scriptManager.createScriptEngine("groovy", "myGroovyEngine");
45 engine = container.getScriptEngine();
48 private void evalScript(String fileName) throws ScriptException, IOException {
49 URL url = bundleContext.getBundle().getResource(path + fileName);
50 engine.eval(new InputStreamReader(url.openStream()));
54 public void jsonSlurper() throws ScriptException, IOException {
55 evalScript("json-slurper.groovy");
59 public void xmlSlurper() throws ScriptException, IOException {
60 evalScript("xml-slurper.groovy");
64 public void yamlSlurper() throws ScriptException, IOException {
65 evalScript("yaml-slurper.groovy");