ko-felix-test/src/test/java/org/netbeans/html/ko/felix/test/KnockoutFelixAriesIT.java
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/ko-felix-test/src/test/java/org/netbeans/html/ko/felix/test/KnockoutFelixAriesIT.java Thu Nov 26 20:59:51 2015 +0100
1.3 @@ -0,0 +1,258 @@
1.4 +/**
1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 + *
1.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
1.8 + *
1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
1.10 + * Other names may be trademarks of their respective owners.
1.11 + *
1.12 + * The contents of this file are subject to the terms of either the GNU
1.13 + * General Public License Version 2 only ("GPL") or the Common
1.14 + * Development and Distribution License("CDDL") (collectively, the
1.15 + * "License"). You may not use this file except in compliance with the
1.16 + * License. You can obtain a copy of the License at
1.17 + * http://www.netbeans.org/cddl-gplv2.html
1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.19 + * specific language governing permissions and limitations under the
1.20 + * License. When distributing the software, include this License Header
1.21 + * Notice in each file and include the License file at
1.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
1.23 + * particular file as subject to the "Classpath" exception as provided
1.24 + * by Oracle in the GPL Version 2 section of the License file that
1.25 + * accompanied this code. If applicable, add the following below the
1.26 + * License Header, with the fields enclosed by brackets [] replaced by
1.27 + * your own identifying information:
1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
1.29 + *
1.30 + * Contributor(s):
1.31 + *
1.32 + * The Original Software is NetBeans. The Initial Developer of the Original
1.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
1.34 + *
1.35 + * If you wish your version of this file to be governed by only the CDDL
1.36 + * or only the GPL Version 2, indicate your decision by adding
1.37 + * "[Contributor] elects to include this software in this distribution
1.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.39 + * single choice of license, a recipient has the option to distribute
1.40 + * your version of this file under either the CDDL, the GPL Version 2 or
1.41 + * to extend the choice of license to its licensees as provided above.
1.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
1.43 + * Version 2 license, then the option applies only if the new code is
1.44 + * made subject to such option by the copyright holder.
1.45 + */
1.46 +package org.netbeans.html.ko.felix.test;
1.47 +
1.48 +import java.io.Closeable;
1.49 +import java.io.File;
1.50 +import java.io.IOException;
1.51 +import java.lang.annotation.Annotation;
1.52 +import java.lang.reflect.Method;
1.53 +import java.net.URI;
1.54 +import java.util.ArrayList;
1.55 +import java.util.HashMap;
1.56 +import java.util.List;
1.57 +import java.util.Map;
1.58 +import java.util.ServiceLoader;
1.59 +import java.util.concurrent.Callable;
1.60 +import java.util.jar.JarFile;
1.61 +import java.util.logging.Level;
1.62 +import java.util.logging.Logger;
1.63 +import org.netbeans.html.boot.spi.Fn;
1.64 +import org.netbeans.html.json.tck.KOTest;
1.65 +import org.netbeans.html.json.tck.KnockoutTCK;
1.66 +import org.osgi.framework.Bundle;
1.67 +import org.osgi.framework.BundleException;
1.68 +import org.osgi.framework.Constants;
1.69 +import org.osgi.framework.launch.Framework;
1.70 +import org.osgi.framework.launch.FrameworkFactory;
1.71 +import static org.testng.Assert.assertNotNull;
1.72 +import static org.testng.Assert.assertTrue;
1.73 +import static org.testng.Assert.fail;
1.74 +import org.testng.annotations.AfterClass;
1.75 +import org.testng.annotations.Factory;
1.76 +
1.77 +public class KnockoutFelixAriesIT {
1.78 + private static final Logger LOG = Logger.getLogger(KnockoutFelixAriesIT.class.getName());
1.79 + private static Framework framework;
1.80 + private static File dir;
1.81 + static Framework framework() throws Exception {
1.82 + if (framework != null) {
1.83 + return framework;
1.84 + }
1.85 + for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
1.86 +
1.87 + String basedir = System.getProperty("basedir");
1.88 + assertNotNull("basedir preperty provided", basedir);
1.89 + File target = new File(basedir, "target");
1.90 + dir = new File(target, "osgi-aries");
1.91 + dir.mkdirs();
1.92 +
1.93 + Map<String,String> config = new HashMap<String, String>();
1.94 + config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
1.95 + config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
1.96 + config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "sun.misc,"
1.97 + + "javafx.application,"
1.98 + + "javafx.beans,"
1.99 + + "javafx.beans.property,"
1.100 + + "javafx.beans.value,"
1.101 + + "javafx.collections,"
1.102 + + "javafx.concurrent,"
1.103 + + "javafx.event,"
1.104 + + "javafx.geometry,"
1.105 + + "javafx.scene,"
1.106 + + "javafx.scene.control,"
1.107 + + "javafx.scene.image,"
1.108 + + "javafx.scene.layout,"
1.109 + + "javafx.scene.text,"
1.110 + + "javafx.scene.web,"
1.111 + + "javafx.stage,"
1.112 + + "javafx.util,"
1.113 + + "netscape.javascript"
1.114 + );
1.115 + framework = ff.newFramework(config);
1.116 + framework.init();
1.117 + loadClassPathBundles(framework);
1.118 + framework.start();
1.119 + boolean ok = false;
1.120 + for (Bundle b : framework.getBundleContext().getBundles()) {
1.121 + try {
1.122 + if (!b.getSymbolicName().equals("org.apache.aries.spifly.dynamic.bundle")) {
1.123 + continue;
1.124 + }
1.125 + b.start();
1.126 + ok = true;
1.127 + LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
1.128 + } catch (BundleException ex) {
1.129 + LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
1.130 + }
1.131 + }
1.132 + assertTrue(ok, "Aries installed");
1.133 + for (Bundle b : framework.getBundleContext().getBundles()) {
1.134 + try {
1.135 + if (b.getSymbolicName().contains("felix.framework")) {
1.136 + continue;
1.137 + }
1.138 + if (b.getSymbolicName().contains("glassfish.grizzly")) {
1.139 + continue;
1.140 + }
1.141 + b.start();
1.142 + LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
1.143 + } catch (BundleException ex) {
1.144 + LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
1.145 + }
1.146 + }
1.147 + return framework;
1.148 + }
1.149 + fail("No OSGi framework in the path");
1.150 + return null;
1.151 + }
1.152 +
1.153 + @AfterClass public static void cleanUp() throws Exception {
1.154 + if (framework != null) framework.stop();
1.155 + clearUpDir(dir);
1.156 + }
1.157 + private static void clearUpDir(File dir) {
1.158 + if (dir.isDirectory()) {
1.159 + for (File f : dir.listFiles()) {
1.160 + clearUpDir(f);
1.161 + }
1.162 + }
1.163 + dir.delete();
1.164 + }
1.165 +
1.166 +
1.167 +
1.168 + private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
1.169 + for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
1.170 + File file = new File(jar);
1.171 + if (!file.isFile()) {
1.172 + LOG.info("Not loading " + file);
1.173 + continue;
1.174 + }
1.175 + JarFile jf = new JarFile(file);
1.176 + final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
1.177 + jf.close();
1.178 + if (name != null) {
1.179 + if (name.contains("org.eclipse.osgi")) {
1.180 + throw new IllegalStateException("Found " + name + " !");
1.181 + }
1.182 + if (name.contains("felix.framework")) {
1.183 + continue;
1.184 + }
1.185 + if (name.contains("testng")) {
1.186 + continue;
1.187 + }
1.188 + final String path = "reference:" + file.toURI().toString();
1.189 + try {
1.190 + Bundle b = f.getBundleContext().installBundle(path);
1.191 + } catch (BundleException ex) {
1.192 + LOG.log(Level.WARNING, "Cannot install " + file, ex);
1.193 + }
1.194 + }
1.195 + }
1.196 + }
1.197 +
1.198 + private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
1.199 + return KnockoutFelixTCKImpl.loadOSGiClass(c.getName(), KnockoutFelixAriesIT.framework().getBundleContext());
1.200 + }
1.201 +
1.202 + private static Class<?> browserClass;
1.203 + private static Object browserContext;
1.204 +
1.205 + @Factory public static Object[] compatibilityTests() throws Exception {
1.206 + Class<?> tck = loadOSGiClass(KnockoutTCK.class);
1.207 + Class<?> peer = loadOSGiClass(KnockoutFelixTCKImpl.class);
1.208 + // initialize the TCK
1.209 + Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
1.210 +
1.211 + Class[] arr = inst.call();
1.212 + for (int i = 0; i < arr.length; i++) {
1.213 + if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
1.214 + fail("Should be an OSGi class: " + arr[i]);
1.215 + }
1.216 + }
1.217 +
1.218 + URI uri = DynamicHTTP.initServer();
1.219 +
1.220 + Method start = peer.getMethod("start", String.class, URI.class, boolean.class);
1.221 + start.invoke(null, KnockoutFelixAriesIT.class.getName(), uri, false);
1.222 +
1.223 + ClassLoader l = getClassLoader();
1.224 + List<Object> res = new ArrayList<Object>();
1.225 + for (int i = 0; i < arr.length; i++) {
1.226 + seekKOTests(arr[i], res);
1.227 + }
1.228 + return res.toArray();
1.229 + }
1.230 +
1.231 + private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
1.232 + Class<? extends Annotation> koTest =
1.233 + c.getClassLoader().loadClass(KOTest.class.getName()).
1.234 + asSubclass(Annotation.class);
1.235 + for (Method m : c.getMethods()) {
1.236 + if (m.getAnnotation(koTest) != null) {
1.237 + res.add(new KOFx(KnockoutFelixAriesIT.class, browserContext, m));
1.238 + }
1.239 + }
1.240 + }
1.241 +
1.242 + static synchronized ClassLoader getClassLoader() throws InterruptedException {
1.243 + while (browserClass == null) {
1.244 + KnockoutFelixAriesIT.class.wait();
1.245 + }
1.246 + return browserClass.getClassLoader();
1.247 + }
1.248 +
1.249 + public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
1.250 + browserClass = browserCls;
1.251 + browserContext = presenter;
1.252 + KnockoutFelixAriesIT.class.notifyAll();
1.253 + }
1.254 +
1.255 + public static Closeable activateInOSGi(Object presenter) throws Exception {
1.256 + Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
1.257 + Class<?> fnClass = loadOSGiClass(Fn.class);
1.258 + Method m = fnClass.getMethod("activate", presenterClass);
1.259 + return (Closeable) m.invoke(null, presenter);
1.260 + }
1.261 +}