xhr4j/src/test/java/org/netbeans/html/xhr4j/JsonKnockoutTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 21 Jul 2016 11:21:58 +0200
changeset 1105 907bcc5dbb00
parent 838 bdc3d696dd4a
permissions -rw-r--r--
Documenting the Truffle based presenter
jaroslav@260
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@260
     3
 *
jaroslav@551
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jaroslav@260
     5
 *
jaroslav@358
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jaroslav@358
     7
 * Other names may be trademarks of their respective owners.
jaroslav@260
     8
 *
jaroslav@358
     9
 * The contents of this file are subject to the terms of either the GNU
jaroslav@358
    10
 * General Public License Version 2 only ("GPL") or the Common
jaroslav@358
    11
 * Development and Distribution License("CDDL") (collectively, the
jaroslav@358
    12
 * "License"). You may not use this file except in compliance with the
jaroslav@358
    13
 * License. You can obtain a copy of the License at
jaroslav@358
    14
 * http://www.netbeans.org/cddl-gplv2.html
jaroslav@358
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jaroslav@358
    16
 * specific language governing permissions and limitations under the
jaroslav@358
    17
 * License.  When distributing the software, include this License Header
jaroslav@358
    18
 * Notice in each file and include the License file at
jaroslav@358
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jaroslav@358
    20
 * particular file as subject to the "Classpath" exception as provided
jaroslav@358
    21
 * by Oracle in the GPL Version 2 section of the License file that
jaroslav@358
    22
 * accompanied this code. If applicable, add the following below the
jaroslav@358
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jaroslav@358
    24
 * your own identifying information:
jaroslav@358
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jaroslav@358
    26
 *
jaroslav@358
    27
 * Contributor(s):
jaroslav@358
    28
 *
jaroslav@358
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jaroslav@551
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jaroslav@358
    31
 *
jaroslav@358
    32
 * If you wish your version of this file to be governed by only the CDDL
jaroslav@358
    33
 * or only the GPL Version 2, indicate your decision by adding
jaroslav@358
    34
 * "[Contributor] elects to include this software in this distribution
jaroslav@358
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jaroslav@358
    36
 * single choice of license, a recipient has the option to distribute
jaroslav@358
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jaroslav@358
    38
 * to extend the choice of license to its licensees as provided above.
jaroslav@358
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jaroslav@358
    40
 * Version 2 license, then the option applies only if the new code is
jaroslav@358
    41
 * made subject to such option by the copyright holder.
jaroslav@260
    42
 */
jtulach@1057
    43
package org.netbeans.html.xhr4j;
jaroslav@260
    44
jaroslav@260
    45
import java.io.BufferedReader;
jaroslav@260
    46
import java.io.IOException;
jaroslav@260
    47
import java.io.InputStreamReader;
jaroslav@260
    48
import java.lang.annotation.Annotation;
jaroslav@260
    49
import java.lang.reflect.Method;
jaroslav@260
    50
import java.net.URI;
jaroslav@260
    51
import java.net.URISyntaxException;
jaroslav@260
    52
import java.net.URL;
jaroslav@260
    53
import java.net.URLConnection;
jaroslav@260
    54
import java.util.ArrayList;
jaroslav@260
    55
import java.util.List;
jaroslav@260
    56
import java.util.Map;
jaroslav@574
    57
import java.util.concurrent.Executor;
jaroslav@260
    58
import java.util.concurrent.Executors;
jaroslav@260
    59
import net.java.html.BrwsrCtx;
jaroslav@260
    60
import net.java.html.boot.BrowserBuilder;
jaroslav@260
    61
import net.java.html.js.JavaScriptBody;
jtulach@838
    62
import org.netbeans.html.boot.spi.Fn;
jtulach@838
    63
import org.netbeans.html.context.spi.Contexts;
jtulach@838
    64
import org.netbeans.html.json.spi.Technology;
jtulach@838
    65
import org.netbeans.html.json.tck.KOTest;
jtulach@838
    66
import org.netbeans.html.json.tck.KnockoutTCK;
jaroslav@444
    67
import org.netbeans.html.ko4j.KO4J;
jaroslav@260
    68
import org.openide.util.lookup.ServiceProvider;
jaroslav@488
    69
import org.testng.Assert;
jaroslav@444
    70
import static org.testng.Assert.*;
jaroslav@260
    71
import org.testng.annotations.Factory;
jaroslav@260
    72
jaroslav@260
    73
/**
jaroslav@260
    74
 *
jtulach@790
    75
 * @author Jaroslav Tulach
jaroslav@260
    76
 */
jaroslav@260
    77
@ServiceProvider(service = KnockoutTCK.class)
jtulach@1057
    78
public final class JsonKnockoutTest extends KnockoutTCK {
jaroslav@260
    79
    private static Class<?> browserClass;
jaroslav@288
    80
    private static Fn.Presenter browserContext;
jaroslav@260
    81
    
jtulach@1057
    82
    public JsonKnockoutTest() {
jaroslav@260
    83
    }
jaroslav@260
    84
    
jaroslav@260
    85
    @Factory public static Object[] compatibilityTests() throws Exception {
jaroslav@260
    86
        Class[] arr = testClasses();
jaroslav@260
    87
        for (int i = 0; i < arr.length; i++) {
jtulach@1057
    88
            assertEquals(arr[i].getClassLoader(),
jtulach@1057
    89
                JsonKnockoutTest.class.getClassLoader(),
jaroslav@260
    90
                "All classes loaded by the same classloader"
jaroslav@260
    91
            );
jaroslav@260
    92
        }
jaroslav@260
    93
        
jtulach@1057
    94
        URI uri = JsonDynamicHTTP.initServer();
jaroslav@260
    95
    
jtulach@1057
    96
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(JsonKnockoutTest.class).
jaroslav@260
    97
            loadPage(uri.toString()).
jaroslav@260
    98
            invoke("initialized");
jaroslav@260
    99
        
jaroslav@260
   100
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@260
   101
            @Override
jaroslav@260
   102
            public void run() {
jaroslav@260
   103
                bb.showAndWait();
jaroslav@260
   104
            }
jaroslav@260
   105
        });
jaroslav@260
   106
        
jaroslav@260
   107
        ClassLoader l = getClassLoader();
jaroslav@260
   108
        List<Object> res = new ArrayList<Object>();
jaroslav@260
   109
        for (int i = 0; i < arr.length; i++) {
jaroslav@260
   110
            Class<?> c = Class.forName(arr[i].getName(), true, l);
jaroslav@260
   111
            Class<? extends Annotation> koTest = 
jaroslav@260
   112
                c.getClassLoader().loadClass(KOTest.class.getName()).
jaroslav@260
   113
                asSubclass(Annotation.class);
jaroslav@260
   114
            for (Method m : c.getMethods()) {
jaroslav@260
   115
                if (m.getAnnotation(koTest) != null) {
jtulach@1057
   116
                    res.add(new JsonFX(browserContext, m));
jaroslav@260
   117
                }
jaroslav@260
   118
            }
jaroslav@260
   119
        }
jaroslav@260
   120
        return res.toArray();
jaroslav@260
   121
    }
jaroslav@260
   122
jaroslav@260
   123
    static synchronized ClassLoader getClassLoader() throws InterruptedException {
jaroslav@260
   124
        while (browserClass == null) {
jtulach@1057
   125
            JsonKnockoutTest.class.wait();
jaroslav@260
   126
        }
jaroslav@260
   127
        return browserClass.getClassLoader();
jaroslav@260
   128
    }
jaroslav@260
   129
    
jaroslav@260
   130
    public static synchronized void initialized(Class<?> browserCls) throws Exception {
jaroslav@260
   131
        browserClass = browserCls;
jaroslav@339
   132
        browserContext = Fn.activePresenter();
jtulach@1057
   133
        JsonKnockoutTest.class.notifyAll();
jaroslav@260
   134
    }
jaroslav@260
   135
    
jaroslav@260
   136
    public static void initialized() throws Exception {
jtulach@1057
   137
        Assert.assertSame(JsonKnockoutTest.class.getClassLoader(),
jaroslav@488
   138
            ClassLoader.getSystemClassLoader(),
jaroslav@488
   139
            "No special classloaders"
jaroslav@488
   140
        );
jtulach@1057
   141
        JsonKnockoutTest.initialized(JsonKnockoutTest.class);
jtulach@1057
   142
    }
jtulach@1057
   143
jtulach@1057
   144
    @Override
jtulach@1057
   145
    public boolean canFailWebSocketTest() {
jtulach@1057
   146
        return true;
jaroslav@260
   147
    }
jaroslav@260
   148
    
jaroslav@260
   149
    @Override
jaroslav@260
   150
    public BrwsrCtx createContext() {
jaroslav@444
   151
        KO4J ko = new KO4J(browserContext);
jtulach@1057
   152
        XmlHttpResourceContext tc = new XmlHttpResourceContext();
jaroslav@260
   153
        Contexts.Builder cb = Contexts.newBuilder().
jaroslav@444
   154
            register(Technology.class, ko.knockout(), 10).
jaroslav@574
   155
            register(Executor.class, (Executor)browserContext, 10).
jaroslav@574
   156
            register(Fn.Presenter.class, (Fn.Presenter)browserContext, 10);
jtulach@1057
   157
        tc.fillContext(cb, browserClass);
jaroslav@260
   158
        return cb.build();
jaroslav@260
   159
    }
jaroslav@260
   160
jaroslav@260
   161
    @Override
jaroslav@260
   162
    public Object createJSON(Map<String, Object> values) {
jtulach@1057
   163
        Object json = createJSON();
jaroslav@260
   164
        for (Map.Entry<String, Object> entry : values.entrySet()) {
jtulach@1057
   165
            setProperty(json, entry.getKey(), entry.getValue());
jaroslav@260
   166
        }
jaroslav@260
   167
        return json;
jaroslav@260
   168
    }
jaroslav@260
   169
jtulach@1057
   170
    @JavaScriptBody(args = {}, body = "return new Object();")
jtulach@1057
   171
    private static native Object createJSON();
jtulach@1057
   172
jtulach@1057
   173
    @JavaScriptBody(args = {"json", "key", "value"}, body = "json[key] = value;")
jtulach@1057
   174
    private static native void setProperty(Object json, String key, Object value);
jtulach@1057
   175
jaroslav@260
   176
    @Override
jaroslav@260
   177
    @JavaScriptBody(args = { "s", "args" }, body = ""
jaroslav@260
   178
        + "var f = new Function(s); "
jaroslav@260
   179
        + "return f.apply(null, args);"
jaroslav@260
   180
    )
jaroslav@260
   181
    public native Object executeScript(String script, Object[] arguments);
jaroslav@260
   182
jaroslav@260
   183
    @JavaScriptBody(args = {  }, body = 
jaroslav@260
   184
          "var h;"
jaroslav@260
   185
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@260
   186
        + "  h = window.location.href;\n"
jaroslav@260
   187
        + "else "
jaroslav@260
   188
        + "  h = null;"
jaroslav@260
   189
        + "return h;\n"
jaroslav@260
   190
    )
jaroslav@260
   191
    private static native String findBaseURL();
jaroslav@260
   192
    
jaroslav@260
   193
    @Override
jaroslav@260
   194
    public URI prepareURL(String content, String mimeType, String[] parameters) {
jaroslav@260
   195
        try {
jaroslav@260
   196
            final URL baseURL = new URL(findBaseURL());
jaroslav@260
   197
            StringBuilder sb = new StringBuilder();
jaroslav@260
   198
            sb.append("/dynamic?mimeType=").append(mimeType);
jaroslav@260
   199
            for (int i = 0; i < parameters.length; i++) {
jaroslav@260
   200
                sb.append("&param" + i).append("=").append(parameters[i]);
jaroslav@260
   201
            }
jaroslav@260
   202
            String mangle = content.replace("\n", "%0a")
jaroslav@260
   203
                .replace("\"", "\\\"").replace(" ", "%20");
jaroslav@260
   204
            sb.append("&content=").append(mangle);
jaroslav@260
   205
jaroslav@260
   206
            URL query = new URL(baseURL, sb.toString());
jaroslav@260
   207
            URLConnection c = query.openConnection();
jaroslav@260
   208
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
jaroslav@260
   209
            URI connectTo = new URI(br.readLine());
jaroslav@260
   210
            return connectTo;
jaroslav@260
   211
        } catch (IOException ex) {
jaroslav@260
   212
            throw new IllegalStateException(ex);
jaroslav@260
   213
        } catch (URISyntaxException ex) {
jaroslav@260
   214
            throw new IllegalStateException(ex);
jaroslav@260
   215
        }
jaroslav@260
   216
    }
jaroslav@260
   217
}