ko-felix-test/src/test/java/org/netbeans/html/ko/felix/test/KnockoutFelixAriesIT.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 26 Nov 2015 20:59:51 +0100
changeset 1021 c918924ad3c5
permissions -rw-r--r--
#256696: Making OSGi headers, enterprise OSGi ready
     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 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.netbeans.html.boot.spi.Fn;
    61 import org.netbeans.html.json.tck.KOTest;
    62 import org.netbeans.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.assertTrue;
    70 import static org.testng.Assert.fail;
    71 import org.testng.annotations.AfterClass;
    72 import org.testng.annotations.Factory;
    73 
    74 public class KnockoutFelixAriesIT {
    75     private static final Logger LOG = Logger.getLogger(KnockoutFelixAriesIT.class.getName());
    76     private static Framework framework;
    77     private static File dir;
    78     static Framework framework() throws Exception {
    79         if (framework != null) {
    80             return framework;
    81         }
    82         for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
    83             
    84             String basedir = System.getProperty("basedir");
    85             assertNotNull("basedir preperty provided", basedir);
    86             File target = new File(basedir, "target");
    87             dir = new File(target, "osgi-aries");
    88             dir.mkdirs();
    89             
    90             Map<String,String> config = new HashMap<String, String>();
    91             config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
    92             config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
    93             config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "sun.misc,"
    94                 + "javafx.application,"
    95                 + "javafx.beans,"
    96                 + "javafx.beans.property,"
    97                 + "javafx.beans.value,"
    98                 + "javafx.collections,"
    99                 + "javafx.concurrent,"
   100                 + "javafx.event,"
   101                 + "javafx.geometry,"
   102                 + "javafx.scene,"
   103                 + "javafx.scene.control,"
   104                 + "javafx.scene.image,"
   105                 + "javafx.scene.layout,"
   106                 + "javafx.scene.text,"
   107                 + "javafx.scene.web,"
   108                 + "javafx.stage,"
   109                 + "javafx.util,"
   110                 + "netscape.javascript"
   111             );
   112             framework = ff.newFramework(config);
   113             framework.init();
   114             loadClassPathBundles(framework);
   115             framework.start();
   116             boolean ok = false;
   117             for (Bundle b : framework.getBundleContext().getBundles()) {
   118                 try {
   119                     if (!b.getSymbolicName().equals("org.apache.aries.spifly.dynamic.bundle")) {
   120                         continue;
   121                     }
   122                     b.start();
   123                     ok = true;
   124                     LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
   125                 } catch (BundleException ex) {
   126                     LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
   127                 }
   128             }
   129             assertTrue(ok, "Aries installed");
   130             for (Bundle b : framework.getBundleContext().getBundles()) {
   131                 try {
   132                     if (b.getSymbolicName().contains("felix.framework")) {
   133                         continue;
   134                     }
   135                     if (b.getSymbolicName().contains("glassfish.grizzly")) {
   136                         continue;
   137                     }
   138                     b.start();
   139                     LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
   140                 } catch (BundleException ex) {
   141                     LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
   142                 }
   143             }
   144             return framework;
   145         }
   146         fail("No OSGi framework in the path");
   147         return null;
   148     }
   149     
   150     @AfterClass public static void cleanUp() throws Exception {
   151         if (framework != null) framework.stop();
   152         clearUpDir(dir);
   153     }
   154     private static void clearUpDir(File dir) {
   155         if (dir.isDirectory()) {
   156             for (File f : dir.listFiles()) {
   157                 clearUpDir(f);
   158             }
   159         }
   160         dir.delete();
   161     }
   162     
   163     
   164 
   165     private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
   166         for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
   167             File file = new File(jar);
   168             if (!file.isFile()) {
   169                 LOG.info("Not loading " + file);
   170                 continue;
   171             }
   172             JarFile jf = new JarFile(file);
   173             final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
   174             jf.close();
   175             if (name != null) {
   176                 if (name.contains("org.eclipse.osgi")) {
   177                     throw new IllegalStateException("Found " + name + " !");
   178                 }
   179                 if (name.contains("felix.framework")) {
   180                     continue;
   181                 }
   182                 if (name.contains("testng")) {
   183                     continue;
   184                 }
   185                 final String path = "reference:" + file.toURI().toString();
   186                 try {
   187                     Bundle b = f.getBundleContext().installBundle(path);
   188                 } catch (BundleException ex) {
   189                     LOG.log(Level.WARNING, "Cannot install " + file, ex);
   190                 }
   191             }
   192         }
   193     }
   194     
   195     private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
   196         return KnockoutFelixTCKImpl.loadOSGiClass(c.getName(), KnockoutFelixAriesIT.framework().getBundleContext());
   197     }
   198     
   199     private static Class<?> browserClass;
   200     private static Object browserContext;
   201 
   202     @Factory public static Object[] compatibilityTests() throws Exception {
   203         Class<?> tck = loadOSGiClass(KnockoutTCK.class);
   204         Class<?> peer = loadOSGiClass(KnockoutFelixTCKImpl.class);
   205         // initialize the TCK
   206         Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
   207         
   208         Class[] arr = inst.call();
   209         for (int i = 0; i < arr.length; i++) {
   210             if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
   211                 fail("Should be an OSGi class: " + arr[i]);
   212             }
   213         }
   214         
   215         URI uri = DynamicHTTP.initServer();
   216 
   217         Method start = peer.getMethod("start", String.class, URI.class, boolean.class);
   218         start.invoke(null, KnockoutFelixAriesIT.class.getName(), uri, false);
   219         
   220         ClassLoader l = getClassLoader();
   221         List<Object> res = new ArrayList<Object>();
   222         for (int i = 0; i < arr.length; i++) {
   223             seekKOTests(arr[i], res);
   224         }
   225         return res.toArray();
   226     }
   227 
   228     private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
   229         Class<? extends Annotation> koTest =
   230             c.getClassLoader().loadClass(KOTest.class.getName()).
   231             asSubclass(Annotation.class);
   232         for (Method m : c.getMethods()) {
   233             if (m.getAnnotation(koTest) != null) {
   234                 res.add(new KOFx(KnockoutFelixAriesIT.class, browserContext, m));
   235             }
   236         }
   237     }
   238 
   239     static synchronized ClassLoader getClassLoader() throws InterruptedException {
   240         while (browserClass == null) {
   241             KnockoutFelixAriesIT.class.wait();
   242         }
   243         return browserClass.getClassLoader();
   244     }
   245 
   246     public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
   247         browserClass = browserCls;
   248         browserContext = presenter;
   249         KnockoutFelixAriesIT.class.notifyAll();
   250     }
   251 
   252     public static Closeable activateInOSGi(Object presenter) throws Exception {
   253         Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
   254         Class<?> fnClass = loadOSGiClass(Fn.class);
   255         Method m = fnClass.getMethod("activate", presenterClass);
   256         return (Closeable) m.invoke(null, presenter);
   257     }
   258 }