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.cache.PortalCache;
018    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
019    import com.liferay.portal.kernel.scripting.ScriptingExecutor;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.Serializable;
025    
026    import java.util.Collections;
027    import java.util.HashMap;
028    import java.util.Map;
029    import java.util.Set;
030    
031    import org.junit.After;
032    import org.junit.Before;
033    import org.junit.Test;
034    
035    import org.mockito.Mock;
036    import org.mockito.Mockito;
037    
038    import org.powermock.api.mockito.PowerMockito;
039    import org.powermock.core.classloader.annotations.PrepareForTest;
040    
041    /**
042     * @author Miguel Pastor
043     */
044    @PrepareForTest(SingleVMPoolUtil.class)
045    public abstract class ScriptingExecutorTestCase extends PowerMockito {
046    
047            public abstract String getScriptExtension();
048    
049            public abstract ScriptingExecutor getScriptingExecutor();
050    
051            @Before
052            public void setUp() {
053                    mockStatic(SingleVMPoolUtil.class);
054    
055                    when(
056                            SingleVMPoolUtil.getCache(Mockito.anyString())
057                    ).thenReturn(
058                            _portalCache
059                    );
060    
061                    _scriptingExecutor = getScriptingExecutor();
062            }
063    
064            @After
065            public void tearDown() {
066                    verifyStatic();
067            }
068    
069            @Test
070            public void testBindingInputVariables() throws Exception {
071                    Map<String, Object> inputObjects = new HashMap<>();
072    
073                    inputObjects.put("variable", "string");
074    
075                    Set<String> outputNames = Collections.emptySet();
076    
077                    execute(inputObjects, outputNames, "binding-input");
078            }
079    
080            @Test
081            public void testSimpleScript() throws Exception {
082                    Map<String, Object> inputObjects = Collections.emptyMap();
083                    Set<String> outputNames = Collections.emptySet();
084    
085                    execute(inputObjects, outputNames, "simple");
086            }
087    
088            protected Map<String, Object> execute(
089                            Map<String, Object> inputObjects, Set<String> outputNames,
090                            String fileName)
091                    throws Exception {
092    
093                    String script = getScript(fileName + getScriptExtension());
094    
095                    return _scriptingExecutor.eval(null, inputObjects, outputNames, script);
096            }
097    
098            protected String getScript(String name) throws IOException {
099                    Class<?> clazz = getClass();
100    
101                    InputStream inputStream = clazz.getResourceAsStream(
102                            "dependencies/" + name);
103    
104                    return StringUtil.read(inputStream);
105            }
106    
107            @Mock
108            private PortalCache<Serializable, Object> _portalCache;
109    
110            private ScriptingExecutor _scriptingExecutor;
111    
112    }