#256178: Only generate the constructor with arguments if it can be compiled - e.g. if the number of arguments in not higher than 255.
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json;
45 import java.io.IOException;
46 import java.util.Locale;
47 import javax.tools.Diagnostic;
48 import javax.tools.JavaFileObject;
49 import static org.testng.Assert.*;
50 import org.testng.annotations.Test;
52 /** Verify errors emitted by the processor.
54 * @author Jaroslav Tulach
56 public class ModelProcessorTest {
57 @Test public void verifyWrongType() throws IOException {
58 String html = "<html><body>"
60 String code = "package x.y.z;\n"
61 + "import net.java.html.json.Model;\n"
62 + "import net.java.html.json.Property;\n"
63 + "@Model(className=\"XModel\", properties={\n"
64 + " @Property(name=\"prop\", type=Runnable.class)\n"
69 Compile c = Compile.create(html, code);
70 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
72 StringBuilder msgs = new StringBuilder();
73 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
74 String msg = e.getMessage(Locale.ENGLISH);
75 if (msg.contains("Runnable")) {
78 msgs.append("\n").append(msg);
81 fail("Should contain warning about Runnable:" + msgs);
85 @Test public void verifyWrongTypeInInnerClass() throws IOException {
86 String html = "<html><body>"
88 String code = "package x.y.z;\n"
89 + "import net.java.html.json.Model;\n"
90 + "import net.java.html.json.Property;\n"
92 + " @Model(className=\"XModel\", properties={\n"
93 + " @Property(name=\"prop\", type=Runnable.class)\n"
95 + " static class Inner {\n"
99 Compile c = Compile.create(html, code);
100 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
102 StringBuilder msgs = new StringBuilder();
103 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
104 String msg = e.getMessage(Locale.ENGLISH);
105 if (msg.contains("Runnable")) {
108 msgs.append("\n").append(msg);
111 fail("Should contain warning about Runnable:" + msgs);
115 @Test public void warnOnNonStatic() throws IOException {
116 String html = "<html><body>"
118 String code = "package x.y.z;\n"
119 + "import net.java.html.json.Model;\n"
120 + "import net.java.html.json.Property;\n"
121 + "import net.java.html.json.ComputedProperty;\n"
122 + "@Model(className=\"XModel\", properties={\n"
123 + " @Property(name=\"prop\", type=int.class)\n"
126 + " @ComputedProperty int y(int prop) {\n"
131 Compile c = Compile.create(html, code);
132 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
134 StringBuilder msgs = new StringBuilder();
135 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
136 String msg = e.getMessage(Locale.ENGLISH);
137 if (msg.contains("y has to be static")) {
140 msgs.append("\n").append(msg);
143 fail("Should contain warning about non-static method:" + msgs);
147 @Test public void tooManyProperties() throws IOException {
148 manyProperties(255, false, 0);
151 @Test public void tooManyArrayPropertiesIsOK() throws IOException {
152 manyProperties(0, true, 300);
155 @Test public void justEnoughProperties() throws IOException {
156 manyProperties(254, true, 0);
159 @Test public void justEnoughPropertiesWithArrayOne() throws IOException {
160 manyProperties(253, true, 300);
163 @Test public void justEnoughPropertiesButOneArrayOne() throws IOException {
164 manyProperties(254, false, 300);
167 private void manyProperties(
168 int cnt, boolean constructorWithParams, int arrayCnt
169 ) throws IOException {
170 String html = "<html><body>"
172 StringBuilder code = new StringBuilder();
173 code.append("package x.y.z;\n"
174 + "import net.java.html.json.Model;\n"
175 + "import net.java.html.json.Property;\n"
176 + "@Model(className=\"XModel\", properties={\n"
178 for (int i = 1; i <= cnt; i++) {
179 code.append(" @Property(name=\"prop").append(i).append("\", ");
180 code.append("type=int.class),\n");
182 for (int i = 1; i <= arrayCnt; i++) {
183 code.append(" @Property(name=\"array").append(i).append("\", ");
184 code.append("array=true, ");
185 code.append("type=int.class),\n");
194 if (constructorWithParams) {
196 for (int i = 1; i < cnt; i++) {
197 code.append(",\n").append(i);
205 Compile c = Compile.create(html, code.toString());
206 assertTrue(c.getErrors().isEmpty(), "Compiles OK: " + c.getErrors());
209 @Test public void writeableComputedPropertyMissingWrite() throws IOException {
210 String html = "<html><body>"
212 String code = "package x.y.z;\n"
213 + "import net.java.html.json.Model;\n"
214 + "import net.java.html.json.Property;\n"
215 + "import net.java.html.json.ComputedProperty;\n"
216 + "@Model(className=\"XModel\", properties={\n"
217 + " @Property(name=\"prop\", type=int.class)\n"
220 + " static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
225 Compile c = Compile.create(html, code);
226 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
228 StringBuilder msgs = new StringBuilder();
229 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
230 String msg = e.getMessage(Locale.ENGLISH);
231 if (msg.contains("Cannot find setY")) {
234 msgs.append("\n").append(msg);
237 fail("Should contain warning about non-static method:" + msgs);
241 @Test public void writeableComputedPropertyWrongWriteType() throws IOException {
242 String html = "<html><body>"
244 String code = "package x.y.z;\n"
245 + "import net.java.html.json.Model;\n"
246 + "import net.java.html.json.Property;\n"
247 + "import net.java.html.json.ComputedProperty;\n"
248 + "@Model(className=\"XModel\", properties={\n"
249 + " @Property(name=\"prop\", type=int.class)\n"
252 + " static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
255 + " static void setY(XModel model, String prop) {\n"
259 Compile c = Compile.create(html, code);
260 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
262 StringBuilder msgs = new StringBuilder();
263 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
264 String msg = e.getMessage(Locale.ENGLISH);
265 if (msg.contains("Write method first argument needs to be XModel and second int or Object")) {
268 msgs.append("\n").append(msg);
271 fail("Should contain warning about non-static method:" + msgs);
275 @Test public void writeableComputedPropertyReturnsVoid() throws IOException {
276 String html = "<html><body>"
278 String code = "package x.y.z;\n"
279 + "import net.java.html.json.Model;\n"
280 + "import net.java.html.json.Property;\n"
281 + "import net.java.html.json.ComputedProperty;\n"
282 + "@Model(className=\"XModel\", properties={\n"
283 + " @Property(name=\"prop\", type=int.class)\n"
286 + " static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
289 + " static Number setY(XModel model, int prop) {\n"
293 Compile c = Compile.create(html, code);
294 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
296 StringBuilder msgs = new StringBuilder();
297 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
298 String msg = e.getMessage(Locale.ENGLISH);
299 if (msg.contains("Write method has to return void")) {
302 msgs.append("\n").append(msg);
305 fail("Should contain warning about non-static method:" + msgs);
309 @Test public void computedCantReturnVoid() throws IOException {
310 String html = "<html><body>"
312 String code = "package x.y.z;\n"
313 + "import net.java.html.json.Model;\n"
314 + "import net.java.html.json.Property;\n"
315 + "import net.java.html.json.ComputedProperty;\n"
316 + "@Model(className=\"XModel\", properties={\n"
317 + " @Property(name=\"prop\", type=int.class)\n"
320 + " @ComputedProperty static void y(int prop) {\n"
324 Compile c = Compile.create(html, code);
325 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
327 StringBuilder msgs = new StringBuilder();
328 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
329 String msg = e.getMessage(Locale.ENGLISH);
330 if (msg.contains("y cannot return void")) {
333 msgs.append("\n").append(msg);
336 fail("Should contain warning about non-static method:" + msgs);
340 @Test public void computedCantReturnRunnable() throws IOException {
341 String html = "<html><body>"
343 String code = "package x.y.z;\n"
344 + "import net.java.html.json.Model;\n"
345 + "import net.java.html.json.Property;\n"
346 + "import net.java.html.json.ComputedProperty;\n"
347 + "@Model(className=\"XModel\", properties={\n"
348 + " @Property(name=\"prop\", type=int.class)\n"
351 + " @ComputedProperty static Runnable y(int prop) {\n"
356 Compile c = Compile.create(html, code);
357 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
359 StringBuilder msgs = new StringBuilder();
360 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
361 String msg = e.getMessage(Locale.ENGLISH);
362 if (msg.contains("y cannot return java.lang.Runnable")) {
365 msgs.append("\n").append(msg);
368 fail("Should contain warning about non-static method:" + msgs);
372 @Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
373 String html = "<html><body>"
375 String code = "package x.y.z;\n"
376 + "import net.java.html.json.Model;\n"
377 + "import net.java.html.json.Property;\n"
378 + "import net.java.html.json.ComputedProperty;\n"
379 + "@Model(className=\"XModel\", properties={\n"
380 + " @Property(name=\"prop\", type=long.class)\n"
383 + " @ComputedProperty static double derived(long prop) { return prop; }"
386 Compile c = Compile.create(html, code, "1.5");
387 assertTrue(c.getErrors().isEmpty(), "No errors: " + c.getErrors());
390 @Test public void putNeedsDataArgument() throws Exception {
394 @Test public void postNeedsDataArgument() throws Exception {
398 private void needsAnArg(String method) throws Exception {
399 String html = "<html><body>"
401 String code = "package x.y.z;\n"
402 + "import net.java.html.json.Model;\n"
403 + "import net.java.html.json.Property;\n"
404 + "import net.java.html.json.OnReceive;\n"
405 + "@Model(className=\"XModel\", properties={\n"
406 + " @Property(name=\"prop\", type=long.class)\n"
409 + " @Model(className=\"PQ\", properties={})\n"
412 + " @OnReceive(method=\"" + method + "\", url=\"whereever\")\n"
413 + " static void obtained(XModel m, PQ p) { }\n"
416 Compile c = Compile.create(html, code);
417 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
418 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
419 String msg = diagnostic.getMessage(Locale.ENGLISH);
420 if (msg.contains("specify a data()")) {
424 fail("Needs an error message about missing data():\n" + c.getErrors());
429 @Test public void jsonNeedsToUseGet () throws Exception {
430 String html = "<html><body>"
432 String code = "package x.y.z;\n"
433 + "import net.java.html.json.Model;\n"
434 + "import net.java.html.json.Property;\n"
435 + "import net.java.html.json.OnReceive;\n"
436 + "@Model(className=\"XModel\", properties={\n"
437 + " @Property(name=\"prop\", type=long.class)\n"
440 + " @Model(className=\"PQ\", properties={})\n"
443 + " @OnReceive(method=\"POST\", jsonp=\"callback\", url=\"whereever\")\n"
444 + " static void obtained(XModel m, PQ p) { }\n"
447 Compile c = Compile.create(html, code);
448 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
449 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
450 String msg = diagnostic.getMessage(Locale.ENGLISH);
451 if (msg.contains("JSONP works only with GET")) {
455 fail("Needs an error message about wrong method:\n" + c.getErrors());
459 @Test public void noHeadersForWebSockets() throws Exception {
460 String html = "<html><body>"
462 String code = "package x.y.z;\n"
463 + "import net.java.html.json.Model;\n"
464 + "import net.java.html.json.Property;\n"
465 + "import net.java.html.json.OnReceive;\n"
466 + "@Model(className=\"XModel\", properties={\n"
467 + " @Property(name=\"prop\", type=long.class)\n"
470 + " @Model(className=\"PQ\", properties={})\n"
473 + " @OnReceive(method=\"WebSocket\", data = PQ.class, headers=\"SomeHeader: {some}\", url=\"whereever\")\n"
474 + " static void obtained(XModel m, PQ p) { }\n"
477 Compile c = Compile.create(html, code);
478 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
479 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
480 String msg = diagnostic.getMessage(Locale.ENGLISH);
481 if (msg.contains("WebSocket spec does not support headers")) {
485 fail("Needs an error message about headers:\n" + c.getErrors());
489 @Test public void webSocketsWithoutDataIsError() throws Exception {
490 String html = "<html><body>"
492 String code = "package x.y.z;\n"
493 + "import net.java.html.json.Model;\n"
494 + "import net.java.html.json.Property;\n"
495 + "import net.java.html.json.OnReceive;\n"
496 + "@Model(className=\"XModel\", properties={\n"
497 + " @Property(name=\"prop\", type=long.class)\n"
500 + " @Model(className=\"PQ\", properties={})\n"
503 + " @OnReceive(method=\"WebSocket\", url=\"whereever\")\n"
504 + " static void obtained(XModel m, PQ p) { }\n"
507 Compile c = Compile.create(html, code);
508 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
509 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
510 String msg = diagnostic.getMessage(Locale.ENGLISH);
511 if (msg.contains("eeds to specify a data()")) {
515 fail("Needs data attribute :\n" + c.getErrors());
518 @Test public void noNewLinesInHeaderLines() throws Exception {
519 String html = "<html><body>"
521 String code = "package x.y.z;\n"
522 + "import net.java.html.json.Model;\n"
523 + "import net.java.html.json.Property;\n"
524 + "import net.java.html.json.OnReceive;\n"
525 + "@Model(className=\"XModel\", properties={\n"
526 + " @Property(name=\"prop\", type=long.class)\n"
529 + " @Model(className=\"PQ\", properties={})\n"
532 + " @OnReceive(headers=\"SomeHeader\\n: {some}\", url=\"whereever\")\n"
533 + " static void obtained(XModel m, PQ p) { }\n"
536 Compile c = Compile.create(html, code);
537 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
538 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
539 String msg = diagnostic.getMessage(Locale.ENGLISH);
540 if (msg.contains("Header line cannot contain line separator")) {
544 fail("Needs an error message about headers:\n" + c.getErrors());
548 @Test public void noReturnInHeaderLines() throws Exception {
549 String html = "<html><body>"
551 String code = "package x.y.z;\n"
552 + "import net.java.html.json.Model;\n"
553 + "import net.java.html.json.Property;\n"
554 + "import net.java.html.json.OnReceive;\n"
555 + "@Model(className=\"XModel\", properties={\n"
556 + " @Property(name=\"prop\", type=long.class)\n"
559 + " @Model(className=\"PQ\", properties={})\n"
562 + " @OnReceive(headers=\"Some\\rHeader: {some}\", url=\"whereever\")\n"
563 + " static void obtained(XModel m, PQ p) { }\n"
566 Compile c = Compile.create(html, code);
567 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
568 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
569 String msg = diagnostic.getMessage(Locale.ENGLISH);
570 if (msg.contains("Header line cannot contain line separator")) {
574 fail("Needs an error message about headers:\n" + c.getErrors());
578 @Test public void onErrorHasToExist() throws IOException {
579 Compile res = Compile.create("", "package x;\n"
580 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
581 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
583 + "class UseOnReceive {\n"
584 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"doesNotExist\")\n"
585 + " static void onMessage(MyModel model, String value) {\n"
590 res.assertError("not find doesNotExist");
593 @Test public void usingListIsOK() throws IOException {
594 Compile res = Compile.create("", "package x;\n"
595 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
596 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
598 + "class UseOnReceive {\n"
599 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\")\n"
600 + " static void onMessage(MyModel model, java.util.List<MyData> value) {\n"
603 + " @net.java.html.json.Model(className=\"MyData\", properties={\n"
605 + " static class MyDataModel {\n"
609 res.assertNoErrors();
612 @Test public void functionAndPropertyCollide() throws IOException {
613 Compile res = Compile.create("", "package x;\n"
614 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
615 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
617 + "class Collision {\n"
618 + " @net.java.html.json.Function\n"
619 + " static void x(MyModel model, String value) {\n"
624 res.assertError("cannot have the name");
627 @Test public void twoPropertiesCollide() throws IOException {
628 Compile res = Compile.create("", "package x;\n"
629 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
630 + " @net.java.html.json.Property(name=\"x\", type=String.class),\n"
631 + " @net.java.html.json.Property(name=\"x\", type=int.class)\n"
633 + "class Collision {\n"
637 res.assertError("Cannot have the name");
640 @Test public void propertyAndComputedOneCollide() throws IOException {
641 Compile res = Compile.create("", "package x;\n"
642 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
643 + " @net.java.html.json.Property(name=\"x\", type=String.class),\n"
645 + "class Collision {\n"
646 + " @net.java.html.json.ComputedProperty static int x(String x) {\n"
647 + " return x.length();\n"
652 res.assertError("Cannot have the name");
655 @Test public void onWebSocketJustTwoArgs() throws IOException {
656 Compile res = Compile.create("", "package x;\n"
657 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
658 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
660 + "class UseOnReceive {\n"
661 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", method=\"WebSocket\", data=String.class)\n"
662 + " static void onMessage(MyModel model, String value, int arg) {\n"
667 res.assertError("only have two arg");
670 @Test public void onErrorWouldHaveToBeStatic() throws IOException {
671 Compile res = Compile.create("", "package x;\n"
672 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
673 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
675 + "class UseOnReceive {\n"
676 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"notStatic\")\n"
677 + " static void onMessage(MyModel model, String value) {\n"
679 + " void notStatic(Exception e) {}\n"
683 res.assertError("have to be static");
686 @Test public void onErrorMustAcceptExceptionArgument() throws IOException {
687 Compile res = Compile.create("", "package x;\n"
688 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
689 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
691 + "class UseOnReceive {\n"
692 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"subclass\")\n"
693 + " static void onMessage(MyModel model, String value) {\n"
695 + " static void subclass(java.io.IOException e) {}\n"
699 res.assertError("Error method first argument needs to be MyModel and second Exception");