boot/src/test/java/org/netbeans/html/boot/impl/JavaScriptProcesorTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 531 ff48a4a875ff
child 790 30f20d9c0986
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@184
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@184
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@184
     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.
jaroslav@184
     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.
jaroslav@184
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.impl;
jaroslav@184
    44
jaroslav@184
    45
import java.io.IOException;
jaroslav@187
    46
import java.lang.reflect.Field;
jaroslav@186
    47
import java.lang.reflect.Method;
jaroslav@186
    48
import static org.testng.Assert.assertEquals;
jaroslav@187
    49
import static org.testng.Assert.assertTrue;
jaroslav@184
    50
import org.testng.annotations.Test;
jaroslav@184
    51
jaroslav@184
    52
/**
jaroslav@184
    53
 *
jaroslav@184
    54
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@184
    55
 */
jaroslav@184
    56
public class JavaScriptProcesorTest {
jaroslav@184
    57
    
jaroslav@184
    58
    @Test public void detectCallbackToNonExistingClass() throws IOException {
jaroslav@184
    59
        String code = "package x.y.z;\n"
jaroslav@184
    60
            + "import net.java.html.js.JavaScriptBody;\n"
jaroslav@184
    61
            + "class X {\n"
jaroslav@184
    62
            + "  @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
jaroslav@184
    63
            + "    \"r.@java.lang.Runable::run()();\"\n" // typo
jaroslav@184
    64
            + "  )\n"
jaroslav@184
    65
            + "  private static native void callback(Runnable r);\n"
jaroslav@184
    66
            + "}\n";
jaroslav@184
    67
        
jaroslav@184
    68
        Compile c = Compile.create("", code);
jaroslav@184
    69
        c.assertErrors();
jaroslav@184
    70
        c.assertError("java.lang.Runable"); // typo
jaroslav@184
    71
    }
jaroslav@185
    72
jaroslav@185
    73
    @Test public void detectCallbackToNonExistingMethod() throws IOException {
jaroslav@185
    74
        String code = "package x.y.z;\n"
jaroslav@185
    75
            + "import net.java.html.js.JavaScriptBody;\n"
jaroslav@185
    76
            + "class X {\n"
jaroslav@185
    77
            + "  @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
jaroslav@185
    78
            + "    \"r.@java.lang.Runnable::cancel()();\"\n"
jaroslav@185
    79
            + "  )\n"
jaroslav@185
    80
            + "  private static native void callback(Runnable r);\n"
jaroslav@185
    81
            + "}\n";
jaroslav@185
    82
        
jaroslav@185
    83
        Compile c = Compile.create("", code);
jaroslav@185
    84
        c.assertErrors();
jaroslav@185
    85
        c.assertError("method cancel");
jaroslav@185
    86
    }
jaroslav@185
    87
jaroslav@185
    88
    @Test public void detectCallbackToNonExistingParams() throws IOException {
jaroslav@185
    89
        String code = "package x.y.z;\n"
jaroslav@185
    90
            + "import net.java.html.js.JavaScriptBody;\n"
jaroslav@185
    91
            + "class X {\n"
jaroslav@185
    92
            + "  @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
jaroslav@185
    93
            + "    \"r.@java.lang.Runnable::run(I)(10);\"\n"
jaroslav@185
    94
            + "  )\n"
jaroslav@185
    95
            + "  private static native void callback(Runnable r);\n"
jaroslav@185
    96
            + "}\n";
jaroslav@185
    97
        
jaroslav@185
    98
        Compile c = Compile.create("", code);
jaroslav@185
    99
        c.assertErrors();
jaroslav@185
   100
        c.assertError("wrong parameters: (I)");
jaroslav@185
   101
    }
jaroslav@185
   102
jaroslav@185
   103
    @Test public void objectTypeParamsAreOK() throws IOException {
jaroslav@185
   104
        String code = "package x.y.z;\n"
jaroslav@185
   105
            + "import net.java.html.js.JavaScriptBody;\n"
jaroslav@185
   106
            + "class X {\n"
jaroslav@185
   107
            + "  @JavaScriptBody(args={\"r\"}, javacall=true, body =\n"
jaroslav@185
   108
            + "    \"r.@java.lang.Object::equals(Ljava/lang/Object;)(null);\"\n"
jaroslav@185
   109
            + "  )\n"
jaroslav@185
   110
            + "  private static native void testEqual(Object r);\n"
jaroslav@185
   111
            + "}\n";
jaroslav@185
   112
        
jaroslav@185
   113
        Compile c = Compile.create("", code);
jaroslav@185
   114
        c.assertNoErrors();
jaroslav@185
   115
    }
jaroslav@531
   116
jaroslav@531
   117
    @Test public void needJavaScriptBodyToUseResource() throws IOException {
jaroslav@531
   118
        String code = "package x.y.z;\n"
jaroslav@531
   119
            + "import net.java.html.js.JavaScriptResource;\n"
jaroslav@531
   120
            + "@JavaScriptResource(\"x.html\")\n"
jaroslav@531
   121
            + "class X {\n"
jaroslav@531
   122
            + "  private static native void callback(Runnable r);\n"
jaroslav@531
   123
            + "}\n";
jaroslav@531
   124
        
jaroslav@531
   125
        Compile c = Compile.create("", code);
jaroslav@531
   126
        c.assertErrors();
jaroslav@531
   127
        c.assertError("needs @JavaScriptBody");
jaroslav@531
   128
    }
jaroslav@184
   129
    
jaroslav@186
   130
    @Test public void generatesCallbacksThatReturnObject() throws Exception {
jaroslav@362
   131
        Class<?> callbacksForTestPkg = Class.forName("org.netbeans.html.boot.impl.$JsCallbacks$");
jaroslav@194
   132
        Method m = callbacksForTestPkg.getDeclaredMethod("java_lang_Runnable$run$", Runnable.class);
jaroslav@186
   133
        assertEquals(m.getReturnType(), Object.class, "All methods always return object");
jaroslav@186
   134
    }
jaroslav@187
   135
    
jaroslav@187
   136
    @Test public void hasInstanceField() throws Exception {
jaroslav@362
   137
        Class<?> callbacksForTestPkg = Class.forName("org.netbeans.html.boot.impl.$JsCallbacks$");
jaroslav@187
   138
        Field f = callbacksForTestPkg.getDeclaredField("VM");
jaroslav@187
   139
        f.setAccessible(true);
jaroslav@187
   140
        assertTrue(callbacksForTestPkg.isInstance(f.get(null)), "Singleton field VM");
jaroslav@187
   141
    }
jaroslav@184
   142
}