ko-ws-tyrus/src/test/java/org/apidesign/html/wstyrus/TyrusKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 12 Sep 2013 09:33:50 +0200
changeset 288 8c5b40231d26
parent 260 23e2ad7e6d23
child 300 aee69a14a859
child 309 7025177bd67e
permissions -rw-r--r--
Support for multiple FX WebViews running at once in isolation
     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.boot.impl.FnUtils;
    40 import org.apidesign.html.boot.spi.Fn;
    41 import org.apidesign.html.context.spi.Contexts;
    42 import org.apidesign.html.json.spi.Technology;
    43 import org.apidesign.html.json.spi.Transfer;
    44 import org.apidesign.html.json.spi.WSTransfer;
    45 import org.apidesign.html.json.tck.KOTest;
    46 import org.apidesign.html.json.tck.KnockoutTCK;
    47 import org.apidesign.html.kofx.FXContext;
    48 import org.json.JSONException;
    49 import org.json.JSONObject;
    50 import org.openide.util.lookup.ServiceProvider;
    51 import org.testng.annotations.Factory;
    52 import static org.testng.Assert.*;
    53 
    54 /**
    55  *
    56  * @author Jaroslav Tulach <jtulach@netbeans.org>
    57  */
    58 @ServiceProvider(service = KnockoutTCK.class)
    59 public final class TyrusKnockoutTest extends KnockoutTCK {
    60     private static Class<?> browserClass;
    61     private static Fn.Presenter browserContext;
    62     
    63     public TyrusKnockoutTest() {
    64     }
    65     
    66     @Factory public static Object[] compatibilityTests() throws Exception {
    67         Class[] arr = testClasses();
    68         for (int i = 0; i < arr.length; i++) {
    69             assertEquals(
    70                 arr[i].getClassLoader(),
    71                 TyrusKnockoutTest.class.getClassLoader(),
    72                 "All classes loaded by the same classloader"
    73             );
    74         }
    75         
    76         URI uri = TyrusDynamicHTTP.initServer();
    77     
    78         final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
    79             loadPage(uri.toString()).
    80             invoke("initialized");
    81         
    82         Executors.newSingleThreadExecutor().submit(new Runnable() {
    83             @Override
    84             public void run() {
    85                 bb.showAndWait();
    86             }
    87         });
    88         
    89         ClassLoader l = getClassLoader();
    90         List<Object> res = new ArrayList<Object>();
    91         for (int i = 0; i < arr.length; i++) {
    92             Class<?> c = Class.forName(arr[i].getName(), true, l);
    93             Class<? extends Annotation> koTest = 
    94                 c.getClassLoader().loadClass(KOTest.class.getName()).
    95                 asSubclass(Annotation.class);
    96             for (Method m : c.getMethods()) {
    97                 if (m.getAnnotation(koTest) != null) {
    98                     res.add(new TyrusFX(browserContext, m));
    99                 }
   100             }
   101         }
   102         return res.toArray();
   103     }
   104 
   105     static synchronized ClassLoader getClassLoader() throws InterruptedException {
   106         while (browserClass == null) {
   107             TyrusKnockoutTest.class.wait();
   108         }
   109         return browserClass.getClassLoader();
   110     }
   111     
   112     public static synchronized void initialized(Class<?> browserCls) throws Exception {
   113         browserClass = browserCls;
   114         browserContext = FnUtils.currentPresenter();
   115         TyrusKnockoutTest.class.notifyAll();
   116     }
   117     
   118     public static void initialized() throws Exception {
   119         Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
   120         Method m = classpathClass.getMethod("initialized", Class.class);
   121         m.invoke(null, TyrusKnockoutTest.class);
   122         browserContext = FnUtils.currentPresenter();
   123     }
   124     
   125     @Override
   126     public BrwsrCtx createContext() {
   127         FXContext fx = new FXContext(browserContext);
   128         TyrusContext tc = new TyrusContext();
   129         Contexts.Builder cb = Contexts.newBuilder().
   130             register(Technology.class, fx, 10).
   131             register(Transfer.class, fx, 10).
   132             register(WSTransfer.class, tc, 10);
   133         return cb.build();
   134     }
   135 
   136     @Override
   137     public Object createJSON(Map<String, Object> values) {
   138         JSONObject json = new JSONObject();
   139         for (Map.Entry<String, Object> entry : values.entrySet()) {
   140             try {
   141                 json.put(entry.getKey(), entry.getValue());
   142             } catch (JSONException ex) {
   143                 throw new IllegalStateException(ex);
   144             }
   145         }
   146         return json;
   147     }
   148 
   149     @Override
   150     @JavaScriptBody(args = { "s", "args" }, body = ""
   151         + "var f = new Function(s); "
   152         + "return f.apply(null, args);"
   153     )
   154     public native Object executeScript(String script, Object[] arguments);
   155 
   156     @JavaScriptBody(args = {  }, body = 
   157           "var h;"
   158         + "if (!!window && !!window.location && !!window.location.href)\n"
   159         + "  h = window.location.href;\n"
   160         + "else "
   161         + "  h = null;"
   162         + "return h;\n"
   163     )
   164     private static native String findBaseURL();
   165     
   166     @Override
   167     public URI prepareURL(String content, String mimeType, String[] parameters) {
   168         try {
   169             final URL baseURL = new URL(findBaseURL());
   170             StringBuilder sb = new StringBuilder();
   171             sb.append("/dynamic?mimeType=").append(mimeType);
   172             for (int i = 0; i < parameters.length; i++) {
   173                 sb.append("&param" + i).append("=").append(parameters[i]);
   174             }
   175             String mangle = content.replace("\n", "%0a")
   176                 .replace("\"", "\\\"").replace(" ", "%20");
   177             sb.append("&content=").append(mangle);
   178 
   179             URL query = new URL(baseURL, sb.toString());
   180             URLConnection c = query.openConnection();
   181             BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
   182             URI connectTo = new URI(br.readLine());
   183             return connectTo;
   184         } catch (IOException ex) {
   185             throw new IllegalStateException(ex);
   186         } catch (URISyntaxException ex) {
   187             throw new IllegalStateException(ex);
   188         }
   189     }
   190 }