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