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