ko/bck2brwsr/src/test/java/org/apidesign/html/ko2brwsr/Bck2BrwsrKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 26 Jun 2013 18:44:21 +0200
branchclassloader
changeset 1227 5a907f38608d
parent 1225 73c0973e8e0a
permissions -rw-r--r--
Successfully compiles the new support for knockout
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 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.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.html.ko2brwsr;
    19 
    20 import java.io.BufferedReader;
    21 import java.io.IOException;
    22 import java.io.InputStreamReader;
    23 import java.net.URI;
    24 import java.net.URISyntaxException;
    25 import java.net.URL;
    26 import java.net.URLConnection;
    27 import java.util.Map;
    28 import net.java.html.BrwsrCtx;
    29 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    30 import org.apidesign.bck2brwsr.vmtest.VMTest;
    31 import org.apidesign.html.context.spi.Contexts;
    32 import org.apidesign.html.json.spi.Technology;
    33 import org.apidesign.html.json.spi.Transfer;
    34 import org.apidesign.html.json.tck.KOTest;
    35 import org.apidesign.html.json.tck.KnockoutTCK;
    36 import org.openide.util.lookup.ServiceProvider;
    37 import org.testng.annotations.Factory;
    38 
    39 /**
    40  *
    41  * @author Jaroslav Tulach <jtulach@netbeans.org>
    42  */
    43 @ServiceProvider(service = KnockoutTCK.class)
    44 public final class Bck2BrwsrKnockoutTest extends KnockoutTCK {
    45     @Factory public static Object[] create() {
    46         return VMTest.newTests().
    47             withClasses(testClasses()).
    48             withLaunchers("bck2brwsr").
    49             withTestAnnotation(KOTest.class).
    50             build();
    51     }
    52     
    53     @Override
    54     public BrwsrCtx createContext() {
    55         return Contexts.newBuilder().
    56             register(Transfer.class, BrwsrCtxImpl.DEFAULT, 9).
    57             register(Technology.class, BrwsrCtxImpl.DEFAULT, 9).build();
    58     }
    59 
    60 
    61     
    62     @Override
    63     public Object createJSON(Map<String, Object> values) {
    64         Object json = createJSON();
    65         
    66         for (Map.Entry<String, Object> entry : values.entrySet()) {
    67             putValue(json, entry.getKey(), entry.getValue());
    68         }
    69         return json;
    70     }
    71 
    72     @JavaScriptBody(args = {}, body = "return new Object();")
    73     private static native Object createJSON();
    74 
    75     @JavaScriptBody(args = { "json", "key", "value" }, body = "json[key] = value;")
    76     private static native void putValue(Object json, String key, Object value);
    77 
    78     @Override
    79     public Object executeScript(String script, Object[] arguments) {
    80         return execScript(script, arguments);
    81     }
    82     
    83     @JavaScriptBody(args = { "s", "args" }, body = 
    84         "var f = new Function(s); return f.apply(null, args);"
    85     )
    86     private static native Object execScript(String s, Object[] arguments);
    87     
    88     @JavaScriptBody(args = {  }, body = 
    89           "var h;"
    90         + "if (!!window && !!window.location && !!window.location.href)\n"
    91         + "  h = window.location.href;\n"
    92         + "else "
    93         + "  h = null;"
    94         + "return h;\n"
    95     )
    96     private static native String findBaseURL();
    97     
    98     @Override
    99     public URI prepareURL(String content, String mimeType, String[] parameters) {
   100         try {
   101             final URL baseURL = new URL(findBaseURL());
   102             StringBuilder sb = new StringBuilder();
   103             sb.append("/dynamic?mimeType=").append(mimeType);
   104             for (int i = 0; i < parameters.length; i++) {
   105                 sb.append("&param" + i).append("=").append(parameters[i]);
   106             }
   107             String mangle = content.replace("\n", "%0a")
   108                 .replace("\"", "\\\"").replace(" ", "%20");
   109             sb.append("&content=").append(mangle);
   110 
   111             URL query = new URL(baseURL, sb.toString());
   112             String uri = (String) query.getContent(new Class[] { String.class });
   113             URI connectTo = new URI(uri.trim());
   114             return connectTo;
   115         } catch (IOException ex) {
   116             throw new IllegalStateException(ex);
   117         } catch (URISyntaxException ex) {
   118             throw new IllegalStateException(ex);
   119         }
   120     }    
   121 }