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