boot/src/test/java/org/netbeans/html/boot/impl/KeepAliveTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 12 Dec 2014 11:22:40 +0100
branchgc
changeset 900 2ee22312e414
child 906 e1291f7b7626
permissions -rw-r--r--
Giving API users better control over GC aspects of their objects
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@900
    50
import static org.testng.Assert.assertFalse;
jtulach@900
    51
import static org.testng.Assert.assertTrue;
jtulach@900
    52
import org.testng.annotations.BeforeMethod;
jtulach@900
    53
import org.testng.annotations.Test;
jtulach@900
    54
jtulach@900
    55
public class KeepAliveTest implements Fn.Presenter, Fn.KeepAlive, FindResources {
jtulach@900
    56
    private Class<?> jsMethods;
jtulach@900
    57
    @Test public void keepAliveIsSetToFalse() throws Exception {
jtulach@900
    58
        Closeable c = Fn.activate(this);
jtulach@900
    59
        boolean ret = (Boolean)jsMethods.getMethod("checkAllowGC", Object.class).invoke(null, this);
jtulach@900
    60
        c.close();
jtulach@900
    61
        assertFalse(ret, "keepAlive returns false when the presenter is invoked");
jtulach@900
    62
    }    
jtulach@900
    63
jtulach@900
    64
    @Test public void keepAliveIsPropagated() throws Exception {
jtulach@900
    65
        Closeable c = Fn.activate(this);
jtulach@900
    66
        boolean ret = (Boolean)jsMethods.getMethod("truth").invoke(null);
jtulach@900
    67
        c.close();
jtulach@900
    68
        assertTrue(ret, "keepAlive returns true when the presenter is invoked");
jtulach@900
    69
    }    
jtulach@900
    70
jtulach@900
    71
    @BeforeMethod
jtulach@900
    72
    public void initClass() throws ClassNotFoundException {
jtulach@900
    73
        ClassLoader loader = FnUtils.newLoader(this, this, KeepAliveTest.class.getClassLoader().getParent());
jtulach@900
    74
        jsMethods = loader.loadClass(JsMethods.class.getName());
jtulach@900
    75
    }
jtulach@900
    76
jtulach@900
    77
    @Override
jtulach@900
    78
    public Fn defineFn(String code, String[] names, final boolean[] keepAlive) {
jtulach@900
    79
        return new Fn(this) {
jtulach@900
    80
            @Override
jtulach@900
    81
            public Object invoke(Object thiz, Object... args) throws Exception {
jtulach@900
    82
                boolean res = true;
jtulach@900
    83
                if (keepAlive != null) {
jtulach@900
    84
                    for (int i = 0; i < keepAlive.length; i++) {
jtulach@900
    85
                        res &= keepAlive[i];
jtulach@900
    86
                    }
jtulach@900
    87
                }
jtulach@900
    88
                return res;
jtulach@900
    89
            }
jtulach@900
    90
        };
jtulach@900
    91
    }
jtulach@900
    92
    
jtulach@900
    93
    @Override
jtulach@900
    94
    public Fn defineFn(String code, String... names) {
jtulach@900
    95
        throw new UnsupportedOperationException("Never called!");
jtulach@900
    96
    }
jtulach@900
    97
jtulach@900
    98
    @Override
jtulach@900
    99
    public void displayPage(URL page, Runnable onPageLoad) {
jtulach@900
   100
        throw new UnsupportedOperationException();
jtulach@900
   101
    }
jtulach@900
   102
jtulach@900
   103
    @Override
jtulach@900
   104
    public void loadScript(Reader code) throws Exception {
jtulach@900
   105
    }
jtulach@900
   106
jtulach@900
   107
    @Override
jtulach@900
   108
    public void findResources(String path, Collection<? super URL> results, boolean oneIsEnough) {
jtulach@900
   109
        URL u = ClassLoader.getSystemClassLoader().getResource(path);
jtulach@900
   110
        if (u != null) {
jtulach@900
   111
            results.add(u);
jtulach@900
   112
        }
jtulach@900
   113
    }
jtulach@900
   114
}