ko-felix-test/src/test/java/org/netbeans/html/ko/felix/test/KnockoutFelixIT.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
child 1015 8a88dc9baa87
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
     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.felix.test;
    44 
    45 import org.netbeans.html.ko.felix.test.KnockoutFelixTCKImpl;
    46 import java.io.Closeable;
    47 import java.io.File;
    48 import java.io.IOException;
    49 import java.lang.annotation.Annotation;
    50 import java.lang.reflect.Method;
    51 import java.net.URI;
    52 import java.util.ArrayList;
    53 import java.util.HashMap;
    54 import java.util.List;
    55 import java.util.Map;
    56 import java.util.ServiceLoader;
    57 import java.util.concurrent.Callable;
    58 import java.util.jar.JarFile;
    59 import java.util.logging.Level;
    60 import java.util.logging.Logger;
    61 import org.netbeans.html.boot.spi.Fn;
    62 import org.netbeans.html.json.tck.KOTest;
    63 import org.netbeans.html.json.tck.KnockoutTCK;
    64 import org.osgi.framework.Bundle;
    65 import org.osgi.framework.BundleException;
    66 import org.osgi.framework.Constants;
    67 import org.osgi.framework.launch.Framework;
    68 import org.osgi.framework.launch.FrameworkFactory;
    69 import static org.testng.Assert.assertNotNull;
    70 import static org.testng.Assert.fail;
    71 import org.testng.annotations.AfterClass;
    72 import org.testng.annotations.Factory;
    73 
    74 /**
    75  *
    76  * @author Jaroslav Tulach
    77  */
    78 public class KnockoutFelixIT {
    79     private static final Logger LOG = Logger.getLogger(KnockoutFelixIT.class.getName());
    80     private static Framework framework;
    81     private static File dir;
    82     static Framework framework() throws Exception {
    83         if (framework != null) {
    84             return framework;
    85         }
    86         for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
    87             
    88             String basedir = System.getProperty("basedir");
    89             assertNotNull("basedir preperty provided", basedir);
    90             File target = new File(basedir, "target");
    91             dir = new File(target, "osgi");
    92             dir.mkdirs();
    93             
    94             Map<String,String> config = new HashMap<String, String>();
    95             config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
    96             config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
    97             config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "sun.misc,"
    98                 + "javafx.application,"
    99                 + "javafx.beans,"
   100                 + "javafx.beans.property,"
   101                 + "javafx.beans.value,"
   102                 + "javafx.collections,"
   103                 + "javafx.concurrent,"
   104                 + "javafx.event,"
   105                 + "javafx.geometry,"
   106                 + "javafx.scene,"
   107                 + "javafx.scene.control,"
   108                 + "javafx.scene.image,"
   109                 + "javafx.scene.layout,"
   110                 + "javafx.scene.text,"
   111                 + "javafx.scene.web,"
   112                 + "javafx.stage,"
   113                 + "javafx.util,"
   114                 + "netscape.javascript"
   115             );
   116             framework = ff.newFramework(config);
   117             framework.init();
   118             loadClassPathBundles(framework);
   119             framework.start();
   120             for (Bundle b : framework.getBundleContext().getBundles()) {
   121                 try {
   122                     if (b.getSymbolicName().contains("felix.framework")) {
   123                         continue;
   124                     }
   125                     if (b.getSymbolicName().contains("grizzly.websockets-server")) {
   126                         continue;
   127                     }
   128                     b.start();
   129                     LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
   130                 } catch (BundleException ex) {
   131                     LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
   132                 }
   133             }
   134             return framework;
   135         }
   136         fail("No OSGi framework in the path");
   137         return null;
   138     }
   139     
   140     @AfterClass public static void cleanUp() throws Exception {
   141         if (framework != null) framework.stop();
   142         clearUpDir(dir);
   143     }
   144     private static void clearUpDir(File dir) {
   145         if (dir.isDirectory()) {
   146             for (File f : dir.listFiles()) {
   147                 clearUpDir(f);
   148             }
   149         }
   150         dir.delete();
   151     }
   152     
   153     
   154 
   155     private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
   156         for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
   157             File file = new File(jar);
   158             if (!file.isFile()) {
   159                 LOG.info("Not loading " + file);
   160                 continue;
   161             }
   162             JarFile jf = new JarFile(file);
   163             final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
   164             jf.close();
   165             if (name != null) {
   166                 if (name.contains("org.eclipse.osgi")) {
   167                     throw new IllegalStateException("Found " + name + " !");
   168                 }
   169                 if (name.contains("felix.framework")) {
   170                     continue;
   171                 }
   172                 if (name.contains("testng")) {
   173                     continue;
   174                 }
   175                 final String path = "reference:" + file.toURI().toString();
   176                 try {
   177                     Bundle b = f.getBundleContext().installBundle(path);
   178                 } catch (BundleException ex) {
   179                     LOG.log(Level.WARNING, "Cannot install " + file, ex);
   180                 }
   181             }
   182         }
   183     }
   184     
   185     private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
   186         return KnockoutFelixTCKImpl.loadOSGiClass(c.getName(), KnockoutFelixIT.framework().getBundleContext());
   187     }
   188     
   189     private static Class<?> browserClass;
   190     private static Object browserContext;
   191     
   192     @Factory public static Object[] compatibilityTests() throws Exception {
   193         Class<?> tck = loadOSGiClass(KnockoutTCK.class);
   194         Class<?> peer = loadOSGiClass(KnockoutFelixTCKImpl.class);
   195         // initialize the TCK
   196         Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
   197         
   198         Class[] arr = inst.call();
   199         for (int i = 0; i < arr.length; i++) {
   200             if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
   201                 fail("Should be an OSGi class: " + arr[i]);
   202             }
   203         }
   204         
   205         URI uri = DynamicHTTP.initServer();
   206 
   207         Method start = peer.getMethod("start", URI.class);
   208         start.invoke(null, uri);
   209         
   210         ClassLoader l = getClassLoader();
   211         List<Object> res = new ArrayList<Object>();
   212         for (int i = 0; i < arr.length; i++) {
   213             seekKOTests(arr[i], res);
   214         }
   215         return res.toArray();
   216     }
   217 
   218     private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
   219         Class<? extends Annotation> koTest =
   220             c.getClassLoader().loadClass(KOTest.class.getName()).
   221             asSubclass(Annotation.class);
   222         for (Method m : c.getMethods()) {
   223             if (m.getAnnotation(koTest) != null) {
   224                 res.add(new KOFx(browserContext, m));
   225             }
   226         }
   227     }
   228 
   229     static synchronized ClassLoader getClassLoader() throws InterruptedException {
   230         while (browserClass == null) {
   231             KnockoutFelixIT.class.wait();
   232         }
   233         return browserClass.getClassLoader();
   234     }
   235     
   236     public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
   237         browserClass = browserCls;
   238         browserContext = presenter;
   239         KnockoutFelixIT.class.notifyAll();
   240     }
   241 
   242     static Closeable activateInOSGi(Object presenter) throws Exception {
   243         Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
   244         Class<?> fnClass = loadOSGiClass(Fn.class);
   245         Method m = fnClass.getMethod("activate", presenterClass);
   246         return (Closeable) m.invoke(null, presenter);
   247     }
   248 }