ko-ws-tyrus/src/test/java/org/apidesign/html/wstyrus/TyrusKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Aug 2013 14:40:16 +0200
changeset 260 23e2ad7e6d23
child 288 8c5b40231d26
permissions -rw-r--r--
Tyrus based implementation of WebSockets for JDK7
     1 /**
     2  * HTML via Java(tm) Language Bindings
     3  * Copyright (C) 2013 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details. apidesign.org
    13  * designates this particular file as subject to the
    14  * "Classpath" exception as provided by apidesign.org
    15  * in the License file that accompanied this code.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program. Look for COPYING file in the top folder.
    19  * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
    20  */
    21 package org.apidesign.html.wstyrus;
    22 
    23 import java.io.BufferedReader;
    24 import java.io.IOException;
    25 import java.io.InputStreamReader;
    26 import java.lang.annotation.Annotation;
    27 import java.lang.reflect.Method;
    28 import java.net.URI;
    29 import java.net.URISyntaxException;
    30 import java.net.URL;
    31 import java.net.URLConnection;
    32 import java.util.ArrayList;
    33 import java.util.List;
    34 import java.util.Map;
    35 import java.util.concurrent.Executors;
    36 import net.java.html.BrwsrCtx;
    37 import net.java.html.boot.BrowserBuilder;
    38 import net.java.html.js.JavaScriptBody;
    39 import org.apidesign.html.context.spi.Contexts;
    40 import org.apidesign.html.json.spi.Technology;
    41 import org.apidesign.html.json.spi.Transfer;
    42 import org.apidesign.html.json.spi.WSTransfer;
    43 import org.apidesign.html.json.tck.KOTest;
    44 import org.apidesign.html.json.tck.KnockoutTCK;
    45 import org.apidesign.html.kofx.FXContext;
    46 import org.json.JSONException;
    47 import org.json.JSONObject;
    48 import org.openide.util.lookup.ServiceProvider;
    49 import org.testng.annotations.Factory;
    50 import static org.testng.Assert.*;
    51 
    52 /**
    53  *
    54  * @author Jaroslav Tulach <jtulach@netbeans.org>
    55  */
    56 @ServiceProvider(service = KnockoutTCK.class)
    57 public final class TyrusKnockoutTest extends KnockoutTCK {
    58     private static Class<?> browserClass;
    59     
    60     public TyrusKnockoutTest() {
    61     }
    62     
    63     @Factory public static Object[] compatibilityTests() throws Exception {
    64         Class[] arr = testClasses();
    65         for (int i = 0; i < arr.length; i++) {
    66             assertEquals(
    67                 arr[i].getClassLoader(),
    68                 TyrusKnockoutTest.class.getClassLoader(),
    69                 "All classes loaded by the same classloader"
    70             );
    71         }
    72         
    73         URI uri = TyrusDynamicHTTP.initServer();
    74     
    75         final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
    76             loadPage(uri.toString()).
    77             invoke("initialized");
    78         
    79         Executors.newSingleThreadExecutor().submit(new Runnable() {
    80             @Override
    81             public void run() {
    82                 bb.showAndWait();
    83             }
    84         });
    85         
    86         ClassLoader l = getClassLoader();
    87         List<Object> res = new ArrayList<Object>();
    88         for (int i = 0; i < arr.length; i++) {
    89             Class<?> c = Class.forName(arr[i].getName(), true, l);
    90             Class<? extends Annotation> koTest = 
    91                 c.getClassLoader().loadClass(KOTest.class.getName()).
    92                 asSubclass(Annotation.class);
    93             for (Method m : c.getMethods()) {
    94                 if (m.getAnnotation(koTest) != null) {
    95                     res.add(new TyrusFX(m));
    96                 }
    97             }
    98         }
    99         return res.toArray();
   100     }
   101 
   102     static synchronized ClassLoader getClassLoader() throws InterruptedException {
   103         while (browserClass == null) {
   104             TyrusKnockoutTest.class.wait();
   105         }
   106         return browserClass.getClassLoader();
   107     }
   108     
   109     public static synchronized void initialized(Class<?> browserCls) throws Exception {
   110         browserClass = browserCls;
   111         TyrusKnockoutTest.class.notifyAll();
   112     }
   113     
   114     public static void initialized() throws Exception {
   115         Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
   116         Method m = classpathClass.getMethod("initialized", Class.class);
   117         m.invoke(null, TyrusKnockoutTest.class);
   118     }
   119     
   120     @Override
   121     public BrwsrCtx createContext() {
   122         FXContext fx = new FXContext();
   123         TyrusContext tc = new TyrusContext();
   124         Contexts.Builder cb = Contexts.newBuilder().
   125             register(Technology.class, fx, 10).
   126             register(Transfer.class, fx, 10).
   127             register(WSTransfer.class, tc, 10);
   128         return cb.build();
   129     }
   130 
   131     @Override
   132     public Object createJSON(Map<String, Object> values) {
   133         JSONObject json = new JSONObject();
   134         for (Map.Entry<String, Object> entry : values.entrySet()) {
   135             try {
   136                 json.put(entry.getKey(), entry.getValue());
   137             } catch (JSONException ex) {
   138                 throw new IllegalStateException(ex);
   139             }
   140         }
   141         return json;
   142     }
   143 
   144     @Override
   145     @JavaScriptBody(args = { "s", "args" }, body = ""
   146         + "var f = new Function(s); "
   147         + "return f.apply(null, args);"
   148     )
   149     public native Object executeScript(String script, Object[] arguments);
   150 
   151     @JavaScriptBody(args = {  }, body = 
   152           "var h;"
   153         + "if (!!window && !!window.location && !!window.location.href)\n"
   154         + "  h = window.location.href;\n"
   155         + "else "
   156         + "  h = null;"
   157         + "return h;\n"
   158     )
   159     private static native String findBaseURL();
   160     
   161     @Override
   162     public URI prepareURL(String content, String mimeType, String[] parameters) {
   163         try {
   164             final URL baseURL = new URL(findBaseURL());
   165             StringBuilder sb = new StringBuilder();
   166             sb.append("/dynamic?mimeType=").append(mimeType);
   167             for (int i = 0; i < parameters.length; i++) {
   168                 sb.append("&param" + i).append("=").append(parameters[i]);
   169             }
   170             String mangle = content.replace("\n", "%0a")
   171                 .replace("\"", "\\\"").replace(" ", "%20");
   172             sb.append("&content=").append(mangle);
   173 
   174             URL query = new URL(baseURL, sb.toString());
   175             URLConnection c = query.openConnection();
   176             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   177             URI connectTo = new URI(br.readLine());
   178             return connectTo;
   179         } catch (IOException ex) {
   180             throw new IllegalStateException(ex);
   181         } catch (URISyntaxException ex) {
   182             throw new IllegalStateException(ex);
   183         }
   184     }
   185 }