boot/src/test/java/org/netbeans/html/boot/impl/JsClassLoaderBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Mon, 15 Feb 2016 05:26:13 +0100
changeset 1052 aca779a0fcec
parent 1051 d0e6c8f97dc3
permissions -rw-r--r--
Fully qualified name to access Object
jaroslav@123
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@123
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@123
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@123
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@123
    42
 */
jaroslav@362
    43
package org.netbeans.html.boot.impl;
jaroslav@123
    44
jaroslav@451
    45
import java.io.Closeable;
jaroslav@123
    46
import java.lang.reflect.InvocationTargetException;
jaroslav@123
    47
import java.lang.reflect.Method;
jaroslav@153
    48
import java.lang.reflect.Modifier;
jtulach@1043
    49
import java.util.HashMap;
jtulach@1043
    50
import java.util.Map;
jtulach@838
    51
import org.netbeans.html.boot.spi.Fn;
jtulach@851
    52
import org.testng.Assert;
jaroslav@123
    53
import static org.testng.Assert.*;
jaroslav@123
    54
import org.testng.annotations.BeforeMethod;
jaroslav@123
    55
import org.testng.annotations.Test;
jaroslav@123
    56
jaroslav@123
    57
/**
jaroslav@123
    58
 *
jtulach@655
    59
 * @author Jaroslav Tulach
jaroslav@123
    60
 */
jaroslav@123
    61
public class JsClassLoaderBase {
jaroslav@123
    62
    protected static Class<?> methodClass;
jaroslav@123
    63
    
jaroslav@123
    64
    public JsClassLoaderBase() {
jaroslav@123
    65
    }
jaroslav@123
    66
    
jaroslav@123
    67
    @BeforeMethod
jaroslav@123
    68
    public void assertClassDefined() {
jaroslav@123
    69
        assertNotNull(methodClass, "BeforeClass set up code should provide methodClass");
jaroslav@123
    70
    }
jaroslav@123
    71
jaroslav@123
    72
    @Test public void noParamMethod() throws Throwable {
jaroslav@123
    73
        Method plus = methodClass.getMethod("fortyTwo");
jaroslav@123
    74
        try {
jtulach@1041
    75
            final java.lang.Object val = plus.invoke(null);
jaroslav@123
    76
            assertTrue(val instanceof Number, "A number returned " + val);
jaroslav@123
    77
            assertEquals(((Number)val).intValue(), 42);
jaroslav@123
    78
        } catch (InvocationTargetException ex) {
jaroslav@123
    79
            throw ex.getTargetException();
jaroslav@123
    80
        }
jaroslav@123
    81
    }
jaroslav@123
    82
    
jaroslav@123
    83
    @Test public void testExecuteScript() throws Throwable {
jaroslav@123
    84
        Method plus = methodClass.getMethod("plus", int.class, int.class);
jaroslav@123
    85
        try {
jaroslav@123
    86
            assertEquals(plus.invoke(null, 10, 20), 30);
jaroslav@123
    87
        } catch (InvocationTargetException ex) {
jaroslav@123
    88
            throw ex.getTargetException();
jaroslav@123
    89
        }
jaroslav@123
    90
    }
jaroslav@123
    91
jaroslav@123
    92
    @Test public void overloadedMethod() throws Throwable {
jaroslav@123
    93
        Method plus = methodClass.getMethod("plus", int.class);
jaroslav@123
    94
        try {
jaroslav@123
    95
            assertEquals(plus.invoke(null, 10), 10);
jaroslav@123
    96
        } catch (InvocationTargetException ex) {
jaroslav@123
    97
            throw ex.getTargetException();
jaroslav@123
    98
        }
jaroslav@123
    99
    }
jaroslav@123
   100
    
jaroslav@123
   101
    @Test public void instanceMethod() throws Throwable {
jaroslav@123
   102
        Method plus = methodClass.getMethod("plusInst", int.class);
jtulach@1041
   103
        java.lang.Object inst = methodClass.newInstance();
jaroslav@123
   104
        try {
jaroslav@123
   105
            assertEquals(plus.invoke(inst, 10), 10);
jaroslav@123
   106
        } catch (InvocationTargetException ex) {
jaroslav@123
   107
            throw ex.getTargetException();
jaroslav@123
   108
        }
jaroslav@123
   109
    }
jaroslav@123
   110
    
jaroslav@123
   111
    @Test public void staticThis() throws Throwable {
jaroslav@123
   112
        Method st = methodClass.getMethod("staticThis");
jaroslav@123
   113
        try {
jaroslav@123
   114
            assertNull(st.invoke(null));
jaroslav@123
   115
        } catch (InvocationTargetException ex) {
jaroslav@123
   116
            throw ex.getTargetException();
jaroslav@123
   117
        }
jaroslav@123
   118
    }
jaroslav@123
   119
jaroslav@123
   120
    @Test public void getThis() throws Throwable {
jtulach@1041
   121
        java.lang.Object th = methodClass.newInstance();
jaroslav@123
   122
        Method st = methodClass.getMethod("getThis");
jaroslav@123
   123
        try {
jaroslav@123
   124
            assertEquals(st.invoke(th), th);
jaroslav@123
   125
        } catch (InvocationTargetException ex) {
jaroslav@123
   126
            throw ex.getTargetException();
jaroslav@123
   127
        }
jaroslav@123
   128
    }
jtulach@1051
   129
jtulach@1051
   130
    @Test public void primitiveArrayReturn() throws Throwable {
jtulach@1051
   131
        Method st = methodClass.getMethod("both", double.class, double.class);
jtulach@1051
   132
        Throwable ex;
jtulach@1051
   133
        try {
jtulach@1052
   134
            java.lang.Object arr = st.invoke(null, 2, 5);
jtulach@1051
   135
            ex = null;
jtulach@1051
   136
        } catch (InvocationTargetException invoke) {
jtulach@1051
   137
            ex = invoke.getTargetException();
jtulach@1051
   138
        }
jtulach@1051
   139
        assertTrue(ex instanceof ClassCastException, "Primitive arrays aren't returned from JavaScript: " + ex);
jtulach@1051
   140
    }
jaroslav@123
   141
    
jaroslav@153
   142
    @Test public void truth() throws Throwable {
jaroslav@153
   143
        Method st = methodClass.getMethod("truth");
jaroslav@153
   144
        assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
jaroslav@153
   145
        assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
jaroslav@153
   146
    }
jaroslav@153
   147
    
jaroslav@160
   148
    @Test public void callback() throws Throwable {
jaroslav@160
   149
        class R implements Runnable {
jaroslav@160
   150
            int cnt;
jaroslav@160
   151
            
jaroslav@160
   152
            @Override
jaroslav@160
   153
            public void run() {
jaroslav@160
   154
                cnt++;
jaroslav@160
   155
            }
jaroslav@160
   156
        }
jaroslav@160
   157
        R r = new R();
jaroslav@160
   158
        
jaroslav@160
   159
        Method inc = methodClass.getMethod("callback", Runnable.class);
jaroslav@160
   160
        inc.invoke(null, r);
jaroslav@160
   161
        
jaroslav@160
   162
        assertEquals(r.cnt, 1, "Callback happened");
jaroslav@160
   163
    }
jaroslav@160
   164
    
jaroslav@161
   165
    @Test public void sumArray() throws Throwable {
jaroslav@161
   166
        Method st = methodClass.getMethod("sumArr", int[].class);
jaroslav@161
   167
        assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
jaroslav@161
   168
    }
jaroslav@163
   169
    
jaroslav@163
   170
    @Test public void javaScriptResource() throws Throwable {
jaroslav@163
   171
        try {
jaroslav@163
   172
            Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
jaroslav@163
   173
            assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
jaroslav@163
   174
        } catch (InvocationTargetException ex) {
jaroslav@163
   175
            throw ex.getTargetException();
jaroslav@163
   176
        }
jaroslav@163
   177
    }
jaroslav@171
   178
    
jaroslav@171
   179
    @Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
jaroslav@171
   180
        try {
jtulach@1041
   181
            java.lang.Object thiz = methodClass.newInstance();
jaroslav@171
   182
            Method st = methodClass.getMethod("returnYourSelf", methodClass);
jaroslav@171
   183
            assertEquals(st.invoke(null, thiz), thiz, "Returns this");
jaroslav@171
   184
        } catch (InvocationTargetException ex) {
jaroslav@171
   185
            throw ex.getTargetException();
jaroslav@171
   186
        }
jaroslav@191
   187
    }
jaroslav@191
   188
    
jaroslav@191
   189
    @Test public void callStaticJavaMethod() throws Throwable {
jaroslav@191
   190
        Method st = methodClass.getMethod("staticCallback", int.class, int.class);
jaroslav@191
   191
        assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
jaroslav@191
   192
    }
jaroslav@191
   193
jaroslav@191
   194
    @Test public void callStaticStringParamMethod() throws Throwable {
jaroslav@191
   195
        Method st = methodClass.getMethod("parseInt", String.class);
jaroslav@191
   196
        assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
jaroslav@171
   197
    }
jtulach@933
   198
jtulach@933
   199
    @Test public void passEnum() throws Throwable {
jtulach@933
   200
        Class<?> enmClazz = methodClass.getDeclaredClasses()[0];
jtulach@933
   201
        assertTrue(Enum.class.isAssignableFrom(enmClazz), "It is an enum: " + enmClazz);
jtulach@933
   202
        Class<? extends Enum> enmClazz2 = enmClazz.asSubclass(Enum.class);
jtulach@933
   203
        Method st = methodClass.getMethod("fromEnum", enmClazz);
jtulach@933
   204
        
jtulach@1041
   205
        java.lang.Object valueB = Enum.valueOf(enmClazz2, "B");
jtulach@933
   206
        assertEquals(st.invoke(null, valueB), "B", "Converts to string");
jtulach@933
   207
    }
jaroslav@192
   208
    
jaroslav@192
   209
    @Test public void firstLong() throws Throwable {
jaroslav@192
   210
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   211
        assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
jaroslav@192
   212
    }
jaroslav@192
   213
jaroslav@192
   214
    @Test public void secondLong() throws Throwable {
jaroslav@192
   215
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   216
        assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
jaroslav@192
   217
    }
jaroslav@192
   218
jaroslav@192
   219
    @Test public void bothLong() throws Throwable {
jaroslav@192
   220
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   221
        assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
jaroslav@192
   222
    }
jaroslav@196
   223
    
jaroslav@196
   224
    @Test public void recordError() throws Throwable {
jtulach@1041
   225
        Method st = methodClass.getMethod("recordError", java.lang.Object.class);
jaroslav@196
   226
        assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
jaroslav@196
   227
    }
jaroslav@439
   228
    
jaroslav@451
   229
    @Test public void plusOrMul() throws Throwable {
jaroslav@451
   230
        Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
jaroslav@451
   231
        assertNotNull(Fn.activePresenter(), "Is there a presenter?");
jaroslav@451
   232
        Closeable c = Fn.activate(null);
jaroslav@451
   233
        try {
jaroslav@451
   234
            assertNull(Fn.activePresenter(), "No presenter now");
jaroslav@451
   235
            assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
jaroslav@451
   236
        } finally {
jaroslav@451
   237
            c.close();
jaroslav@451
   238
        }
jaroslav@451
   239
        assertNotNull(Fn.activePresenter(), "Is there a presenter again");
jaroslav@451
   240
        assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
jaroslav@451
   241
        c = Fn.activate(null);
jaroslav@451
   242
        try {
jaroslav@451
   243
            assertNull(Fn.activePresenter(), "No presenter again");
jaroslav@451
   244
            assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
jaroslav@451
   245
        } finally {
jaroslav@451
   246
            c.close();
jaroslav@451
   247
        }
jaroslav@451
   248
        assertNotNull(Fn.activePresenter(), "Is there a presenter again");
jaroslav@451
   249
        assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
jaroslav@451
   250
    }
jaroslav@451
   251
    
jaroslav@439
   252
    @Test public void arrayInOut() throws Throwable {
jaroslav@439
   253
        String[] arr = { "Ahoj" };
jtulach@1041
   254
        Method st = methodClass.getMethod("arr", java.lang.Object[].class);
jtulach@1041
   255
        java.lang.Object ret = st.invoke(null, (java.lang.Object) arr);
jtulach@1041
   256
        assertTrue(ret instanceof java.lang.Object[], "Expecting array: " + ret);
jtulach@1041
   257
        java.lang.Object[] res = (java.lang.Object[]) ret;
jaroslav@439
   258
        assertEquals(res.length, 1, "One element");
jaroslav@439
   259
        assertEquals(res[0], "Ahoj", "The right string");
jaroslav@439
   260
    }
jtulach@1043
   261
jtulach@1043
   262
    @Test public void parametricCallback() throws Throwable {
jtulach@1049
   263
        Map<String,Number> map = new HashMap<String, Number>();
jtulach@1043
   264
        Method st = methodClass.getMethod("callParamTypes", Map.class, int.class);
jtulach@1043
   265
        st.invoke(null, map, 42);
jtulach@1048
   266
        assertEquals(map.get("key").intValue(), 42, "The right value");
jtulach@1043
   267
    }
jtulach@851
   268
    
jtulach@851
   269
   @Test public void checkTheTypeOfThrownException() throws Throwable {
jtulach@851
   270
        FnContext.currentPresenter(null);
jtulach@851
   271
        assertNull(Fn.activePresenter(), "No presenter is activer right now");
jtulach@1041
   272
        java.lang.Object res = null;
jtulach@851
   273
        try {
jtulach@851
   274
            Method st = methodClass.getMethod("plus", int.class, int.class);
jtulach@851
   275
            try {
jtulach@851
   276
                res = st.invoke(null, 40, 2);
jtulach@852
   277
                Assert.fail("Native method should throw IllegalStateException. Was: " + res);
jtulach@851
   278
            } catch (InvocationTargetException ex) {
jtulach@851
   279
                throw ex.getTargetException();
jtulach@851
   280
            }
jtulach@851
   281
        } catch (IllegalStateException ex) {
jtulach@851
   282
            assertEquals(ex.getMessage(), "No presenter active. Use BrwsrCtx.execute!");
jtulach@851
   283
        }
jtulach@851
   284
    }    
jtulach@851
   285
}