2 * Copyright (c) 2010-2023 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.jsscriptingnashorn;
15 import static org.junit.jupiter.api.Assertions.assertThrows;
17 import java.io.IOException;
18 import java.io.InputStreamReader;
21 import javax.script.ScriptEngine;
22 import javax.script.ScriptException;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.core.automation.module.script.ScriptEngineContainer;
28 import org.openhab.core.automation.module.script.ScriptEngineManager;
29 import org.openhab.core.test.java.JavaOSGiTest;
32 * This tests the script modules using the Nashorn scripting engine.
34 * @author Kai Kreuzer - Initial contribution
37 public class ScriptScopeOSGiTest extends JavaOSGiTest {
39 private @NonNullByDefault({}) ScriptEngine engine;
41 private final String path = "OH-INF/automation/jsr223/";
42 private final String workingFile = "scopeWorking.nashornjs";
43 private final String failureFile = "scopeFailure.nashornjs";
47 ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
48 ScriptEngineContainer container = scriptManager.createScriptEngine("nashornjs", "myJSEngine");
49 engine = container.getScriptEngine();
53 public void testScopeDefinesItemTypes() throws ScriptException, IOException {
54 URL url = bundleContext.getBundle().getResource(path + workingFile);
55 engine.eval(new InputStreamReader(url.openStream()));
59 public void testScopeDoesNotDefineFoobar() throws ScriptException, IOException {
60 URL url = bundleContext.getBundle().getResource(path + failureFile);
61 assertThrows(ScriptException.class, () -> engine.eval(new InputStreamReader(url.openStream())));