boot/src/test/java/org/netbeans/html/boot/impl/FnTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 1041 36165f49f598
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@582
    46
import java.io.Flushable;
jaroslav@582
    47
import java.io.IOException;
jaroslav@163
    48
import java.io.Reader;
jaroslav@123
    49
import java.net.URL;
jaroslav@123
    50
import java.net.URLClassLoader;
jaroslav@123
    51
import java.util.ArrayList;
jaroslav@123
    52
import java.util.Arrays;
jaroslav@123
    53
import java.util.Collection;
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;
jtulach@838
    59
import org.netbeans.html.boot.spi.Fn;
jaroslav@582
    60
import static org.testng.Assert.assertEquals;
jaroslav@123
    61
import org.testng.annotations.BeforeClass;
jaroslav@288
    62
import org.testng.annotations.BeforeMethod;
jaroslav@582
    63
import org.testng.annotations.Test;
jaroslav@123
    64
jaroslav@123
    65
/**
jaroslav@123
    66
 *
jtulach@790
    67
 * @author Jaroslav Tulach
jaroslav@123
    68
 */
jaroslav@123
    69
public class FnTest extends JsClassLoaderBase {
jaroslav@288
    70
    private static Fn.Presenter presenter;
jaroslav@123
    71
    
jaroslav@123
    72
    public FnTest() {
jaroslav@123
    73
    }
jaroslav@123
    74
jaroslav@123
    75
    @BeforeClass
jaroslav@123
    76
    public static void createClassLoader() throws Exception {
jaroslav@123
    77
        ScriptEngineManager sem = new ScriptEngineManager();
jaroslav@123
    78
        final ScriptEngine eng = sem.getEngineByMimeType("text/javascript");
jaroslav@123
    79
        
jaroslav@123
    80
        final URL my = FnTest.class.getProtectionDomain().getCodeSource().getLocation();
jaroslav@123
    81
        ClassLoader parent = JsClassLoaderTest.class.getClassLoader().getParent();
jaroslav@123
    82
        final URLClassLoader ul = new URLClassLoader(new URL[] { my }, parent);
jaroslav@123
    83
        
jaroslav@128
    84
        class Impl implements FindResources, Fn.Presenter {
jaroslav@123
    85
            @Override
jaroslav@123
    86
            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
jaroslav@123
    87
                URL u = ul.findResource(path);
jaroslav@123
    88
                if (u != null) {
jaroslav@123
    89
                    results.add(u);
jaroslav@123
    90
                }
jaroslav@123
    91
            }
jaroslav@123
    92
jaroslav@123
    93
            @Override
jaroslav@123
    94
            public Fn defineFn(String code, String... names) {
jaroslav@123
    95
                StringBuilder sb = new StringBuilder();
jaroslav@123
    96
                sb.append("(function() {");
jaroslav@123
    97
                sb.append("return function(");
jaroslav@123
    98
                String sep = "";
jaroslav@123
    99
                for (String n : names) {
jaroslav@123
   100
                    sb.append(sep);
jaroslav@123
   101
                    sb.append(n);
jaroslav@123
   102
                    sep = ", ";
jaroslav@123
   103
                }
jaroslav@123
   104
                sb.append(") {");
jaroslav@123
   105
                sb.append(code);
jaroslav@123
   106
                sb.append("};");
jaroslav@123
   107
                sb.append("})()");
jaroslav@123
   108
                try {
jaroslav@123
   109
                    final Object val = eng.eval(sb.toString());
jaroslav@288
   110
                    return new Fn(this) {
jaroslav@123
   111
                        @Override
jaroslav@289
   112
                        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@123
   113
                            List<Object> all = new ArrayList<Object>(args.length + 1);
jaroslav@123
   114
                            all.add(thiz == null ? val : thiz);
jaroslav@123
   115
                            all.addAll(Arrays.asList(args));
jaroslav@123
   116
                            Invocable inv = (Invocable)eng;
jaroslav@161
   117
                            try {
jaroslav@161
   118
                                Object ret = inv.invokeMethod(val, "call", all.toArray());
jaroslav@214
   119
                                return val.equals(ret) ? null : ret;
jaroslav@161
   120
                            } catch (ScriptException ex) {
jaroslav@161
   121
                                throw ex;
jaroslav@161
   122
                            }
jaroslav@123
   123
                        }
jaroslav@123
   124
                    };
jaroslav@123
   125
                } catch (ScriptException ex) {
jaroslav@123
   126
                    throw new LinkageError("Can't parse: " + sb, ex);
jaroslav@123
   127
                }
jaroslav@123
   128
            }
jaroslav@127
   129
jaroslav@127
   130
            @Override
jaroslav@128
   131
            public void displayPage(URL resource, Runnable r) {
jaroslav@127
   132
                throw new UnsupportedOperationException();
jaroslav@127
   133
            }
jaroslav@163
   134
jaroslav@163
   135
            @Override
jaroslav@163
   136
            public void loadScript(Reader code) throws Exception {
jaroslav@163
   137
                eng.eval(code);
jaroslav@163
   138
            }
jaroslav@123
   139
        }
jaroslav@123
   140
        Impl impl = new Impl();
jaroslav@126
   141
        ClassLoader loader = FnUtils.newLoader(impl, impl, parent);
jaroslav@288
   142
        presenter = impl;
jaroslav@123
   143
        
jaroslav@323
   144
        Closeable close = FnContext.activate(impl);
jaroslav@123
   145
        methodClass = loader.loadClass(JsMethods.class.getName());
jaroslav@323
   146
        close.close();
jaroslav@123
   147
    }
jaroslav@582
   148
    
jaroslav@582
   149
    @Test public void flushingPresenter() throws IOException {
jaroslav@582
   150
        class FP implements Fn.Presenter, Flushable {
jaroslav@582
   151
            int flush;
jaroslav@582
   152
jaroslav@582
   153
            @Override
jaroslav@582
   154
            public Fn defineFn(String code, String... names) {
jaroslav@582
   155
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@582
   156
            }
jaroslav@582
   157
jaroslav@582
   158
            @Override
jaroslav@582
   159
            public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@582
   160
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@582
   161
            }
jaroslav@582
   162
jaroslav@582
   163
            @Override
jaroslav@582
   164
            public void loadScript(Reader code) throws Exception {
jaroslav@582
   165
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
jaroslav@582
   166
            }
jaroslav@582
   167
jaroslav@582
   168
            @Override
jaroslav@582
   169
            public void flush() throws IOException {
jaroslav@582
   170
                flush++;
jaroslav@582
   171
            }
jaroslav@582
   172
        }
jaroslav@582
   173
        
jaroslav@582
   174
        FP p = new FP();
jaroslav@582
   175
        Closeable c1 = Fn.activate(p);
jaroslav@582
   176
        Closeable c2 = Fn.activate(p);
jaroslav@582
   177
        c2.close();
jaroslav@582
   178
        assertEquals(p.flush, 0, "No flush yet");
jaroslav@582
   179
        c1.close();
jaroslav@582
   180
        assertEquals(p.flush, 1, "Now flushed");
jaroslav@582
   181
    }
jaroslav@288
   182
jaroslav@288
   183
    @BeforeMethod public void initPresenter() {
jaroslav@309
   184
        FnContext.currentPresenter(presenter);
jaroslav@288
   185
    }
jaroslav@123
   186
}