boot/src/test/java/org/netbeans/html/boot/impl/JsClassLoaderBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 15 Jan 2016 11:40:28 +0100
changeset 1041 36165f49f598
parent 933 9d158eb4a797
child 1043 b189d001b9bd
permissions -rw-r--r--
Use fully qualified names when referencing java.lang.Object
     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 org.testng.Assert;
    51 import static org.testng.Assert.*;
    52 import org.testng.annotations.BeforeMethod;
    53 import org.testng.annotations.Test;
    54 
    55 /**
    56  *
    57  * @author Jaroslav Tulach
    58  */
    59 public class JsClassLoaderBase {
    60     protected static Class<?> methodClass;
    61     
    62     public JsClassLoaderBase() {
    63     }
    64     
    65     @BeforeMethod
    66     public void assertClassDefined() {
    67         assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
    68     }
    69 
    70     @Test public void noParamMethod() throws Throwable {
    71         Method plus = methodClass.getMethod("fortyTwo");
    72         try {
    73             final java.lang.Object val = plus.invoke(null);
    74             assertTrue(val instanceof Number, "A number returned " + val);
    75             assertEquals(((Number)val).intValue(), 42);
    76         } catch (InvocationTargetException ex) {
    77             throw ex.getTargetException();
    78         }
    79     }
    80     
    81     @Test public void testExecuteScript() throws Throwable {
    82         Method plus = methodClass.getMethod("plus", int.class, int.class);
    83         try {
    84             assertEquals(plus.invoke(null, 10, 20), 30);
    85         } catch (InvocationTargetException ex) {
    86             throw ex.getTargetException();
    87         }
    88     }
    89 
    90     @Test public void overloadedMethod() throws Throwable {
    91         Method plus = methodClass.getMethod("plus", int.class);
    92         try {
    93             assertEquals(plus.invoke(null, 10), 10);
    94         } catch (InvocationTargetException ex) {
    95             throw ex.getTargetException();
    96         }
    97     }
    98     
    99     @Test public void instanceMethod() throws Throwable {
   100         Method plus = methodClass.getMethod("plusInst", int.class);
   101         java.lang.Object inst = methodClass.newInstance();
   102         try {
   103             assertEquals(plus.invoke(inst, 10), 10);
   104         } catch (InvocationTargetException ex) {
   105             throw ex.getTargetException();
   106         }
   107     }
   108     
   109     @Test public void staticThis() throws Throwable {
   110         Method st = methodClass.getMethod("staticThis");
   111         try {
   112             assertNull(st.invoke(null));
   113         } catch (InvocationTargetException ex) {
   114             throw ex.getTargetException();
   115         }
   116     }
   117 
   118     @Test public void getThis() throws Throwable {
   119         java.lang.Object th = methodClass.newInstance();
   120         Method st = methodClass.getMethod("getThis");
   121         try {
   122             assertEquals(st.invoke(th), th);
   123         } catch (InvocationTargetException ex) {
   124             throw ex.getTargetException();
   125         }
   126     }
   127     
   128     @Test public void truth() throws Throwable {
   129         Method st = methodClass.getMethod("truth");
   130         assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
   131         assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
   132     }
   133     
   134     @Test public void callback() throws Throwable {
   135         class R implements Runnable {
   136             int cnt;
   137             
   138             @Override
   139             public void run() {
   140                 cnt++;
   141             }
   142         }
   143         R r = new R();
   144         
   145         Method inc = methodClass.getMethod("callback", Runnable.class);
   146         inc.invoke(null, r);
   147         
   148         assertEquals(r.cnt, 1, "Callback happened");
   149     }
   150     
   151     @Test public void sumArray() throws Throwable {
   152         Method st = methodClass.getMethod("sumArr", int[].class);
   153         assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
   154     }
   155     
   156     @Test public void javaScriptResource() throws Throwable {
   157         try {
   158             Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
   159             assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
   160         } catch (InvocationTargetException ex) {
   161             throw ex.getTargetException();
   162         }
   163     }
   164     
   165     @Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
   166         try {
   167             java.lang.Object thiz = methodClass.newInstance();
   168             Method st = methodClass.getMethod("returnYourSelf", methodClass);
   169             assertEquals(st.invoke(null, thiz), thiz, "Returns this");
   170         } catch (InvocationTargetException ex) {
   171             throw ex.getTargetException();
   172         }
   173     }
   174     
   175     @Test public void callStaticJavaMethod() throws Throwable {
   176         Method st = methodClass.getMethod("staticCallback", int.class, int.class);
   177         assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
   178     }
   179 
   180     @Test public void callStaticStringParamMethod() throws Throwable {
   181         Method st = methodClass.getMethod("parseInt", String.class);
   182         assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
   183     }
   184 
   185     @Test public void passEnum() throws Throwable {
   186         Class<?> enmClazz = methodClass.getDeclaredClasses()[0];
   187         assertTrue(Enum.class.isAssignableFrom(enmClazz), "It is an enum: " + enmClazz);
   188         Class<? extends Enum> enmClazz2 = enmClazz.asSubclass(Enum.class);
   189         Method st = methodClass.getMethod("fromEnum", enmClazz);
   190         
   191         java.lang.Object valueB = Enum.valueOf(enmClazz2, "B");
   192         assertEquals(st.invoke(null, valueB), "B", "Converts to string");
   193     }
   194     
   195     @Test public void firstLong() throws Throwable {
   196         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   197         assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
   198     }
   199 
   200     @Test public void secondLong() throws Throwable {
   201         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   202         assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
   203     }
   204 
   205     @Test public void bothLong() throws Throwable {
   206         Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
   207         assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
   208     }
   209     
   210     @Test public void recordError() throws Throwable {
   211         Method st = methodClass.getMethod("recordError", java.lang.Object.class);
   212         assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
   213     }
   214     
   215     @Test public void plusOrMul() throws Throwable {
   216         Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
   217         assertNotNull(Fn.activePresenter(), "Is there a presenter?");
   218         Closeable c = Fn.activate(null);
   219         try {
   220             assertNull(Fn.activePresenter(), "No presenter now");
   221             assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
   222         } finally {
   223             c.close();
   224         }
   225         assertNotNull(Fn.activePresenter(), "Is there a presenter again");
   226         assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
   227         c = Fn.activate(null);
   228         try {
   229             assertNull(Fn.activePresenter(), "No presenter again");
   230             assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
   231         } finally {
   232             c.close();
   233         }
   234         assertNotNull(Fn.activePresenter(), "Is there a presenter again");
   235         assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
   236     }
   237     
   238     @Test public void arrayInOut() throws Throwable {
   239         String[] arr = { "Ahoj" };
   240         Method st = methodClass.getMethod("arr", java.lang.Object[].class);
   241         java.lang.Object ret = st.invoke(null, (java.lang.Object) arr);
   242         assertTrue(ret instanceof java.lang.Object[], "Expecting array: " + ret);
   243         java.lang.Object[] res = (java.lang.Object[]) ret;
   244         assertEquals(res.length, 1, "One element");
   245         assertEquals(res[0], "Ahoj", "The right string");
   246     }
   247     
   248    @Test public void checkTheTypeOfThrownException() throws Throwable {
   249         FnContext.currentPresenter(null);
   250         assertNull(Fn.activePresenter(), "No presenter is activer right now");
   251         java.lang.Object res = null;
   252         try {
   253             Method st = methodClass.getMethod("plus", int.class, int.class);
   254             try {
   255                 res = st.invoke(null, 40, 2);
   256                 Assert.fail("Native method should throw IllegalStateException. Was: " + res);
   257             } catch (InvocationTargetException ex) {
   258                 throw ex.getTargetException();
   259             }
   260         } catch (IllegalStateException ex) {
   261             assertEquals(ex.getMessage(), "No presenter active. Use BrwsrCtx.execute!");
   262         }
   263     }    
   264 }