ko-ws-tyrus/src/test/java/org/netbeans/html/wstyrus/TyrusKnockoutTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Tue, 26 Aug 2014 18:13:30 +0200
changeset 838 bdc3d696dd4a
parent 790 30f20d9c0986
permissions -rw-r--r--
During the API review process (bug 246133) the reviewers decided that in order to include html4j to NetBeans Platform, we need to stop using org.apidesign namespace and switch to NetBeans one. Repackaging all SPI packages into org.netbeans.html.smthng.spi.
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
 */
jaroslav@362
    43
package org.netbeans.html.wstyrus;
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.spi.Transfer;
jtulach@838
    66
import org.netbeans.html.json.spi.WSTransfer;
jtulach@838
    67
import org.netbeans.html.json.tck.KOTest;
jtulach@838
    68
import org.netbeans.html.json.tck.KnockoutTCK;
jaroslav@260
    69
import org.json.JSONException;
jaroslav@260
    70
import org.json.JSONObject;
jaroslav@444
    71
import org.netbeans.html.boot.impl.FnContext;
jaroslav@444
    72
import org.netbeans.html.ko4j.KO4J;
jaroslav@260
    73
import org.openide.util.lookup.ServiceProvider;
jaroslav@488
    74
import org.testng.Assert;
jaroslav@444
    75
import static org.testng.Assert.*;
jaroslav@260
    76
import org.testng.annotations.Factory;
jaroslav@260
    77
jaroslav@260
    78
/**
jaroslav@260
    79
 *
jtulach@790
    80
 * @author Jaroslav Tulach
jaroslav@260
    81
 */
jaroslav@260
    82
@ServiceProvider(service = KnockoutTCK.class)
jaroslav@260
    83
public final class TyrusKnockoutTest extends KnockoutTCK {
jaroslav@260
    84
    private static Class<?> browserClass;
jaroslav@288
    85
    private static Fn.Presenter browserContext;
jaroslav@260
    86
    
jaroslav@260
    87
    public TyrusKnockoutTest() {
jaroslav@260
    88
    }
jaroslav@260
    89
    
jaroslav@260
    90
    @Factory public static Object[] compatibilityTests() throws Exception {
jaroslav@260
    91
        Class[] arr = testClasses();
jaroslav@260
    92
        for (int i = 0; i < arr.length; i++) {
jaroslav@260
    93
            assertEquals(
jaroslav@260
    94
                arr[i].getClassLoader(),
jaroslav@260
    95
                TyrusKnockoutTest.class.getClassLoader(),
jaroslav@260
    96
                "All classes loaded by the same classloader"
jaroslav@260
    97
            );
jaroslav@260
    98
        }
jaroslav@260
    99
        
jaroslav@260
   100
        URI uri = TyrusDynamicHTTP.initServer();
jaroslav@260
   101
    
jaroslav@260
   102
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
jaroslav@260
   103
            loadPage(uri.toString()).
jaroslav@260
   104
            invoke("initialized");
jaroslav@260
   105
        
jaroslav@260
   106
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@260
   107
            @Override
jaroslav@260
   108
            public void run() {
jaroslav@260
   109
                bb.showAndWait();
jaroslav@260
   110
            }
jaroslav@260
   111
        });
jaroslav@260
   112
        
jaroslav@260
   113
        ClassLoader l = getClassLoader();
jaroslav@260
   114
        List<Object> res = new ArrayList<Object>();
jaroslav@260
   115
        for (int i = 0; i < arr.length; i++) {
jaroslav@260
   116
            Class<?> c = Class.forName(arr[i].getName(), true, l);
jaroslav@260
   117
            Class<? extends Annotation> koTest = 
jaroslav@260
   118
                c.getClassLoader().loadClass(KOTest.class.getName()).
jaroslav@260
   119
                asSubclass(Annotation.class);
jaroslav@260
   120
            for (Method m : c.getMethods()) {
jaroslav@260
   121
                if (m.getAnnotation(koTest) != null) {
jaroslav@288
   122
                    res.add(new TyrusFX(browserContext, m));
jaroslav@260
   123
                }
jaroslav@260
   124
            }
jaroslav@260
   125
        }
jaroslav@260
   126
        return res.toArray();
jaroslav@260
   127
    }
jaroslav@260
   128
jaroslav@260
   129
    static synchronized ClassLoader getClassLoader() throws InterruptedException {
jaroslav@260
   130
        while (browserClass == null) {
jaroslav@260
   131
            TyrusKnockoutTest.class.wait();
jaroslav@260
   132
        }
jaroslav@260
   133
        return browserClass.getClassLoader();
jaroslav@260
   134
    }
jaroslav@260
   135
    
jaroslav@260
   136
    public static synchronized void initialized(Class<?> browserCls) throws Exception {
jaroslav@260
   137
        browserClass = browserCls;
jaroslav@339
   138
        browserContext = Fn.activePresenter();
jaroslav@260
   139
        TyrusKnockoutTest.class.notifyAll();
jaroslav@260
   140
    }
jaroslav@260
   141
    
jaroslav@260
   142
    public static void initialized() throws Exception {
jaroslav@488
   143
        Assert.assertSame(
jaroslav@488
   144
            TyrusKnockoutTest.class.getClassLoader(),
jaroslav@488
   145
            ClassLoader.getSystemClassLoader(),
jaroslav@488
   146
            "No special classloaders"
jaroslav@488
   147
        );
jaroslav@488
   148
        TyrusKnockoutTest.initialized(TyrusKnockoutTest.class);
jaroslav@260
   149
    }
jaroslav@260
   150
    
jaroslav@260
   151
    @Override
jaroslav@260
   152
    public BrwsrCtx createContext() {
jaroslav@444
   153
        KO4J ko = new KO4J(browserContext);
jaroslav@260
   154
        TyrusContext tc = new TyrusContext();
jaroslav@260
   155
        Contexts.Builder cb = Contexts.newBuilder().
jaroslav@444
   156
            register(Technology.class, ko.knockout(), 10).
jaroslav@446
   157
            register(Transfer.class, tc, 10).
jaroslav@574
   158
            register(WSTransfer.class, tc, 10).
jaroslav@574
   159
            register(Executor.class, (Executor)browserContext, 10).
jaroslav@574
   160
            register(Fn.Presenter.class, (Fn.Presenter)browserContext, 10);
jaroslav@574
   161
                
jaroslav@260
   162
        return cb.build();
jaroslav@260
   163
    }
jaroslav@260
   164
jaroslav@260
   165
    @Override
jaroslav@260
   166
    public Object createJSON(Map<String, Object> values) {
jaroslav@260
   167
        JSONObject json = new JSONObject();
jaroslav@260
   168
        for (Map.Entry<String, Object> entry : values.entrySet()) {
jaroslav@260
   169
            try {
jaroslav@260
   170
                json.put(entry.getKey(), entry.getValue());
jaroslav@260
   171
            } catch (JSONException ex) {
jaroslav@260
   172
                throw new IllegalStateException(ex);
jaroslav@260
   173
            }
jaroslav@260
   174
        }
jaroslav@260
   175
        return json;
jaroslav@260
   176
    }
jaroslav@260
   177
jaroslav@260
   178
    @Override
jaroslav@260
   179
    @JavaScriptBody(args = { "s", "args" }, body = ""
jaroslav@260
   180
        + "var f = new Function(s); "
jaroslav@260
   181
        + "return f.apply(null, args);"
jaroslav@260
   182
    )
jaroslav@260
   183
    public native Object executeScript(String script, Object[] arguments);
jaroslav@260
   184
jaroslav@260
   185
    @JavaScriptBody(args = {  }, body = 
jaroslav@260
   186
          "var h;"
jaroslav@260
   187
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@260
   188
        + "  h = window.location.href;\n"
jaroslav@260
   189
        + "else "
jaroslav@260
   190
        + "  h = null;"
jaroslav@260
   191
        + "return h;\n"
jaroslav@260
   192
    )
jaroslav@260
   193
    private static native String findBaseURL();
jaroslav@260
   194
    
jaroslav@260
   195
    @Override
jaroslav@260
   196
    public URI prepareURL(String content, String mimeType, String[] parameters) {
jaroslav@260
   197
        try {
jaroslav@260
   198
            final URL baseURL = new URL(findBaseURL());
jaroslav@260
   199
            StringBuilder sb = new StringBuilder();
jaroslav@260
   200
            sb.append("/dynamic?mimeType=").append(mimeType);
jaroslav@260
   201
            for (int i = 0; i < parameters.length; i++) {
jaroslav@260
   202
                sb.append("&param" + i).append("=").append(parameters[i]);
jaroslav@260
   203
            }
jaroslav@260
   204
            String mangle = content.replace("\n", "%0a")
jaroslav@260
   205
                .replace("\"", "\\\"").replace(" ", "%20");
jaroslav@260
   206
            sb.append("&content=").append(mangle);
jaroslav@260
   207
jaroslav@260
   208
            URL query = new URL(baseURL, sb.toString());
jaroslav@260
   209
            URLConnection c = query.openConnection();
jaroslav@260
   210
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
jaroslav@260
   211
            URI connectTo = new URI(br.readLine());
jaroslav@260
   212
            return connectTo;
jaroslav@260
   213
        } catch (IOException ex) {
jaroslav@260
   214
            throw new IllegalStateException(ex);
jaroslav@260
   215
        } catch (URISyntaxException ex) {
jaroslav@260
   216
            throw new IllegalStateException(ex);
jaroslav@260
   217
        }
jaroslav@260
   218
    }
jaroslav@260
   219
}