Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/tests/CustomSetterAcceptNullScriptableTest.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 package org.mozilla.javascript.tests;
6
7 import java.lang.reflect.Method;
8
9 import junit.framework.TestCase;
10
11 import org.mozilla.javascript.Context;
12 import org.mozilla.javascript.ContextFactory;
13 import org.mozilla.javascript.Scriptable;
14 import org.mozilla.javascript.ScriptableObject;
15
16 /**
17 * Takes care that it's possible to set <code>null</code> value
18 * when using custom setter for a {@link Scriptable} object.
19 * See https://bugzilla.mozilla.org/show_bug.cgi?id=461138
20 * @author Marc Guillemot
21 */
22 public class CustomSetterAcceptNullScriptableTest extends TestCase
23 {
24 public static class Foo extends ScriptableObject {
25 private static final long serialVersionUID = -8771045033217033529L;
26
27 @Override
28 public String getClassName()
29 {
30 return "Foo";
31 }
32
33 public void setMyProp(final Foo2 s) { }
34 }
35
36 public static class Foo2 extends ScriptableObject {
37 private static final long serialVersionUID = -8880603824656138628L;
38
39 @Override
40 public String getClassName()
41 {
42 return "Foo2";
43 }
44 }
45
46 public void testSetNullForScriptableSetter() throws Exception {
47
48 final String scriptCode = "foo.myProp = new Foo2();\n"
49 + "foo.myProp = null;";
50
51 final ContextFactory factory = new ContextFactory();
52 final Context cx = factory.enterContext();
53
54 try {
55 final ScriptableObject topScope = cx.initStandardObjects();
56 final Foo foo = new Foo();
57
58 // define custom setter method
59 final Method setMyPropMethod = Foo.class.getMethod("setMyProp", Foo2.class);
60 foo.defineProperty("myProp", null, null, setMyPropMethod, ScriptableObject.EMPTY);
61
62 topScope.put("foo", topScope, foo);
63
64 ScriptableObject.defineClass(topScope, Foo2.class);
65
66 cx.evaluateString(topScope, scriptCode, "myScript", 1, null);
67 }
68 finally {
69 Context.exit();
70 }
71 }
72 }