Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/Bug421071Test.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /*
6 * @(#)Bug421071Test.java
7 *
8 */
9
10 package org.mozilla.javascript.tests;
11
12 import junit.framework.TestCase;
13 import org.mozilla.javascript.Context;
14 import org.mozilla.javascript.ContextFactory;
15 import org.mozilla.javascript.ImporterTopLevel;
16 import org.mozilla.javascript.Script;
17 import org.mozilla.javascript.Scriptable;
18
19 public class Bug421071Test extends TestCase {
20 private ContextFactory factory;
21 private TopLevelScope globalScope;
22 private Script testScript;
23
24 public void testProblemReplicator() throws Exception {
25 // before debugging please put the breakpoint in the
26 // NativeJavaPackage.getPkgProperty()
27 // and observe names passed in there
28 testScript = compileScript();
29 runTestScript(); // this one does not get to the
30 // NativeJavaPackage.getPkgProperty() on my
31 // variables
32 runTestScript(); // however this one does
33 }
34
35 private Script compileScript() {
36 String scriptSource = "importPackage(java.util);\n"
37 + "var searchmon = 3;\n"
38 + "var searchday = 10;\n"
39 + "var searchyear = 2008;\n"
40 + "var searchwkday = 0;\n"
41 + "\n"
42 + "var myDate = Calendar.getInstance();\n // this is a java.util.Calendar"
43 + "myDate.set(Calendar.MONTH, searchmon);\n"
44 + "myDate.set(Calendar.DATE, searchday);\n"
45 + "myDate.set(Calendar.YEAR, searchyear);\n"
46 + "searchwkday.value = myDate.get(Calendar.DAY_OF_WEEK);";
47 Script script;
48 Context context = factory.enterContext();
49 try {
50 script = context.compileString(scriptSource, "testScript", 1, null);
51 return script;
52 } finally {
53 Context.exit();
54 }
55 }
56
57 private void runTestScript() throws InterruptedException {
58 // will start new thread to get as close as possible to original
59 // environment, however the same behavior is exposed using new
60 // ScriptRunner(script).run();
61 Thread thread = new Thread(new ScriptRunner(testScript));
62 thread.start();
63 thread.join();
64 }
65
66 static class DynamicScopeContextFactory extends ContextFactory {
67 @Override
68 public boolean hasFeature(Context cx, int featureIndex) {
69 if (featureIndex == Context.FEATURE_DYNAMIC_SCOPE)
70 return true;
71 return super.hasFeature(cx, featureIndex);
72 }
73 }
74
75 private TopLevelScope createGlobalScope() {
76 factory = new DynamicScopeContextFactory();
77 Context context = factory.enterContext();
78 // noinspection deprecation
79 TopLevelScope globalScope = new TopLevelScope(context);
80 Context.exit();
81 return globalScope;
82 }
83
84 @Override
85 protected void setUp() throws Exception {
86 super.setUp();
87 globalScope = createGlobalScope();
88 }
89
90 private class TopLevelScope extends ImporterTopLevel {
91 private static final long serialVersionUID = 7831526694313927899L;
92
93 public TopLevelScope(Context context) {
94 super(context);
95 }
96 }
97
98 private class ScriptRunner implements Runnable {
99 private Script script;
100
101 public ScriptRunner(Script script) {
102 this.script = script;
103 }
104
105 public void run() {
106 Context context = factory.enterContext();
107 try {
108 // Run each script in its own scope, to keep global variables
109 // defined in each script separate
110 Scriptable threadScope = context.newObject(globalScope);
111 threadScope.setPrototype(globalScope);
112 threadScope.setParentScope(null);
113 script.exec(context, threadScope);
114 } catch (Exception ee) {
115 ee.printStackTrace();
116 } finally {
117 Context.exit();
118 }
119 }
120 }
121 }