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