json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 09 Jan 2014 20:39:23 +0100
branchUniversalKO
changeset 446 6dce58c06f58
parent 440 7012d021ae87
child 467 6d3613b454ab
permissions -rw-r--r--
ko4j registers implementation of Transfer and WSTransfer based on XHR and WebSocket from a browser. The Java implementations of these interfaces has been moved to ko-ws-tyrus module. JavaScript arrays passed as parameters to Java callback methods need to be wrapped.
     1 /**
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013-2013 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-2013 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.js.tests;
    44 
    45 import java.util.Arrays;
    46 import java.util.concurrent.Callable;
    47 import org.apidesign.html.json.tck.KOTest;
    48 
    49 /**
    50  *
    51  * @author Jaroslav Tulach <jtulach@netbeans.org>
    52  */
    53 public class JavaScriptBodyTest {
    54     @KOTest public void sumTwoNumbers() {
    55         int res = Bodies.sum(5, 3);
    56         assert res == 8 : "Expecting 8: " + res;
    57     }
    58     
    59     @KOTest public void accessJsObject() {
    60         Object o = Bodies.instance(10);
    61         int ten = Bodies.readX(o);
    62         assert ten == 10 : "Expecting ten: " + ten;
    63     }
    64 
    65     @KOTest public void callWithNoReturnType() {
    66         Object o = Bodies.instance(10);
    67         Bodies.incrementX(o);
    68         int ten = Bodies.readX(o);
    69         assert ten == 11 : "Expecting eleven: " + ten;
    70     }
    71     
    72     @KOTest public void callbackToRunnable() {
    73         R run = new R();
    74         Bodies.callback(run);
    75         assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
    76     }
    77     
    78     @KOTest public void typeOfCharacter() {
    79         String charType = Bodies.typeof('a', false);
    80         assert "number".equals(charType) : "Expecting number type: " + charType;
    81     }
    82     @KOTest public void typeOfBoolean() {
    83         String booleanType = Bodies.typeof(true, false);
    84         assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
    85     }
    86 
    87     @KOTest public void typeOfPrimitiveBoolean() {
    88         String booleanType = Bodies.typeof(true);
    89         assert "boolean".equals(booleanType) || "number".equals(booleanType): 
    90             "Expecting boolean or at least number type: " + booleanType;
    91     }
    92 
    93     @KOTest public void typeOfInteger() {
    94         String intType = Bodies.typeof(1, false);
    95         assert "number".equals(intType) : "Expecting number type: " + intType;
    96     }
    97 
    98     @KOTest public void typeOfString() {
    99         String strType = Bodies.typeof("Ahoj", false);
   100         assert "string".equals(strType) : "Expecting string type: " + strType;
   101     }
   102 
   103     @KOTest public void typeOfDouble() {
   104         String doubleType = Bodies.typeof(0.33, false);
   105         assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
   106     }
   107     
   108     @KOTest public void typeOfBooleanValueOf() {
   109         String booleanType = Bodies.typeof(true, true);
   110         assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
   111     }
   112 
   113     @KOTest public void typeOfIntegerValueOf() {
   114         String intType = Bodies.typeof(1, true);
   115         assert "number".equals(intType) : "Expecting number type: " + intType;
   116     }
   117 
   118     @KOTest public void typeOfStringValueOf() {
   119         String strType = Bodies.typeof("Ahoj", true);
   120         assert "string".equals(strType) : "Expecting string type: " + strType;
   121     }
   122 
   123     @KOTest public void typeOfDoubleValueOf() {
   124         String doubleType = Bodies.typeof(0.33, true);
   125         assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
   126     }
   127 
   128     @KOTest public void computeInARunnable() {
   129         final int[] sum = new int[2];
   130         class First implements Runnable {
   131             @Override public void run() {
   132                 sum[0] = Bodies.sum(22, 20);
   133                 sum[1] = Bodies.sum(32, 10);
   134             }
   135         }
   136         Bodies.callback(new First());
   137         assert sum[0] == 42 : "Computed OK " + sum[0];
   138         assert sum[1] == 42 : "Computed OK too: " + sum[1];
   139     }
   140     
   141     @KOTest public void doubleCallbackToRunnable() {
   142         final R run = new R();
   143         final R r2 = new R();
   144         class First implements Runnable {
   145             @Override public void run() {
   146                 Bodies.callback(run);
   147                 Bodies.callback(r2);
   148             }
   149         }
   150         Bodies.callback(new First());
   151         assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
   152         assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
   153     }
   154     
   155     @KOTest public void identity() {
   156         Object p = new Object();
   157         Object r = Bodies.id(p);
   158         assert r == p : "The object is the same";
   159     }
   160 
   161     @KOTest public void encodingString() {
   162         Object p = "Ji\n\"Hi\"\nHon";
   163         Object r = Bodies.id(p);
   164         assert p.equals(r) : "The object is the same: " + p + " != " + r;
   165     }
   166 
   167     @KOTest public void nullIsNull() {
   168         Object p = null;
   169         Object r = Bodies.id(p);
   170         assert r == p : "The null is the same";
   171     }
   172     
   173     @KOTest public void callbackWithResult() {
   174         Callable<Boolean> c = new C();
   175         Object b = Bodies.callback(c);
   176         assert b == Boolean.TRUE : "Should return true";
   177     }
   178     
   179     @KOTest public void callbackWithParameters() {
   180         int res = Bodies.sumIndirect(new Sum());
   181         assert res == 42 : "Expecting 42";
   182     }
   183     
   184     @KOTest public void selectFromStringJavaArray() {
   185         String[] arr = { "Ahoj", "World" };
   186         Object res = Bodies.select(arr, 1);
   187         assert "World".equals(res) : "Expecting World, but was: " + res;
   188     }
   189 
   190     @KOTest public void selectFromObjectJavaArray() {
   191         Object[] arr = { new Object(), new Object() };
   192         Object res = Bodies.select(arr, 1);
   193         assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
   194     }
   195 
   196     @KOTest public void lengthOfJavaArray() {
   197         String[] arr = { "Ahoj", "World" };
   198         int res = Bodies.length(arr);
   199         assert res == 2 : "Expecting 2, but was: " + res;
   200     }
   201 
   202     @KOTest public void isJavaArray() {
   203         String[] arr = { "Ahoj", "World" };
   204         boolean is = Bodies.isArray(arr);
   205         assert is: "Expecting it to be an array: " + is;
   206     }
   207 
   208     @KOTest public void javaArrayInOutIsCopied() {
   209         String[] arr = { "Ahoj", "World" };
   210         Object res = Bodies.id(arr);
   211         assert res != null : "Non-null is returned";
   212         assert res instanceof Object[] : "Returned an array: " + res;
   213         assert !(res instanceof String[]) : "Not returned a string array: " + res;
   214         
   215         Object[] ret = (Object[]) res;
   216         assert arr.length == ret.length : "Same length: " + ret.length;
   217         assert arr[0].equals(ret[0]) : "Same first elem";
   218         assert arr[1].equals(ret[1]) : "Same 2nd elem";
   219     }
   220 
   221     @KOTest public void modifyJavaArrayHasNoEffect() {
   222         String[] arr = { "Ahoj", "World" };
   223         String value = Bodies.modify(arr, 0, "Hello");
   224         assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
   225         assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
   226     }
   227 
   228     @KOTest
   229     public void callbackWithArray() {
   230         class A implements Callable<String[]> {
   231             @Override
   232             public String[] call() throws Exception {
   233                 return new String[] { "Hello" };
   234             }
   235         }
   236         Callable<String[]> a = new A();
   237         Object b = Bodies.callbackAndPush(a, "World!");
   238         assert b instanceof Object[] : "Returns an array: " + b;
   239         Object[] arr = (Object[]) b;
   240         String str = Arrays.toString(arr);
   241         assert arr.length == 2 : "Size is two " + str;
   242         assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
   243         assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
   244     }
   245 
   246     @KOTest public void truth() {
   247         assert Bodies.truth() : "True is true";
   248     }
   249     
   250     @KOTest public void factorial2() {
   251         assert new Factorial().factorial(2) == 2;
   252     }
   253     
   254     @KOTest public void factorial3() {
   255         assert new Factorial().factorial(3) == 6;
   256     }
   257     
   258     @KOTest public void factorial4() {
   259         assert new Factorial().factorial(4) == 24;
   260     }
   261     
   262     @KOTest public void factorial5() {
   263         assert new Factorial().factorial(5) == 120;
   264     }
   265     
   266     @KOTest public void factorial6() {
   267         assert new Factorial().factorial(6) == 720;
   268     }
   269     
   270     @KOTest public void sumArray() {
   271         int r = Bodies.sumArr(new Sum());
   272         assert r == 6 : "Sum is six: " + r;
   273     }
   274     
   275     private static class R implements Runnable {
   276         int cnt;
   277         private final Thread initThread;
   278         
   279         public R() {
   280             initThread = Thread.currentThread();
   281         }
   282 
   283         @Override
   284         public void run() {
   285             assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
   286             cnt++;
   287         }
   288     }
   289     
   290     private static class C implements Callable<Boolean> {
   291         @Override
   292         public Boolean call() throws Exception {
   293             return Boolean.TRUE;
   294         }
   295     }
   296 }