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/KnockoutFelixIT.java Thu Jul 31 14:36:00 2014 +0200
1.3 @@ -0,0 +1,248 @@
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 org.netbeans.html.ko.felix.test.KnockoutFelixTCKImpl;
1.49 +import java.io.Closeable;
1.50 +import java.io.File;
1.51 +import java.io.IOException;
1.52 +import java.lang.annotation.Annotation;
1.53 +import java.lang.reflect.Method;
1.54 +import java.net.URI;
1.55 +import java.util.ArrayList;
1.56 +import java.util.HashMap;
1.57 +import java.util.List;
1.58 +import java.util.Map;
1.59 +import java.util.ServiceLoader;
1.60 +import java.util.concurrent.Callable;
1.61 +import java.util.jar.JarFile;
1.62 +import java.util.logging.Level;
1.63 +import java.util.logging.Logger;
1.64 +import org.apidesign.html.boot.spi.Fn;
1.65 +import org.apidesign.html.json.tck.KOTest;
1.66 +import org.apidesign.html.json.tck.KnockoutTCK;
1.67 +import org.osgi.framework.Bundle;
1.68 +import org.osgi.framework.BundleException;
1.69 +import org.osgi.framework.Constants;
1.70 +import org.osgi.framework.launch.Framework;
1.71 +import org.osgi.framework.launch.FrameworkFactory;
1.72 +import static org.testng.Assert.assertNotNull;
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 +/**
1.78 + *
1.79 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.80 + */
1.81 +public class KnockoutFelixIT {
1.82 + private static final Logger LOG = Logger.getLogger(KnockoutFelixIT.class.getName());
1.83 + private static Framework framework;
1.84 + private static File dir;
1.85 + static Framework framework() throws Exception {
1.86 + if (framework != null) {
1.87 + return framework;
1.88 + }
1.89 + for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
1.90 +
1.91 + String basedir = System.getProperty("basedir");
1.92 + assertNotNull("basedir preperty provided", basedir);
1.93 + File target = new File(basedir, "target");
1.94 + dir = new File(target, "osgi");
1.95 + dir.mkdirs();
1.96 +
1.97 + Map<String,String> config = new HashMap<String, String>();
1.98 + config.put(Constants.FRAMEWORK_STORAGE, dir.getPath());
1.99 + config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");
1.100 + config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, "sun.misc,"
1.101 + + "javafx.application,"
1.102 + + "javafx.beans,"
1.103 + + "javafx.beans.property,"
1.104 + + "javafx.beans.value,"
1.105 + + "javafx.collections,"
1.106 + + "javafx.concurrent,"
1.107 + + "javafx.event,"
1.108 + + "javafx.geometry,"
1.109 + + "javafx.scene,"
1.110 + + "javafx.scene.control,"
1.111 + + "javafx.scene.image,"
1.112 + + "javafx.scene.layout,"
1.113 + + "javafx.scene.text,"
1.114 + + "javafx.scene.web,"
1.115 + + "javafx.stage,"
1.116 + + "javafx.util,"
1.117 + + "netscape.javascript"
1.118 + );
1.119 + framework = ff.newFramework(config);
1.120 + framework.init();
1.121 + loadClassPathBundles(framework);
1.122 + framework.start();
1.123 + for (Bundle b : framework.getBundleContext().getBundles()) {
1.124 + try {
1.125 + if (b.getSymbolicName().contains("felix.framework")) {
1.126 + continue;
1.127 + }
1.128 + if (b.getSymbolicName().contains("grizzly.websockets-server")) {
1.129 + continue;
1.130 + }
1.131 + b.start();
1.132 + LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
1.133 + } catch (BundleException ex) {
1.134 + LOG.log(Level.WARNING, "Cannot start bundle " + b.getSymbolicName(), ex);
1.135 + }
1.136 + }
1.137 + return framework;
1.138 + }
1.139 + fail("No OSGi framework in the path");
1.140 + return null;
1.141 + }
1.142 +
1.143 + @AfterClass public static void cleanUp() throws Exception {
1.144 + if (framework != null) framework.stop();
1.145 + clearUpDir(dir);
1.146 + }
1.147 + private static void clearUpDir(File dir) {
1.148 + if (dir.isDirectory()) {
1.149 + for (File f : dir.listFiles()) {
1.150 + clearUpDir(f);
1.151 + }
1.152 + }
1.153 + dir.delete();
1.154 + }
1.155 +
1.156 +
1.157 +
1.158 + private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
1.159 + for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
1.160 + File file = new File(jar);
1.161 + if (!file.isFile()) {
1.162 + LOG.info("Not loading " + file);
1.163 + continue;
1.164 + }
1.165 + JarFile jf = new JarFile(file);
1.166 + final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
1.167 + jf.close();
1.168 + if (name != null) {
1.169 + if (name.contains("org.eclipse.osgi")) {
1.170 + throw new IllegalStateException("Found " + name + " !");
1.171 + }
1.172 + if (name.contains("felix.framework")) {
1.173 + continue;
1.174 + }
1.175 + if (name.contains("testng")) {
1.176 + continue;
1.177 + }
1.178 + final String path = "reference:" + file.toURI().toString();
1.179 + try {
1.180 + Bundle b = f.getBundleContext().installBundle(path);
1.181 + } catch (BundleException ex) {
1.182 + LOG.log(Level.WARNING, "Cannot install " + file, ex);
1.183 + }
1.184 + }
1.185 + }
1.186 + }
1.187 +
1.188 + private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
1.189 + return KnockoutFelixTCKImpl.loadOSGiClass(c.getName(), KnockoutFelixIT.framework().getBundleContext());
1.190 + }
1.191 +
1.192 + private static Class<?> browserClass;
1.193 + private static Object browserContext;
1.194 +
1.195 + @Factory public static Object[] compatibilityTests() throws Exception {
1.196 + Class<?> tck = loadOSGiClass(KnockoutTCK.class);
1.197 + Class<?> peer = loadOSGiClass(KnockoutFelixTCKImpl.class);
1.198 + // initialize the TCK
1.199 + Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
1.200 +
1.201 + Class[] arr = inst.call();
1.202 + for (int i = 0; i < arr.length; i++) {
1.203 + if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
1.204 + fail("Should be an OSGi class: " + arr[i]);
1.205 + }
1.206 + }
1.207 +
1.208 + URI uri = DynamicHTTP.initServer();
1.209 +
1.210 + Method start = peer.getMethod("start", URI.class);
1.211 + start.invoke(null, uri);
1.212 +
1.213 + ClassLoader l = getClassLoader();
1.214 + List<Object> res = new ArrayList<Object>();
1.215 + for (int i = 0; i < arr.length; i++) {
1.216 + seekKOTests(arr[i], res);
1.217 + }
1.218 + return res.toArray();
1.219 + }
1.220 +
1.221 + private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
1.222 + Class<? extends Annotation> koTest =
1.223 + c.getClassLoader().loadClass(KOTest.class.getName()).
1.224 + asSubclass(Annotation.class);
1.225 + for (Method m : c.getMethods()) {
1.226 + if (m.getAnnotation(koTest) != null) {
1.227 + res.add(new KOFx(browserContext, m));
1.228 + }
1.229 + }
1.230 + }
1.231 +
1.232 + static synchronized ClassLoader getClassLoader() throws InterruptedException {
1.233 + while (browserClass == null) {
1.234 + KnockoutFelixIT.class.wait();
1.235 + }
1.236 + return browserClass.getClassLoader();
1.237 + }
1.238 +
1.239 + public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
1.240 + browserClass = browserCls;
1.241 + browserContext = presenter;
1.242 + KnockoutFelixIT.class.notifyAll();
1.243 + }
1.244 +
1.245 + static Closeable activateInOSGi(Object presenter) throws Exception {
1.246 + Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
1.247 + Class<?> fnClass = loadOSGiClass(Fn.class);
1.248 + Method m = fnClass.getMethod("activate", presenterClass);
1.249 + return (Closeable) m.invoke(null, presenter);
1.250 + }
1.251 +}