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