]> git.basschouten.com Git - openhab-addons.git/blob
6a091bc551cf3629c961f6cbc093bd412d7d5e9c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.automation.jsscriptingnashorn;
14
15 import static org.junit.jupiter.api.Assertions.assertThrows;
16
17 import java.io.IOException;
18 import java.io.InputStreamReader;
19 import java.net.URL;
20
21 import javax.script.ScriptEngine;
22 import javax.script.ScriptException;
23
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;
30
31 /**
32  * This tests the script modules using the Nashorn scripting engine.
33  *
34  * @author Kai Kreuzer - Initial contribution
35  */
36 @NonNullByDefault
37 public class ScriptScopeOSGiTest extends JavaOSGiTest {
38
39     private @NonNullByDefault({}) ScriptEngine engine;
40
41     private final String path = "OH-INF/automation/jsr223/";
42     private final String workingFile = "scopeWorking.nashornjs";
43     private final String failureFile = "scopeFailure.nashornjs";
44
45     @BeforeEach
46     public void init() {
47         ScriptEngineManager scriptManager = getService(ScriptEngineManager.class);
48         ScriptEngineContainer container = scriptManager.createScriptEngine("nashornjs", "myJSEngine");
49         engine = container.getScriptEngine();
50     }
51
52     @Test
53     public void testScopeDefinesItemTypes() throws ScriptException, IOException {
54         URL url = bundleContext.getBundle().getResource(path + workingFile);
55         engine.eval(new InputStreamReader(url.openStream()));
56     }
57
58     @Test
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())));
62     }
63 }