boot/src/test/java/org/netbeans/html/boot/impl/JsClassLoaderTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 655 7211ec5f3172
child 932 f2de2ae88589
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@323
    45
import java.io.Closeable;
jaroslav@163
    46
import java.io.Reader;
jtulach@838
    47
import org.netbeans.html.boot.spi.Fn;
jaroslav@123
    48
import java.net.URL;
jaroslav@123
    49
import java.net.URLClassLoader;
jaroslav@123
    50
import java.util.ArrayList;
jaroslav@123
    51
import java.util.Arrays;
jaroslav@459
    52
import java.util.Collections;
jaroslav@123
    53
import java.util.Enumeration;
jaroslav@123
    54
import java.util.List;
jaroslav@123
    55
import javax.script.Invocable;
jaroslav@123
    56
import javax.script.ScriptEngine;
jaroslav@123
    57
import javax.script.ScriptEngineManager;
jaroslav@123
    58
import javax.script.ScriptException;
jaroslav@123
    59
import org.testng.annotations.AfterClass;
jaroslav@123
    60
import org.testng.annotations.BeforeClass;
jaroslav@288
    61
import org.testng.annotations.BeforeMethod;
jaroslav@123
    62
jaroslav@123
    63
/**
jaroslav@123
    64
 *
jtulach@655
    65
 * @author Jaroslav Tulach
jaroslav@123
    66
 */
jaroslav@123
    67
public class JsClassLoaderTest extends JsClassLoaderBase{
jaroslav@288
    68
    private static Fn.Presenter loader;
jaroslav@123
    69
jaroslav@123
    70
    @BeforeClass
jaroslav@123
    71
    public static void setUpClass() throws Exception {
jaroslav@123
    72
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@123
    73
        final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
jaroslav@123
    74
        
jaroslav@123
    75
        final URL my = JsClassLoaderTest.class.getProtectionDomain().getCodeSource().getLocation();
jaroslav@123
    76
        ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
jaroslav@123
    77
        final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
jaroslav@288
    78
        class MyCL extends JsClassLoader implements Fn.Presenter {
jaroslav@288
    79
jaroslav@288
    80
            public MyCL(ClassLoader parent) {
jaroslav@288
    81
                super(parent);
jaroslav@288
    82
            }
jaroslav@288
    83
            
jaroslav@123
    84
            @Override
jaroslav@123
    85
            protected URL findResource(String name) {
jaroslav@123
    86
                return ul.getResource(name);
jaroslav@123
    87
            }
jaroslav@123
    88
            @Override
jaroslav@288
    89
            public Fn defineFn(String code, String... names) {
jaroslav@123
    90
                StringBuilder sb = new StringBuilder();
jaroslav@123
    91
                sb.append("(function() {");
jaroslav@123
    92
                sb.append("return function(");
jaroslav@123
    93
                String sep = "";
jaroslav@123
    94
                for (String n : names) {
jaroslav@123
    95
                    sb.append(sep);
jaroslav@123
    96
                    sb.append(n);
jaroslav@123
    97
                    sep = ", ";
jaroslav@123
    98
                }
jaroslav@123
    99
                sb.append(") {");
jaroslav@123
   100
                sb.append(code);
jaroslav@123
   101
                sb.append("};");
jaroslav@123
   102
                sb.append("})()");
jaroslav@123
   103
                try {
jaroslav@123
   104
                    final Object val = eng.eval(sb.toString());
jaroslav@288
   105
                    return new Fn(this) {
jaroslav@123
   106
                        @Override
jaroslav@289
   107
                        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@123
   108
                            List<Object> all = new ArrayList<Object>(args.length + 1);
jaroslav@123
   109
                            all.add(thiz == null ? val : thiz);
jaroslav@123
   110
                            all.addAll(Arrays.asList(args));
jaroslav@123
   111
                            Invocable inv = (Invocable)eng;
jaroslav@161
   112
                            try {
jaroslav@161
   113
                                Object ret = inv.invokeMethod(val, "call", all.toArray());
jaroslav@214
   114
                                return val.equals(ret) ? null : ret;
jaroslav@161
   115
                            } catch (Exception ex) {
jaroslav@161
   116
                                throw ex;
jaroslav@161
   117
                            }
jaroslav@123
   118
                        }
jaroslav@123
   119
                    };
jaroslav@123
   120
                } catch (ScriptException ex) {
jaroslav@123
   121
                    throw new LinkageError("Can't parse: " + sb, ex);
jaroslav@123
   122
                }
jaroslav@123
   123
            }
jaroslav@123
   124
jaroslav@123
   125
            @Override
jaroslav@123
   126
            protected Enumeration<URL> findResources(String name) {
jaroslav@459
   127
                URL u = findResource(name);
jaroslav@459
   128
                List<URL> arr = new ArrayList<URL>();
jaroslav@459
   129
                if (u != null) {
jaroslav@459
   130
                    arr.add(u);
jaroslav@459
   131
                }
jaroslav@459
   132
                return Collections.enumeration(arr);
jaroslav@123
   133
            }
jaroslav@163
   134
jaroslav@163
   135
            @Override
jaroslav@288
   136
            public void loadScript(Reader code) throws ScriptException {
jaroslav@163
   137
                eng.eval(code);
jaroslav@163
   138
            }
jaroslav@288
   139
jaroslav@288
   140
            @Override
jaroslav@288
   141
            public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@288
   142
                throw new UnsupportedOperationException();
jaroslav@288
   143
            }
jaroslav@123
   144
        };
jaroslav@123
   145
        
jaroslav@288
   146
        MyCL l = new MyCL(parent);
jaroslav@323
   147
        Closeable close = FnContext.activate(l);
jaroslav@288
   148
        methodClass = l.loadClass(JsMethods.class.getName());
jaroslav@323
   149
        close.close();
jaroslav@288
   150
        loader = l;
jaroslav@288
   151
    }
jaroslav@288
   152
    
jaroslav@288
   153
    @BeforeMethod public void initPresenter() {
jaroslav@309
   154
        FnContext.currentPresenter(loader);
jaroslav@123
   155
    }
jaroslav@123
   156
jaroslav@123
   157
    @AfterClass
jaroslav@123
   158
    public static void cleanUp() {
jaroslav@123
   159
        methodClass = null;
jaroslav@123
   160
    }
jaroslav@123
   161
}