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