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