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