boot-agent-test/src/test/java/org/netbeans/html/bootagent/DynamicClassLoaderTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 29 Nov 2014 22:25:40 +0100
changeset 884 af690d50d7d6
parent 838 boot-fx/src/test/java/org/netbeans/html/boot/fx/BootstrapTest.java@bdc3d696dd4a
permissions -rw-r--r--
Use optional dependency on asm-5.0.jar, so we don't have to bundle it when the classes are post processed.
jaroslav@182
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@182
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@182
     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@182
     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@182
    42
 */
jtulach@884
    43
package org.netbeans.html.bootagent;
jaroslav@182
    44
jtulach@884
    45
import java.lang.reflect.Field;
jaroslav@182
    46
import java.lang.reflect.Method;
jtulach@884
    47
import java.lang.reflect.Modifier;
jaroslav@182
    48
import java.util.ArrayList;
jaroslav@182
    49
import java.util.List;
jaroslav@182
    50
import java.util.concurrent.Executors;
jtulach@569
    51
import net.java.html.BrwsrCtx;
jaroslav@182
    52
import net.java.html.boot.BrowserBuilder;
jtulach@838
    53
import org.netbeans.html.boot.spi.Fn;
jaroslav@488
    54
import org.testng.Assert;
jtulach@569
    55
import static org.testng.Assert.assertNotSame;
jtulach@569
    56
import static org.testng.Assert.assertSame;
jaroslav@182
    57
import org.testng.annotations.Factory;
jaroslav@182
    58
jaroslav@182
    59
/**
jaroslav@182
    60
 *
jtulach@790
    61
 * @author Jaroslav Tulach
jaroslav@182
    62
 */
jtulach@884
    63
public class DynamicClassLoaderTest {
jaroslav@182
    64
    private static Class<?> browserClass;
jaroslav@288
    65
    private static Fn.Presenter browserPresenter;
jaroslav@182
    66
    
jtulach@884
    67
    public DynamicClassLoaderTest() {
jaroslav@182
    68
    }
jaroslav@182
    69
jaroslav@182
    70
    @Factory public static Object[] compatibilityTests() throws Exception {
jtulach@884
    71
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(DynamicClassLoaderTest.class).
jaroslav@182
    72
            loadPage("empty.html").
jaroslav@182
    73
            invoke("initialized");
jaroslav@182
    74
jaroslav@182
    75
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@182
    76
            @Override
jaroslav@182
    77
            public void run() {
jaroslav@182
    78
                bb.showAndWait();
jaroslav@182
    79
            }
jaroslav@182
    80
        });
jaroslav@182
    81
jaroslav@182
    82
        List<Object> res = new ArrayList<Object>();
jaroslav@333
    83
jtulach@884
    84
        Class[] arr = new Class[] { loadClass() };
jaroslav@333
    85
        for (Class c : arr) {
jtulach@884
    86
            for (Method m : c.getDeclaredMethods()) {
jtulach@884
    87
                if ((m.getModifiers() & Modifier.PUBLIC) != 0) {
jaroslav@333
    88
                    res.add(new KOFx(browserPresenter, m));
jaroslav@333
    89
                }
jaroslav@182
    90
            }
jaroslav@182
    91
        }
jaroslav@182
    92
        return res.toArray();
jaroslav@182
    93
    }
jaroslav@182
    94
jaroslav@182
    95
    static synchronized Class<?> loadClass() throws InterruptedException {
jaroslav@182
    96
        while (browserClass == null) {
jtulach@884
    97
            DynamicClassLoaderTest.class.wait();
jaroslav@182
    98
        }
jaroslav@182
    99
        return browserClass;
jaroslav@182
   100
    }
jaroslav@182
   101
    
jtulach@884
   102
    public static void ready(Class<?> browserCls) throws Exception {
jtulach@884
   103
        Class<?> origClazz = ClassLoader.getSystemClassLoader().loadClass(DynamicClassLoaderTest.class.getName());
jtulach@884
   104
        final Field f1 = origClazz.getDeclaredField("browserClass");
jtulach@884
   105
        f1.setAccessible(true);
jtulach@884
   106
        f1.set(null, browserCls);
jtulach@884
   107
        final Field f2 = origClazz.getDeclaredField("browserPresenter");
jtulach@884
   108
        f2.setAccessible(true);
jtulach@884
   109
        f2.set(null, Fn.activePresenter());
jtulach@884
   110
        synchronized (origClazz) {
jtulach@884
   111
            origClazz.notifyAll();
jtulach@884
   112
        }
jaroslav@182
   113
    }
jaroslav@182
   114
    
jaroslav@182
   115
    public static void initialized() throws Exception {
jtulach@884
   116
        BrwsrCtx b1 = BrwsrCtx.findDefault(DynamicClassLoaderTest.class);
jtulach@569
   117
        assertNotSame(b1, BrwsrCtx.EMPTY, "Browser context is not empty");
jtulach@884
   118
        BrwsrCtx b2 = BrwsrCtx.findDefault(DynamicClassLoaderTest.class);
jtulach@569
   119
        assertSame(b1, b2, "Browser context remains stable");
jtulach@884
   120
        Assert.assertNotSame(DynamicClassLoaderTest.class.getClassLoader(),
jaroslav@488
   121
            ClassLoader.getSystemClassLoader(),
jtulach@884
   122
            "Should use special classloader, not system one"
jaroslav@488
   123
        );
jtulach@884
   124
        DynamicClassLoaderTest.ready(JavaScriptBodyTst.class);
jaroslav@182
   125
    }
jaroslav@182
   126
}