boot-truffle/src/test/java/net/java/html/boot/truffle/TruffleJavaScriptTest.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 21 Jul 2016 11:21:58 +0200
changeset 1105 907bcc5dbb00
parent 1094 47cd2110ed8d
permissions -rw-r--r--
Documenting the Truffle based presenter
jtulach@1094
     1
/**
jtulach@1094
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jtulach@1094
     3
 *
jtulach@1094
     4
 * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
jtulach@1094
     5
 *
jtulach@1094
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jtulach@1094
     7
 * Other names may be trademarks of their respective owners.
jtulach@1094
     8
 *
jtulach@1094
     9
 * The contents of this file are subject to the terms of either the GNU
jtulach@1094
    10
 * General Public License Version 2 only ("GPL") or the Common
jtulach@1094
    11
 * Development and Distribution License("CDDL") (collectively, the
jtulach@1094
    12
 * "License"). You may not use this file except in compliance with the
jtulach@1094
    13
 * License. You can obtain a copy of the License at
jtulach@1094
    14
 * http://www.netbeans.org/cddl-gplv2.html
jtulach@1094
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jtulach@1094
    16
 * specific language governing permissions and limitations under the
jtulach@1094
    17
 * License.  When distributing the software, include this License Header
jtulach@1094
    18
 * Notice in each file and include the License file at
jtulach@1094
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jtulach@1094
    20
 * particular file as subject to the "Classpath" exception as provided
jtulach@1094
    21
 * by Oracle in the GPL Version 2 section of the License file that
jtulach@1094
    22
 * accompanied this code. If applicable, add the following below the
jtulach@1094
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jtulach@1094
    24
 * your own identifying information:
jtulach@1094
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jtulach@1094
    26
 *
jtulach@1094
    27
 * Contributor(s):
jtulach@1094
    28
 *
jtulach@1094
    29
 * The Original Software is NetBeans. The Initial Developer of the Original
jtulach@1094
    30
 * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
jtulach@1094
    31
 *
jtulach@1094
    32
 * If you wish your version of this file to be governed by only the CDDL
jtulach@1094
    33
 * or only the GPL Version 2, indicate your decision by adding
jtulach@1094
    34
 * "[Contributor] elects to include this software in this distribution
jtulach@1094
    35
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jtulach@1094
    36
 * single choice of license, a recipient has the option to distribute
jtulach@1094
    37
 * your version of this file under either the CDDL, the GPL Version 2 or
jtulach@1094
    38
 * to extend the choice of license to its licensees as provided above.
jtulach@1094
    39
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jtulach@1094
    40
 * Version 2 license, then the option applies only if the new code is
jtulach@1094
    41
 * made subject to such option by the copyright holder.
jtulach@1094
    42
 */
jtulach@1094
    43
package net.java.html.boot.truffle;
jtulach@1094
    44
jtulach@1094
    45
import com.oracle.truffle.api.source.Source;
jtulach@1094
    46
import com.oracle.truffle.api.vm.PolyglotEngine;
jtulach@1094
    47
import java.lang.annotation.Annotation;
jtulach@1094
    48
import java.lang.reflect.Method;
jtulach@1094
    49
import java.util.ArrayList;
jtulach@1094
    50
import java.util.List;
jtulach@1094
    51
import java.util.concurrent.Executors;
jtulach@1094
    52
import net.java.html.boot.BrowserBuilder;
jtulach@1094
    53
import org.netbeans.html.boot.spi.Fn;
jtulach@1094
    54
import org.netbeans.html.json.tck.JavaScriptTCK;
jtulach@1094
    55
import org.netbeans.html.json.tck.KOTest;
jtulach@1094
    56
import org.testng.Assert;
jtulach@1094
    57
import static org.testng.Assert.assertEquals;
jtulach@1094
    58
import org.testng.SkipException;
jtulach@1094
    59
import org.testng.annotations.Factory;
jtulach@1094
    60
import org.testng.annotations.Test;
jtulach@1094
    61
jtulach@1094
    62
/**
jtulach@1094
    63
 *
jtulach@1094
    64
 * @author Jaroslav Tulach
jtulach@1094
    65
 */
jtulach@1094
    66
public class TruffleJavaScriptTest {
jtulach@1094
    67
    private static Class<?> browserClass;
jtulach@1094
    68
    private static Fn.Presenter browserPresenter;
jtulach@1094
    69
    
jtulach@1094
    70
    public TruffleJavaScriptTest() {
jtulach@1094
    71
    }
jtulach@1094
    72
jtulach@1094
    73
    @Factory public static Object[] compatibilityTests() throws Exception {
jtulach@1094
    74
        PolyglotEngine engine = PolyglotEngine.newBuilder().build();
jtulach@1094
    75
        PolyglotEngine.Value result = null;
jtulach@1094
    76
        try {
jtulach@1094
    77
            result = engine.eval(Source.fromText("6 * 7", "test.js").withMimeType("text/javascript"));
jtulach@1094
    78
        } catch (Exception notSupported) {
jtulach@1094
    79
            if (notSupported.getMessage().contains("text/javascript")) {
jtulach@1094
    80
                return new Object[] { new SkipTest(notSupported.getMessage()) };
jtulach@1094
    81
            }
jtulach@1094
    82
        }
jtulach@1094
    83
        assertEquals(42, result.as(Number.class).intValue(), "Executed OK");
jtulach@1094
    84
jtulach@1105
    85
        final BrowserBuilder bb = BrowserBuilder.newBrowser(TrufflePresenters.create(SingleCase.JS)).
jtulach@1094
    86
            loadClass(TruffleJavaScriptTest.class).
jtulach@1094
    87
            loadPage("empty.html").
jtulach@1094
    88
            invoke("initialized");
jtulach@1094
    89
jtulach@1094
    90
        Executors.newSingleThreadExecutor().submit(new Runnable() {
jtulach@1094
    91
            @Override
jtulach@1094
    92
            public void run() {
jtulach@1094
    93
                bb.showAndWait();
jtulach@1094
    94
            }
jtulach@1094
    95
        });
jtulach@1094
    96
jtulach@1094
    97
        List<Object> res = new ArrayList<>();
jtulach@1094
    98
        Class<? extends Annotation> test = 
jtulach@1094
    99
            loadClass().getClassLoader().loadClass(KOTest.class.getName()).
jtulach@1094
   100
            asSubclass(Annotation.class);
jtulach@1094
   101
jtulach@1094
   102
        Class[] arr = (Class[]) loadClass().getDeclaredMethod("tests").invoke(null);
jtulach@1094
   103
        for (Class c : arr) {
jtulach@1094
   104
            if (c.getSimpleName().contains("GC")) {
jtulach@1094
   105
                continue;
jtulach@1094
   106
            }
jtulach@1094
   107
            for (Method m : c.getMethods()) {
jtulach@1094
   108
                if (m.getAnnotation(test) != null) {
jtulach@1094
   109
                    res.add(new SingleCase(browserPresenter, m));
jtulach@1094
   110
                }
jtulach@1094
   111
            }
jtulach@1094
   112
        }
jtulach@1094
   113
        return res.toArray();
jtulach@1094
   114
    }
jtulach@1094
   115
jtulach@1094
   116
    static synchronized Class<?> loadClass() throws InterruptedException {
jtulach@1094
   117
        while (browserClass == null) {
jtulach@1094
   118
            TruffleJavaScriptTest.class.wait();
jtulach@1094
   119
        }
jtulach@1094
   120
        return browserClass;
jtulach@1094
   121
    }
jtulach@1094
   122
    
jtulach@1094
   123
    public static synchronized void ready(Class<?> browserCls) throws Exception {
jtulach@1094
   124
        browserClass = browserCls;
jtulach@1094
   125
        browserPresenter = Fn.activePresenter();
jtulach@1094
   126
        TruffleJavaScriptTest.class.notifyAll();
jtulach@1094
   127
    }
jtulach@1094
   128
    
jtulach@1094
   129
    public static void initialized() throws Exception {
jtulach@1094
   130
        Assert.assertSame(TruffleJavaScriptTest.class.getClassLoader(),
jtulach@1094
   131
            ClassLoader.getSystemClassLoader(),
jtulach@1094
   132
            "No special classloaders"
jtulach@1094
   133
        );
jtulach@1094
   134
        TruffleJavaScriptTest.ready(Tck.class);
jtulach@1094
   135
    }
jtulach@1094
   136
jtulach@1094
   137
    public static final class Tck extends JavaScriptTCK {
jtulach@1094
   138
jtulach@1094
   139
        public static Class[] tests() {
jtulach@1094
   140
            return testClasses();
jtulach@1094
   141
        }
jtulach@1094
   142
    }
jtulach@1094
   143
jtulach@1094
   144
    public static class SkipTest {
jtulach@1094
   145
        private final String message;
jtulach@1094
   146
jtulach@1094
   147
        public SkipTest(String message) {
jtulach@1094
   148
            this.message = message;
jtulach@1094
   149
        }
jtulach@1094
   150
jtulach@1094
   151
        @Test
jtulach@1094
   152
        public void needsGraalVMToExecuteTheTests() {
jtulach@1094
   153
            throw new SkipException(message);
jtulach@1094
   154
        }
jtulach@1094
   155
    }
jtulach@1094
   156
}