json/src/test/java/net/java/html/json/ModelProcessorTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 365 5c93ad8c7a15
child 647 989ce2017405
permissions -rw-r--r--
Updating copyright headers to mention current year
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
     5  *
     6  * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
     7  * Other names may be trademarks of their respective owners.
     8  *
     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]"
    26  *
    27  * Contributor(s):
    28  *
    29  * The Original Software is NetBeans. The Initial Developer of the Original
    30  * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
    31  *
    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.
    42  */
    43 package net.java.html.json;
    44 
    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;
    51 
    52 /** Verify errors emitted by the processor.
    53  *
    54  * @author Jaroslav Tulach <jtulach@netbeans.org>
    55  */
    56 public class ModelProcessorTest {
    57     @Test public void verifyWrongType() throws IOException {
    58         String html = "<html><body>"
    59             + "</body></html>";
    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"
    65             + "})\n"
    66             + "class X {\n"
    67             + "}\n";
    68         
    69         Compile c = Compile.create(html, code);
    70         assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
    71         boolean ok = false;
    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")) {
    76                 ok = true;
    77             }
    78             msgs.append("\n").append(msg);
    79         }
    80         if (!ok) {
    81             fail("Should contain warning about Runnable:" + msgs);
    82         }
    83     }
    84     
    85     @Test public void warnOnNonStatic() throws IOException {
    86         String html = "<html><body>"
    87             + "</body></html>";
    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"
    94             + "})\n"
    95             + "class X {\n"
    96             + "    @ComputedProperty int y(int prop) {\n"
    97             + "        return prop;\n"
    98             + "    }\n"
    99             + "}\n";
   100         
   101         Compile c = Compile.create(html, code);
   102         assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
   103         boolean ok = false;
   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")) {
   108                 ok = true;
   109             }
   110             msgs.append("\n").append(msg);
   111         }
   112         if (!ok) {
   113             fail("Should contain warning about non-static method:" + msgs);
   114         }
   115     }
   116     
   117     @Test public void computedCantReturnVoid() throws IOException {
   118         String html = "<html><body>"
   119             + "</body></html>";
   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"
   126             + "})\n"
   127             + "class X {\n"
   128             + "    @ComputedProperty static void y(int prop) {\n"
   129             + "    }\n"
   130             + "}\n";
   131         
   132         Compile c = Compile.create(html, code);
   133         assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
   134         boolean ok = false;
   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")) {
   139                 ok = true;
   140             }
   141             msgs.append("\n").append(msg);
   142         }
   143         if (!ok) {
   144             fail("Should contain warning about non-static method:" + msgs);
   145         }
   146     }
   147     
   148     @Test public void computedCantReturnRunnable() throws IOException {
   149         String html = "<html><body>"
   150             + "</body></html>";
   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"
   157             + "})\n"
   158             + "class X {\n"
   159             + "    @ComputedProperty static Runnable y(int prop) {\n"
   160             + "       return null;\n"
   161             + "    }\n"
   162             + "}\n";
   163         
   164         Compile c = Compile.create(html, code);
   165         assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
   166         boolean ok = false;
   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")) {
   171                 ok = true;
   172             }
   173             msgs.append("\n").append(msg);
   174         }
   175         if (!ok) {
   176             fail("Should contain warning about non-static method:" + msgs);
   177         }
   178     }
   179     
   180     @Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
   181         String html = "<html><body>"
   182             + "</body></html>";
   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"
   189             + "})\n"
   190             + "class X {\n"
   191             + "  @ComputedProperty static double derived(long prop) { return prop; }"
   192             + "}\n";
   193         
   194         Compile c = Compile.create(html, code, "1.5");
   195         assertTrue(c.getErrors().isEmpty(), "No errors: " + c.getErrors());
   196     }
   197     
   198     @Test public void putNeedsDataArgument() throws Exception {
   199         needsAnArg("PUT");
   200     }
   201 
   202     @Test public void postNeedsDataArgument() throws Exception {
   203         needsAnArg("POST");
   204     }
   205     
   206     private void needsAnArg(String method) throws Exception {
   207         String html = "<html><body>"
   208             + "</body></html>";
   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"
   215             + "})\n"
   216             + "class X {\n"
   217             + "  @Model(className=\"PQ\", properties={})\n"
   218             + "  class PImpl {\n"
   219             + "  }\n"
   220             + "  @OnReceive(method=\"" + method + "\", url=\"whereever\")\n"
   221             + "  static void obtained(XModel m, PQ p) { }\n"
   222             + "}\n";
   223         
   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()")) {
   229                 return;
   230             }
   231         }
   232         fail("Needs an error message about missing data():\n" + c.getErrors());
   233         
   234     }
   235     
   236     
   237     @Test public void jsonNeedsToUseGet () throws Exception {
   238         String html = "<html><body>"
   239             + "</body></html>";
   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"
   246             + "})\n"
   247             + "class X {\n"
   248             + "  @Model(className=\"PQ\", properties={})\n"
   249             + "  class PImpl {\n"
   250             + "  }\n"
   251             + "  @OnReceive(method=\"POST\", jsonp=\"callback\", url=\"whereever\")\n"
   252             + "  static void obtained(XModel m, PQ p) { }\n"
   253             + "}\n";
   254         
   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")) {
   260                 return;
   261             }
   262         }
   263         fail("Needs an error message about wrong method:\n" + c.getErrors());
   264         
   265     }
   266     
   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"
   271             + "})\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"
   275             + "  }\n"
   276             + "}\n"
   277         );
   278         res.assertErrors();
   279         res.assertError("not find doesNotExist");
   280     }
   281 
   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"
   286             + "})\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"
   290             + "  }\n"
   291             + "  void notStatic(Exception e) {}\n"
   292             + "}\n"
   293         );
   294         res.assertErrors();
   295         res.assertError("have to be static");
   296     }
   297 
   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"
   302             + "})\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"
   306             + "  }\n"
   307             + "  static void subclass(java.io.IOException e) {}\n"
   308             + "}\n"
   309         );
   310         res.assertErrors();
   311         res.assertError("Error method first argument needs to be MyModel and second Exception");
   312     }
   313 }