xhr4j/src/test/java/org/netbeans/html/xhr4j/JsonKnockoutTest.java
branchxhr4j
changeset 1057 b547f8f663f5
parent 838 bdc3d696dd4a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xhr4j/src/test/java/org/netbeans/html/xhr4j/JsonKnockoutTest.java	Mon Feb 29 05:25:31 2016 +0100
     1.3 @@ -0,0 +1,217 @@
     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.xhr4j;
    1.47 +
    1.48 +import java.io.BufferedReader;
    1.49 +import java.io.IOException;
    1.50 +import java.io.InputStreamReader;
    1.51 +import java.lang.annotation.Annotation;
    1.52 +import java.lang.reflect.Method;
    1.53 +import java.net.URI;
    1.54 +import java.net.URISyntaxException;
    1.55 +import java.net.URL;
    1.56 +import java.net.URLConnection;
    1.57 +import java.util.ArrayList;
    1.58 +import java.util.List;
    1.59 +import java.util.Map;
    1.60 +import java.util.concurrent.Executor;
    1.61 +import java.util.concurrent.Executors;
    1.62 +import net.java.html.BrwsrCtx;
    1.63 +import net.java.html.boot.BrowserBuilder;
    1.64 +import net.java.html.js.JavaScriptBody;
    1.65 +import org.netbeans.html.boot.spi.Fn;
    1.66 +import org.netbeans.html.context.spi.Contexts;
    1.67 +import org.netbeans.html.json.spi.Technology;
    1.68 +import org.netbeans.html.json.tck.KOTest;
    1.69 +import org.netbeans.html.json.tck.KnockoutTCK;
    1.70 +import org.netbeans.html.ko4j.KO4J;
    1.71 +import org.openide.util.lookup.ServiceProvider;
    1.72 +import org.testng.Assert;
    1.73 +import static org.testng.Assert.*;
    1.74 +import org.testng.annotations.Factory;
    1.75 +
    1.76 +/**
    1.77 + *
    1.78 + * @author Jaroslav Tulach
    1.79 + */
    1.80 +@ServiceProvider(service = KnockoutTCK.class)
    1.81 +public final class JsonKnockoutTest extends KnockoutTCK {
    1.82 +    private static Class<?> browserClass;
    1.83 +    private static Fn.Presenter browserContext;
    1.84 +    
    1.85 +    public JsonKnockoutTest() {
    1.86 +    }
    1.87 +    
    1.88 +    @Factory public static Object[] compatibilityTests() throws Exception {
    1.89 +        Class[] arr = testClasses();
    1.90 +        for (int i = 0; i < arr.length; i++) {
    1.91 +            assertEquals(arr[i].getClassLoader(),
    1.92 +                JsonKnockoutTest.class.getClassLoader(),
    1.93 +                "All classes loaded by the same classloader"
    1.94 +            );
    1.95 +        }
    1.96 +        
    1.97 +        URI uri = JsonDynamicHTTP.initServer();
    1.98 +    
    1.99 +        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(JsonKnockoutTest.class).
   1.100 +            loadPage(uri.toString()).
   1.101 +            invoke("initialized");
   1.102 +        
   1.103 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
   1.104 +            @Override
   1.105 +            public void run() {
   1.106 +                bb.showAndWait();
   1.107 +            }
   1.108 +        });
   1.109 +        
   1.110 +        ClassLoader l = getClassLoader();
   1.111 +        List<Object> res = new ArrayList<Object>();
   1.112 +        for (int i = 0; i < arr.length; i++) {
   1.113 +            Class<?> c = Class.forName(arr[i].getName(), true, l);
   1.114 +            Class<? extends Annotation> koTest = 
   1.115 +                c.getClassLoader().loadClass(KOTest.class.getName()).
   1.116 +                asSubclass(Annotation.class);
   1.117 +            for (Method m : c.getMethods()) {
   1.118 +                if (m.getAnnotation(koTest) != null) {
   1.119 +                    res.add(new JsonFX(browserContext, m));
   1.120 +                }
   1.121 +            }
   1.122 +        }
   1.123 +        return res.toArray();
   1.124 +    }
   1.125 +
   1.126 +    static synchronized ClassLoader getClassLoader() throws InterruptedException {
   1.127 +        while (browserClass == null) {
   1.128 +            JsonKnockoutTest.class.wait();
   1.129 +        }
   1.130 +        return browserClass.getClassLoader();
   1.131 +    }
   1.132 +    
   1.133 +    public static synchronized void initialized(Class<?> browserCls) throws Exception {
   1.134 +        browserClass = browserCls;
   1.135 +        browserContext = Fn.activePresenter();
   1.136 +        JsonKnockoutTest.class.notifyAll();
   1.137 +    }
   1.138 +    
   1.139 +    public static void initialized() throws Exception {
   1.140 +        Assert.assertSame(JsonKnockoutTest.class.getClassLoader(),
   1.141 +            ClassLoader.getSystemClassLoader(),
   1.142 +            "No special classloaders"
   1.143 +        );
   1.144 +        JsonKnockoutTest.initialized(JsonKnockoutTest.class);
   1.145 +    }
   1.146 +
   1.147 +    @Override
   1.148 +    public boolean canFailWebSocketTest() {
   1.149 +        return true;
   1.150 +    }
   1.151 +    
   1.152 +    @Override
   1.153 +    public BrwsrCtx createContext() {
   1.154 +        KO4J ko = new KO4J(browserContext);
   1.155 +        XmlHttpResourceContext tc = new XmlHttpResourceContext();
   1.156 +        Contexts.Builder cb = Contexts.newBuilder().
   1.157 +            register(Technology.class, ko.knockout(), 10).
   1.158 +            register(Executor.class, (Executor)browserContext, 10).
   1.159 +            register(Fn.Presenter.class, (Fn.Presenter)browserContext, 10);
   1.160 +        tc.fillContext(cb, browserClass);
   1.161 +        return cb.build();
   1.162 +    }
   1.163 +
   1.164 +    @Override
   1.165 +    public Object createJSON(Map<String, Object> values) {
   1.166 +        Object json = createJSON();
   1.167 +        for (Map.Entry<String, Object> entry : values.entrySet()) {
   1.168 +            setProperty(json, entry.getKey(), entry.getValue());
   1.169 +        }
   1.170 +        return json;
   1.171 +    }
   1.172 +
   1.173 +    @JavaScriptBody(args = {}, body = "return new Object();")
   1.174 +    private static native Object createJSON();
   1.175 +
   1.176 +    @JavaScriptBody(args = {"json", "key", "value"}, body = "json[key] = value;")
   1.177 +    private static native void setProperty(Object json, String key, Object value);
   1.178 +
   1.179 +    @Override
   1.180 +    @JavaScriptBody(args = { "s", "args" }, body = ""
   1.181 +        + "var f = new Function(s); "
   1.182 +        + "return f.apply(null, args);"
   1.183 +    )
   1.184 +    public native Object executeScript(String script, Object[] arguments);
   1.185 +
   1.186 +    @JavaScriptBody(args = {  }, body = 
   1.187 +          "var h;"
   1.188 +        + "if (!!window && !!window.location && !!window.location.href)\n"
   1.189 +        + "  h = window.location.href;\n"
   1.190 +        + "else "
   1.191 +        + "  h = null;"
   1.192 +        + "return h;\n"
   1.193 +    )
   1.194 +    private static native String findBaseURL();
   1.195 +    
   1.196 +    @Override
   1.197 +    public URI prepareURL(String content, String mimeType, String[] parameters) {
   1.198 +        try {
   1.199 +            final URL baseURL = new URL(findBaseURL());
   1.200 +            StringBuilder sb = new StringBuilder();
   1.201 +            sb.append("/dynamic?mimeType=").append(mimeType);
   1.202 +            for (int i = 0; i < parameters.length; i++) {
   1.203 +                sb.append("&param" + i).append("=").append(parameters[i]);
   1.204 +            }
   1.205 +            String mangle = content.replace("\n", "%0a")
   1.206 +                .replace("\"", "\\\"").replace(" ", "%20");
   1.207 +            sb.append("&content=").append(mangle);
   1.208 +
   1.209 +            URL query = new URL(baseURL, sb.toString());
   1.210 +            URLConnection c = query.openConnection();
   1.211 +            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   1.212 +            URI connectTo = new URI(br.readLine());
   1.213 +            return connectTo;
   1.214 +        } catch (IOException ex) {
   1.215 +            throw new IllegalStateException(ex);
   1.216 +        } catch (URISyntaxException ex) {
   1.217 +            throw new IllegalStateException(ex);
   1.218 +        }
   1.219 +    }
   1.220 +}