jtulach@3
|
1 |
/**
|
jaroslav@358
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
jtulach@3
|
3 |
*
|
jaroslav@551
|
4 |
* Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
|
jtulach@3
|
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.
|
jtulach@3
|
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.
|
jtulach@3
|
42 |
*/
|
jtulach@3
|
43 |
package net.java.html.json;
|
jtulach@3
|
44 |
|
jtulach@3
|
45 |
import java.io.IOException;
|
jtulach@3
|
46 |
import java.util.Locale;
|
jtulach@3
|
47 |
import javax.tools.Diagnostic;
|
jtulach@3
|
48 |
import javax.tools.JavaFileObject;
|
jtulach@3
|
49 |
import static org.testng.Assert.*;
|
jtulach@3
|
50 |
import org.testng.annotations.Test;
|
jtulach@3
|
51 |
|
jtulach@3
|
52 |
/** Verify errors emitted by the processor.
|
jtulach@3
|
53 |
*
|
jtulach@3
|
54 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jtulach@3
|
55 |
*/
|
jtulach@3
|
56 |
public class ModelProcessorTest {
|
jtulach@3
|
57 |
@Test public void verifyWrongType() throws IOException {
|
jtulach@3
|
58 |
String html = "<html><body>"
|
jtulach@3
|
59 |
+ "</body></html>";
|
jtulach@3
|
60 |
String code = "package x.y.z;\n"
|
jtulach@3
|
61 |
+ "import net.java.html.json.Model;\n"
|
jtulach@3
|
62 |
+ "import net.java.html.json.Property;\n"
|
jtulach@3
|
63 |
+ "@Model(className=\"XModel\", properties={\n"
|
jtulach@3
|
64 |
+ " @Property(name=\"prop\", type=Runnable.class)\n"
|
jtulach@3
|
65 |
+ "})\n"
|
jtulach@3
|
66 |
+ "class X {\n"
|
jtulach@3
|
67 |
+ "}\n";
|
jtulach@3
|
68 |
|
jtulach@3
|
69 |
Compile c = Compile.create(html, code);
|
jtulach@3
|
70 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jtulach@3
|
71 |
boolean ok = false;
|
jtulach@3
|
72 |
StringBuilder msgs = new StringBuilder();
|
jtulach@3
|
73 |
for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
|
jtulach@3
|
74 |
String msg = e.getMessage(Locale.ENGLISH);
|
jaroslav@68
|
75 |
if (msg.contains("Runnable")) {
|
jtulach@3
|
76 |
ok = true;
|
jtulach@3
|
77 |
}
|
jtulach@3
|
78 |
msgs.append("\n").append(msg);
|
jtulach@3
|
79 |
}
|
jtulach@3
|
80 |
if (!ok) {
|
jtulach@3
|
81 |
fail("Should contain warning about Runnable:" + msgs);
|
jtulach@3
|
82 |
}
|
jtulach@3
|
83 |
}
|
jtulach@3
|
84 |
|
jaroslav@227
|
85 |
@Test public void warnOnNonStatic() throws IOException {
|
jaroslav@227
|
86 |
String html = "<html><body>"
|
jaroslav@227
|
87 |
+ "</body></html>";
|
jaroslav@227
|
88 |
String code = "package x.y.z;\n"
|
jaroslav@227
|
89 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@227
|
90 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@227
|
91 |
+ "import net.java.html.json.ComputedProperty;\n"
|
jaroslav@227
|
92 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@227
|
93 |
+ " @Property(name=\"prop\", type=int.class)\n"
|
jaroslav@227
|
94 |
+ "})\n"
|
jaroslav@227
|
95 |
+ "class X {\n"
|
jaroslav@227
|
96 |
+ " @ComputedProperty int y(int prop) {\n"
|
jaroslav@227
|
97 |
+ " return prop;\n"
|
jaroslav@227
|
98 |
+ " }\n"
|
jaroslav@227
|
99 |
+ "}\n";
|
jaroslav@227
|
100 |
|
jaroslav@227
|
101 |
Compile c = Compile.create(html, code);
|
jaroslav@227
|
102 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jaroslav@227
|
103 |
boolean ok = false;
|
jaroslav@227
|
104 |
StringBuilder msgs = new StringBuilder();
|
jaroslav@227
|
105 |
for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
|
jaroslav@227
|
106 |
String msg = e.getMessage(Locale.ENGLISH);
|
jaroslav@227
|
107 |
if (msg.contains("y has to be static")) {
|
jaroslav@227
|
108 |
ok = true;
|
jaroslav@227
|
109 |
}
|
jaroslav@227
|
110 |
msgs.append("\n").append(msg);
|
jaroslav@227
|
111 |
}
|
jaroslav@227
|
112 |
if (!ok) {
|
jaroslav@227
|
113 |
fail("Should contain warning about non-static method:" + msgs);
|
jaroslav@227
|
114 |
}
|
jaroslav@227
|
115 |
}
|
jaroslav@227
|
116 |
|
jaroslav@235
|
117 |
@Test public void computedCantReturnVoid() throws IOException {
|
jaroslav@235
|
118 |
String html = "<html><body>"
|
jaroslav@235
|
119 |
+ "</body></html>";
|
jaroslav@235
|
120 |
String code = "package x.y.z;\n"
|
jaroslav@235
|
121 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@235
|
122 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@235
|
123 |
+ "import net.java.html.json.ComputedProperty;\n"
|
jaroslav@235
|
124 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@235
|
125 |
+ " @Property(name=\"prop\", type=int.class)\n"
|
jaroslav@235
|
126 |
+ "})\n"
|
jaroslav@235
|
127 |
+ "class X {\n"
|
jaroslav@235
|
128 |
+ " @ComputedProperty static void y(int prop) {\n"
|
jaroslav@235
|
129 |
+ " }\n"
|
jaroslav@235
|
130 |
+ "}\n";
|
jaroslav@235
|
131 |
|
jaroslav@235
|
132 |
Compile c = Compile.create(html, code);
|
jaroslav@235
|
133 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jaroslav@235
|
134 |
boolean ok = false;
|
jaroslav@235
|
135 |
StringBuilder msgs = new StringBuilder();
|
jaroslav@235
|
136 |
for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
|
jaroslav@235
|
137 |
String msg = e.getMessage(Locale.ENGLISH);
|
jaroslav@235
|
138 |
if (msg.contains("y cannot return void")) {
|
jaroslav@235
|
139 |
ok = true;
|
jaroslav@235
|
140 |
}
|
jaroslav@235
|
141 |
msgs.append("\n").append(msg);
|
jaroslav@235
|
142 |
}
|
jaroslav@235
|
143 |
if (!ok) {
|
jaroslav@235
|
144 |
fail("Should contain warning about non-static method:" + msgs);
|
jaroslav@235
|
145 |
}
|
jaroslav@235
|
146 |
}
|
jaroslav@235
|
147 |
|
jaroslav@235
|
148 |
@Test public void computedCantReturnRunnable() throws IOException {
|
jaroslav@235
|
149 |
String html = "<html><body>"
|
jaroslav@235
|
150 |
+ "</body></html>";
|
jaroslav@235
|
151 |
String code = "package x.y.z;\n"
|
jaroslav@235
|
152 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@235
|
153 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@235
|
154 |
+ "import net.java.html.json.ComputedProperty;\n"
|
jaroslav@235
|
155 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@235
|
156 |
+ " @Property(name=\"prop\", type=int.class)\n"
|
jaroslav@235
|
157 |
+ "})\n"
|
jaroslav@235
|
158 |
+ "class X {\n"
|
jaroslav@235
|
159 |
+ " @ComputedProperty static Runnable y(int prop) {\n"
|
jaroslav@235
|
160 |
+ " return null;\n"
|
jaroslav@235
|
161 |
+ " }\n"
|
jaroslav@235
|
162 |
+ "}\n";
|
jaroslav@235
|
163 |
|
jaroslav@235
|
164 |
Compile c = Compile.create(html, code);
|
jaroslav@235
|
165 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jaroslav@235
|
166 |
boolean ok = false;
|
jaroslav@235
|
167 |
StringBuilder msgs = new StringBuilder();
|
jaroslav@235
|
168 |
for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
|
jaroslav@235
|
169 |
String msg = e.getMessage(Locale.ENGLISH);
|
jaroslav@235
|
170 |
if (msg.contains("y cannot return java.lang.Runnable")) {
|
jaroslav@235
|
171 |
ok = true;
|
jaroslav@235
|
172 |
}
|
jaroslav@235
|
173 |
msgs.append("\n").append(msg);
|
jaroslav@235
|
174 |
}
|
jaroslav@235
|
175 |
if (!ok) {
|
jaroslav@235
|
176 |
fail("Should contain warning about non-static method:" + msgs);
|
jaroslav@235
|
177 |
}
|
jaroslav@235
|
178 |
}
|
jaroslav@235
|
179 |
|
jaroslav@68
|
180 |
@Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
|
jaroslav@68
|
181 |
String html = "<html><body>"
|
jaroslav@68
|
182 |
+ "</body></html>";
|
jaroslav@68
|
183 |
String code = "package x.y.z;\n"
|
jaroslav@68
|
184 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@68
|
185 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@68
|
186 |
+ "import net.java.html.json.ComputedProperty;\n"
|
jaroslav@68
|
187 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@68
|
188 |
+ " @Property(name=\"prop\", type=long.class)\n"
|
jaroslav@68
|
189 |
+ "})\n"
|
jaroslav@68
|
190 |
+ "class X {\n"
|
jaroslav@68
|
191 |
+ " @ComputedProperty static double derived(long prop) { return prop; }"
|
jaroslav@68
|
192 |
+ "}\n";
|
jaroslav@68
|
193 |
|
jaroslav@68
|
194 |
Compile c = Compile.create(html, code, "1.5");
|
jaroslav@68
|
195 |
assertTrue(c.getErrors().isEmpty(), "No errors: " + c.getErrors());
|
jaroslav@68
|
196 |
}
|
jaroslav@70
|
197 |
|
jaroslav@70
|
198 |
@Test public void putNeedsDataArgument() throws Exception {
|
jaroslav@70
|
199 |
needsAnArg("PUT");
|
jaroslav@70
|
200 |
}
|
jaroslav@70
|
201 |
|
jaroslav@70
|
202 |
@Test public void postNeedsDataArgument() throws Exception {
|
jaroslav@70
|
203 |
needsAnArg("POST");
|
jaroslav@70
|
204 |
}
|
jaroslav@70
|
205 |
|
jaroslav@70
|
206 |
private void needsAnArg(String method) throws Exception {
|
jaroslav@70
|
207 |
String html = "<html><body>"
|
jaroslav@70
|
208 |
+ "</body></html>";
|
jaroslav@70
|
209 |
String code = "package x.y.z;\n"
|
jaroslav@70
|
210 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@70
|
211 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@70
|
212 |
+ "import net.java.html.json.OnReceive;\n"
|
jaroslav@70
|
213 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@70
|
214 |
+ " @Property(name=\"prop\", type=long.class)\n"
|
jaroslav@70
|
215 |
+ "})\n"
|
jaroslav@70
|
216 |
+ "class X {\n"
|
jaroslav@70
|
217 |
+ " @Model(className=\"PQ\", properties={})\n"
|
jaroslav@70
|
218 |
+ " class PImpl {\n"
|
jaroslav@70
|
219 |
+ " }\n"
|
jaroslav@70
|
220 |
+ " @OnReceive(method=\"" + method + "\", url=\"whereever\")\n"
|
jaroslav@70
|
221 |
+ " static void obtained(XModel m, PQ p) { }\n"
|
jaroslav@70
|
222 |
+ "}\n";
|
jaroslav@70
|
223 |
|
jaroslav@70
|
224 |
Compile c = Compile.create(html, code);
|
jaroslav@70
|
225 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jaroslav@70
|
226 |
for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
|
jaroslav@70
|
227 |
String msg = diagnostic.getMessage(Locale.ENGLISH);
|
jaroslav@70
|
228 |
if (msg.contains("specify a data()")) {
|
jaroslav@70
|
229 |
return;
|
jaroslav@70
|
230 |
}
|
jaroslav@70
|
231 |
}
|
jaroslav@70
|
232 |
fail("Needs an error message about missing data():\n" + c.getErrors());
|
jaroslav@70
|
233 |
|
jaroslav@70
|
234 |
}
|
jaroslav@73
|
235 |
|
jaroslav@73
|
236 |
|
jaroslav@73
|
237 |
@Test public void jsonNeedsToUseGet () throws Exception {
|
jaroslav@73
|
238 |
String html = "<html><body>"
|
jaroslav@73
|
239 |
+ "</body></html>";
|
jaroslav@73
|
240 |
String code = "package x.y.z;\n"
|
jaroslav@73
|
241 |
+ "import net.java.html.json.Model;\n"
|
jaroslav@73
|
242 |
+ "import net.java.html.json.Property;\n"
|
jaroslav@73
|
243 |
+ "import net.java.html.json.OnReceive;\n"
|
jaroslav@73
|
244 |
+ "@Model(className=\"XModel\", properties={\n"
|
jaroslav@73
|
245 |
+ " @Property(name=\"prop\", type=long.class)\n"
|
jaroslav@73
|
246 |
+ "})\n"
|
jaroslav@73
|
247 |
+ "class X {\n"
|
jaroslav@73
|
248 |
+ " @Model(className=\"PQ\", properties={})\n"
|
jaroslav@73
|
249 |
+ " class PImpl {\n"
|
jaroslav@73
|
250 |
+ " }\n"
|
jaroslav@73
|
251 |
+ " @OnReceive(method=\"POST\", jsonp=\"callback\", url=\"whereever\")\n"
|
jaroslav@73
|
252 |
+ " static void obtained(XModel m, PQ p) { }\n"
|
jaroslav@73
|
253 |
+ "}\n";
|
jaroslav@73
|
254 |
|
jaroslav@73
|
255 |
Compile c = Compile.create(html, code);
|
jaroslav@73
|
256 |
assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
|
jaroslav@73
|
257 |
for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
|
jaroslav@73
|
258 |
String msg = diagnostic.getMessage(Locale.ENGLISH);
|
jaroslav@73
|
259 |
if (msg.contains("JSONP works only with GET")) {
|
jaroslav@73
|
260 |
return;
|
jaroslav@73
|
261 |
}
|
jaroslav@73
|
262 |
}
|
jaroslav@73
|
263 |
fail("Needs an error message about wrong method:\n" + c.getErrors());
|
jaroslav@73
|
264 |
|
jaroslav@73
|
265 |
}
|
jaroslav@245
|
266 |
|
jaroslav@245
|
267 |
@Test public void onErrorHasToExist() throws IOException {
|
jaroslav@245
|
268 |
Compile res = Compile.create("", "package x;\n"
|
jaroslav@245
|
269 |
+ "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
|
jaroslav@245
|
270 |
+ " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
|
jaroslav@245
|
271 |
+ "})\n"
|
jaroslav@245
|
272 |
+ "class UseOnReceive {\n"
|
jaroslav@245
|
273 |
+ " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"doesNotExist\")\n"
|
jaroslav@245
|
274 |
+ " static void onMessage(MyModel model, String value) {\n"
|
jaroslav@245
|
275 |
+ " }\n"
|
jaroslav@245
|
276 |
+ "}\n"
|
jaroslav@245
|
277 |
);
|
jaroslav@245
|
278 |
res.assertErrors();
|
jaroslav@245
|
279 |
res.assertError("not find doesNotExist");
|
jaroslav@245
|
280 |
}
|
jaroslav@245
|
281 |
|
jaroslav@245
|
282 |
@Test public void onErrorWouldHaveToBeStatic() throws IOException {
|
jaroslav@245
|
283 |
Compile res = Compile.create("", "package x;\n"
|
jaroslav@245
|
284 |
+ "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
|
jaroslav@245
|
285 |
+ " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
|
jaroslav@245
|
286 |
+ "})\n"
|
jaroslav@245
|
287 |
+ "class UseOnReceive {\n"
|
jaroslav@245
|
288 |
+ " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"notStatic\")\n"
|
jaroslav@245
|
289 |
+ " static void onMessage(MyModel model, String value) {\n"
|
jaroslav@245
|
290 |
+ " }\n"
|
jaroslav@245
|
291 |
+ " void notStatic(Exception e) {}\n"
|
jaroslav@245
|
292 |
+ "}\n"
|
jaroslav@245
|
293 |
);
|
jaroslav@245
|
294 |
res.assertErrors();
|
jaroslav@245
|
295 |
res.assertError("have to be static");
|
jaroslav@245
|
296 |
}
|
jaroslav@245
|
297 |
|
jaroslav@245
|
298 |
@Test public void onErrorMustAcceptExceptionArgument() throws IOException {
|
jaroslav@245
|
299 |
Compile res = Compile.create("", "package x;\n"
|
jaroslav@245
|
300 |
+ "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
|
jaroslav@245
|
301 |
+ " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
|
jaroslav@245
|
302 |
+ "})\n"
|
jaroslav@245
|
303 |
+ "class UseOnReceive {\n"
|
jaroslav@245
|
304 |
+ " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"subclass\")\n"
|
jaroslav@245
|
305 |
+ " static void onMessage(MyModel model, String value) {\n"
|
jaroslav@245
|
306 |
+ " }\n"
|
jaroslav@245
|
307 |
+ " static void subclass(java.io.IOException e) {}\n"
|
jaroslav@245
|
308 |
+ "}\n"
|
jaroslav@245
|
309 |
);
|
jaroslav@245
|
310 |
res.assertErrors();
|
jaroslav@245
|
311 |
res.assertError("Error method first argument needs to be MyModel and second Exception");
|
jaroslav@245
|
312 |
}
|
jtulach@3
|
313 |
}
|