ko-bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 24 Jun 2013 14:19:09 +0200
branchclassloader
changeset 1223 aa8587804f92
parent 1220 cf9ba1d883c5
permissions -rw-r--r--
Using HTTP protocol rather than reflection tricks as HTTP will work on all known implementations
     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.ko2brwsr;
    22 
    23 import java.io.BufferedReader;
    24 import java.io.IOException;
    25 import java.io.InputStreamReader;
    26 import java.net.URI;
    27 import java.net.URISyntaxException;
    28 import java.net.URL;
    29 import java.net.URLConnection;
    30 import java.util.Map;
    31 import net.java.html.BrwsrCtx;
    32 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    33 import org.apidesign.bck2brwsr.vmtest.VMTest;
    34 import org.apidesign.html.context.spi.Contexts;
    35 import org.apidesign.html.json.spi.Technology;
    36 import org.apidesign.html.json.spi.Transfer;
    37 import org.apidesign.html.json.tck.KOTest;
    38 import org.apidesign.html.json.tck.KnockoutTCK;
    39 import org.openide.util.lookup.ServiceProvider;
    40 import org.testng.annotations.Factory;
    41 
    42 /**
    43  *
    44  * @author Jaroslav Tulach <jtulach@netbeans.org>
    45  */
    46 @ServiceProvider(service = KnockoutTCK.class)
    47 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
    48     @Factory public static Object[] create() {
    49         return VMTest.newTests().
    50             withClasses(testClasses()).
    51             withLaunchers("bck2brwsr").
    52             withTestAnnotation(KOTest.class).
    53             build();
    54     }
    55     
    56     @Override
    57     public BrwsrCtx createContext() {
    58         return Contexts.newBuilder().
    59             register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
    60             register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
    61     }
    62 
    63 
    64     
    65     @Override
    66     public Object createJSON(Map<String, Object> values) {
    67         Object json = createJSON();
    68         
    69         for (Map.Entry<String, Object> entry : values.entrySet()) {
    70             putValue(json, entry.getKey(), entry.getValue());
    71         }
    72         return json;
    73     }
    74 
    75     @JavaScriptBody(args = {}, body = "return new Object();")
    76     private static native Object createJSON();
    77 
    78     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    79     private static native void putValue(Object json, String key, Object value);
    80 
    81     @Override
    82     public Object executeScript(String script, Object[] arguments) {
    83         return execScript(script, arguments);
    84     }
    85     
    86     @JavaScriptBody(args = { "s", "args" }, body = 
    87         "var f = new Function(s); return f.apply(null, args);"
    88     )
    89     private static native Object execScript(String s, Object[] arguments);
    90     
    91     @JavaScriptBody(args = {  }, body = 
    92           "var h;"
    93         + "if (!!window && !!window.location && !!window.location.href)\n"
    94         + "  h = window.location.href;\n"
    95         + "else "
    96         + "  h = null;"
    97         + "return h;\n"
    98     )
    99     private static native String findBaseURL();
   100     
   101     @Override
   102     public URI prepareURL(String content, String mimeType, String[] parameters) {
   103         try {
   104             final URL baseURL = new URL(findBaseURL());
   105             StringBuilder sb = new StringBuilder();
   106             sb.append("/dynamic?mimeType=").append(mimeType);
   107             for (int i = 0; i < parameters.length; i++) {
   108                 sb.append("&param" + i).append("=").append(parameters[i]);
   109             }
   110             String mangle = content.replace("\n", "%0a")
   111                 .replace("\"", "\\\"").replace(" ", "%20");
   112             sb.append("&content=").append(mangle);
   113 
   114             URL query = new URL(baseURL, sb.toString());
   115             String uri = (String) query.getContent(new Class[] { String.class });
   116             URI connectTo = new URI(uri.trim());
   117             return connectTo;
   118         } catch (IOException ex) {
   119             throw new IllegalStateException(ex);
   120         } catch (URISyntaxException ex) {
   121             throw new IllegalStateException(ex);
   122         }
   123     }    
   124 }