boot/src/test/java/org/netbeans/html/boot/impl/CountFnCreationTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 02 Aug 2014 12:59:31 +0200
changeset 790 30f20d9c0986
parent 555 cdae29ad68ec
child 838 bdc3d696dd4a
permissions -rw-r--r--
Fixing Javadoc to succeed on JDK8
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 java.util.logging.Level;
jaroslav@555
    53
import java.util.logging.Logger;
jaroslav@555
    54
import net.java.html.js.JavaScriptBody;
jaroslav@555
    55
import net.java.html.js.JavaScriptResource;
jaroslav@555
    56
import org.apidesign.html.boot.spi.Fn;
jaroslav@555
    57
import static org.testng.Assert.assertEquals;
jaroslav@555
    58
import org.testng.annotations.Test;
jaroslav@555
    59
jaroslav@555
    60
/**
jaroslav@555
    61
 *
jtulach@790
    62
 * @author Jaroslav Tulach
jaroslav@555
    63
 */
jaroslav@555
    64
@JavaScriptResource("empty.js")
jaroslav@555
    65
public class CountFnCreationTest implements Fn.Presenter {
jaroslav@555
    66
    private int cnt;
jaroslav@555
    67
    
jaroslav@555
    68
    @JavaScriptBody(args = {}, body = "return;")
jaroslav@555
    69
    public static native void body();
jaroslav@555
    70
    
jaroslav@555
    71
    @Test public void countManyTimes() throws Exception {
jaroslav@555
    72
        class Res implements FindResources {
jaroslav@555
    73
            @Override
jaroslav@555
    74
            public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
jaroslav@555
    75
                try {
jaroslav@555
    76
                    ClassLoader l = CountFnCreationTest.class.getClassLoader();
jaroslav@555
    77
                    Enumeration<URL> en = l.getResources(path);
jaroslav@555
    78
                    while (en.hasMoreElements()) {
jaroslav@555
    79
                        results.add(en.nextElement());
jaroslav@555
    80
                    }
jaroslav@555
    81
                } catch (IOException ex) {
jaroslav@555
    82
                    throw new IllegalStateException(ex);
jaroslav@555
    83
                }
jaroslav@555
    84
            }
jaroslav@555
    85
        }
jaroslav@555
    86
        ClassLoader l = FnUtils.newLoader(new Res(), this, CountFnCreationTest.class.getClassLoader().getParent());
jaroslav@555
    87
        Method m = l.loadClass(CountFnCreationTest.class.getName()).getMethod("body");
jaroslav@555
    88
        Closeable c = Fn.activate(this);
jaroslav@555
    89
        try {
jaroslav@555
    90
            assertEquals(cnt, 0, "No functions yet");
jaroslav@555
    91
            m.invoke(null);
jaroslav@555
    92
            assertEquals(cnt, 1, "One function defined");
jaroslav@555
    93
            m.invoke(null);
jaroslav@555
    94
            assertEquals(cnt, 1, "Still one function");
jaroslav@555
    95
        } finally {
jaroslav@555
    96
            c.close();
jaroslav@555
    97
        }
jaroslav@555
    98
    }
jaroslav@555
    99
jaroslav@555
   100
    @Override
jaroslav@555
   101
    public Fn defineFn(String code, String... names) {
jaroslav@555
   102
        cnt++;
jaroslav@555
   103
        return new MyFn(this);
jaroslav@555
   104
    }
jaroslav@555
   105
jaroslav@555
   106
    @Override
jaroslav@555
   107
    public void displayPage(URL page, Runnable onPageLoad) {
jaroslav@555
   108
    }
jaroslav@555
   109
jaroslav@555
   110
    @Override
jaroslav@555
   111
    public void loadScript(Reader code) throws Exception {
jaroslav@555
   112
    }
jaroslav@555
   113
    
jaroslav@555
   114
    private static final class MyFn extends Fn {
jaroslav@555
   115
jaroslav@555
   116
        public MyFn(Presenter presenter) {
jaroslav@555
   117
            super(presenter);
jaroslav@555
   118
        }
jaroslav@555
   119
jaroslav@555
   120
        @Override
jaroslav@555
   121
        public Object invoke(Object thiz, Object... args) throws Exception {
jaroslav@555
   122
            return null;
jaroslav@555
   123
        }
jaroslav@555
   124
    }
jaroslav@555
   125
}