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