ko-ws-tyrus/src/test/java/org/netbeans/html/wstyrus/TyrusKnockoutTest.java
author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
Thu, 09 Jan 2014 15:43:04 +0100
branchUniversalKO
changeset 444 03eeeb863fa1
parent 365 5c93ad8c7a15
child 446 6dce58c06f58
permissions -rw-r--r--
Updating references to ko4j
jaroslav@260
     1
/**
jaroslav@358
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jaroslav@260
     3
 *
jaroslav@365
     4
 * Copyright 2013-2013 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@358
    30
 * Software is Oracle. Portions Copyright 2013-2013 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@260
    57
import java.util.concurrent.Executors;
jaroslav@260
    58
import net.java.html.BrwsrCtx;
jaroslav@260
    59
import net.java.html.boot.BrowserBuilder;
jaroslav@260
    60
import net.java.html.js.JavaScriptBody;
jaroslav@288
    61
import org.apidesign.html.boot.spi.Fn;
jaroslav@260
    62
import org.apidesign.html.context.spi.Contexts;
jaroslav@260
    63
import org.apidesign.html.json.spi.Technology;
jaroslav@260
    64
import org.apidesign.html.json.spi.Transfer;
jaroslav@260
    65
import org.apidesign.html.json.spi.WSTransfer;
jaroslav@260
    66
import org.apidesign.html.json.tck.KOTest;
jaroslav@260
    67
import org.apidesign.html.json.tck.KnockoutTCK;
jaroslav@260
    68
import org.json.JSONException;
jaroslav@260
    69
import org.json.JSONObject;
jaroslav@444
    70
import org.netbeans.html.boot.impl.FnContext;
jaroslav@444
    71
import org.netbeans.html.ko4j.KO4J;
jaroslav@260
    72
import org.openide.util.lookup.ServiceProvider;
jaroslav@444
    73
import static org.testng.Assert.*;
jaroslav@260
    74
import org.testng.annotations.Factory;
jaroslav@260
    75
jaroslav@260
    76
/**
jaroslav@260
    77
 *
jaroslav@260
    78
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@260
    79
 */
jaroslav@260
    80
@ServiceProvider(service = KnockoutTCK.class)
jaroslav@260
    81
public final class TyrusKnockoutTest extends KnockoutTCK {
jaroslav@260
    82
    private static Class<?> browserClass;
jaroslav@288
    83
    private static Fn.Presenter browserContext;
jaroslav@260
    84
    
jaroslav@260
    85
    public TyrusKnockoutTest() {
jaroslav@260
    86
    }
jaroslav@260
    87
    
jaroslav@260
    88
    @Factory public static Object[] compatibilityTests() throws Exception {
jaroslav@260
    89
        Class[] arr = testClasses();
jaroslav@260
    90
        for (int i = 0; i < arr.length; i++) {
jaroslav@260
    91
            assertEquals(
jaroslav@260
    92
                arr[i].getClassLoader(),
jaroslav@260
    93
                TyrusKnockoutTest.class.getClassLoader(),
jaroslav@260
    94
                "All classes loaded by the same classloader"
jaroslav@260
    95
            );
jaroslav@260
    96
        }
jaroslav@260
    97
        
jaroslav@260
    98
        URI uri = TyrusDynamicHTTP.initServer();
jaroslav@260
    99
    
jaroslav@260
   100
        final BrowserBuilder bb = BrowserBuilder.newBrowser().loadClass(TyrusKnockoutTest.class).
jaroslav@260
   101
            loadPage(uri.toString()).
jaroslav@260
   102
            invoke("initialized");
jaroslav@260
   103
        
jaroslav@260
   104
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jaroslav@260
   105
            @Override
jaroslav@260
   106
            public void run() {
jaroslav@260
   107
                bb.showAndWait();
jaroslav@260
   108
            }
jaroslav@260
   109
        });
jaroslav@260
   110
        
jaroslav@260
   111
        ClassLoader l = getClassLoader();
jaroslav@260
   112
        List<Object> res = new ArrayList<Object>();
jaroslav@260
   113
        for (int i = 0; i < arr.length; i++) {
jaroslav@260
   114
            Class<?> c = Class.forName(arr[i].getName(), true, l);
jaroslav@260
   115
            Class<? extends Annotation> koTest = 
jaroslav@260
   116
                c.getClassLoader().loadClass(KOTest.class.getName()).
jaroslav@260
   117
                asSubclass(Annotation.class);
jaroslav@260
   118
            for (Method m : c.getMethods()) {
jaroslav@260
   119
                if (m.getAnnotation(koTest) != null) {
jaroslav@288
   120
                    res.add(new TyrusFX(browserContext, m));
jaroslav@260
   121
                }
jaroslav@260
   122
            }
jaroslav@260
   123
        }
jaroslav@260
   124
        return res.toArray();
jaroslav@260
   125
    }
jaroslav@260
   126
jaroslav@260
   127
    static synchronized ClassLoader getClassLoader() throws InterruptedException {
jaroslav@260
   128
        while (browserClass == null) {
jaroslav@260
   129
            TyrusKnockoutTest.class.wait();
jaroslav@260
   130
        }
jaroslav@260
   131
        return browserClass.getClassLoader();
jaroslav@260
   132
    }
jaroslav@260
   133
    
jaroslav@260
   134
    public static synchronized void initialized(Class<?> browserCls) throws Exception {
jaroslav@260
   135
        browserClass = browserCls;
jaroslav@309
   136
        browserContext = FnContext.currentPresenter();
jaroslav@260
   137
        TyrusKnockoutTest.class.notifyAll();
jaroslav@260
   138
    }
jaroslav@260
   139
    
jaroslav@260
   140
    public static void initialized() throws Exception {
jaroslav@260
   141
        Class<?> classpathClass = ClassLoader.getSystemClassLoader().loadClass(TyrusKnockoutTest.class.getName());
jaroslav@260
   142
        Method m = classpathClass.getMethod("initialized", Class.class);
jaroslav@260
   143
        m.invoke(null, TyrusKnockoutTest.class);
jaroslav@309
   144
        browserContext = FnContext.currentPresenter();
jaroslav@260
   145
    }
jaroslav@260
   146
    
jaroslav@260
   147
    @Override
jaroslav@260
   148
    public BrwsrCtx createContext() {
jaroslav@444
   149
        KO4J ko = new KO4J(browserContext);
jaroslav@260
   150
        TyrusContext tc = new TyrusContext();
jaroslav@260
   151
        Contexts.Builder cb = Contexts.newBuilder().
jaroslav@444
   152
            register(Technology.class, ko.knockout(), 10).
jaroslav@444
   153
            register(Transfer.class, ko.transferViaOrgJSON(), 10).
jaroslav@260
   154
            register(WSTransfer.class, tc, 10);
jaroslav@260
   155
        return cb.build();
jaroslav@260
   156
    }
jaroslav@260
   157
jaroslav@260
   158
    @Override
jaroslav@260
   159
    public Object createJSON(Map<String, Object> values) {
jaroslav@260
   160
        JSONObject json = new JSONObject();
jaroslav@260
   161
        for (Map.Entry<String, Object> entry : values.entrySet()) {
jaroslav@260
   162
            try {
jaroslav@260
   163
                json.put(entry.getKey(), entry.getValue());
jaroslav@260
   164
            } catch (JSONException ex) {
jaroslav@260
   165
                throw new IllegalStateException(ex);
jaroslav@260
   166
            }
jaroslav@260
   167
        }
jaroslav@260
   168
        return json;
jaroslav@260
   169
    }
jaroslav@260
   170
jaroslav@260
   171
    @Override
jaroslav@260
   172
    @JavaScriptBody(args = { "s", "args" }, body = ""
jaroslav@260
   173
        + "var f = new Function(s); "
jaroslav@260
   174
        + "return f.apply(null, args);"
jaroslav@260
   175
    )
jaroslav@260
   176
    public native Object executeScript(String script, Object[] arguments);
jaroslav@260
   177
jaroslav@260
   178
    @JavaScriptBody(args = {  }, body = 
jaroslav@260
   179
          "var h;"
jaroslav@260
   180
        + "if (!!window && !!window.location && !!window.location.href)\n"
jaroslav@260
   181
        + "  h = window.location.href;\n"
jaroslav@260
   182
        + "else "
jaroslav@260
   183
        + "  h = null;"
jaroslav@260
   184
        + "return h;\n"
jaroslav@260
   185
    )
jaroslav@260
   186
    private static native String findBaseURL();
jaroslav@260
   187
    
jaroslav@260
   188
    @Override
jaroslav@260
   189
    public URI prepareURL(String content, String mimeType, String[] parameters) {
jaroslav@260
   190
        try {
jaroslav@260
   191
            final URL baseURL = new URL(findBaseURL());
jaroslav@260
   192
            StringBuilder sb = new StringBuilder();
jaroslav@260
   193
            sb.append("/dynamic?mimeType=").append(mimeType);
jaroslav@260
   194
            for (int i = 0; i < parameters.length; i++) {
jaroslav@260
   195
                sb.append("&param" + i).append("=").append(parameters[i]);
jaroslav@260
   196
            }
jaroslav@260
   197
            String mangle = content.replace("\n", "%0a")
jaroslav@260
   198
                .replace("\"", "\\\"").replace(" ", "%20");
jaroslav@260
   199
            sb.append("&content=").append(mangle);
jaroslav@260
   200
jaroslav@260
   201
            URL query = new URL(baseURL, sb.toString());
jaroslav@260
   202
            URLConnection c = query.openConnection();
jaroslav@260
   203
            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
jaroslav@260
   204
            URI connectTo = new URI(br.readLine());
jaroslav@260
   205
            return connectTo;
jaroslav@260
   206
        } catch (IOException ex) {
jaroslav@260
   207
            throw new IllegalStateException(ex);
jaroslav@260
   208
        } catch (URISyntaxException ex) {
jaroslav@260
   209
            throw new IllegalStateException(ex);
jaroslav@260
   210
        }
jaroslav@260
   211
    }
jaroslav@260
   212
}