< prev index next >

test/src/jdk/nashorn/api/scripting/test/ScopeTest.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.api.scripting.test;
  26 
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertNotNull;

  29 import static org.testng.Assert.assertTrue;
  30 import static org.testng.Assert.fail;
  31 import javax.script.Bindings;
  32 import javax.script.ScriptContext;
  33 import javax.script.ScriptEngine;
  34 import javax.script.ScriptEngineFactory;
  35 import javax.script.ScriptEngineManager;
  36 import javax.script.ScriptException;
  37 import javax.script.SimpleBindings;
  38 import javax.script.SimpleScriptContext;
  39 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
  40 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  41 import jdk.nashorn.api.scripting.URLReader;
  42 import org.testng.Assert;
  43 import org.testng.annotations.Test;
  44 
  45 /**
  46  * Tests for jsr223 Bindings "scope" (engine, global scopes)
  47  */
  48 @SuppressWarnings("javadoc")


 802         }
 803 
 804         public void method() throws ScriptException {
 805             // a context with a new global bindings, same engine bindings
 806             final ScriptContext sc = new SimpleScriptContext();
 807             final Bindings global = new SimpleBindings();
 808             sc.setBindings(global, ScriptContext.GLOBAL_SCOPE);
 809             sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
 810             global.put("text", "methodText");
 811             String value = engine.eval("text", sc).toString();
 812             Assert.assertEquals(value, "methodText");
 813         }
 814     }
 815 
 816     // @bug 8081609: engine.eval call from a java method which
 817     // was called from a previous engine.eval results in wrong
 818     // ScriptContext being used.
 819     @Test
 820     public void recursiveEvalCallScriptContextTest() throws ScriptException {
 821         new RecursiveEval().program();


































 822     }
 823 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.api.scripting.test;
  26 
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertNotNull;
  29 import static org.testng.Assert.assertFalse;
  30 import static org.testng.Assert.assertTrue;
  31 import static org.testng.Assert.fail;
  32 import javax.script.Bindings;
  33 import javax.script.ScriptContext;
  34 import javax.script.ScriptEngine;
  35 import javax.script.ScriptEngineFactory;
  36 import javax.script.ScriptEngineManager;
  37 import javax.script.ScriptException;
  38 import javax.script.SimpleBindings;
  39 import javax.script.SimpleScriptContext;
  40 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
  41 import jdk.nashorn.api.scripting.ScriptObjectMirror;
  42 import jdk.nashorn.api.scripting.URLReader;
  43 import org.testng.Assert;
  44 import org.testng.annotations.Test;
  45 
  46 /**
  47  * Tests for jsr223 Bindings "scope" (engine, global scopes)
  48  */
  49 @SuppressWarnings("javadoc")


 803         }
 804 
 805         public void method() throws ScriptException {
 806             // a context with a new global bindings, same engine bindings
 807             final ScriptContext sc = new SimpleScriptContext();
 808             final Bindings global = new SimpleBindings();
 809             sc.setBindings(global, ScriptContext.GLOBAL_SCOPE);
 810             sc.setBindings(engineBindings, ScriptContext.ENGINE_SCOPE);
 811             global.put("text", "methodText");
 812             String value = engine.eval("text", sc).toString();
 813             Assert.assertEquals(value, "methodText");
 814         }
 815     }
 816 
 817     // @bug 8081609: engine.eval call from a java method which
 818     // was called from a previous engine.eval results in wrong
 819     // ScriptContext being used.
 820     @Test
 821     public void recursiveEvalCallScriptContextTest() throws ScriptException {
 822         new RecursiveEval().program();
 823     }
 824 
 825     private static final String VAR_NAME = "myvar";
 826 
 827     private static boolean lookupVar(final ScriptEngine engine, final String varName) {
 828         try {
 829             engine.eval(varName);
 830             return true;
 831         } catch (final ScriptException se) {
 832             return false;
 833         }
 834     }
 835 
 836     // @bug 8136544: Call site switching to megamorphic causes incorrect property read
 837     @Test
 838     public void megamorphicPropertyReadTest() throws ScriptException {
 839         final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
 840         final ScriptEngine engine = factory.getScriptEngine();
 841         final Bindings scope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
 842         boolean ret;
 843 
 844         // Why 16 is the upper limit of this loop? The default nashorn dynalink megamorphic threshold is 16.
 845         // See jdk.nashorn.internal.runtime.linker.Bootstrap.NASHORN_DEFAULT_UNSTABLE_RELINK_THRESHOLD
 846         // We do, 'eval' of the same in this loop twice. So, 16*2 = 32 times that callsite in the script
 847         // is exercised - much beyond the default megamorphic threshold.
 848 
 849         for (int i = 0; i < 16; i++) {
 850             scope.remove(VAR_NAME);
 851             ret = lookupVar(engine, VAR_NAME);
 852             assertFalse(ret, "Expected false in iteration " + i);
 853             scope.put(VAR_NAME, "foo");
 854             ret = lookupVar(engine, VAR_NAME);
 855             assertTrue(ret, "Expected true in iteration " + i);
 856         }
 857     }
 858 }
< prev index next >