2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
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]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
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.
43 package org.netbeans.html.ko.felix.test;
45 import java.io.Closeable;
47 import java.io.IOException;
48 import java.lang.annotation.Annotation;
49 import java.lang.reflect.Method;
51 import java.util.ArrayList;
52 import java.util.HashMap;
53 import java.util.List;
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.fail;
70 import org.testng.annotations.AfterClass;
71 import org.testng.annotations.Factory;
75 * @author Jaroslav Tulach
77 public class KnockoutFelixIT {
78 private static final Logger LOG = Logger.getLogger(KnockoutFelixIT.class.getName());
79 private static Framework framework;
80 private static File dir;
81 static Framework framework() throws Exception {
82 if (framework != null) {
85 for (FrameworkFactory ff : ServiceLoader.load(FrameworkFactory.class)) {
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");
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,"
99 + "javafx.beans.property,"
100 + "javafx.beans.value,"
101 + "javafx.collections,"
102 + "javafx.concurrent,"
106 + "javafx.scene.control,"
107 + "javafx.scene.image,"
108 + "javafx.scene.layout,"
109 + "javafx.scene.text,"
110 + "javafx.scene.web,"
113 + "netscape.javascript"
115 framework = ff.newFramework(config);
117 loadClassPathBundles(framework);
119 for (Bundle b : framework.getBundleContext().getBundles()) {
121 if (b.getSymbolicName().contains("felix.framework")) {
124 if (b.getSymbolicName().contains("glassfish.grizzly")) {
128 LOG.log(Level.INFO, "Started {0}", b.getSymbolicName());
129 } catch (BundleException ex) {
130 throw new IllegalStateException("Cannot start bundle " + b.getSymbolicName(), ex);
135 fail("No OSGi framework in the path");
139 @AfterClass public static void cleanUp() throws Exception {
140 if (framework != null) framework.stop();
143 private static void clearUpDir(File dir) {
144 if (dir.isDirectory()) {
145 for (File f : dir.listFiles()) {
154 private static void loadClassPathBundles(Framework f) throws IOException, BundleException {
155 for (String jar : System.getProperty("java.class.path").split(File.pathSeparator)) {
156 File file = new File(jar);
157 if (!file.isFile()) {
158 LOG.info("Not loading " + file);
161 JarFile jf = new JarFile(file);
162 final String name = jf.getManifest().getMainAttributes().getValue("Bundle-SymbolicName");
165 if (name.contains("org.eclipse.osgi")) {
166 throw new IllegalStateException("Found " + name + " !");
168 if (name.contains("felix.framework")) {
171 if (name.contains("testng")) {
174 if (name.equals("org.apache.aries.spifly.dynamic.bundle")) {
177 final String path = "reference:" + file.toURI().toString();
179 Bundle b = f.getBundleContext().installBundle(path);
180 } catch (BundleException ex) {
181 LOG.log(Level.WARNING, "Cannot install " + file, ex);
187 private static Class<?> loadOSGiClass(Class<?> c) throws Exception {
188 return KnockoutFelixTCKImpl.loadOSGiClass(c.getName(), KnockoutFelixIT.framework().getBundleContext());
191 private static Class<?> browserClass;
192 private static Object browserContext;
194 @Factory public static Object[] compatibilityTests() throws Exception {
195 Class<?> tck = loadOSGiClass(KnockoutTCK.class);
196 Class<?> peer = loadOSGiClass(KnockoutFelixTCKImpl.class);
197 // initialize the TCK
198 Callable<Class[]> inst = (Callable<Class[]>) peer.newInstance();
200 Class[] arr = inst.call();
201 for (int i = 0; i < arr.length; i++) {
202 if (arr[i].getClassLoader() == ClassLoader.getSystemClassLoader()) {
203 fail("Should be an OSGi class: " + arr[i]);
207 URI uri = DynamicHTTP.initServer();
209 Method start = peer.getMethod("start", String.class, URI.class, boolean.class);
210 start.invoke(null, KnockoutFelixIT.class.getName(), uri, true);
212 ClassLoader l = getClassLoader(null);
213 List<Object> res = new ArrayList<Object>();
214 for (int i = 0; i < arr.length; i++) {
215 seekKOTests(arr[i], res);
217 return res.toArray();
220 private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
221 Class<? extends Annotation> koTest =
222 c.getClassLoader().loadClass(KOTest.class.getName()).
223 asSubclass(Annotation.class);
224 for (Method m : c.getMethods()) {
225 if (m.getAnnotation(koTest) != null) {
226 res.add(new KOFx(KnockoutFelixIT.class, browserContext, m));
231 static synchronized ClassLoader getClassLoader(Object[] presenter) throws InterruptedException {
232 while (browserClass == null) {
233 KnockoutFelixIT.class.wait();
235 if (presenter != null) {
236 presenter[0] = browserContext;
238 return browserClass.getClassLoader();
241 public static synchronized void initialized(Class<?> browserCls, Object presenter) throws Exception {
242 browserClass = browserCls;
243 browserContext = presenter;
244 KnockoutFelixIT.class.notifyAll();
247 public static Closeable activateInOSGi(Object presenter) throws Exception {
248 Class<?> presenterClass = loadOSGiClass(Fn.Presenter.class);
249 Class<?> fnClass = loadOSGiClass(Fn.class);
250 Method m = fnClass.getMethod("activate", presenterClass);
251 return (Closeable) m.invoke(null, presenter);