json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 710 28766ecb3f72
child 838 bdc3d696dd4a
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
     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
    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     private R asyncRun;
    81     @KOTest public void asyncCallbackToRunnable() throws InterruptedException {
    82         if (asyncRun == null) {
    83             asyncRun = new R();
    84             Bodies.asyncCallback(asyncRun);
    85         }
    86         if (asyncRun.cnt == 0) {
    87             throw new InterruptedException();
    88         }
    89         assert asyncRun.cnt == 1 : "Even async callback must arrive once: " + asyncRun.cnt;
    90     }
    91 
    92     @KOTest public void asyncCallbackFlushed() throws InterruptedException {
    93         R r = new R();
    94         for (int i = 0; i < 10; i++) {
    95             Bodies.asyncCallback(r);
    96         }
    97         int fourtyTwo = Bodies.sum(35, 7);
    98         assert r.cnt == 10 : "Ten calls: " + r.cnt;
    99         assert fourtyTwo == 42 : "Meaning of the world expected: " + fourtyTwo;
   100     }
   101     
   102     @KOTest public void typeOfCharacter() {
   103         String charType = Bodies.typeof('a', false);
   104         assert "number".equals(charType) : "Expecting number type: " + charType;
   105     }
   106     @KOTest public void typeOfBoolean() {
   107         String booleanType = Bodies.typeof(true, false);
   108         assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
   109     }
   110 
   111     @KOTest public void typeOfPrimitiveBoolean() {
   112         String booleanType = Bodies.typeof(true);
   113         assert "boolean".equals(booleanType) || "number".equals(booleanType): 
   114             "Expecting boolean or at least number type: " + booleanType;
   115     }
   116 
   117     @KOTest public void typeOfInteger() {
   118         String intType = Bodies.typeof(1, false);
   119         assert "number".equals(intType) : "Expecting number type: " + intType;
   120     }
   121 
   122     @KOTest public void typeOfString() {
   123         String strType = Bodies.typeof("Ahoj", false);
   124         assert "string".equals(strType) : "Expecting string type: " + strType;
   125     }
   126 
   127     @KOTest public void typeOfDouble() {
   128         String doubleType = Bodies.typeof(0.33, false);
   129         assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
   130     }
   131     
   132     @KOTest public void typeOfBooleanValueOf() {
   133         String booleanType = Bodies.typeof(true, true);
   134         assert "boolean".equals(booleanType) : "Expecting boolean type: " + booleanType;
   135     }
   136 
   137     @KOTest public void typeOfIntegerValueOf() {
   138         String intType = Bodies.typeof(1, true);
   139         assert "number".equals(intType) : "Expecting number type: " + intType;
   140     }
   141 
   142     @KOTest public void typeOfStringValueOf() {
   143         String strType = Bodies.typeof("Ahoj", true);
   144         assert "string".equals(strType) : "Expecting string type: " + strType;
   145     }
   146 
   147     @KOTest public void typeOfDoubleValueOf() {
   148         String doubleType = Bodies.typeof(0.33, true);
   149         assert "number".equals(doubleType) : "Expecting number type: " + doubleType;
   150     }
   151 
   152     @KOTest public void computeInARunnable() {
   153         final int[] sum = new int[2];
   154         class First implements Runnable {
   155             @Override public void run() {
   156                 sum[0] = Bodies.sum(22, 20);
   157                 sum[1] = Bodies.sum(32, 10);
   158             }
   159         }
   160         Bodies.callback(new First());
   161         assert sum[0] == 42 : "Computed OK " + sum[0];
   162         assert sum[1] == 42 : "Computed OK too: " + sum[1];
   163     }
   164     
   165     @KOTest public void doubleCallbackToRunnable() {
   166         final R run = new R();
   167         final R r2 = new R();
   168         class First implements Runnable {
   169             @Override public void run() {
   170                 Bodies.callback(run);
   171                 Bodies.callback(r2);
   172             }
   173         }
   174         Bodies.callback(new First());
   175         assert run.cnt == 1 : "Can call even private implementation classes: " + run.cnt;
   176         assert r2.cnt == 1 : "Can call even private implementation classes: " + r2.cnt;
   177     }
   178     
   179     @KOTest public void identity() {
   180         Object p = new Object();
   181         Object r = Bodies.id(p);
   182         assert r == p : "The object is the same";
   183     }
   184 
   185     @KOTest public void encodingString() {
   186         Object p = "Ji\n\"Hi\"\nHon";
   187         Object r = Bodies.id(p);
   188         assert p.equals(r) : "The object is the same: " + p + " != " + r;
   189     }
   190 
   191     @KOTest public void encodingBackslashString() {
   192         Object p = "{\"firstName\":\"/*\\n * Copyright (c) 2013\",\"lastName\":null,\"sex\":\"MALE\",\"address\":{\"street\":null}}";
   193         Object r = Bodies.id(p);
   194         assert p.equals(r) : "The object is the same: " + p + " != " + r;
   195     }
   196 
   197     @KOTest public void nullIsNull() {
   198         Object p = null;
   199         Object r = Bodies.id(p);
   200         assert r == p : "The null is the same";
   201     }
   202     
   203     @KOTest public void callbackWithResult() {
   204         Callable<Boolean> c = new C();
   205         Object b = Bodies.callback(c);
   206         assert b == Boolean.TRUE : "Should return true";
   207     }
   208     
   209     @KOTest public void callbackWithParameters() {
   210         int res = Bodies.sumIndirect(new Sum());
   211         assert res == 42 : "Expecting 42";
   212     }
   213     
   214     @KOTest public void selectFromStringJavaArray() {
   215         String[] arr = { "Ahoj", "World" };
   216         Object res = Bodies.select(arr, 1);
   217         assert "World".equals(res) : "Expecting World, but was: " + res;
   218     }
   219 
   220     @KOTest public void selectFromObjectJavaArray() {
   221         Object[] arr = { new Object(), new Object() };
   222         Object res = Bodies.select(arr, 1);
   223         assert arr[1].equals(res) : "Expecting " + arr[1] + ", but was: " + res;
   224     }
   225 
   226     @KOTest public void lengthOfJavaArray() {
   227         String[] arr = { "Ahoj", "World" };
   228         int res = Bodies.length(arr);
   229         assert res == 2 : "Expecting 2, but was: " + res;
   230     }
   231 
   232     @KOTest public void isJavaArray() {
   233         String[] arr = { "Ahoj", "World" };
   234         boolean is = Bodies.isArray(arr);
   235         assert is: "Expecting it to be an array: " + is;
   236     }
   237 
   238     @KOTest public void javaArrayInOutIsCopied() {
   239         String[] arr = { "Ahoj", "World" };
   240         Object res = Bodies.id(arr);
   241         assert res != null : "Non-null is returned";
   242         assert res instanceof Object[] : "Returned an array: " + res;
   243         assert !(res instanceof String[]) : "Not returned a string array: " + res;
   244         
   245         Object[] ret = (Object[]) res;
   246         assert arr.length == ret.length : "Same length: " + ret.length;
   247         assert arr[0].equals(ret[0]) : "Same first elem";
   248         assert arr[1].equals(ret[1]) : "Same 2nd elem";
   249     }
   250 
   251     @KOTest public void modifyJavaArrayHasNoEffect() {
   252         String[] arr = { "Ahoj", "World" };
   253         String value = Bodies.modify(arr, 0, "Hello");
   254         assert "Hello".equals(value) : "Inside JS the value is changed: " + value;
   255         assert "Ahoj".equals(arr[0]) : "From a Java point of view it remains: " + arr[0];
   256     }
   257 
   258     @KOTest
   259     public void callbackWithArray() {
   260         class A implements Callable<String[]> {
   261             @Override
   262             public String[] call() throws Exception {
   263                 return new String[] { "Hello" };
   264             }
   265         }
   266         Callable<String[]> a = new A();
   267         Object b = Bodies.callbackAndPush(a, "World!");
   268         assert b instanceof Object[] : "Returns an array: " + b;
   269         Object[] arr = (Object[]) b;
   270         String str = Arrays.toString(arr);
   271         assert arr.length == 2 : "Size is two " + str;
   272         assert "Hello".equals(arr[0]) : "Hello expected: " + arr[0];
   273         assert "World!".equals(arr[1]) : "World! expected: " + arr[1];
   274     }
   275     
   276     @KOTest public void sumVector() {
   277         double[] arr = { 1.0, 2.0, 3.0 };
   278         double res = Bodies.sumVector(arr);
   279         assert 6.0 == res : "Expecting six: " + res;
   280     }
   281 
   282     @KOTest public void sumMatrix() {
   283         double[][] arr = { { 1.0 }, { 1.0, 1.0 }, { 1.0, 1.0, 1.0 } };
   284         double res = Bodies.sumMatrix(arr);
   285         assert 6.0 == res : "Expecting six: " + res;
   286     }
   287 
   288     @KOTest public void truth() {
   289         assert Bodies.truth() : "True is true";
   290     }
   291     
   292     @KOTest public void factorial2() {
   293         assert new Factorial().factorial(2) == 2;
   294     }
   295     
   296     @KOTest public void factorial3() {
   297         assert new Factorial().factorial(3) == 6;
   298     }
   299     
   300     @KOTest public void factorial4() {
   301         assert new Factorial().factorial(4) == 24;
   302     }
   303     
   304     @KOTest public void factorial5() {
   305         assert new Factorial().factorial(5) == 120;
   306     }
   307     
   308     @KOTest public void factorial6() {
   309         assert new Factorial().factorial(6) == 720;
   310     }
   311     
   312     @KOTest public void sumArray() {
   313         int r = Bodies.sumArr(new Sum());
   314         assert r == 6 : "Sum is six: " + r;
   315     }
   316     
   317     @KOTest public void staticCallback() {
   318         int r = Bodies.staticCallback();
   319         assert r == 42 : "Expecting 42: " + r;
   320     }
   321     
   322     Later l;
   323     @KOTest public void callLater() throws Exception{
   324         final Fn.Presenter p = Fn.activePresenter();
   325         if (p == null) {
   326             return;
   327         }
   328         if (l == null) {
   329             p.loadScript(new StringReader(
   330                 "if (typeof window === 'undefined') window = {};"
   331             ));
   332             l = new Later();
   333             l.register();
   334             p.loadScript(new StringReader(
   335                 "window.later();"
   336             ));
   337         }
   338         if (l.call != 42) {
   339             throw new InterruptedException();
   340         }
   341         assert l.call == 42 : "Method was called: " + l.call;
   342     }
   343     
   344     private static class R implements Runnable {
   345         int cnt;
   346         private final Thread initThread;
   347         
   348         public R() {
   349             initThread = Thread.currentThread();
   350         }
   351 
   352         @Override
   353         public void run() {
   354             assert initThread == Thread.currentThread() : "Expecting to run in " + initThread + " but running in " + Thread.currentThread();
   355             cnt++;
   356         }
   357     }
   358     
   359     private static class C implements Callable<Boolean> {
   360         @Override
   361         public Boolean call() throws Exception {
   362             return Boolean.TRUE;
   363         }
   364     }
   365 }