ko-osgi-test/src/test/java/org/netbeans/html/ko/osgi/test/KnockoutEquinoxIT.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Fri, 07 Feb 2014 07:44:34 +0100
changeset 551 7ca2253fa86d
parent 475 57857c9eec5b
child 743 e2aa389d4968
permissions -rw-r--r--
Updating copyright headers to mention current year
jaroslav@370
     1
/**
jaroslav@370
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@370
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@370
     5
 *
jaroslav@370
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@370
     7
 * Other names may be trademarks of their respective owners.
jaroslav@370
     8
 *
jaroslav@370
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@370
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@370
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@370
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@370
    13
 * License. You can obtain a copy of the License at
jaroslav@370
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@370
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@370
    16
 * specific language governing permissions and limitations under the
jaroslav@370
    17
 * License.  When distributing the software, include this License Header
jaroslav@370
    18
 * Notice in each file and include the License file at
jaroslav@370
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@370
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@370
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@370
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@370
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@370
    24
 * your own identifying information:
jaroslav@370
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@370
    26
 *
jaroslav@370
    27
 * Contributor(s):
jaroslav@370
    28
 *
jaroslav@370
    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@370
    31
 *
jaroslav@370
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@370
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@370
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@370
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@370
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@370
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@370
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@370
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@370
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@370
    41
 * made subject to such option by the copyright holder.
jaroslav@370
    42
 */
jaroslav@370
    43
package org.netbeans.html.ko.osgi.test;
jaroslav@370
    44
jaroslav@396
    45
import java.io.Closeable;
jaroslav@370
    46
import java.io.File;
jaroslav@370
    47
import java.io.IOException;
jaroslav@371
    48
import java.lang.annotation.Annotation;
jaroslav@371
    49
import java.lang.reflect.Method;
jaroslav@371
    50
import java.net.URI;
jaroslav@371
    51
import java.util.ArrayList;
jaroslav@370
    52
import java.util.HashMap;
jaroslav@371
    53
import java.util.List;
jaroslav@370
    54
import java.util.Map;
jaroslav@370
    55
import java.util.ServiceLoader;
jaroslav@371
    56
import java.util.concurrent.Callable;
jaroslav@370
    57
import java.util.jar.JarFile;
jaroslav@389
    58
import java.util.logging.Level;
jaroslav@389
    59
import java.util.logging.Logger;
jaroslav@371
    60
import org.apidesign.html.boot.spi.Fn;
jaroslav@371
    61
import org.apidesign.html.json.tck.KOTest;
jaroslav@371
    62
import org.apidesign.html.json.tck.KnockoutTCK;
jaroslav@370
    63
import org.osgi.framework.Bundle;
jaroslav@370
    64
import org.osgi.framework.BundleException;
jaroslav@371
    65
import org.osgi.framework.Constants;
jaroslav@370
    66
import org.osgi.framework.launch.Framework;
jaroslav@370
    67
import org.osgi.framework.launch.FrameworkFactory;
jaroslav@391
    68
import static org.testng.Assert.assertNotNull;
jaroslav@391
    69
import static org.testng.Assert.fail;
jaroslav@371
    70
import org.testng.annotations.AfterClass;
jaroslav@371
    71
import org.testng.annotations.Factory;
jaroslav@370
    72
jaroslav@370
    73
/**
jaroslav@370
    74
 *
jaroslav@370
    75
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@370
    76
 */
jaroslav@391
    77
public class KnockoutEquinoxIT {
jaroslav@391
    78
    private static final Logger LOG = Logger.getLogger(KnockoutEquinoxIT.class.getName());
jaroslav@370
    79
    private static Framework framework;
jaroslav@371
    80
    private static File dir;
jaroslav@371
    81
    static Framework framework() throws Exception {
jaroslav@371
    82
        if (framework != null) {
jaroslav@371
    83
            return framework;
jaroslav@371
    84
        }
jaroslav@370
    85
        for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
jaroslav@391
    86
            
jaroslav@391
    87
            String basedir = System.getProperty("basedir");
jaroslav@391
    88
            assertNotNull("basedir preperty provided", basedir);
jaroslav@391
    89
            File target = new File(basedir, "target");
jaroslav@391
    90
            dir = new File(target, "osgi");
jaroslav@391
    91
            dir.mkdirs();
jaroslav@391
    92
            
jaroslav@470
    93
            Map<String,String> config = new HashMap<String, String>();
jaroslav@371
    94
            config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
jaroslav@371
    95
            config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
jaroslav@390
    96
            config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "sun.misc,"
jaroslav@390
    97
                + "javafx.application,"
jaroslav@390
    98
                + "javafx.beans.property,"
jaroslav@390
    99
                + "javafx.beans.value,"
jaroslav@390
   100
                + "javafx.collections,"
jaroslav@390
   101
                + "javafx.concurrent,"
jaroslav@390
   102
                + "javafx.event,"
jaroslav@390
   103
                + "javafx.geometry,"
jaroslav@390
   104
                + "javafx.scene,"
jaroslav@390
   105
                + "javafx.scene.control,"
jaroslav@390
   106
                + "javafx.scene.layout,"
jaroslav@390
   107
                + "javafx.scene.text,"
jaroslav@390
   108
                + "javafx.scene.web,"
jaroslav@390
   109
                + "javafx.stage,"
jaroslav@390
   110
                + "javafx.util,"
jaroslav@390
   111
                + "netscape.javascript"
jaroslav@390
   112
            );
jaroslav@393
   113
            config.put("osgi.hook.configurators.include", "org.netbeans.html.equinox.agentclass.AgentHook");
jaroslav@370
   114
            framework = ff.newFramework(config);
jaroslav@370
   115
            framework.init();
jaroslav@370
   116
            loadClassPathBundles(framework);
jaroslav@370
   117
            framework.start();
jaroslav@371
   118
            for (Bundle b : framework.getBundleContext().getBundles()) {
jaroslav@389
   119
                try {
jaroslav@401
   120
                    if (b.getSymbolicName().contains("equinox-agentclass-hook")) {
jaroslav@401
   121
                        continue;
jaroslav@401
   122
                    }
jaroslav@401
   123
                    if (b.getSymbolicName().contains("grizzly.websockets-server")) {
jaroslav@401
   124
                        continue;
jaroslav@401
   125
                    }
jaroslav@389
   126
                    b.start();
jaroslav@389
   127
                    LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
jaroslav@389
   128
                } catch (BundleException ex) {
jaroslav@389
   129
                    LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
jaroslav@389
   130
                }
jaroslav@371
   131
            }
jaroslav@371
   132
            return framework;
jaroslav@370
   133
        }
jaroslav@370
   134
        fail("No OSGi framework in the path");
jaroslav@371
   135
        return null;
jaroslav@370
   136
    }
jaroslav@371
   137
    
jaroslav@371
   138
    @AfterClass public static void cleanUp() throws Exception {
jaroslav@371
   139
        if (framework != null) framework.stop();
jaroslav@371
   140
        clearUpDir(dir);
jaroslav@371
   141
    }
jaroslav@371
   142
    private static void clearUpDir(File dir) {
jaroslav@371
   143
        if (dir.isDirectory()) {
jaroslav@371
   144
            for (File f : dir.listFiles()) {
jaroslav@371
   145
                clearUpDir(f);
jaroslav@371
   146
            }
jaroslav@371
   147
        }
jaroslav@371
   148
        dir.delete();
jaroslav@371
   149
    }
jaroslav@371
   150
    
jaroslav@371
   151
    
jaroslav@370
   152
jaroslav@370
   153
    private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
jaroslav@370
   154
        for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
jaroslav@370
   155
            File file = new File(jar);
jaroslav@370
   156
            if (!file.isFile()) {
jaroslav@390
   157
                LOG.info("Not loading " + file);
jaroslav@370
   158
                continue;
jaroslav@370
   159
            }
jaroslav@370
   160
            JarFile jf = new JarFile(file);
jaroslav@370
   161
            final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
jaroslav@370
   162
            jf.close();
jaroslav@370
   163
            if (name != null) {
jaroslav@370
   164
                if (name.contains("org.eclipse.osgi")) {
jaroslav@370
   165
                    continue;
jaroslav@370
   166
                }
jaroslav@371
   167
                if (name.contains("testng")) {
jaroslav@371
   168
                    continue;
jaroslav@371
   169
                }
jaroslav@371
   170
                final String path = "reference:" + file.toURI().toString();
jaroslav@398
   171
                try {
jaroslav@398
   172
                    Bundle b = f.getBundleContext().installBundle(path);
jaroslav@398
   173
                } catch (BundleException ex) {
jaroslav@398
   174
                    LOG.log(Level.WARNING, "Cannot install " + file, ex);
jaroslav@398
   175
                }
jaroslav@370
   176
            }
jaroslav@370
   177
        }
jaroslav@370
   178
    }
jaroslav@370
   179
    
jaroslav@396
   180
    private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
jaroslav@396
   181
        return KnockoutEquinoxTCKImpl.loadOSGiClass(c.getName(), KnockoutEquinoxIT.framework().getBundleContext());
jaroslav@370
   182
    }
jaroslav@371
   183
    
jaroslav@371
   184
    private static Class<?> browserClass;
jaroslav@396
   185
    private static Object browserContext;
jaroslav@371
   186
    
jaroslav@371
   187
    @Factory public static Object[] compatibilityTests() throws Exception {
jaroslav@396
   188
        Class<?> tck = loadOSGiClass(KnockoutTCK.class);
jaroslav@396
   189
        Class<?> peer = loadOSGiClass(KnockoutEquinoxTCKImpl.class);
jaroslav@371
   190
        // initialize the TCK
jaroslav@371
   191
        Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
jaroslav@371
   192
        
jaroslav@371
   193
        Class[] arr = inst.call();
jaroslav@371
   194
        for (int i = 0; i < arr.length; i++) {
jaroslav@371
   195
            if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
jaroslav@371
   196
                fail("Should be an OSGi class: " + arr[i]);
jaroslav@371
   197
            }
jaroslav@371
   198
        }
jaroslav@371
   199
        
jaroslav@371
   200
        URI uri = DynamicHTTP.initServer();
jaroslav@371
   201
jaroslav@396
   202
        Method start = peer.getMethod("start", URI.class);
jaroslav@396
   203
        start.invoke(null, uri);
jaroslav@371
   204
        
jaroslav@371
   205
        ClassLoader l = getClassLoader();
jaroslav@371
   206
        List<Object> res = new ArrayList<Object>();
jaroslav@371
   207
        for (int i = 0; i < arr.length; i++) {
jaroslav@393
   208
            seekKOTests(arr[i], res);
jaroslav@371
   209
        }
jaroslav@371
   210
        return res.toArray();
jaroslav@371
   211
    }
jaroslav@371
   212
jaroslav@371
   213
    private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
jaroslav@371
   214
        Class<? extends Annotation> koTest =
jaroslav@371
   215
            c.getClassLoader().loadClass(KOTest.class.getName()).
jaroslav@371
   216
            asSubclass(Annotation.class);
jaroslav@371
   217
        for (Method m : c.getMethods()) {
jaroslav@371
   218
            if (m.getAnnotation(koTest) != null) {
jaroslav@371
   219
                res.add(new KOFx(browserContext, m));
jaroslav@371
   220
            }
jaroslav@371
   221
        }
jaroslav@371
   222
    }
jaroslav@371
   223
jaroslav@371
   224
    static synchronized ClassLoader getClassLoader() throws InterruptedException {
jaroslav@371
   225
        while (browserClass == null) {
jaroslav@391
   226
            KnockoutEquinoxIT.class.wait();
jaroslav@371
   227
        }
jaroslav@371
   228
        return browserClass.getClassLoader();
jaroslav@371
   229
    }
jaroslav@371
   230
    
jaroslav@396
   231
    public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
jaroslav@371
   232
        browserClass = browserCls;
jaroslav@396
   233
        browserContext = presenter;
jaroslav@391
   234
        KnockoutEquinoxIT.class.notifyAll();
jaroslav@371
   235
    }
jaroslav@396
   236
jaroslav@396
   237
    static Closeable activateInOSGi(Object presenter) throws Exception {
jaroslav@396
   238
        Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
jaroslav@396
   239
        Class<?> fnClass = loadOSGiClass(Fn.class);
jaroslav@396
   240
        Method m = fnClass.getMethod("activate", presenterClass);
jaroslav@396
   241
        return (Closeable) m.invoke(null, presenter);
jaroslav@396
   242
    }
jaroslav@370
   243
}