ko-ws-tyrus/src/test/java/org/netbeans/html/wstyrus/TyrusKnockoutTest.java
branchnetbeans
changeset 362 92fb71afdc0e
parent 358 80702021b851
child 365 5c93ad8c7a15
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ko-ws-tyrus/src/test/java/org/netbeans/html/wstyrus/TyrusKnockoutTest.java	Mon Dec 16 16:59:43 2013 +0100
     1.3 @@ -0,0 +1,212 @@
     1.4 +/**
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 1997-2010 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-2013 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.wstyrus;
    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.Executors;
    1.61 +import net.java.html.BrwsrCtx;
    1.62 +import net.java.html.boot.BrowserBuilder;
    1.63 +import net.java.html.js.JavaScriptBody;
    1.64 +import org.netbeans.html.boot.impl.FnContext;
    1.65 +import org.apidesign.html.boot.spi.Fn;
    1.66 +import org.apidesign.html.context.spi.Contexts;
    1.67 +import org.apidesign.html.json.spi.Technology;
    1.68 +import org.apidesign.html.json.spi.Transfer;
    1.69 +import org.apidesign.html.json.spi.WSTransfer;
    1.70 +import org.apidesign.html.json.tck.KOTest;
    1.71 +import org.apidesign.html.json.tck.KnockoutTCK;
    1.72 +import org.json.JSONException;
    1.73 +import org.json.JSONObject;
    1.74 +import org.netbeans.html.kofx.FXContext;
    1.75 +import org.openide.util.lookup.ServiceProvider;
    1.76 +import org.testng.annotations.Factory;
    1.77 +import static org.testng.Assert.*;
    1.78 +
    1.79 +/**
    1.80 + *
    1.81 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.82 + */
    1.83 +@ServiceProvider(service = KnockoutTCK.class)
    1.84 +public final class TyrusKnockoutTest extends KnockoutTCK {
    1.85 +    private static Class<?> browserClass;
    1.86 +    private static Fn.Presenter browserContext;
    1.87 +    
    1.88 +    public TyrusKnockoutTest() {
    1.89 +    }
    1.90 +    
    1.91 +    @Factory public static Object[] compatibilityTests() throws Exception {
    1.92 +        Class[] arr = testClasses();
    1.93 +        for (int i = 0; i < arr.length; i++) {
    1.94 +            assertEquals(
    1.95 +                arr[i].getClassLoader(),
    1.96 +                TyrusKnockoutTest.class.getClassLoader(),
    1.97 +                "All classes loaded by the same classloader"
    1.98 +            );
    1.99 +        }
   1.100 +        
   1.101 +        URI uri = TyrusDynamicHTTP.initServer();
   1.102 +    
   1.103 +        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
   1.104 +            loadPage(uri.toString()).
   1.105 +            invoke("initialized");
   1.106 +        
   1.107 +        Executors.newSingleThreadExecutor().submit(new Runnable() {
   1.108 +            @Override
   1.109 +            public void run() {
   1.110 +                bb.showAndWait();
   1.111 +            }
   1.112 +        });
   1.113 +        
   1.114 +        ClassLoader l = getClassLoader();
   1.115 +        List<Object> res = new ArrayList<Object>();
   1.116 +        for (int i = 0; i < arr.length; i++) {
   1.117 +            Class<?> c = Class.forName(arr[i].getName(), true, l);
   1.118 +            Class<? extends Annotation> koTest = 
   1.119 +                c.getClassLoader().loadClass(KOTest.class.getName()).
   1.120 +                asSubclass(Annotation.class);
   1.121 +            for (Method m : c.getMethods()) {
   1.122 +                if (m.getAnnotation(koTest) != null) {
   1.123 +                    res.add(new TyrusFX(browserContext, m));
   1.124 +                }
   1.125 +            }
   1.126 +        }
   1.127 +        return res.toArray();
   1.128 +    }
   1.129 +
   1.130 +    static synchronized ClassLoader getClassLoader() throws InterruptedException {
   1.131 +        while (browserClass == null) {
   1.132 +            TyrusKnockoutTest.class.wait();
   1.133 +        }
   1.134 +        return browserClass.getClassLoader();
   1.135 +    }
   1.136 +    
   1.137 +    public static synchronized void initialized(Class<?> browserCls) throws Exception {
   1.138 +        browserClass = browserCls;
   1.139 +        browserContext = FnContext.currentPresenter();
   1.140 +        TyrusKnockoutTest.class.notifyAll();
   1.141 +    }
   1.142 +    
   1.143 +    public static void initialized() throws Exception {
   1.144 +        Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
   1.145 +        Method m = classpathClass.getMethod("initialized", Class.class);
   1.146 +        m.invoke(null, TyrusKnockoutTest.class);
   1.147 +        browserContext = FnContext.currentPresenter();
   1.148 +    }
   1.149 +    
   1.150 +    @Override
   1.151 +    public BrwsrCtx createContext() {
   1.152 +        FXContext fx = new FXContext(browserContext);
   1.153 +        TyrusContext tc = new TyrusContext();
   1.154 +        Contexts.Builder cb = Contexts.newBuilder().
   1.155 +            register(Technology.class, fx, 10).
   1.156 +            register(Transfer.class, fx, 10).
   1.157 +            register(WSTransfer.class, tc, 10);
   1.158 +        return cb.build();
   1.159 +    }
   1.160 +
   1.161 +    @Override
   1.162 +    public Object createJSON(Map<String, Object> values) {
   1.163 +        JSONObject json = new JSONObject();
   1.164 +        for (Map.Entry<String, Object> entry : values.entrySet()) {
   1.165 +            try {
   1.166 +                json.put(entry.getKey(), entry.getValue());
   1.167 +            } catch (JSONException ex) {
   1.168 +                throw new IllegalStateException(ex);
   1.169 +            }
   1.170 +        }
   1.171 +        return json;
   1.172 +    }
   1.173 +
   1.174 +    @Override
   1.175 +    @JavaScriptBody(args = { "s", "args" }, body = ""
   1.176 +        + "var f = new Function(s); "
   1.177 +        + "return f.apply(null, args);"
   1.178 +    )
   1.179 +    public native Object executeScript(String script, Object[] arguments);
   1.180 +
   1.181 +    @JavaScriptBody(args = {  }, body = 
   1.182 +          "var h;"
   1.183 +        + "if (!!window && !!window.location && !!window.location.href)\n"
   1.184 +        + "  h = window.location.href;\n"
   1.185 +        + "else "
   1.186 +        + "  h = null;"
   1.187 +        + "return h;\n"
   1.188 +    )
   1.189 +    private static native String findBaseURL();
   1.190 +    
   1.191 +    @Override
   1.192 +    public URI prepareURL(String content, String mimeType, String[] parameters) {
   1.193 +        try {
   1.194 +            final URL baseURL = new URL(findBaseURL());
   1.195 +            StringBuilder sb = new StringBuilder();
   1.196 +            sb.append("/dynamic?mimeType=").append(mimeType);
   1.197 +            for (int i = 0; i < parameters.length; i++) {
   1.198 +                sb.append("&param" + i).append("=").append(parameters[i]);
   1.199 +            }
   1.200 +            String mangle = content.replace("\n", "%0a")
   1.201 +                .replace("\"", "\\\"").replace(" ", "%20");
   1.202 +            sb.append("&content=").append(mangle);
   1.203 +
   1.204 +            URL query = new URL(baseURL, sb.toString());
   1.205 +            URLConnection c = query.openConnection();
   1.206 +            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   1.207 +            URI connectTo = new URI(br.readLine());
   1.208 +            return connectTo;
   1.209 +        } catch (IOException ex) {
   1.210 +            throw new IllegalStateException(ex);
   1.211 +        } catch (URISyntaxException ex) {
   1.212 +            throw new IllegalStateException(ex);
   1.213 +        }
   1.214 +    }
   1.215 +}