boot/src/test/java/org/netbeans/html/boot/impl/CountFnCreationTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 15 Jan 2016 11:40:28 +0100
changeset 1041 36165f49f598
parent 838 bdc3d696dd4a
permissions -rw-r--r--
Use fully qualified names when referencing java.lang.Object
jaroslav@555
     1
/**
jaroslav@555
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@555
     3
 *
jaroslav@555
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@555
     5
 *
jaroslav@555
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@555
     7
 * Other names may be trademarks of their respective owners.
jaroslav@555
     8
 *
jaroslav@555
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@555
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@555
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@555
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@555
    13
 * License. You can obtain a copy of the License at
jaroslav@555
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@555
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@555
    16
 * specific language governing permissions and limitations under the
jaroslav@555
    17
 * License.  When distributing the software, include this License Header
jaroslav@555
    18
 * Notice in each file and include the License file at
jaroslav@555
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@555
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@555
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@555
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@555
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@555
    24
 * your own identifying information:
jaroslav@555
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@555
    26
 *
jaroslav@555
    27
 * Contributor(s):
jaroslav@555
    28
 *
jaroslav@555
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@555
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@555
    31
 *
jaroslav@555
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@555
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@555
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@555
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@555
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@555
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@555
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@555
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@555
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@555
    41
 * made subject to such option by the copyright holder.
jaroslav@555
    42
 */
jaroslav@555
    43
package org.netbeans.html.boot.impl;
jaroslav@555
    44
jaroslav@555
    45
import java.io.Closeable;
jaroslav@555
    46
import java.io.IOException;
jaroslav@555
    47
import java.io.Reader;
jaroslav@555
    48
import java.lang.reflect.Method;
jaroslav@555
    49
import java.net.URL;
jaroslav@555
    50
import java.util.Collection;
jaroslav@555
    51
import java.util.Enumeration;
jaroslav@555
    52
import net.java.html.js.JavaScriptBody;
jaroslav@555
    53
import net.java.html.js.JavaScriptResource;
jtulach@838
    54
import org.netbeans.html.boot.spi.Fn;
jaroslav@555
    55
import static org.testng.Assert.assertEquals;
jaroslav@555
    56
import org.testng.annotations.Test;
jaroslav@555
    57
jaroslav@555
    58
/**
jaroslav@555
    59
 *
jtulach@790
    60
 * @author Jaroslav Tulach
jaroslav@555
    61
 */
jaroslav@555
    62
@JavaScriptResource("empty.js")
jaroslav@555
    63
public class CountFnCreationTest implements Fn.Presenter {
jaroslav@555
    64
    private int cnt;
jaroslav@555
    65
    
jaroslav@555
    66
    @JavaScriptBody(args = {}, body = "return;")
jaroslav@555
    67
    public static native void body();
jaroslav@555
    68
    
jaroslav@555
    69
    @Test public void countManyTimes() throws Exception {
jaroslav@555
    70
        class Res implements FindResources {
jaroslav@555
    71
            @Override
jaroslav@555
    72
            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
jaroslav@555
    73
                try {
jaroslav@555
    74
                    ClassLoader l = CountFnCreationTest.class.getClassLoader();
jaroslav@555
    75
                    Enumeration<URL> en = l.getResources(path);
jaroslav@555
    76
                    while (en.hasMoreElements()) {
jaroslav@555
    77
                        results.add(en.nextElement());
jaroslav@555
    78
                    }
jaroslav@555
    79
                } catch (IOException ex) {
jaroslav@555
    80
                    throw new IllegalStateException(ex);
jaroslav@555
    81
                }
jaroslav@555
    82
            }
jaroslav@555
    83
        }
jaroslav@555
    84
        ClassLoader l = FnUtils.newLoader(new Res(), this, CountFnCreationTest.class.getClassLoader().getParent());
jaroslav@555
    85
        Method m = l.loadClass(CountFnCreationTest.class.getName()).getMethod("body");
jaroslav@555
    86
        Closeable c = Fn.activate(this);
jaroslav@555
    87
        try {
jaroslav@555
    88
            assertEquals(cnt, 0, "No functions yet");
jaroslav@555
    89
            m.invoke(null);
jaroslav@555
    90
            assertEquals(cnt, 1, "One function defined");
jaroslav@555
    91
            m.invoke(null);
jaroslav@555
    92
            assertEquals(cnt, 1, "Still one function");
jaroslav@555
    93
        } finally {
jaroslav@555
    94
            c.close();
jaroslav@555
    95
        }
jaroslav@555
    96
    }
jaroslav@555
    97
jaroslav@555
    98
    @Override
jaroslav@555
    99
    public Fn defineFn(String code, String... names) {
jaroslav@555
   100
        cnt++;
jaroslav@555
   101
        return new MyFn(this);
jaroslav@555
   102
    }
jaroslav@555
   103
jaroslav@555
   104
    @Override
jaroslav@555
   105
    public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@555
   106
    }
jaroslav@555
   107
jaroslav@555
   108
    @Override
jaroslav@555
   109
    public void loadScript(Reader code) throws Exception {
jaroslav@555
   110
    }
jaroslav@555
   111
    
jaroslav@555
   112
    private static final class MyFn extends Fn {
jaroslav@555
   113
jaroslav@555
   114
        public MyFn(Presenter presenter) {
jaroslav@555
   115
            super(presenter);
jaroslav@555
   116
        }
jaroslav@555
   117
jaroslav@555
   118
        @Override
jtulach@1041
   119
        public java.lang.Object invoke(java.lang.Object thiz, java.lang.Object... args) throws Exception {
jaroslav@555
   120
            return null;
jaroslav@555
   121
        }
jaroslav@555
   122
    }
jaroslav@555
   123
}