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 net.java.html.boot.script.ko4j;
45 import java.io.BufferedReader;
46 import java.io.IOException;
47 import java.io.InputStream;
48 import java.io.InputStreamReader;
49 import java.lang.annotation.Annotation;
50 import java.lang.reflect.Method;
52 import java.net.URISyntaxException;
54 import java.net.URLConnection;
55 import java.util.ArrayList;
56 import java.util.List;
58 import java.util.concurrent.Executor;
59 import java.util.concurrent.Executors;
60 import net.java.html.BrwsrCtx;
61 import net.java.html.boot.BrowserBuilder;
62 import net.java.html.boot.script.Scripts;
63 import net.java.html.js.JavaScriptBody;
64 import org.apidesign.html.boot.spi.Fn;
65 import org.apidesign.html.context.spi.Contexts;
66 import org.apidesign.html.json.spi.Technology;
67 import org.apidesign.html.json.spi.Transfer;
68 import org.apidesign.html.json.tck.KOTest;
69 import org.apidesign.html.json.tck.KnockoutTCK;
70 import org.netbeans.html.ko4j.KO4J;
71 import org.netbeans.html.wstyrus.TyrusContext;
72 import org.openide.util.lookup.ServiceProvider;
73 import org.testng.Assert;
74 import static org.testng.Assert.*;
75 import org.testng.annotations.Factory;
79 * @author Jaroslav Tulach
81 @ServiceProvider(service = KnockoutTCK.class)
82 public final class KnockoutEnvJSTest extends KnockoutTCK {
83 private static Class<?> browserClass;
84 private static Fn.Presenter browserContext;
85 private static URI baseUri;
87 public KnockoutEnvJSTest() {
90 @Factory public static Object[] compatibilityTests() throws Exception {
92 Class.forName("java.lang.FunctionalInterface");
93 } catch (ClassNotFoundException ex) {
99 Class[] arr = testClasses();
100 for (int i = 0; i < arr.length; i++) {
102 arr[i].getClassLoader(),
103 KnockoutEnvJSTest.class.getClassLoader(),
104 "All classes loaded by the same classloader"
108 baseUri = DynamicHTTP.initServer();
110 final Fn.Presenter p = Scripts.createPresenter(KOCase.JS);
111 InputStream is = KnockoutEnvJSTest.class.getResourceAsStream("env.nashorn.1.2-debug.js");
112 p.loadScript(new InputStreamReader(is));
115 final BrowserBuilder bb = BrowserBuilder.newBrowser(p).
116 loadClass(KnockoutEnvJSTest.class).
117 loadPage(baseUri.toString()).
118 invoke("initialized");
120 Executors.newSingleThreadExecutor().submit(new Runnable() {
127 ClassLoader l = getClassLoader();
128 List<Object> res = new ArrayList<Object>();
129 for (int i = 0; i < arr.length; i++) {
130 Class<?> c = Class.forName(arr[i].getName(), true, l);
133 return res.toArray();
136 private static void seekKOTests(Class<?> c, List<Object> res) throws SecurityException, ClassNotFoundException {
137 Class<? extends Annotation> koTest =
138 c.getClassLoader().loadClass(KOTest.class.getName()).
139 asSubclass(Annotation.class);
140 for (Method m : c.getMethods()) {
141 if (m.getAnnotation(koTest) != null) {
142 res.add(new KOCase(browserContext, m, skipMsg(m.getName())));
147 private static String skipMsg(String methodName) {
148 final String ver = System.getProperty("java.runtime.version"); // NOI18N
150 !"1.8.0_05-b13".equals(ver) &&
151 !"1.8.0_11-b12".equals(ver)
153 // we know that 1.8.0_05 and 1.8.0_11 are broken,
154 // let's not speculate about anything else
157 switch (methodName) {
158 case "paintTheGridOnClick":
159 case "displayContentOfArrayOfPeople":
160 case "connectUsingWebSocket":
161 return "Does not work on JDK8, due to JDK-8046013";
166 static synchronized ClassLoader getClassLoader() throws InterruptedException {
167 while (browserClass == null) {
168 KnockoutEnvJSTest.class.wait();
170 return browserClass.getClassLoader();
173 public static synchronized void initialized(Class<?> browserCls) throws Exception {
174 browserClass = browserCls;
175 browserContext = Fn.activePresenter();
176 KnockoutEnvJSTest.class.notifyAll();
179 public static void initialized() throws Exception {
181 KnockoutEnvJSTest.class.getClassLoader(),
182 ClassLoader.getSystemClassLoader(),
183 "No special classloaders"
185 KnockoutEnvJSTest.initialized(KnockoutEnvJSTest.class);
186 browserContext = Fn.activePresenter();
190 public BrwsrCtx createContext() {
191 KO4J fx = new KO4J(browserContext);
192 TyrusContext tc = new TyrusContext();
193 Contexts.Builder cb = Contexts.newBuilder().
194 register(Technology.class, fx.knockout(), 10).
195 register(Transfer.class, tc, 10);
196 cb.register(Fn.Presenter.class, browserContext, 10);
197 cb.register(Executor.class, (Executor)browserContext, 10);
198 BrwsrCtx ctx = cb.build();
203 public Object createJSON(Map<String, Object> values) {
204 Object json = createJSON();
205 for (Map.Entry<String, Object> entry : values.entrySet()) {
206 setProperty(json, entry.getKey(), entry.getValue());
211 @JavaScriptBody(args = {}, body = "return new Object();")
212 private static native Object createJSON();
213 @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
214 private static native void setProperty(Object json, String key, Object value);
217 @JavaScriptBody(args = { "s", "args" }, body = "\n"
218 + "var f = new Function(s);\n"
219 + "return f.apply(null, args);\n"
221 public native Object executeScript(String script, Object[] arguments);
223 private static String findBaseURL() {
224 return baseUri.toString();
228 public URI prepareURL(String content, String mimeType, String[] parameters) {
230 final URL baseURL = new URL(findBaseURL());
231 StringBuilder sb = new StringBuilder();
232 sb.append("/dynamic?mimeType=").append(mimeType);
233 for (int i = 0; i < parameters.length; i++) {
234 sb.append("¶m" + i).append("=").append(parameters[i]);
236 String mangle = content.replace("\n", "%0a")
237 .replace("\"", "\\\"").replace(" ", "%20");
238 sb.append("&content=").append(mangle);
240 URL query = new URL(baseURL, sb.toString());
241 URLConnection c = query.openConnection();
242 BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
243 URI connectTo = new URI(br.readLine());
245 } catch (IOException ex) {
246 throw new IllegalStateException(ex);
247 } catch (URISyntaxException ex) {
248 throw new IllegalStateException(ex);
253 public boolean canFailWebSocketTest() {