boot/src/test/java/org/netbeans/html/boot/impl/JsClassLoaderBase.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 15 Jan 2016 13:05:42 +0100
changeset 1043 b189d001b9bd
parent 1041 36165f49f598
child 1048 acc9ba4af659
permissions -rw-r--r--
#257579: Erase the parameter types before computing the signature
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
    }
jaroslav@123
   129
    
jaroslav@153
   130
    @Test public void truth() throws Throwable {
jaroslav@153
   131
        Method st = methodClass.getMethod("truth");
jaroslav@153
   132
        assertTrue((st.getModifiers() & Modifier.STATIC) != 0, "Is static");
jaroslav@153
   133
        assertEquals(st.invoke(null), Boolean.TRUE, "Can return boolean");
jaroslav@153
   134
    }
jaroslav@153
   135
    
jaroslav@160
   136
    @Test public void callback() throws Throwable {
jaroslav@160
   137
        class R implements Runnable {
jaroslav@160
   138
            int cnt;
jaroslav@160
   139
            
jaroslav@160
   140
            @Override
jaroslav@160
   141
            public void run() {
jaroslav@160
   142
                cnt++;
jaroslav@160
   143
            }
jaroslav@160
   144
        }
jaroslav@160
   145
        R r = new R();
jaroslav@160
   146
        
jaroslav@160
   147
        Method inc = methodClass.getMethod("callback", Runnable.class);
jaroslav@160
   148
        inc.invoke(null, r);
jaroslav@160
   149
        
jaroslav@160
   150
        assertEquals(r.cnt, 1, "Callback happened");
jaroslav@160
   151
    }
jaroslav@160
   152
    
jaroslav@161
   153
    @Test public void sumArray() throws Throwable {
jaroslav@161
   154
        Method st = methodClass.getMethod("sumArr", int[].class);
jaroslav@161
   155
        assertEquals(st.invoke(null, new int[] { 1, 2, 3 }), 6, "1+2+3 is six");
jaroslav@161
   156
    }
jaroslav@163
   157
    
jaroslav@163
   158
    @Test public void javaScriptResource() throws Throwable {
jaroslav@163
   159
        try {
jaroslav@163
   160
            Method st = methodClass.getMethod("useExternalMul", int.class, int.class);
jaroslav@163
   161
            assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
jaroslav@163
   162
        } catch (InvocationTargetException ex) {
jaroslav@163
   163
            throw ex.getTargetException();
jaroslav@163
   164
        }
jaroslav@163
   165
    }
jaroslav@171
   166
    
jaroslav@171
   167
    @Test public void callJavaScriptMethodOnOwnClass() throws Throwable {
jaroslav@171
   168
        try {
jtulach@1041
   169
            java.lang.Object thiz = methodClass.newInstance();
jaroslav@171
   170
            Method st = methodClass.getMethod("returnYourSelf", methodClass);
jaroslav@171
   171
            assertEquals(st.invoke(null, thiz), thiz, "Returns this");
jaroslav@171
   172
        } catch (InvocationTargetException ex) {
jaroslav@171
   173
            throw ex.getTargetException();
jaroslav@171
   174
        }
jaroslav@191
   175
    }
jaroslav@191
   176
    
jaroslav@191
   177
    @Test public void callStaticJavaMethod() throws Throwable {
jaroslav@191
   178
        Method st = methodClass.getMethod("staticCallback", int.class, int.class);
jaroslav@191
   179
        assertEquals(st.invoke(null, 6, 7), 42, "Meaning of JavaScript?");
jaroslav@191
   180
    }
jaroslav@191
   181
jaroslav@191
   182
    @Test public void callStaticStringParamMethod() throws Throwable {
jaroslav@191
   183
        Method st = methodClass.getMethod("parseInt", String.class);
jaroslav@191
   184
        assertEquals(st.invoke(null, "42"), 42, "Meaning of JavaScript?");
jaroslav@171
   185
    }
jtulach@933
   186
jtulach@933
   187
    @Test public void passEnum() throws Throwable {
jtulach@933
   188
        Class<?> enmClazz = methodClass.getDeclaredClasses()[0];
jtulach@933
   189
        assertTrue(Enum.class.isAssignableFrom(enmClazz), "It is an enum: " + enmClazz);
jtulach@933
   190
        Class<? extends Enum> enmClazz2 = enmClazz.asSubclass(Enum.class);
jtulach@933
   191
        Method st = methodClass.getMethod("fromEnum", enmClazz);
jtulach@933
   192
        
jtulach@1041
   193
        java.lang.Object valueB = Enum.valueOf(enmClazz2, "B");
jtulach@933
   194
        assertEquals(st.invoke(null, valueB), "B", "Converts to string");
jtulach@933
   195
    }
jaroslav@192
   196
    
jaroslav@192
   197
    @Test public void firstLong() throws Throwable {
jaroslav@192
   198
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   199
        assertEquals(st.invoke(null, true, false, 10, 20), 10L, "Take first value");
jaroslav@192
   200
    }
jaroslav@192
   201
jaroslav@192
   202
    @Test public void secondLong() throws Throwable {
jaroslav@192
   203
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   204
        assertEquals(st.invoke(null, false, true, 10, 20), 20L, "Take 2nd value");
jaroslav@192
   205
    }
jaroslav@192
   206
jaroslav@192
   207
    @Test public void bothLong() throws Throwable {
jaroslav@192
   208
        Method st = methodClass.getMethod("chooseLong", boolean.class, boolean.class, long.class, long.class);
jaroslav@192
   209
        assertEquals(st.invoke(null, true, true, 10, 20), 30L, "Take both values");
jaroslav@192
   210
    }
jaroslav@196
   211
    
jaroslav@196
   212
    @Test public void recordError() throws Throwable {
jtulach@1041
   213
        Method st = methodClass.getMethod("recordError", java.lang.Object.class);
jaroslav@196
   214
        assertEquals(st.invoke(methodClass.newInstance(), "Hello"), "Hello", "The same parameter returned");
jaroslav@196
   215
    }
jaroslav@439
   216
    
jaroslav@451
   217
    @Test public void plusOrMul() throws Throwable {
jaroslav@451
   218
        Method st = methodClass.getMethod("plusOrMul", int.class, int.class);
jaroslav@451
   219
        assertNotNull(Fn.activePresenter(), "Is there a presenter?");
jaroslav@451
   220
        Closeable c = Fn.activate(null);
jaroslav@451
   221
        try {
jaroslav@451
   222
            assertNull(Fn.activePresenter(), "No presenter now");
jaroslav@451
   223
            assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
jaroslav@451
   224
        } finally {
jaroslav@451
   225
            c.close();
jaroslav@451
   226
        }
jaroslav@451
   227
        assertNotNull(Fn.activePresenter(), "Is there a presenter again");
jaroslav@451
   228
        assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript");
jaroslav@451
   229
        c = Fn.activate(null);
jaroslav@451
   230
        try {
jaroslav@451
   231
            assertNull(Fn.activePresenter(), "No presenter again");
jaroslav@451
   232
            assertEquals(st.invoke(null, 6, 7), 42, "Mul in Java");
jaroslav@451
   233
        } finally {
jaroslav@451
   234
            c.close();
jaroslav@451
   235
        }
jaroslav@451
   236
        assertNotNull(Fn.activePresenter(), "Is there a presenter again");
jaroslav@451
   237
        assertEquals(st.invoke(null, 6, 7), 13, "Plus in JavaScript again");
jaroslav@451
   238
    }
jaroslav@451
   239
    
jaroslav@439
   240
    @Test public void arrayInOut() throws Throwable {
jaroslav@439
   241
        String[] arr = { "Ahoj" };
jtulach@1041
   242
        Method st = methodClass.getMethod("arr", java.lang.Object[].class);
jtulach@1041
   243
        java.lang.Object ret = st.invoke(null, (java.lang.Object) arr);
jtulach@1041
   244
        assertTrue(ret instanceof java.lang.Object[], "Expecting array: " + ret);
jtulach@1041
   245
        java.lang.Object[] res = (java.lang.Object[]) ret;
jaroslav@439
   246
        assertEquals(res.length, 1, "One element");
jaroslav@439
   247
        assertEquals(res[0], "Ahoj", "The right string");
jaroslav@439
   248
    }
jtulach@1043
   249
jtulach@1043
   250
    @Test public void parametricCallback() throws Throwable {
jtulach@1043
   251
        Map<String,Integer> map = new HashMap<String, Integer>();
jtulach@1043
   252
        Method st = methodClass.getMethod("callParamTypes", Map.class, int.class);
jtulach@1043
   253
        st.invoke(null, map, 42);
jtulach@1043
   254
        assertEquals(map.get("key"), Integer.valueOf(42), "The right value");
jtulach@1043
   255
    }
jtulach@851
   256
    
jtulach@851
   257
   @Test public void checkTheTypeOfThrownException() throws Throwable {
jtulach@851
   258
        FnContext.currentPresenter(null);
jtulach@851
   259
        assertNull(Fn.activePresenter(), "No presenter is activer right now");
jtulach@1041
   260
        java.lang.Object res = null;
jtulach@851
   261
        try {
jtulach@851
   262
            Method st = methodClass.getMethod("plus", int.class, int.class);
jtulach@851
   263
            try {
jtulach@851
   264
                res = st.invoke(null, 40, 2);
jtulach@852
   265
                Assert.fail("Native method should throw IllegalStateException. Was: " + res);
jtulach@851
   266
            } catch (InvocationTargetException ex) {
jtulach@851
   267
                throw ex.getTargetException();
jtulach@851
   268
            }
jtulach@851
   269
        } catch (IllegalStateException ex) {
jtulach@851
   270
            assertEquals(ex.getMessage(), "No presenter active. Use BrwsrCtx.execute!");
jtulach@851
   271
        }
jtulach@851
   272
    }    
jtulach@851
   273
}