jaroslav@184
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jaroslav@184
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jaroslav@184
|
5 |
*
|
jaroslav@358
|
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates.
|
jaroslav@358
|
7 |
* Other names may be trademarks of their respective owners.
|
jaroslav@184
|
8 |
*
|
jaroslav@358
|
9 |
* The contents of this file are subject to the terms of either the GNU
|
jaroslav@358
|
10 |
* General Public License Version 2 only ("GPL") or the Common
|
jaroslav@358
|
11 |
* Development and Distribution License("CDDL") (collectively, the
|
jaroslav@358
|
12 |
* "License"). You may not use this file except in compliance with the
|
jaroslav@358
|
13 |
* License. You can obtain a copy of the License at
|
jaroslav@358
|
14 |
* http://www.netbeans.org/cddl-gplv2.html
|
jaroslav@358
|
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
|
jaroslav@358
|
16 |
* specific language governing permissions and limitations under the
|
jaroslav@358
|
17 |
* License. When distributing the software, include this License Header
|
jaroslav@358
|
18 |
* Notice in each file and include the License file at
|
jaroslav@358
|
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
|
jaroslav@358
|
20 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@358
|
21 |
* by Oracle in the GPL Version 2 section of the License file that
|
jaroslav@358
|
22 |
* accompanied this code. If applicable, add the following below the
|
jaroslav@358
|
23 |
* License Header, with the fields enclosed by brackets [] replaced by
|
jaroslav@358
|
24 |
* your own identifying information:
|
jaroslav@358
|
25 |
* "Portions Copyrighted [year] [name of copyright owner]"
|
jaroslav@358
|
26 |
*
|
jaroslav@358
|
27 |
* Contributor(s):
|
jaroslav@358
|
28 |
*
|
jaroslav@358
|
29 |
* The Original Software is NetBeans. The Initial Developer of the Original
|
jaroslav@551
|
30 |
* Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
|
jaroslav@358
|
31 |
*
|
jaroslav@358
|
32 |
* If you wish your version of this file to be governed by only the CDDL
|
jaroslav@358
|
33 |
* or only the GPL Version 2, indicate your decision by adding
|
jaroslav@358
|
34 |
* "[Contributor] elects to include this software in this distribution
|
jaroslav@358
|
35 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a
|
jaroslav@358
|
36 |
* single choice of license, a recipient has the option to distribute
|
jaroslav@358
|
37 |
* your version of this file under either the CDDL, the GPL Version 2 or
|
jaroslav@358
|
38 |
* to extend the choice of license to its licensees as provided above.
|
jaroslav@358
|
39 |
* However, if you add GPL Version 2 code and therefore, elected the GPL
|
jaroslav@358
|
40 |
* Version 2 license, then the option applies only if the new code is
|
jaroslav@358
|
41 |
* made subject to such option by the copyright holder.
|
jaroslav@184
|
42 |
*/
|
jaroslav@362
|
43 |
package org.netbeans.html.boot.impl;
|
jaroslav@184
|
44 |
|
jaroslav@184
|
45 |
import java.io.IOException;
|
jaroslav@187
|
46 |
import java.lang.reflect.Field;
|
jaroslav@186
|
47 |
import java.lang.reflect.Method;
|
jtulach@959
|
48 |
import java.util.List;
|
jtulach@959
|
49 |
import java.util.Locale;
|
jtulach@959
|
50 |
import javax.tools.Diagnostic;
|
jtulach@959
|
51 |
import javax.tools.JavaFileObject;
|
jaroslav@186
|
52 |
import static org.testng.Assert.assertEquals;
|
jaroslav@187
|
53 |
import static org.testng.Assert.assertTrue;
|
jtulach@959
|
54 |
import static org.testng.Assert.fail;
|
jaroslav@184
|
55 |
import org.testng.annotations.Test;
|
jaroslav@184
|
56 |
|
jaroslav@184
|
57 |
/**
|
jaroslav@184
|
58 |
*
|
jtulach@790
|
59 |
* @author Jaroslav Tulach
|
jaroslav@184
|
60 |
*/
|
jaroslav@184
|
61 |
public class JavaScriptProcesorTest {
|
jaroslav@184
|
62 |
|
jaroslav@184
|
63 |
@Test public void detectCallbackToNonExistingClass() throws IOException {
|
jaroslav@184
|
64 |
String code = "package x.y.z;\n"
|
jaroslav@184
|
65 |
+ "import net.java.html.js.JavaScriptBody;\n"
|
jaroslav@184
|
66 |
+ "class X {\n"
|
jaroslav@184
|
67 |
+ " @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
|
jaroslav@184
|
68 |
+ " \"r.@java.lang.Runable::run()();\"\n" // typo
|
jaroslav@184
|
69 |
+ " )\n"
|
jaroslav@184
|
70 |
+ " private static native void callback(Runnable r);\n"
|
jaroslav@184
|
71 |
+ "}\n";
|
jaroslav@184
|
72 |
|
jaroslav@184
|
73 |
Compile c = Compile.create("", code);
|
jaroslav@184
|
74 |
c.assertErrors();
|
jaroslav@184
|
75 |
c.assertError("java.lang.Runable"); // typo
|
jaroslav@184
|
76 |
}
|
jaroslav@185
|
77 |
|
jaroslav@185
|
78 |
@Test public void detectCallbackToNonExistingMethod() throws IOException {
|
jaroslav@185
|
79 |
String code = "package x.y.z;\n"
|
jaroslav@185
|
80 |
+ "import net.java.html.js.JavaScriptBody;\n"
|
jaroslav@185
|
81 |
+ "class X {\n"
|
jaroslav@185
|
82 |
+ " @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
|
jaroslav@185
|
83 |
+ " \"r.@java.lang.Runnable::cancel()();\"\n"
|
jaroslav@185
|
84 |
+ " )\n"
|
jaroslav@185
|
85 |
+ " private static native void callback(Runnable r);\n"
|
jaroslav@185
|
86 |
+ "}\n";
|
jaroslav@185
|
87 |
|
jaroslav@185
|
88 |
Compile c = Compile.create("", code);
|
jaroslav@185
|
89 |
c.assertErrors();
|
jaroslav@185
|
90 |
c.assertError("method cancel");
|
jaroslav@185
|
91 |
}
|
jaroslav@185
|
92 |
|
jaroslav@185
|
93 |
@Test public void detectCallbackToNonExistingParams() throws IOException {
|
jaroslav@185
|
94 |
String code = "package x.y.z;\n"
|
jaroslav@185
|
95 |
+ "import net.java.html.js.JavaScriptBody;\n"
|
jaroslav@185
|
96 |
+ "class X {\n"
|
jaroslav@185
|
97 |
+ " @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
|
jaroslav@185
|
98 |
+ " \"r.@java.lang.Runnable::run(I)(10);\"\n"
|
jaroslav@185
|
99 |
+ " )\n"
|
jaroslav@185
|
100 |
+ " private static native void callback(Runnable r);\n"
|
jaroslav@185
|
101 |
+ "}\n";
|
jaroslav@185
|
102 |
|
jaroslav@185
|
103 |
Compile c = Compile.create("", code);
|
jaroslav@185
|
104 |
c.assertErrors();
|
jaroslav@185
|
105 |
c.assertError("wrong parameters: (I)");
|
jaroslav@185
|
106 |
}
|
jaroslav@185
|
107 |
|
jaroslav@185
|
108 |
@Test public void objectTypeParamsAreOK() throws IOException {
|
jaroslav@185
|
109 |
String code = "package x.y.z;\n"
|
jaroslav@185
|
110 |
+ "import net.java.html.js.JavaScriptBody;\n"
|
jaroslav@185
|
111 |
+ "class X {\n"
|
jaroslav@185
|
112 |
+ " @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
|
jaroslav@185
|
113 |
+ " \"r.@java.lang.Object::equals(Ljava/lang/Object;)(null);\"\n"
|
jaroslav@185
|
114 |
+ " )\n"
|
jaroslav@185
|
115 |
+ " private static native void testEqual(Object r);\n"
|
jaroslav@185
|
116 |
+ "}\n";
|
jaroslav@185
|
117 |
|
jaroslav@185
|
118 |
Compile c = Compile.create("", code);
|
jaroslav@185
|
119 |
c.assertNoErrors();
|
jaroslav@185
|
120 |
}
|
jtulach@959
|
121 |
|
jtulach@959
|
122 |
@Test public void misorderNotified() throws IOException {
|
jtulach@959
|
123 |
String code = "package x.y.z;\n"
|
jtulach@959
|
124 |
+ "import net.java.html.js.JavaScriptBody;\n"
|
jtulach@959
|
125 |
+ "class X {\n"
|
jtulach@959
|
126 |
+ " @JavaScriptBody(args={\"r\", \"a\", \"b\"}, body =\"\"\n"
|
jtulach@959
|
127 |
+ " )\n"
|
jtulach@959
|
128 |
+ " private static native void testEqual(Object p, String q, int r);\n"
|
jtulach@959
|
129 |
+ "}\n";
|
jtulach@959
|
130 |
|
jtulach@959
|
131 |
Compile c = Compile.create("", code);
|
jtulach@959
|
132 |
List<Diagnostic<? extends JavaFileObject>> warnings = c.getDiagnostics(Diagnostic.Kind.WARNING);
|
jtulach@959
|
133 |
assertTrue(warnings.size() >= 1, "There are warnings: " + warnings);
|
jtulach@959
|
134 |
for (Diagnostic<? extends JavaFileObject> w : warnings) {
|
jtulach@959
|
135 |
if (w.getMessage(Locale.US).contains("Actual method parameter names and args")) {
|
jtulach@959
|
136 |
return;
|
jtulach@959
|
137 |
}
|
jtulach@959
|
138 |
}
|
jtulach@959
|
139 |
fail("Expecting order warning: " + warnings);
|
jtulach@959
|
140 |
}
|
jaroslav@531
|
141 |
|
jaroslav@531
|
142 |
@Test public void needJavaScriptBodyToUseResource() throws IOException {
|
jaroslav@531
|
143 |
String code = "package x.y.z;\n"
|
jaroslav@531
|
144 |
+ "import net.java.html.js.JavaScriptResource;\n"
|
jaroslav@531
|
145 |
+ "@JavaScriptResource(\"x.html\")\n"
|
jaroslav@531
|
146 |
+ "class X {\n"
|
jaroslav@531
|
147 |
+ " private static native void callback(Runnable r);\n"
|
jaroslav@531
|
148 |
+ "}\n";
|
jaroslav@531
|
149 |
|
jaroslav@531
|
150 |
Compile c = Compile.create("", code);
|
jaroslav@531
|
151 |
c.assertErrors();
|
jaroslav@531
|
152 |
c.assertError("needs @JavaScriptBody");
|
jaroslav@531
|
153 |
}
|
jaroslav@184
|
154 |
|
jaroslav@186
|
155 |
@Test public void generatesCallbacksThatReturnObject() throws Exception {
|
jaroslav@362
|
156 |
Class<?> callbacksForTestPkg = Class.forName("org.netbeans.html.boot.impl.$JsCallbacks$");
|
jaroslav@194
|
157 |
Method m = callbacksForTestPkg.getDeclaredMethod("java_lang_Runnable$run$", Runnable.class);
|
jtulach@1041
|
158 |
assertEquals(m.getReturnType(), java.lang.Object.class, "All methods always return object");
|
jaroslav@186
|
159 |
}
|
jaroslav@187
|
160 |
|
jaroslav@187
|
161 |
@Test public void hasInstanceField() throws Exception {
|
jaroslav@362
|
162 |
Class<?> callbacksForTestPkg = Class.forName("org.netbeans.html.boot.impl.$JsCallbacks$");
|
jaroslav@187
|
163 |
Field f = callbacksForTestPkg.getDeclaredField("VM");
|
jaroslav@187
|
164 |
f.setAccessible(true);
|
jaroslav@187
|
165 |
assertTrue(callbacksForTestPkg.isInstance(f.get(null)), "Singleton field VM");
|
jaroslav@187
|
166 |
}
|
jaroslav@184
|
167 |
}
|