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