http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/geo/src/test/java/org/netbeans/html/geo/impl/Compile.java ---------------------------------------------------------------------- diff --git a/geo/src/test/java/org/netbeans/html/geo/impl/Compile.java b/geo/src/test/java/org/netbeans/html/geo/impl/Compile.java new file mode 100644 index 0000000..69c0273 --- /dev/null +++ b/geo/src/test/java/org/netbeans/html/geo/impl/Compile.java @@ -0,0 +1,286 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.geo.impl; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; +import javax.tools.FileObject; +import javax.tools.ForwardingJavaFileManager; +import javax.tools.JavaFileManager; +import javax.tools.JavaFileObject; +import javax.tools.JavaFileObject.Kind; +import javax.tools.SimpleJavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.StandardLocation; +import javax.tools.ToolProvider; +import static org.testng.Assert.*; + +/** + * + * @author Jaroslav Tulach + */ +final class Compile implements DiagnosticListener<JavaFileObject> { + private final List<Diagnostic<? extends JavaFileObject>> errors = + new ArrayList<Diagnostic<? extends JavaFileObject>>(); + private final Map<String, byte[]> classes; + private final String pkg; + private final String cls; + private final String html; + private final String sourceLevel; + + private Compile(String html, String code, String sl) throws IOException { + this.pkg = findPkg(code); + this.cls = findCls(code); + this.html = html; + this.sourceLevel = sl; + classes = compile(html, code); + } + + /** Performs compilation of given HTML page and associated Java code + */ + public static Compile create(String html, String code) throws IOException { + return create(html, code, "1.7"); + } + static Compile create(String html, String code, String sourceLevel) throws IOException { + return new Compile(html, code, sourceLevel); + } + + /** Checks for given class among compiled resources */ + public byte[] get(String res) { + return classes.get(res); + } + + /** Obtains errors created during compilation. + */ + public List<Diagnostic<? extends JavaFileObject>> getErrors() { + List<Diagnostic<? extends JavaFileObject>> err; + err = new ArrayList<Diagnostic<? extends JavaFileObject>>(); + for (Diagnostic<? extends JavaFileObject> diagnostic : errors) { + if (diagnostic.getKind() == Diagnostic.Kind.ERROR) { + err.add(diagnostic); + } + } + return err; + } + + private Map<String, byte[]> compile(final String html, final String code) throws IOException { + StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null); + + final Map<String, ByteArrayOutputStream> class2BAOS; + class2BAOS = new HashMap<String, ByteArrayOutputStream>(); + + JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) { + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + return code; + } + }; + final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) { + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + return html; + } + + @Override + public InputStream openInputStream() throws IOException { + return new ByteArrayInputStream(html.getBytes()); + } + }; + + final URI scratch; + try { + scratch = new URI("mem://mem3"); + } catch (URISyntaxException ex) { + throw new IOException(ex); + } + + JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) { + @Override + public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException { + if (kind == Kind.CLASS) { + final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + + class2BAOS.put(className.replace('.', '/') + ".class", buffer); + return new SimpleJavaFileObject(sibling.toUri(), kind) { + @Override + public OutputStream openOutputStream() throws IOException { + return buffer; + } + }; + } + + if (kind == Kind.SOURCE) { + final String n = className.replace('.', '/') + ".java"; + final URI un; + try { + un = new URI("mem://" + n); + } catch (URISyntaxException ex) { + throw new IOException(ex); + } + return new VirtFO(un/*sibling.toUri()*/, kind, n); + } + + throw new IllegalStateException(); + } + + @Override + public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException { + if (location == StandardLocation.SOURCE_PATH) { + if (packageName.equals(pkg)) { + return htmlFile; + } + } + + return null; + } + + @Override + public boolean isSameFile(FileObject a, FileObject b) { + if (a instanceof VirtFO && b instanceof VirtFO) { + return ((VirtFO)a).getName().equals(((VirtFO)b).getName()); + } + + return super.isSameFile(a, b); + } + + class VirtFO extends SimpleJavaFileObject { + + private final String n; + + public VirtFO(URI uri, Kind kind, String n) { + super(uri, kind); + this.n = n; + } + private final ByteArrayOutputStream data = new ByteArrayOutputStream(); + + @Override + public OutputStream openOutputStream() throws IOException { + return data; + } + + @Override + public String getName() { + return n; + } + + @Override + public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException { + data.close(); + return new String(data.toByteArray()); + } + } + }; + + ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", sourceLevel, "-target", "1.7"), null, Arrays.asList(file)).call(); + + Map<String, byte[]> result = new HashMap<String, byte[]>(); + + for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) { + result.put(e.getKey(), e.getValue().toByteArray()); + } + + return result; + } + + + @Override + public void report(Diagnostic<? extends JavaFileObject> diagnostic) { + errors.add(diagnostic); + } + private static String findPkg(String java) throws IOException { + Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE); + Matcher m = p.matcher(java); + if (!m.find()) { + throw new IOException("Can't find package declaration in the java file"); + } + String pkg = m.group(1); + return pkg; + } + private static String findCls(String java) throws IOException { + Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE); + Matcher m = p.matcher(java); + if (!m.find()) { + throw new IOException("Can't find package declaration in the java file"); + } + String cls = m.group(1); + return cls; + } + + String getHtml() { + String fqn = "'" + pkg + '.' + cls + "'"; + return html.replace("'${fqn}'", fqn); + } + + void assertErrors() { + assertFalse(getErrors().isEmpty(), "There are supposed to be some errors"); + } + + void assertError(String expMsg) { + StringBuilder sb = new StringBuilder(); + sb.append("Can't find ").append(expMsg).append(" among:"); + for (Diagnostic<? extends JavaFileObject> e : errors) { + String msg = e.getMessage(Locale.US); + if (msg.contains(expMsg)) { + return; + } + sb.append("\n"); + sb.append(msg); + } + fail(sb.toString()); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/geo/src/test/java/org/netbeans/html/geo/impl/GeoProcessorTest.java ---------------------------------------------------------------------- diff --git a/geo/src/test/java/org/netbeans/html/geo/impl/GeoProcessorTest.java b/geo/src/test/java/org/netbeans/html/geo/impl/GeoProcessorTest.java new file mode 100644 index 0000000..8ef0846 --- /dev/null +++ b/geo/src/test/java/org/netbeans/html/geo/impl/GeoProcessorTest.java @@ -0,0 +1,114 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.geo.impl; + +import java.io.IOException; +import org.testng.annotations.Test; + +/** Test whether the annotation processor detects errors correctly. + * + * @author Jaroslav Tulach + */ +public class GeoProcessorTest { + + public GeoProcessorTest() { + } + + @Test public void onLocationMethodHasToTakePositionParameter() throws IOException { + Compile res = Compile.create("", "package x;\n" + + "class UseOnLocation {\n" + + " @net.java.html.geo.OnLocation\n" + + " public static void cantCallMe() {}\n" + + "}\n" + ); + res.assertErrors(); + res.assertError("first argument must be net.java.html.geo.Position"); + } + + @Test public void onLocationMethodCannotBePrivate() throws IOException { + Compile res = Compile.create("", "package x;\n" + + "class UseOnLocation {\n" + + " @net.java.html.geo.OnLocation\n" + + " private static void cantCallMe(net.java.html.geo.Position p) {}\n" + + "}\n" + ); + res.assertErrors(); + res.assertError("cannot be private"); + } + + @Test public void onErrorHasToExist() throws IOException { + Compile res = Compile.create("", "package x;\n" + + "class UseOnLocation {\n" + + " @net.java.html.geo.OnLocation(onError=\"doesNotExist\")\n" + + " static void cantCallMe(net.java.html.geo.Position p) {}\n" + + "}\n" + ); + res.assertErrors(); + res.assertError("not find doesNotExist"); + } + + @Test public void onErrorWouldHaveToBeStatic() throws IOException { + Compile res = Compile.create("", "package x;\n" + + "class UseOnLocation {\n" + + " @net.java.html.geo.OnLocation(onError=\"notStatic\")\n" + + " static void cantCallMe(net.java.html.geo.Position p) {}\n" + + " void notStatic(Exception e) {}\n" + + "}\n" + ); + res.assertErrors(); + res.assertError("have to be static"); + } + + @Test public void onErrorMustAcceptExceptionArgument() throws IOException { + Compile res = Compile.create("", "package x;\n" + + "class UseOnLocation {\n" + + " @net.java.html.geo.OnLocation(onError=\"notStatic\")\n" + + " static void cantCallMe(net.java.html.geo.Position p) {}\n" + + " static void notStatic(java.io.IOException e) {}\n" + + "}\n" + ); + res.assertErrors(); + res.assertError("Error method first argument needs to be Exception"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/geo/src/test/java/org/netbeans/html/geo/impl/JsGLProviderTest.java ---------------------------------------------------------------------- diff --git a/geo/src/test/java/org/netbeans/html/geo/impl/JsGLProviderTest.java b/geo/src/test/java/org/netbeans/html/geo/impl/JsGLProviderTest.java new file mode 100644 index 0000000..d2a73a4 --- /dev/null +++ b/geo/src/test/java/org/netbeans/html/geo/impl/JsGLProviderTest.java @@ -0,0 +1,76 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.geo.impl; + +import net.java.html.geo.Position; +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach <[email protected]> + */ +public class JsGLProviderTest extends Position.Handle { + public JsGLProviderTest() { + super(true); + } + + @Test public void checkWhetherWeCanInstantiate() { + assertNotNull(new JsGLProvider()); + } + + @Test public void canCallIsSupported() { + assertFalse(isSupported(), "Well, it is not, as we are not in a browser context"); + } + + @Override + protected void onLocation(Position p) throws Throwable { + throw new UnsupportedOperationException(); + } + + @Override + protected void onError(Exception ex) throws Throwable { + throw new UnsupportedOperationException(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/geo/src/test/java/org/netbeans/html/geo/spi/CoordImplTest.java ---------------------------------------------------------------------- diff --git a/geo/src/test/java/org/netbeans/html/geo/spi/CoordImplTest.java b/geo/src/test/java/org/netbeans/html/geo/spi/CoordImplTest.java new file mode 100644 index 0000000..01f1b31 --- /dev/null +++ b/geo/src/test/java/org/netbeans/html/geo/spi/CoordImplTest.java @@ -0,0 +1,106 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.geo.spi; + +import static org.testng.Assert.*; +import org.testng.annotations.Test; + +/** + * + * @author Jaroslav Tulach + */ +public class CoordImplTest extends GLProvider<Double, Object> { + + public CoordImplTest() { + } + @Test public void testGetLatitude() { + CoordImpl<Double> c = new CoordImpl<Double>(50.5, this); + assertEquals(c.getLatitude(), 50.5, 0.1, "Latitude returned as provided"); + } + + @Override + protected Object start(Query c) { + throw new UnsupportedOperationException(); + } + + @Override + protected void stop(Object watch) { + throw new UnsupportedOperationException(); + } + + @Override + protected double latitude(Double coords) { + return coords; + } + + @Override + protected double longitude(Double coords) { + throw new UnsupportedOperationException(); + } + + @Override + protected double accuracy(Double coords) { + throw new UnsupportedOperationException(); + } + + @Override + protected Double altitude(Double coords) { + throw new UnsupportedOperationException(); + } + + @Override + protected Double altitudeAccuracy(Double coords) { + throw new UnsupportedOperationException(); + } + + @Override + protected Double heading(Double coords) { + throw new UnsupportedOperationException(); + } + + @Override + protected Double speed(Double coords) { + throw new UnsupportedOperationException(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/html4j-maven-plugin/pom.xml ---------------------------------------------------------------------- diff --git a/html4j-maven-plugin/pom.xml b/html4j-maven-plugin/pom.xml new file mode 100644 index 0000000..56cf9b6 --- /dev/null +++ b/html4j-maven-plugin/pom.xml @@ -0,0 +1,119 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + + Copyright 2013-2016 Oracle and/or its affiliates. All rights reserved. + + Oracle and Java are registered trademarks of Oracle and/or its affiliates. + Other names may be trademarks of their respective owners. + + The contents of this file are subject to the terms of either the GNU + General Public License Version 2 only ("GPL") or the Common + Development and Distribution License("CDDL") (collectively, the + "License"). You may not use this file except in compliance with the + License. You can obtain a copy of the License at + http://www.netbeans.org/cddl-gplv2.html + or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + specific language governing permissions and limitations under the + License. When distributing the software, include this License Header + Notice in each file and include the License file at + nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + particular file as subject to the "Classpath" exception as provided + by Oracle in the GPL Version 2 section of the License file that + accompanied this code. If applicable, add the following below the + License Header, with the fields enclosed by brackets [] replaced by + your own identifying information: + "Portions Copyrighted [year] [name of copyright owner]" + + Contributor(s): + + The Original Software is NetBeans. The Initial Developer of the Original + Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + + If you wish your version of this file to be governed by only the CDDL + or only the GPL Version 2, indicate your decision by adding + "[Contributor] elects to include this software in this distribution + under the [CDDL or GPL Version 2] license." If you do not indicate a + single choice of license, a recipient has the option to distribute + your version of this file under either the CDDL, the GPL Version 2 or + to extend the choice of license to its licensees as provided above. + However, if you add GPL Version 2 code and therefore, elected the GPL + Version 2 license, then the option applies only if the new code is + made subject to such option by the copyright holder. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.netbeans.html</groupId> + <artifactId>pom</artifactId> + <version>2.0-SNAPSHOT</version> + </parent> + <packaging>maven-plugin</packaging> + <groupId>org.netbeans.html</groupId> + <artifactId>html4j-maven-plugin</artifactId> + <version>2.0-SNAPSHOT</version> + <name>Html for Java Maven Plugin</name> + <url>http://maven.apache.org</url> + <description>Maven plugin to post process the classes with @JavaScriptBody annotations</description> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + </properties> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-plugin-plugin</artifactId> + <version>3.1</version> + <configuration> + <extractors> + <extractor>java-annotations</extractor> + </extractors> + <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound> + </configuration> + <executions> + <execution> + <id>mojo-descriptor</id> + <phase>process-classes</phase> + <goals> + <goal>descriptor</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>3.0.4</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.apache.maven.plugin-tools</groupId> + <artifactId>maven-plugin-annotations</artifactId> + <version>3.0</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <version>3.0.2</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.ow2.asm</groupId> + <artifactId>asm</artifactId> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <scope>test</scope> + <type>jar</type> + </dependency> + </dependencies> +</project> http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java ---------------------------------------------------------------------- diff --git a/html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java b/html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java new file mode 100644 index 0000000..4755ee2 --- /dev/null +++ b/html4j-maven-plugin/src/main/java/org/netbeans/html/mojo/ProcessJsAnnotationsMojo.java @@ -0,0 +1,223 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package org.netbeans.html.mojo; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.objectweb.asm.ClassReader; + +@Mojo( + name="process-js-annotations", + requiresDependencyResolution = ResolutionScope.COMPILE, + defaultPhase= LifecyclePhase.PROCESS_CLASSES +) +public final class ProcessJsAnnotationsMojo extends AbstractMojo { + @Component + private MavenProject prj; + + @Parameter(defaultValue = "${project.build.directory}/classes") + private File classes; + + public ProcessJsAnnotationsMojo() { + } + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + LinkedList<URL> arr = new LinkedList<URL>(); + boolean foundAsm = false; + for (Artifact a : prj.getArtifacts()) { + final File f = a.getFile(); + if (f != null) { + if (a.getArtifactId().equals("asm")) { + foundAsm = true; + } + try { + arr.add(f.toURI().toURL()); + } catch (MalformedURLException ex) { + throw new IllegalStateException(ex); + } + } + } + if (!foundAsm) { + URL loc = ClassReader.class.getProtectionDomain().getCodeSource().getLocation(); + arr.addFirst(loc); + } + try { + arr.add(classes.toURI().toURL()); + URLClassLoader l = new URLClassLoader(arr.toArray(new URL[arr.size()])); + File master = new File(new File(classes, "META-INF"), "net.java.html.js.classes"); + processClasses(l, master, classes); + } catch (IOException ex) { + throw new MojoExecutionException("Problem converting JavaScriptXXX annotations", ex); + } + } + + private void processClasses(ClassLoader l, File master, File f) throws IOException, MojoExecutionException { + if (!f.exists()) { + return; + } + if (f.isDirectory()) { + boolean classes = new File(f, "net.java.html.js.classes").exists(); + File[] arr = f.listFiles(); + if (arr != null) { + for (File file : arr) { + if (classes || file.isDirectory()) { + processClasses(l, master, file); + } + } + } + return; + } + + if (!f.getName().endsWith(".class")) { + return; + } + + byte[] arr = new byte[(int)f.length()]; + FileInputStream is = new FileInputStream(f); + try { + readArr(arr, is); + } finally { + is.close(); + } + + byte[] newArr = null; + try { + Class<?> fnUtils = l.loadClass("org.netbeans.html.boot.impl.FnUtils"); + Method transform = fnUtils.getMethod("transform", byte[].class, ClassLoader.class); + + newArr = (byte[]) transform.invoke(null, arr, l); + if (newArr == null || newArr == arr) { + return; + } + filterClass(new File(f.getParentFile(), "net.java.html.js.classes"), f.getName()); + filterClass(master, f.getName()); + } catch (Exception ex) { + throw new MojoExecutionException("Can't process " + f, ex); + } + getLog().info("Processing " + f); + writeArr(f, newArr); + } + + private void writeArr(File f, byte[] newArr) throws IOException, FileNotFoundException { + FileOutputStream os = new FileOutputStream(f); + try { + os.write(newArr); + } finally { + os.close(); + } + } + + private static void readArr(byte[] arr, InputStream is) throws IOException { + int off = 0; + while (off< arr.length) { + int read = is.read(arr, off, arr.length - off); + if (read == -1) { + break; + } + off += read; + } + } + + private static void filterClass(File f, String className) throws IOException { + if (!f.exists()) { + return; + } + if (className.endsWith(".class")) { + className = className.substring(0, className.length() - 6); + } + + BufferedReader r = new BufferedReader(new FileReader(f)); + List<String> arr = new ArrayList<String>(); + boolean modified = false; + for (;;) { + String line = r.readLine(); + if (line == null) { + break; + } + if (line.endsWith(className)) { + modified = true; + continue; + } + arr.add(line); + } + r.close(); + + if (modified) { + if (arr.isEmpty()) { + f.delete(); + } else { + FileWriter w = new FileWriter(f); + for (String l : arr) { + w.write(l); + w.write("\n"); + } + w.close(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/pom.xml ---------------------------------------------------------------------- diff --git a/json-tck/pom.xml b/json-tck/pom.xml new file mode 100644 index 0000000..161b19d --- /dev/null +++ b/json-tck/pom.xml @@ -0,0 +1,116 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + + Copyright 2013-2016 Oracle and/or its affiliates. All rights reserved. + + Oracle and Java are registered trademarks of Oracle and/or its affiliates. + Other names may be trademarks of their respective owners. + + The contents of this file are subject to the terms of either the GNU + General Public License Version 2 only ("GPL") or the Common + Development and Distribution License("CDDL") (collectively, the + "License"). You may not use this file except in compliance with the + License. You can obtain a copy of the License at + http://www.netbeans.org/cddl-gplv2.html + or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + specific language governing permissions and limitations under the + License. When distributing the software, include this License Header + Notice in each file and include the License file at + nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + particular file as subject to the "Classpath" exception as provided + by Oracle in the GPL Version 2 section of the License file that + accompanied this code. If applicable, add the following below the + License Header, with the fields enclosed by brackets [] replaced by + your own identifying information: + "Portions Copyrighted [year] [name of copyright owner]" + + Contributor(s): + + The Original Software is NetBeans. The Initial Developer of the Original + Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + + If you wish your version of this file to be governed by only the CDDL + or only the GPL Version 2, indicate your decision by adding + "[Contributor] elects to include this software in this distribution + under the [CDDL or GPL Version 2] license." If you do not indicate a + single choice of license, a recipient has the option to distribute + your version of this file under either the CDDL, the GPL Version 2 or + to extend the choice of license to its licensees as provided above. + However, if you add GPL Version 2 code and therefore, elected the GPL + Version 2 license, then the option applies only if the new code is + made subject to such option by the copyright holder. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.netbeans.html</groupId> + <artifactId>pom</artifactId> + <version>2.0-SNAPSHOT</version> + </parent> + <groupId>org.netbeans.html</groupId> + <artifactId>net.java.html.json.tck</artifactId> + <version>2.0-SNAPSHOT</version> + <packaging>bundle</packaging> + <name>HTML for Java TCK</name> + <url>http://maven.apache.org</url> + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <publicPackages>org.netbeans.html.json.tck</publicPackages> + </properties> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.netbeans.html</groupId> + <artifactId>html4j-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-javadoc-plugin</artifactId> + <configuration> + <subpackages>org.netbeans.html.json.tck,org.netbeans.html.json.spi</subpackages> + <skip>false</skip> + <includeDependencySources>true</includeDependencySources> + </configuration> + </plugin> + </plugins> + </build> + <dependencies> + <dependency> + <groupId>org.netbeans.html</groupId> + <artifactId>net.java.html.json</artifactId> + <version>2.0-SNAPSHOT</version> + <type>jar</type> + </dependency> + <dependency> + <groupId>org.testng</groupId> + <artifactId>testng</artifactId> + <scope>compile</scope> + <type>jar</type> + <exclusions> + <exclusion> + <artifactId>bsh</artifactId> + <groupId>org.beanshell</groupId> + </exclusion> + </exclusions> + </dependency> + <dependency> + <groupId>org.netbeans.api</groupId> + <artifactId>org-openide-util</artifactId> + <scope>provided</scope> + </dependency> + <dependency> + <groupId>${project.groupId}</groupId> + <artifactId>net.java.html.boot</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + <description>Test Compatibility Kit for anyone who wants to consume the net.java.html.json APIs and +render their objects using own technology (e.g. own browser, MVVC, etc.).</description> +</project> http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/src/main/java/net/java/html/js/tests/Bodies.java ---------------------------------------------------------------------- diff --git a/json-tck/src/main/java/net/java/html/js/tests/Bodies.java b/json-tck/src/main/java/net/java/html/js/tests/Bodies.java new file mode 100644 index 0000000..029a3a2 --- /dev/null +++ b/json-tck/src/main/java/net/java/html/js/tests/Bodies.java @@ -0,0 +1,254 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import java.util.concurrent.Callable; +import net.java.html.js.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +final class Bodies { + @JavaScriptBody(args = { "a", "b" }, body = "return a + b;") + public static native int sum(int a, int b); + + @JavaScriptBody(args = { "a", "b" }, javacall = true, body = + "return @net.java.html.js.tests.Bodies::sum(II)(a, b);" + ) + public static native int sumJS(int a, int b); + + @JavaScriptBody(args = {"r"}, javacall = true, body = "[email protected]::run()();") + static native void callback(Runnable r); + + @JavaScriptBody(args = {"r"}, wait4js = false, keepAlive = false, javacall = true, body = "[email protected]::run()();") + static native void asyncCallback(Runnable r); + + @JavaScriptBody(args = {"c", "v"}, javacall = true, body = "var arr = [email protected]::call()(); arr.push(v); return arr;") + static native Object callbackAndPush(Callable<String[]> c, String v); + + @JavaScriptBody(args = { "v" }, body = "return v;") + public static native Object id(Object v); + + @JavaScriptBody(args = { "v" }, body = "return { 'x' : v };") + public static native Object instance(int v); + + @JavaScriptBody(args = "o", body = "o.x++;") + public static native void incrementX(Object o); + + @JavaScriptBody(args = "o", wait4js = true, body = "o.x++;") + static native void incrementXAsync(Object o); + + @JavaScriptBody(args = "o", body = "return o.x;") + public static native int readIntX(Object o); + + @JavaScriptBody(args = "o", body = "return o.x;") + public static native Object readX(Object o); + + @JavaScriptBody(args = { "o", "x" }, keepAlive = false, body = "o.x = x;") + public static native Object setX(Object o, Object x); + + @JavaScriptBody(args = { "c", "a", "b" }, keepAlive = false, javacall = true, body = + "return [email protected]::sum(II)(a, b);" + ) + public static native int sumIndirect(Sum c, int a, int b); + + @JavaScriptBody(args = { "arr", "index" }, body = "return arr[index];") + public static native Object select(Object[] arr, int index); + + @JavaScriptBody(args = { "arr" }, body = "return arr.length;") + public static native int length(Object[] arr); + + @JavaScriptBody(args = { "o", "vo" }, body = "if (vo) o = o.valueOf(); return typeof o;") + public static native String typeof(Object o, boolean useValueOf); + + @JavaScriptBody(args = { "b" }, body = "return typeof b;") + public static native String typeof(boolean b); + + @JavaScriptBody(args = { "o" }, body = "return Array.isArray(o);") + public static native boolean isArray(Object o); + + @JavaScriptBody(args = { "arr", "i", "value" }, body = "arr[i] = value; return arr[i];") + public static native String modify(String[] arr, int i, String value); + + @JavaScriptBody(args = {}, body = "return true;") + public static native boolean truth(); + + @JavaScriptBody(args = { "s" }, javacall = true, body = + "return [email protected]::sum([Ljava/lang/Object;)([1, 2, 3]);" + ) + public static native int sumArr(Sum s); + + @JavaScriptBody(args = {}, javacall = true, body = + "return @net.java.html.js.tests.Bodies::fourtyTwo()();" + ) + public static native int staticCallback(); + + @JavaScriptBody(args = {}, javacall = true, body = + "return function() { return @net.java.html.js.tests.Bodies::fourtyTwo()(); }" + ) + public static native Object delayCallback(); + + @JavaScriptBody(args = { "fn" }, body = "return fn();") + public static native Object invokeFn(Object fn); + + static int fourtyTwo() { + return 42; + } + + @JavaScriptBody(args = { "arr" }, body = + "var sum = 0;\n" + + "for (var i = 0; i < arr.length; i++) {\n" + + " sum += arr[i];\n" + + "}\n" + + "return sum;\n" + ) + public static native double sumVector(double[] arr); + + @JavaScriptBody(args = { "arr" }, body = + "var sum = 0;\n" + + "for (var i = 0; i < arr.length; i++) {\n" + + " for (var j = 0; j < arr[i].length; j++) {\n" + + " sum += arr[i][j];\n" + + " }\n" + + "}\n" + + "return sum;\n" + ) + public static native double sumMatrix(double[][] arr); + + static void incCounter(int howMuch, final Object js) { + for (int i = 0; i < howMuch; i++) { + asyncCallback(new Runnable() { + @Override + public void run() { + incrementXAsync(js); + } + }); + } + } + + @JavaScriptBody(args = {}, javacall = true, body = + "var v = { x : 0 };\n" + + "@net.java.html.js.tests.Bodies::incCounter(ILjava/lang/Object;)(42, v);\n" + + "return v.x;\n" + ) + static native int incAsync(); + + @JavaScriptBody(args = { "arr" }, body = + "var ret = [];\n" + + "for (var i in arr) {\n" + + " ret.push(arr[i]);\n" + + "}\n" + + "return ret;\n" + ) + static native Object[] forIn(Object[] in); + + @JavaScriptBody(args = { "max" }, body = + "var arr = [];\n" + + "for (var i = 0; i < max; i++) {\n" + + " arr.push(i);\n" + + "}\n" + + "return arr.length;" + ) + static native int gc(double max); + + @JavaScriptBody(args = {}, body = "" + + "var o = {};\n" + + "return o.x;\n" + ) + static native Object unknown(); + + @JavaScriptBody(args = {}, body = "" + + "return new Array(2);\n" + ) + static native Object[] unknownArray(); + + @JavaScriptBody(args = { "sum" }, javacall = true, body = "" + + "var arr = [];\n" + + "arr[1] = null;\n" + + "arr[2] = 1;\n" + + "return [email protected]::sumNonNull([Ljava/lang/Object;)(arr);\n" + ) + static native int sumNonNull(Sum sum); + + @JavaScriptBody(args = { "sum", "p" }, javacall = true, body = "" + + "var obj = {};\n" + + "obj.x = 1;\n" + + "return [email protected]::checkNonNull(Ljava/lang/Object;)(obj[p]);\n" + ) + static native boolean nonNull(Sum sum, String p); + + @JavaScriptBody(args = {}, javacall = true, body = + "return @net.java.html.js.tests.Bodies::problematicString()();" + ) + public static native String problematicCallback(); + + @JavaScriptBody(args = { "sum" }, javacall = true, body = + "return [email protected]::all(ZBSIJFDCLjava/lang/String;)(false, 1, 2, 3, 5, 6, 7, 32, 'TheEND');\n" + ) + static native String primitiveTypes(Sum sum); + + @JavaScriptBody(args = { "call" }, javacall = true, body = "" + + "var b = [email protected]::call()();\n" + + "return b ? 'yes' : 'no';\n" + ) + static native String yesNo(Callable<Boolean> call); + + @JavaScriptBody(args = {"arr", "val"}, body = "return arr[0] === val;") + public static native boolean isInArray(Object[] arr, Object val); + + @JavaScriptBody(args = {}, body = "return globalString;") + static native String readGlobalString(); + + @JavaScriptBody(args = {}, body = "return global2String;") + static native String readGlobal2String(); + + static String problematicString() { + return "{\n" + +" MyViewModel: {\n" + +"// ViewModel: JavaViewModel,\n" + +"\n" + +" } \n" + +"}"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/src/main/java/net/java/html/js/tests/Factorial.java ---------------------------------------------------------------------- diff --git a/json-tck/src/main/java/net/java/html/js/tests/Factorial.java b/json-tck/src/main/java/net/java/html/js/tests/Factorial.java new file mode 100644 index 0000000..d5d631a --- /dev/null +++ b/json-tck/src/main/java/net/java/html/js/tests/Factorial.java @@ -0,0 +1,62 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import net.java.html.js.JavaScriptBody; + +/** + * + * @author Jaroslav Tulach + */ +public final class Factorial { + int minusOne(int i) { + return i - 1; + } + + @JavaScriptBody(args = { "i" }, javacall = true,body = + "if (i <= 1) return 1;\n" + + "var im1 = [email protected]::minusOne(I)(i);\n" + + "return [email protected]::factorial(I)(im1) * i;" + ) + native int factorial(int n); +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java ---------------------------------------------------------------------- diff --git a/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java b/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java new file mode 100644 index 0000000..46d0775 --- /dev/null +++ b/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java @@ -0,0 +1,178 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import java.lang.ref.Reference; +import java.lang.ref.WeakReference; +import org.netbeans.html.json.tck.KOTest; +import static net.java.html.js.tests.JavaScriptBodyTest.*; + +/** + * + * @author Jaroslav Tulach + */ +public class GCBodyTest { + Reference<?> ref; + int[] arr; + + @KOTest public void callbackInterfaceCanDisappear() throws InterruptedException { + if (ref != null) { + assertGC(ref, "Can disappear!"); + return; + } + Sum s = new Sum(); + int res = Bodies.sumIndirect(s, 22, 20); + assertEquals(res, 42, "Expecting 42"); + Reference<?> ref = new WeakReference<Object>(s); + s = null; + assertGC(ref, "Can disappear!"); + } + + private Object assignInst() { + Object obj = Bodies.instance(0); + Object s = new EmptyInstance(); + Bodies.setX(obj, s); + assertEquals(s, Bodies.readX(obj)); + ref = new WeakReference<Object>(s); + return obj; +} + + @KOTest public void holdObjectAndReleaseObject() throws InterruptedException { + if (ref != null) { + assertGC(ref, "Can disappear!"); + return; + } + + Object obj = assignInst(); + assertNotNull(ref, "Reference assigned"); + + assertGC(ref, "Can disappear as it is keepAlive false!"); + assertNotNull(obj, "Object is still present"); + } + + @KOTest public void strongReceiverBehavior() { + Object v = new EmptyInstance(); + Receiver r = new Receiver(v); + r.apply(); + assertEquals(v, r.value, "Value is as expected"); + } + + @KOTest public void gcReceiverBehavior() throws InterruptedException { + Receiver r = new Receiver(new EmptyInstance()); + assertGC(r.ref, "The empty instance can be GCed even when referenced from JS"); + r.apply(); + assertEquals(r.value, null, "Setter called with null value"); + } + + private static Reference<?> sendRunnable(final int[] arr) { + Runnable r = new Runnable() { + @Override + public void run() { + arr[0]++; + } + }; + Bodies.asyncCallback(r); + return new WeakReference<Object>(r); + } + + private static class EmptyInstance { + } + + @KOTest public void parametersNeedToRemainInAsyncMode() throws InterruptedException { + if (ref != null) { + if (arr[0] != 1) { + throw new InterruptedException(); + } + assertGC(ref, "Now the runnable can disappear"); + return; + } + arr = new int[] { 0 }; + ref = sendRunnable(arr); + if (arr[0] == 1) { + return; + } + assertNotGC(ref, false, "The runnable should not be GCed"); + throw new InterruptedException(); + } + + private static void assertGC(Reference<?> ref, String msg) throws InterruptedException { + for (int i = 0; i < 100; i++) { + if (isGone(ref)) return; + long then = System.currentTimeMillis(); + int size = Bodies.gc(Math.pow(2.0, i)); + long took = System.currentTimeMillis() - then; + if (took > 3000) { + throw new InterruptedException(msg + " - giving up after " + took + " ms at size of " + size); + } + + try { + System.gc(); + System.runFinalization(); + } catch (Error err) { + err.printStackTrace(); + } + } + throw new InterruptedException(msg); + } + + private static boolean isGone(Reference<?> ref) { + return ref.get() == null; + } + + private static void assertNotGC(Reference<?> ref, boolean clearJS, String msg) throws InterruptedException { + for (int i = 0; i < 10; i++) { + if (ref.get() == null) { + throw new IllegalStateException(msg); + } + if (clearJS) { + Bodies.gc(Math.pow(2.0, i)); + } + try { + System.gc(); + System.runFinalization(); + } catch (Error err) { + err.printStackTrace(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/src/main/java/net/java/html/js/tests/Global2String.java ---------------------------------------------------------------------- diff --git a/json-tck/src/main/java/net/java/html/js/tests/Global2String.java b/json-tck/src/main/java/net/java/html/js/tests/Global2String.java new file mode 100644 index 0000000..afd847c --- /dev/null +++ b/json-tck/src/main/java/net/java/html/js/tests/Global2String.java @@ -0,0 +1,52 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import net.java.html.js.JavaScriptBody; +import net.java.html.js.JavaScriptResource; + +@JavaScriptResource("global2.js") +class Global2String { + @JavaScriptBody(args = {}, body = "return global2String;") + public static native String init(); +} http://git-wip-us.apache.org/repos/asf/incubator-netbeans-html4j/blob/226089a5/json-tck/src/main/java/net/java/html/js/tests/GlobalString.java ---------------------------------------------------------------------- diff --git a/json-tck/src/main/java/net/java/html/js/tests/GlobalString.java b/json-tck/src/main/java/net/java/html/js/tests/GlobalString.java new file mode 100644 index 0000000..fe46bff --- /dev/null +++ b/json-tck/src/main/java/net/java/html/js/tests/GlobalString.java @@ -0,0 +1,52 @@ +/** + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * Contributor(s): + * + * The Original Software is NetBeans. The Initial Developer of the Original + * Software is Oracle. Portions Copyright 2013-2016 Oracle. All Rights Reserved. + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + */ +package net.java.html.js.tests; + +import net.java.html.js.JavaScriptBody; +import net.java.html.js.JavaScriptResource; + +@JavaScriptResource("global.js") +class GlobalString { + @JavaScriptBody(args = {}, body = "return globalString;") + public static native String init(); +}
