json/src/test/java/net/java/html/json/ModelProcessorTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 28 Oct 2015 19:29:17 +0100
changeset 1011 267ca1bfeb6f
parent 955 8e0439c4cc72
child 1023 4f906bde3a2e
permissions -rw-r--r--
#256178: Only generate the constructor with arguments if it can be compiled - e.g. if the number of arguments in not higher than 255.
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@790
    54
 * @author Jaroslav Tulach
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@940
    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@940
    84
jtulach@926
    85
    @Test public void verifyWrongTypeInInnerClass() throws IOException {
jtulach@926
    86
        String html = "<html><body>"
jtulach@926
    87
            + "</body></html>";
jtulach@926
    88
        String code = "package x.y.z;\n"
jtulach@926
    89
            + "import net.java.html.json.Model;\n"
jtulach@926
    90
            + "import net.java.html.json.Property;\n"
jtulach@926
    91
            + "class X {\n"
jtulach@926
    92
            + "  @Model(className=\"XModel\", properties={\n"
jtulach@926
    93
            + "    @Property(name=\"prop\", type=Runnable.class)\n"
jtulach@926
    94
            + "  })\n"
jtulach@926
    95
            + "  static class Inner {\n"
jtulach@926
    96
            + "  }\n"
jtulach@926
    97
            + "}\n";
jtulach@940
    98
jtulach@926
    99
        Compile c = Compile.create(html, code);
jtulach@926
   100
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@926
   101
        boolean ok = false;
jtulach@926
   102
        StringBuilder msgs = new StringBuilder();
jtulach@926
   103
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jtulach@926
   104
            String msg = e.getMessage(Locale.ENGLISH);
jtulach@926
   105
            if (msg.contains("Runnable")) {
jtulach@926
   106
                ok = true;
jtulach@926
   107
            }
jtulach@926
   108
            msgs.append("\n").append(msg);
jtulach@926
   109
        }
jtulach@926
   110
        if (!ok) {
jtulach@926
   111
            fail("Should contain warning about Runnable:" + msgs);
jtulach@926
   112
        }
jtulach@926
   113
    }
jtulach@940
   114
jaroslav@227
   115
    @Test public void warnOnNonStatic() throws IOException {
jaroslav@227
   116
        String html = "<html><body>"
jaroslav@227
   117
            + "</body></html>";
jaroslav@227
   118
        String code = "package x.y.z;\n"
jaroslav@227
   119
            + "import net.java.html.json.Model;\n"
jaroslav@227
   120
            + "import net.java.html.json.Property;\n"
jaroslav@227
   121
            + "import net.java.html.json.ComputedProperty;\n"
jaroslav@227
   122
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@227
   123
            + "  @Property(name=\"prop\", type=int.class)\n"
jaroslav@227
   124
            + "})\n"
jaroslav@227
   125
            + "class X {\n"
jaroslav@227
   126
            + "    @ComputedProperty int y(int prop) {\n"
jaroslav@227
   127
            + "        return prop;\n"
jaroslav@227
   128
            + "    }\n"
jaroslav@227
   129
            + "}\n";
jtulach@940
   130
jaroslav@227
   131
        Compile c = Compile.create(html, code);
jaroslav@227
   132
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jaroslav@227
   133
        boolean ok = false;
jaroslav@227
   134
        StringBuilder msgs = new StringBuilder();
jaroslav@227
   135
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jaroslav@227
   136
            String msg = e.getMessage(Locale.ENGLISH);
jaroslav@227
   137
            if (msg.contains("y has to be static")) {
jaroslav@227
   138
                ok = true;
jaroslav@227
   139
            }
jaroslav@227
   140
            msgs.append("\n").append(msg);
jaroslav@227
   141
        }
jaroslav@227
   142
        if (!ok) {
jaroslav@227
   143
            fail("Should contain warning about non-static method:" + msgs);
jaroslav@227
   144
        }
jaroslav@227
   145
    }
jtulach@940
   146
jtulach@1011
   147
    @Test public void tooManyProperties() throws IOException {
jtulach@1011
   148
        manyProperties(255, false, 0);
jtulach@1011
   149
    }
jtulach@1011
   150
jtulach@1011
   151
    @Test public void tooManyArrayPropertiesIsOK() throws IOException {
jtulach@1011
   152
        manyProperties(0, true, 300);
jtulach@1011
   153
    }
jtulach@1011
   154
jtulach@1011
   155
    @Test public void justEnoughProperties() throws IOException {
jtulach@1011
   156
        manyProperties(254, true, 0);
jtulach@1011
   157
    }
jtulach@1011
   158
jtulach@1011
   159
    @Test public void justEnoughPropertiesWithArrayOne() throws IOException {
jtulach@1011
   160
        manyProperties(253, true, 300);
jtulach@1011
   161
    }
jtulach@1011
   162
jtulach@1011
   163
    @Test public void justEnoughPropertiesButOneArrayOne() throws IOException {
jtulach@1011
   164
        manyProperties(254, false, 300);
jtulach@1011
   165
    }
jtulach@1011
   166
jtulach@1011
   167
    private void manyProperties(
jtulach@1011
   168
        int cnt, boolean constructorWithParams, int arrayCnt
jtulach@1011
   169
    ) throws IOException {
jtulach@1011
   170
        String html = "<html><body>"
jtulach@1011
   171
            + "</body></html>";
jtulach@1011
   172
        StringBuilder code = new StringBuilder();
jtulach@1011
   173
        code.append("package x.y.z;\n"
jtulach@1011
   174
            + "import net.java.html.json.Model;\n"
jtulach@1011
   175
            + "import net.java.html.json.Property;\n"
jtulach@1011
   176
            + "@Model(className=\"XModel\", properties={\n"
jtulach@1011
   177
        );
jtulach@1011
   178
        for (int i = 1; i <= cnt; i++) {
jtulach@1011
   179
            code.append("  @Property(name=\"prop").append(i).append("\", ");
jtulach@1011
   180
            code.append("type=int.class),\n");
jtulach@1011
   181
        }
jtulach@1011
   182
        for (int i = 1; i <= arrayCnt; i++) {
jtulach@1011
   183
            code.append("  @Property(name=\"array").append(i).append("\", ");
jtulach@1011
   184
            code.append("array=true, ");
jtulach@1011
   185
            code.append("type=int.class),\n");
jtulach@1011
   186
        }
jtulach@1011
   187
        code.append(""
jtulach@1011
   188
            + "})\n"
jtulach@1011
   189
            + "class X {\n"
jtulach@1011
   190
            + "    static {\n"
jtulach@1011
   191
            + "      new XModel();\n"
jtulach@1011
   192
            + "      new XModel("
jtulach@1011
   193
        );
jtulach@1011
   194
        if (constructorWithParams) {
jtulach@1011
   195
            code.append("0");
jtulach@1011
   196
            for (int i = 1; i < cnt; i++) {
jtulach@1011
   197
                code.append(",\n").append(i);
jtulach@1011
   198
            }
jtulach@1011
   199
        }
jtulach@1011
   200
        code.append(");\n"
jtulach@1011
   201
            + "    }\n"
jtulach@1011
   202
            + "}\n"
jtulach@1011
   203
        );
jtulach@1011
   204
jtulach@1011
   205
        Compile c = Compile.create(html, code.toString());
jtulach@1011
   206
        assertTrue(c.getErrors().isEmpty(), "Compiles OK: " + c.getErrors());
jtulach@1011
   207
    }
jtulach@1011
   208
jtulach@951
   209
    @Test public void writeableComputedPropertyMissingWrite() throws IOException {
jtulach@951
   210
        String html = "<html><body>"
jtulach@951
   211
            + "</body></html>";
jtulach@951
   212
        String code = "package x.y.z;\n"
jtulach@951
   213
            + "import net.java.html.json.Model;\n"
jtulach@951
   214
            + "import net.java.html.json.Property;\n"
jtulach@951
   215
            + "import net.java.html.json.ComputedProperty;\n"
jtulach@951
   216
            + "@Model(className=\"XModel\", properties={\n"
jtulach@951
   217
            + "  @Property(name=\"prop\", type=int.class)\n"
jtulach@951
   218
            + "})\n"
jtulach@951
   219
            + "class X {\n"
jtulach@951
   220
            + "    static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
jtulach@951
   221
            + "        return prop;\n"
jtulach@951
   222
            + "    }\n"
jtulach@951
   223
            + "}\n";
jtulach@951
   224
jtulach@951
   225
        Compile c = Compile.create(html, code);
jtulach@951
   226
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@951
   227
        boolean ok = false;
jtulach@951
   228
        StringBuilder msgs = new StringBuilder();
jtulach@951
   229
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jtulach@951
   230
            String msg = e.getMessage(Locale.ENGLISH);
jtulach@951
   231
            if (msg.contains("Cannot find setY")) {
jtulach@951
   232
                ok = true;
jtulach@951
   233
            }
jtulach@951
   234
            msgs.append("\n").append(msg);
jtulach@951
   235
        }
jtulach@951
   236
        if (!ok) {
jtulach@951
   237
            fail("Should contain warning about non-static method:" + msgs);
jtulach@951
   238
        }
jtulach@951
   239
    }
jtulach@951
   240
jtulach@951
   241
    @Test public void writeableComputedPropertyWrongWriteType() throws IOException {
jtulach@951
   242
        String html = "<html><body>"
jtulach@951
   243
            + "</body></html>";
jtulach@951
   244
        String code = "package x.y.z;\n"
jtulach@951
   245
            + "import net.java.html.json.Model;\n"
jtulach@951
   246
            + "import net.java.html.json.Property;\n"
jtulach@951
   247
            + "import net.java.html.json.ComputedProperty;\n"
jtulach@951
   248
            + "@Model(className=\"XModel\", properties={\n"
jtulach@951
   249
            + "  @Property(name=\"prop\", type=int.class)\n"
jtulach@951
   250
            + "})\n"
jtulach@951
   251
            + "class X {\n"
jtulach@951
   252
            + "    static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
jtulach@951
   253
            + "        return prop;\n"
jtulach@951
   254
            + "    }\n"
jtulach@954
   255
            + "    static void setY(XModel model, String prop) {\n"
jtulach@951
   256
            + "    }\n"
jtulach@951
   257
            + "}\n";
jtulach@951
   258
jtulach@951
   259
        Compile c = Compile.create(html, code);
jtulach@951
   260
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@951
   261
        boolean ok = false;
jtulach@951
   262
        StringBuilder msgs = new StringBuilder();
jtulach@951
   263
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jtulach@951
   264
            String msg = e.getMessage(Locale.ENGLISH);
jtulach@951
   265
            if (msg.contains("Write method first argument needs to be XModel and second int or Object")) {
jtulach@951
   266
                ok = true;
jtulach@951
   267
            }
jtulach@951
   268
            msgs.append("\n").append(msg);
jtulach@951
   269
        }
jtulach@951
   270
        if (!ok) {
jtulach@951
   271
            fail("Should contain warning about non-static method:" + msgs);
jtulach@951
   272
        }
jtulach@951
   273
    }
jtulach@951
   274
jtulach@955
   275
    @Test public void writeableComputedPropertyReturnsVoid() throws IOException {
jtulach@955
   276
        String html = "<html><body>"
jtulach@955
   277
            + "</body></html>";
jtulach@955
   278
        String code = "package x.y.z;\n"
jtulach@955
   279
            + "import net.java.html.json.Model;\n"
jtulach@955
   280
            + "import net.java.html.json.Property;\n"
jtulach@955
   281
            + "import net.java.html.json.ComputedProperty;\n"
jtulach@955
   282
            + "@Model(className=\"XModel\", properties={\n"
jtulach@955
   283
            + "  @Property(name=\"prop\", type=int.class)\n"
jtulach@955
   284
            + "})\n"
jtulach@955
   285
            + "class X {\n"
jtulach@955
   286
            + "    static @ComputedProperty(write=\"setY\") int y(int prop) {\n"
jtulach@955
   287
            + "        return prop;\n"
jtulach@955
   288
            + "    }\n"
jtulach@955
   289
            + "    static Number setY(XModel model, int prop) {\n"
jtulach@955
   290
            + "    }\n"
jtulach@955
   291
            + "}\n";
jtulach@955
   292
jtulach@955
   293
        Compile c = Compile.create(html, code);
jtulach@955
   294
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@955
   295
        boolean ok = false;
jtulach@955
   296
        StringBuilder msgs = new StringBuilder();
jtulach@955
   297
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jtulach@955
   298
            String msg = e.getMessage(Locale.ENGLISH);
jtulach@955
   299
            if (msg.contains("Write method has to return void")) {
jtulach@955
   300
                ok = true;
jtulach@955
   301
            }
jtulach@955
   302
            msgs.append("\n").append(msg);
jtulach@955
   303
        }
jtulach@955
   304
        if (!ok) {
jtulach@955
   305
            fail("Should contain warning about non-static method:" + msgs);
jtulach@955
   306
        }
jtulach@955
   307
    }
jtulach@955
   308
jaroslav@235
   309
    @Test public void computedCantReturnVoid() throws IOException {
jaroslav@235
   310
        String html = "<html><body>"
jaroslav@235
   311
            + "</body></html>";
jaroslav@235
   312
        String code = "package x.y.z;\n"
jaroslav@235
   313
            + "import net.java.html.json.Model;\n"
jaroslav@235
   314
            + "import net.java.html.json.Property;\n"
jaroslav@235
   315
            + "import net.java.html.json.ComputedProperty;\n"
jaroslav@235
   316
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@235
   317
            + "  @Property(name=\"prop\", type=int.class)\n"
jaroslav@235
   318
            + "})\n"
jaroslav@235
   319
            + "class X {\n"
jaroslav@235
   320
            + "    @ComputedProperty static void y(int prop) {\n"
jaroslav@235
   321
            + "    }\n"
jaroslav@235
   322
            + "}\n";
jtulach@940
   323
jaroslav@235
   324
        Compile c = Compile.create(html, code);
jaroslav@235
   325
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jaroslav@235
   326
        boolean ok = false;
jaroslav@235
   327
        StringBuilder msgs = new StringBuilder();
jaroslav@235
   328
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jaroslav@235
   329
            String msg = e.getMessage(Locale.ENGLISH);
jaroslav@235
   330
            if (msg.contains("y cannot return void")) {
jaroslav@235
   331
                ok = true;
jaroslav@235
   332
            }
jaroslav@235
   333
            msgs.append("\n").append(msg);
jaroslav@235
   334
        }
jaroslav@235
   335
        if (!ok) {
jaroslav@235
   336
            fail("Should contain warning about non-static method:" + msgs);
jaroslav@235
   337
        }
jaroslav@235
   338
    }
jtulach@940
   339
jaroslav@235
   340
    @Test public void computedCantReturnRunnable() throws IOException {
jaroslav@235
   341
        String html = "<html><body>"
jaroslav@235
   342
            + "</body></html>";
jaroslav@235
   343
        String code = "package x.y.z;\n"
jaroslav@235
   344
            + "import net.java.html.json.Model;\n"
jaroslav@235
   345
            + "import net.java.html.json.Property;\n"
jaroslav@235
   346
            + "import net.java.html.json.ComputedProperty;\n"
jaroslav@235
   347
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@235
   348
            + "  @Property(name=\"prop\", type=int.class)\n"
jaroslav@235
   349
            + "})\n"
jaroslav@235
   350
            + "class X {\n"
jaroslav@235
   351
            + "    @ComputedProperty static Runnable y(int prop) {\n"
jaroslav@235
   352
            + "       return null;\n"
jaroslav@235
   353
            + "    }\n"
jaroslav@235
   354
            + "}\n";
jtulach@940
   355
jaroslav@235
   356
        Compile c = Compile.create(html, code);
jaroslav@235
   357
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jaroslav@235
   358
        boolean ok = false;
jaroslav@235
   359
        StringBuilder msgs = new StringBuilder();
jaroslav@235
   360
        for (Diagnostic<? extends JavaFileObject> e : c.getErrors()) {
jaroslav@235
   361
            String msg = e.getMessage(Locale.ENGLISH);
jaroslav@235
   362
            if (msg.contains("y cannot return java.lang.Runnable")) {
jaroslav@235
   363
                ok = true;
jaroslav@235
   364
            }
jaroslav@235
   365
            msgs.append("\n").append(msg);
jaroslav@235
   366
        }
jaroslav@235
   367
        if (!ok) {
jaroslav@235
   368
            fail("Should contain warning about non-static method:" + msgs);
jaroslav@235
   369
        }
jaroslav@235
   370
    }
jtulach@940
   371
jaroslav@68
   372
    @Test public void canWeCompileWithJDK1_5SourceLevel() throws IOException {
jaroslav@68
   373
        String html = "<html><body>"
jaroslav@68
   374
            + "</body></html>";
jaroslav@68
   375
        String code = "package x.y.z;\n"
jaroslav@68
   376
            + "import net.java.html.json.Model;\n"
jaroslav@68
   377
            + "import net.java.html.json.Property;\n"
jaroslav@68
   378
            + "import net.java.html.json.ComputedProperty;\n"
jaroslav@68
   379
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@68
   380
            + "  @Property(name=\"prop\", type=long.class)\n"
jaroslav@68
   381
            + "})\n"
jaroslav@68
   382
            + "class X {\n"
jaroslav@68
   383
            + "  @ComputedProperty static double derived(long prop) { return prop; }"
jaroslav@68
   384
            + "}\n";
jtulach@940
   385
jaroslav@68
   386
        Compile c = Compile.create(html, code, "1.5");
jaroslav@68
   387
        assertTrue(c.getErrors().isEmpty(), "No errors: " + c.getErrors());
jaroslav@68
   388
    }
jtulach@940
   389
jaroslav@70
   390
    @Test public void putNeedsDataArgument() throws Exception {
jaroslav@70
   391
        needsAnArg("PUT");
jaroslav@70
   392
    }
jaroslav@70
   393
jaroslav@70
   394
    @Test public void postNeedsDataArgument() throws Exception {
jaroslav@70
   395
        needsAnArg("POST");
jaroslav@70
   396
    }
jtulach@940
   397
jaroslav@70
   398
    private void needsAnArg(String method) throws Exception {
jaroslav@70
   399
        String html = "<html><body>"
jaroslav@70
   400
            + "</body></html>";
jaroslav@70
   401
        String code = "package x.y.z;\n"
jaroslav@70
   402
            + "import net.java.html.json.Model;\n"
jaroslav@70
   403
            + "import net.java.html.json.Property;\n"
jaroslav@70
   404
            + "import net.java.html.json.OnReceive;\n"
jaroslav@70
   405
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@70
   406
            + "  @Property(name=\"prop\", type=long.class)\n"
jaroslav@70
   407
            + "})\n"
jaroslav@70
   408
            + "class X {\n"
jaroslav@70
   409
            + "  @Model(className=\"PQ\", properties={})\n"
jaroslav@70
   410
            + "  class PImpl {\n"
jaroslav@70
   411
            + "  }\n"
jaroslav@70
   412
            + "  @OnReceive(method=\"" + method + "\", url=\"whereever\")\n"
jaroslav@70
   413
            + "  static void obtained(XModel m, PQ p) { }\n"
jaroslav@70
   414
            + "}\n";
jtulach@940
   415
jaroslav@70
   416
        Compile c = Compile.create(html, code);
jaroslav@70
   417
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jaroslav@70
   418
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jaroslav@70
   419
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jaroslav@70
   420
            if (msg.contains("specify a data()")) {
jaroslav@70
   421
                return;
jaroslav@70
   422
            }
jaroslav@70
   423
        }
jaroslav@70
   424
        fail("Needs an error message about missing data():\n" + c.getErrors());
jtulach@940
   425
jaroslav@70
   426
    }
jtulach@940
   427
jtulach@940
   428
jaroslav@73
   429
    @Test public void jsonNeedsToUseGet () throws Exception {
jaroslav@73
   430
        String html = "<html><body>"
jaroslav@73
   431
            + "</body></html>";
jaroslav@73
   432
        String code = "package x.y.z;\n"
jaroslav@73
   433
            + "import net.java.html.json.Model;\n"
jaroslav@73
   434
            + "import net.java.html.json.Property;\n"
jaroslav@73
   435
            + "import net.java.html.json.OnReceive;\n"
jaroslav@73
   436
            + "@Model(className=\"XModel\", properties={\n"
jaroslav@73
   437
            + "  @Property(name=\"prop\", type=long.class)\n"
jaroslav@73
   438
            + "})\n"
jaroslav@73
   439
            + "class X {\n"
jaroslav@73
   440
            + "  @Model(className=\"PQ\", properties={})\n"
jaroslav@73
   441
            + "  class PImpl {\n"
jaroslav@73
   442
            + "  }\n"
jaroslav@73
   443
            + "  @OnReceive(method=\"POST\", jsonp=\"callback\", url=\"whereever\")\n"
jaroslav@73
   444
            + "  static void obtained(XModel m, PQ p) { }\n"
jaroslav@73
   445
            + "}\n";
jtulach@940
   446
jaroslav@73
   447
        Compile c = Compile.create(html, code);
jaroslav@73
   448
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jaroslav@73
   449
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jaroslav@73
   450
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jaroslav@73
   451
            if (msg.contains("JSONP works only with GET")) {
jaroslav@73
   452
                return;
jaroslav@73
   453
            }
jaroslav@73
   454
        }
jaroslav@73
   455
        fail("Needs an error message about wrong method:\n" + c.getErrors());
jtulach@940
   456
jaroslav@73
   457
    }
jtulach@940
   458
jtulach@940
   459
    @Test public void noHeadersForWebSockets() throws Exception {
jtulach@940
   460
        String html = "<html><body>"
jtulach@940
   461
            + "</body></html>";
jtulach@940
   462
        String code = "package x.y.z;\n"
jtulach@940
   463
            + "import net.java.html.json.Model;\n"
jtulach@940
   464
            + "import net.java.html.json.Property;\n"
jtulach@940
   465
            + "import net.java.html.json.OnReceive;\n"
jtulach@940
   466
            + "@Model(className=\"XModel\", properties={\n"
jtulach@940
   467
            + "  @Property(name=\"prop\", type=long.class)\n"
jtulach@940
   468
            + "})\n"
jtulach@940
   469
            + "class X {\n"
jtulach@940
   470
            + "  @Model(className=\"PQ\", properties={})\n"
jtulach@940
   471
            + "  class PImpl {\n"
jtulach@940
   472
            + "  }\n"
jtulach@940
   473
            + "  @OnReceive(method=\"WebSocket\", data = PQ.class, headers=\"SomeHeader: {some}\", url=\"whereever\")\n"
jtulach@940
   474
            + "  static void obtained(XModel m, PQ p) { }\n"
jtulach@940
   475
            + "}\n";
jtulach@940
   476
jtulach@940
   477
        Compile c = Compile.create(html, code);
jtulach@940
   478
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@940
   479
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jtulach@940
   480
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jtulach@940
   481
            if (msg.contains("WebSocket spec does not support headers")) {
jtulach@940
   482
                return;
jtulach@940
   483
            }
jtulach@940
   484
        }
jtulach@940
   485
        fail("Needs an error message about headers:\n" + c.getErrors());
jtulach@940
   486
jtulach@940
   487
    }
jtulach@940
   488
jtulach@948
   489
    @Test public void webSocketsWithoutDataIsError() throws Exception {
jtulach@948
   490
        String html = "<html><body>"
jtulach@948
   491
            + "</body></html>";
jtulach@948
   492
        String code = "package x.y.z;\n"
jtulach@948
   493
            + "import net.java.html.json.Model;\n"
jtulach@948
   494
            + "import net.java.html.json.Property;\n"
jtulach@948
   495
            + "import net.java.html.json.OnReceive;\n"
jtulach@948
   496
            + "@Model(className=\"XModel\", properties={\n"
jtulach@948
   497
            + "  @Property(name=\"prop\", type=long.class)\n"
jtulach@948
   498
            + "})\n"
jtulach@948
   499
            + "class X {\n"
jtulach@948
   500
            + "  @Model(className=\"PQ\", properties={})\n"
jtulach@948
   501
            + "  class PImpl {\n"
jtulach@948
   502
            + "  }\n"
jtulach@948
   503
            + "  @OnReceive(method=\"WebSocket\", url=\"whereever\")\n"
jtulach@948
   504
            + "  static void obtained(XModel m, PQ p) { }\n"
jtulach@948
   505
            + "}\n";
jtulach@948
   506
jtulach@948
   507
        Compile c = Compile.create(html, code);
jtulach@948
   508
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@948
   509
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jtulach@948
   510
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jtulach@948
   511
            if (msg.contains("eeds to specify a data()")) {
jtulach@948
   512
                return;
jtulach@948
   513
            }
jtulach@948
   514
        }
jtulach@948
   515
        fail("Needs data attribute :\n" + c.getErrors());
jtulach@948
   516
    }
jtulach@948
   517
jtulach@940
   518
    @Test public void noNewLinesInHeaderLines() throws Exception {
jtulach@940
   519
        String html = "<html><body>"
jtulach@940
   520
            + "</body></html>";
jtulach@940
   521
        String code = "package x.y.z;\n"
jtulach@940
   522
            + "import net.java.html.json.Model;\n"
jtulach@940
   523
            + "import net.java.html.json.Property;\n"
jtulach@940
   524
            + "import net.java.html.json.OnReceive;\n"
jtulach@940
   525
            + "@Model(className=\"XModel\", properties={\n"
jtulach@940
   526
            + "  @Property(name=\"prop\", type=long.class)\n"
jtulach@940
   527
            + "})\n"
jtulach@940
   528
            + "class X {\n"
jtulach@940
   529
            + "  @Model(className=\"PQ\", properties={})\n"
jtulach@940
   530
            + "  class PImpl {\n"
jtulach@940
   531
            + "  }\n"
jtulach@940
   532
            + "  @OnReceive(headers=\"SomeHeader\\n: {some}\", url=\"whereever\")\n"
jtulach@940
   533
            + "  static void obtained(XModel m, PQ p) { }\n"
jtulach@940
   534
            + "}\n";
jtulach@940
   535
jtulach@940
   536
        Compile c = Compile.create(html, code);
jtulach@940
   537
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@940
   538
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jtulach@940
   539
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jtulach@940
   540
            if (msg.contains("Header line cannot contain line separator")) {
jtulach@940
   541
                return;
jtulach@940
   542
            }
jtulach@940
   543
        }
jtulach@940
   544
        fail("Needs an error message about headers:\n" + c.getErrors());
jtulach@940
   545
jtulach@940
   546
    }
jtulach@940
   547
jtulach@940
   548
    @Test public void noReturnInHeaderLines() throws Exception {
jtulach@940
   549
        String html = "<html><body>"
jtulach@940
   550
            + "</body></html>";
jtulach@940
   551
        String code = "package x.y.z;\n"
jtulach@940
   552
            + "import net.java.html.json.Model;\n"
jtulach@940
   553
            + "import net.java.html.json.Property;\n"
jtulach@940
   554
            + "import net.java.html.json.OnReceive;\n"
jtulach@940
   555
            + "@Model(className=\"XModel\", properties={\n"
jtulach@940
   556
            + "  @Property(name=\"prop\", type=long.class)\n"
jtulach@940
   557
            + "})\n"
jtulach@940
   558
            + "class X {\n"
jtulach@940
   559
            + "  @Model(className=\"PQ\", properties={})\n"
jtulach@940
   560
            + "  class PImpl {\n"
jtulach@940
   561
            + "  }\n"
jtulach@940
   562
            + "  @OnReceive(headers=\"Some\\rHeader: {some}\", url=\"whereever\")\n"
jtulach@940
   563
            + "  static void obtained(XModel m, PQ p) { }\n"
jtulach@940
   564
            + "}\n";
jtulach@940
   565
jtulach@940
   566
        Compile c = Compile.create(html, code);
jtulach@940
   567
        assertFalse(c.getErrors().isEmpty(), "One error: " + c.getErrors());
jtulach@940
   568
        for (Diagnostic<? extends JavaFileObject> diagnostic : c.getErrors()) {
jtulach@940
   569
            String msg = diagnostic.getMessage(Locale.ENGLISH);
jtulach@940
   570
            if (msg.contains("Header line cannot contain line separator")) {
jtulach@940
   571
                return;
jtulach@940
   572
            }
jtulach@940
   573
        }
jtulach@940
   574
        fail("Needs an error message about headers:\n" + c.getErrors());
jtulach@940
   575
jtulach@940
   576
    }
jtulach@940
   577
jaroslav@245
   578
    @Test public void onErrorHasToExist() throws IOException {
jaroslav@245
   579
        Compile res = Compile.create("", "package x;\n"
jaroslav@245
   580
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jaroslav@245
   581
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jaroslav@245
   582
            + "})\n"
jaroslav@245
   583
            + "class UseOnReceive {\n"
jaroslav@245
   584
            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"doesNotExist\")\n"
jaroslav@245
   585
            + "  static void onMessage(MyModel model, String value) {\n"
jaroslav@245
   586
            + "  }\n"
jaroslav@245
   587
            + "}\n"
jaroslav@245
   588
        );
jaroslav@245
   589
        res.assertErrors();
jaroslav@245
   590
        res.assertError("not find doesNotExist");
jaroslav@245
   591
    }
jtulach@940
   592
jtulach@647
   593
    @Test public void usingListIsOK() throws IOException {
jtulach@647
   594
        Compile res = Compile.create("", "package x;\n"
jtulach@647
   595
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jtulach@647
   596
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jtulach@647
   597
            + "})\n"
jtulach@647
   598
            + "class UseOnReceive {\n"
jtulach@647
   599
            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\")\n"
jtulach@647
   600
            + "  static void onMessage(MyModel model, java.util.List<MyData> value) {\n"
jtulach@647
   601
            + "  }\n"
jtulach@647
   602
            + "\n"
jtulach@647
   603
            + "  @net.java.html.json.Model(className=\"MyData\", properties={\n"
jtulach@647
   604
            + "  })\n"
jtulach@647
   605
            + "  static class MyDataModel {\n"
jtulach@647
   606
            + "  }\n"
jtulach@647
   607
            + "}\n"
jtulach@647
   608
        );
jtulach@647
   609
        res.assertNoErrors();
jtulach@647
   610
    }
jaroslav@245
   611
jtulach@648
   612
    @Test public void functionAndPropertyCollide() throws IOException {
jtulach@648
   613
        Compile res = Compile.create("", "package x;\n"
jtulach@648
   614
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jtulach@648
   615
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jtulach@648
   616
            + "})\n"
jtulach@648
   617
            + "class Collision {\n"
jtulach@648
   618
            + "  @net.java.html.json.Function\n"
jtulach@648
   619
            + "  static void x(MyModel model, String value) {\n"
jtulach@648
   620
            + "  }\n"
jtulach@648
   621
            + "}\n"
jtulach@648
   622
        );
jtulach@648
   623
        res.assertErrors();
jtulach@648
   624
        res.assertError("cannot have the name");
jtulach@648
   625
    }
jtulach@648
   626
jtulach@648
   627
    @Test public void twoPropertiesCollide() throws IOException {
jtulach@648
   628
        Compile res = Compile.create("", "package x;\n"
jtulach@648
   629
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jtulach@648
   630
            + "  @net.java.html.json.Property(name=\"x\", type=String.class),\n"
jtulach@648
   631
            + "  @net.java.html.json.Property(name=\"x\", type=int.class)\n"
jtulach@648
   632
            + "})\n"
jtulach@648
   633
            + "class Collision {\n"
jtulach@648
   634
            + "}\n"
jtulach@648
   635
        );
jtulach@648
   636
        res.assertErrors();
jtulach@648
   637
        res.assertError("Cannot have the name");
jtulach@648
   638
    }
jtulach@648
   639
jtulach@648
   640
    @Test public void propertyAndComputedOneCollide() throws IOException {
jtulach@648
   641
        Compile res = Compile.create("", "package x;\n"
jtulach@648
   642
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jtulach@648
   643
            + "  @net.java.html.json.Property(name=\"x\", type=String.class),\n"
jtulach@648
   644
            + "})\n"
jtulach@648
   645
            + "class Collision {\n"
jtulach@648
   646
            + "  @net.java.html.json.ComputedProperty static int x(String x) {\n"
jtulach@648
   647
            + "    return x.length();\n"
jtulach@648
   648
            + "  }\n"
jtulach@648
   649
            + "}\n"
jtulach@648
   650
        );
jtulach@648
   651
        res.assertErrors();
jtulach@648
   652
        res.assertError("Cannot have the name");
jtulach@648
   653
    }
jtulach@940
   654
jtulach@650
   655
    @Test public void onWebSocketJustTwoArgs() throws IOException {
jtulach@650
   656
        Compile res = Compile.create("", "package x;\n"
jtulach@650
   657
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jtulach@650
   658
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jtulach@650
   659
            + "})\n"
jtulach@650
   660
            + "class UseOnReceive {\n"
jtulach@948
   661
            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\", method=\"WebSocket\", data=String.class)\n"
jtulach@650
   662
            + "  static void onMessage(MyModel model, String value, int arg) {\n"
jtulach@650
   663
            + "  }\n"
jtulach@650
   664
            + "}\n"
jtulach@650
   665
        );
jtulach@650
   666
        res.assertErrors();
jtulach@650
   667
        res.assertError("only have two arg");
jtulach@650
   668
    }
jtulach@940
   669
jaroslav@245
   670
    @Test public void onErrorWouldHaveToBeStatic() throws IOException {
jaroslav@245
   671
        Compile res = Compile.create("", "package x;\n"
jaroslav@245
   672
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jaroslav@245
   673
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jaroslav@245
   674
            + "})\n"
jaroslav@245
   675
            + "class UseOnReceive {\n"
jaroslav@245
   676
            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"notStatic\")\n"
jaroslav@245
   677
            + "  static void onMessage(MyModel model, String value) {\n"
jaroslav@245
   678
            + "  }\n"
jaroslav@245
   679
            + "  void notStatic(Exception e) {}\n"
jaroslav@245
   680
            + "}\n"
jaroslav@245
   681
        );
jaroslav@245
   682
        res.assertErrors();
jaroslav@245
   683
        res.assertError("have to be static");
jaroslav@245
   684
    }
jaroslav@245
   685
jaroslav@245
   686
    @Test public void onErrorMustAcceptExceptionArgument() throws IOException {
jaroslav@245
   687
        Compile res = Compile.create("", "package x;\n"
jaroslav@245
   688
            + "@net.java.html.json.Model(className=\"MyModel\", properties= {\n"
jaroslav@245
   689
            + "  @net.java.html.json.Property(name=\"x\", type=String.class)\n"
jaroslav@245
   690
            + "})\n"
jaroslav@245
   691
            + "class UseOnReceive {\n"
jaroslav@245
   692
            + "  @net.java.html.json.OnReceive(url=\"http://nowhere.com\", onError=\"subclass\")\n"
jaroslav@245
   693
            + "  static void onMessage(MyModel model, String value) {\n"
jaroslav@245
   694
            + "  }\n"
jaroslav@245
   695
            + "  static void subclass(java.io.IOException e) {}\n"
jaroslav@245
   696
            + "}\n"
jaroslav@245
   697
        );
jaroslav@245
   698
        res.assertErrors();
jaroslav@245
   699
        res.assertError("Error method first argument needs to be MyModel and second Exception");
jaroslav@245
   700
    }
jtulach@3
   701
}