boot/src/test/java/org/netbeans/html/boot/impl/JsClassLoaderBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 655 7211ec5f3172
child 851 69ed96e7f41b
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
     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 org.netbeans.html.boot.impl;
    44 
    45 import java.io.Closeable;
    46 import java.lang.reflect.InvocationTargetException;
    47 import java.lang.reflect.Method;
    48 import java.lang.reflect.Modifier;
    49 import org.netbeans.html.boot.spi.Fn;
    50 import static org.testng.Assert.*;
    51 import org.testng.annotations.BeforeMethod;
    52 import org.testng.annotations.Test;
    53 
    54 /**
    55  *
    56  * @author Jaroslav Tulach
    57  */
    58 public class JsClassLoaderBase {
    59     protected static Class<?> methodClass;
    60     
    61     public JsClassLoaderBase() {
    62     }
    63     
    64     @BeforeMethod
    65     public void assertClassDefined() {
    66         assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
    67     }
    68 
    69     @Test public void noParamMethod() throws Throwable {
    70         Method plus = methodClass.getMethod("fortyTwo");
    71         try {
    72             final Object val = plus.invoke(null);
    73             assertTrue(val instanceof Number, "A number returned " + val);
    74             assertEquals(((Number)val).intValue(), 42);
    75         } catch (InvocationTargetException ex) {
    76             throw ex.getTargetException();
    77         }
    78     }
    79     
    80     @Test public void testExecuteScript() throws Throwable {
    81         Method plus = methodClass.getMethod("plus", int.class, int.class);
    82         try {
    83             assertEquals(plus.invoke(null, 10, 20), 30);
    84         } catch (InvocationTargetException ex) {
    85             throw ex.getTargetException();
    86         }
    87     }
    88 
    89     @Test public void overloadedMethod() throws Throwable {
    90         Method plus = methodClass.getMethod("plus", int.class);
    91         try {
    92             assertEquals(plus.invoke(null, 10), 10);
    93         } catch (InvocationTargetException ex) {
    94             throw ex.getTargetException();
    95         }
    96     }
    97     
    98     @Test public void instanceMethod() throws Throwable {
    99         Method plus = methodClass.getMethod("plusInst", int.class);
   100         Object inst = methodClass.newInstance();
   101         try {
   102             assertEquals(plus.invoke(inst, 10), 10);
   103         } catch (InvocationTargetException ex) {
   104             throw ex.getTargetException();
   105         }
   106     }
   107     
   108     @Test public void staticThis() throws Throwable {
   109         Method st = methodClass.getMethod("staticThis");
   110         try {
   111             assertNull(st.invoke(null));
   112         } catch (InvocationTargetException ex) {
   113             throw ex.getTargetException();
   114         }
   115     }
   116 
   117     @Test public void getThis() throws Throwable {
   118         Object th = methodClass.newInstance();
   119         Method st = methodClass.getMethod("getThis");
   120         try {
   121             assertEquals(st.invoke(th), th);
   122         } catch (InvocationTargetException ex) {
   123             throw ex.getTargetException();
   124         }
   125     }
   126     
   127     @Test public void truth() throws Throwable {
   128         Method st = methodClass.getMethod("truth");
   129         assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
   130         assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
   131     }
   132     
   133     @Test public void callback() throws Throwable {
   134         class R implements Runnable {
   135             int cnt;
   136             
   137             @Override
   138             public void run() {
   139                 cnt++;
   140             }
   141         }
   142         R r = new R();
   143         
   144         Method inc = methodClass.getMethod("callback", Runnable.class);
   145         inc.invoke(null, r);
   146         
   147         assertEquals(r.cnt, 1, "Callback happened");
   148     }
   149     
   150     @Test public void sumArray() throws Throwable {
   151         Method st = methodClass.getMethod("sumArr", int[].class);
   152         assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
   153     }
   154     
   155     @Test public void javaScriptResource() throws Throwable {
   156         try {
   157             Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
   158             assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
   159         } catch (InvocationTargetException ex) {
   160             throw ex.getTargetException();
   161         }
   162     }
   163     
   164     @Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
   165         try {
   166             Object thiz = methodClass.newInstance();
   167             Method st = methodClass.getMethod("returnYourSelf", methodClass);
   168             assertEquals(st.invoke(null, thiz), thiz, "Returns this");
   169         } catch (InvocationTargetException ex) {
   170             throw ex.getTargetException();
   171         }
   172     }
   173     
   174     @Test public void callStaticJavaMethod() throws Throwable {
   175         Method st = methodClass.getMethod("staticCallback", int.class, int.class);
   176         assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
   177     }
   178 
   179     @Test public void callStaticStringParamMethod() throws Throwable {
   180         Method st = methodClass.getMethod("parseInt", String.class);
   181         assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
   182     }
   183     
   184     @Test public void firstLong() throws Throwable {
   185         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   186         assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
   187     }
   188 
   189     @Test public void secondLong() throws Throwable {
   190         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   191         assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
   192     }
   193 
   194     @Test public void bothLong() throws Throwable {
   195         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   196         assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
   197     }
   198     
   199     @Test public void recordError() throws Throwable {
   200         Method st = methodClass.getMethod("recordError", Object.class);
   201         assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
   202     }
   203     
   204     @Test public void plusOrMul() throws Throwable {
   205         Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
   206         assertNotNull(Fn.activePresenter(), "Is there a presenter?");
   207         Closeable c = Fn.activate(null);
   208         try {
   209             assertNull(Fn.activePresenter(), "No presenter now");
   210             assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
   211         } finally {
   212             c.close();
   213         }
   214         assertNotNull(Fn.activePresenter(), "Is there a presenter again");
   215         assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
   216         c = Fn.activate(null);
   217         try {
   218             assertNull(Fn.activePresenter(), "No presenter again");
   219             assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
   220         } finally {
   221             c.close();
   222         }
   223         assertNotNull(Fn.activePresenter(), "Is there a presenter again");
   224         assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
   225     }
   226     
   227     @Test public void arrayInOut() throws Throwable {
   228         String[] arr = { "Ahoj" };
   229         Method st = methodClass.getMethod("arr", Object[].class);
   230         Object ret = st.invoke(null, (Object) arr);
   231         assertTrue(ret instanceof Object[], "Expecting array: " + ret);
   232         Object[] res = (Object[]) ret;
   233         assertEquals(res.length, 1, "One element");
   234         assertEquals(res[0], "Ahoj", "The right string");
   235     }
   236 }