001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.scripting;
016    
017    import com.liferay.portal.kernel.scripting.ScriptingExecutor;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.tools.ToolDependencies;
020    
021    import java.io.IOException;
022    import java.io.InputStream;
023    
024    import java.util.Collections;
025    import java.util.HashMap;
026    import java.util.Map;
027    import java.util.Set;
028    
029    import org.junit.Before;
030    import org.junit.BeforeClass;
031    import org.junit.Test;
032    
033    /**
034     * @author Miguel Pastor
035     */
036    public abstract class ScriptingExecutorTestCase {
037    
038            @BeforeClass
039            public static void setUpClass() throws Exception {
040                    ToolDependencies.wireCaches();
041            }
042    
043            public abstract String getScriptExtension();
044    
045            public abstract ScriptingExecutor getScriptingExecutor();
046    
047            @Before
048            public void setUp() {
049                    _scriptingExecutor = getScriptingExecutor();
050            }
051    
052            @Test
053            public void testBindingInputVariables() throws Exception {
054                    Map<String, Object> inputObjects = new HashMap<>();
055    
056                    inputObjects.put("variable", "string");
057    
058                    Set<String> outputNames = Collections.emptySet();
059    
060                    execute(inputObjects, outputNames, "binding-input");
061            }
062    
063            @Test
064            public void testSimpleScript() throws Exception {
065                    Map<String, Object> inputObjects = Collections.emptyMap();
066                    Set<String> outputNames = Collections.emptySet();
067    
068                    execute(inputObjects, outputNames, "simple");
069            }
070    
071            protected Map<String, Object> execute(
072                            Map<String, Object> inputObjects, Set<String> outputNames,
073                            String fileName)
074                    throws Exception {
075    
076                    String script = getScript(fileName + getScriptExtension());
077    
078                    return _scriptingExecutor.eval(null, inputObjects, outputNames, script);
079            }
080    
081            protected String getScript(String name) throws IOException {
082                    Class<?> clazz = getClass();
083    
084                    InputStream inputStream = clazz.getResourceAsStream(
085                            "dependencies/" + name);
086    
087                    return StringUtil.read(inputStream);
088            }
089    
090            private ScriptingExecutor _scriptingExecutor;
091    
092    }