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 <jtulach@netbeans.org>
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 warnOnNonStatic() 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"
91 + "import net.java.html.json.ComputedProperty;\n"
92 + "@Model(className=\"XModel\", properties={\n"
93 + " @Property(name=\"prop\", type=int.class)\n"
96 + " @ComputedProperty int y(int prop) {\n"
101 Compile c = Compile.create(html, code);
102 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
104 StringBuilder msgs = new StringBuilder();
105 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
106 String msg = e.getMessage(Locale.ENGLISH);
107 if (msg.contains("y has to be static")) {
110 msgs.append("\n").append(msg);
113 fail("Should contain warning about non-static method:" + msgs);
117 @Test public void computedCantReturnVoid() throws IOException {
118 String html = "<html><body>"
120 String code = "package x.y.z;\n"
121 + "import net.java.html.json.Model;\n"
122 + "import net.java.html.json.Property;\n"
123 + "import net.java.html.json.ComputedProperty;\n"
124 + "@Model(className=\"XModel\", properties={\n"
125 + " @Property(name=\"prop\", type=int.class)\n"
128 + " @ComputedProperty static void y(int prop) {\n"
132 Compile c = Compile.create(html, code);
133 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
135 StringBuilder msgs = new StringBuilder();
136 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
137 String msg = e.getMessage(Locale.ENGLISH);
138 if (msg.contains("y cannot return void")) {
141 msgs.append("\n").append(msg);
144 fail("Should contain warning about non-static method:" + msgs);
148 @Test public void computedCantReturnRunnable() throws IOException {
149 String html = "<html><body>"
151 String code = "package x.y.z;\n"
152 + "import net.java.html.json.Model;\n"
153 + "import net.java.html.json.Property;\n"
154 + "import net.java.html.json.ComputedProperty;\n"
155 + "@Model(className=\"XModel\", properties={\n"
156 + " @Property(name=\"prop\", type=int.class)\n"
159 + " @ComputedProperty static Runnable y(int prop) {\n"
164 Compile c = Compile.create(html, code);
165 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
167 StringBuilder msgs = new StringBuilder();
168 for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
169 String msg = e.getMessage(Locale.ENGLISH);
170 if (msg.contains("y cannot return java.lang.Runnable")) {
173 msgs.append("\n").append(msg);
176 fail("Should contain warning about non-static method:" + msgs);
180 @Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
181 String html = "<html><body>"
183 String code = "package x.y.z;\n"
184 + "import net.java.html.json.Model;\n"
185 + "import net.java.html.json.Property;\n"
186 + "import net.java.html.json.ComputedProperty;\n"
187 + "@Model(className=\"XModel\", properties={\n"
188 + " @Property(name=\"prop\", type=long.class)\n"
191 + " @ComputedProperty static double derived(long prop) { return prop; }"
194 Compile c = Compile.create(html, code, "1.5");
195 assertTrue(c.getErrors().isEmpty(), "No errors: " + c.getErrors());
198 @Test public void putNeedsDataArgument() throws Exception {
202 @Test public void postNeedsDataArgument() throws Exception {
206 private void needsAnArg(String method) throws Exception {
207 String html = "<html><body>"
209 String code = "package x.y.z;\n"
210 + "import net.java.html.json.Model;\n"
211 + "import net.java.html.json.Property;\n"
212 + "import net.java.html.json.OnReceive;\n"
213 + "@Model(className=\"XModel\", properties={\n"
214 + " @Property(name=\"prop\", type=long.class)\n"
217 + " @Model(className=\"PQ\", properties={})\n"
220 + " @OnReceive(method=\"" + method + "\", url=\"whereever\")\n"
221 + " static void obtained(XModel m, PQ p) { }\n"
224 Compile c = Compile.create(html, code);
225 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
226 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
227 String msg = diagnostic.getMessage(Locale.ENGLISH);
228 if (msg.contains("specify a data()")) {
232 fail("Needs an error message about missing data():\n" + c.getErrors());
237 @Test public void jsonNeedsToUseGet () throws Exception {
238 String html = "<html><body>"
240 String code = "package x.y.z;\n"
241 + "import net.java.html.json.Model;\n"
242 + "import net.java.html.json.Property;\n"
243 + "import net.java.html.json.OnReceive;\n"
244 + "@Model(className=\"XModel\", properties={\n"
245 + " @Property(name=\"prop\", type=long.class)\n"
248 + " @Model(className=\"PQ\", properties={})\n"
251 + " @OnReceive(method=\"POST\", jsonp=\"callback\", url=\"whereever\")\n"
252 + " static void obtained(XModel m, PQ p) { }\n"
255 Compile c = Compile.create(html, code);
256 assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
257 for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
258 String msg = diagnostic.getMessage(Locale.ENGLISH);
259 if (msg.contains("JSONP works only with GET")) {
263 fail("Needs an error message about wrong method:\n" + c.getErrors());
267 @Test public void onErrorHasToExist() throws IOException {
268 Compile res = Compile.create("", "package x;\n"
269 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
270 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
272 + "class UseOnReceive {\n"
273 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"doesNotExist\")\n"
274 + " static void onMessage(MyModel model, String value) {\n"
279 res.assertError("not find doesNotExist");
282 @Test public void onErrorWouldHaveToBeStatic() throws IOException {
283 Compile res = Compile.create("", "package x;\n"
284 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
285 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
287 + "class UseOnReceive {\n"
288 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"notStatic\")\n"
289 + " static void onMessage(MyModel model, String value) {\n"
291 + " void notStatic(Exception e) {}\n"
295 res.assertError("have to be static");
298 @Test public void onErrorMustAcceptExceptionArgument() throws IOException {
299 Compile res = Compile.create("", "package x;\n"
300 + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
301 + " @net.java.html.json.Property(name=\"x\", type=String.class)\n"
303 + "class UseOnReceive {\n"
304 + " @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"subclass\")\n"
305 + " static void onMessage(MyModel model, String value) {\n"
307 + " static void subclass(java.io.IOException e) {}\n"
311 res.assertError("Error method first argument needs to be MyModel and second Exception");