1.1 --- a/.hgtags Fri May 24 16:45:32 2013 +0200
1.2 +++ b/.hgtags Fri May 24 18:04:55 2013 +0200
1.3 @@ -5,3 +5,8 @@
1.4 caf1e66268fd4100d57922d973ae09a6bf3be847 release-0.4
1.5 caf1e66268fd4100d57922d973ae09a6bf3be847 release-${releaseVersion}
1.6 0000000000000000000000000000000000000000 release-${releaseVersion}
1.7 +52a4a5f868bccc67d50ad17f793b9ebabdf75d88 release-0.6
1.8 +6792dc0bafb9c76a099e45bfc9e967d6a2823827 release-0.7
1.9 +623816269b75e53fffb4b19960df7040a3c20056 release-0.7
1.10 +23572dc719bd630817d11eaabdee4565f63ef8e1 release-0.7.1
1.11 +56abd247f421febd8b2c5e59d666968692e11555 release-0.7.2
2.1 --- a/benchmarks/matrix-multiplication/pom.xml Fri May 24 16:45:32 2013 +0200
2.2 +++ b/benchmarks/matrix-multiplication/pom.xml Fri May 24 18:04:55 2013 +0200
2.3 @@ -4,12 +4,12 @@
2.4
2.5 <groupId>org.apidesign.bck2brwsr</groupId>
2.6 <artifactId>matrix.multiplication</artifactId>
2.7 - <version>0.6-SNAPSHOT</version>
2.8 + <version>0.8-SNAPSHOT</version>
2.9 <packaging>jar</packaging>
2.10 <parent>
2.11 <artifactId>benchmarks</artifactId>
2.12 <groupId>org.apidesign.bck2brwsr</groupId>
2.13 - <version>0.6-SNAPSHOT</version>
2.14 + <version>0.8-SNAPSHOT</version>
2.15 </parent>
2.16
2.17 <name>Matrix multiplication</name>
2.18 @@ -74,7 +74,7 @@
2.19 <dependency>
2.20 <groupId>org.apidesign.bck2brwsr</groupId>
2.21 <artifactId>emul.mini</artifactId>
2.22 - <version>0.6-SNAPSHOT</version>
2.23 + <version>0.8-SNAPSHOT</version>
2.24 </dependency>
2.25 <dependency>
2.26 <groupId>org.testng</groupId>
2.27 @@ -91,7 +91,13 @@
2.28 <dependency>
2.29 <groupId>org.apidesign.bck2brwsr</groupId>
2.30 <artifactId>vmtest</artifactId>
2.31 - <version>0.6-SNAPSHOT</version>
2.32 + <version>0.8-SNAPSHOT</version>
2.33 + <scope>test</scope>
2.34 + </dependency>
2.35 + <dependency>
2.36 + <groupId>org.apidesign.bck2brwsr</groupId>
2.37 + <artifactId>launcher.http</artifactId>
2.38 + <version>0.8-SNAPSHOT</version>
2.39 <scope>test</scope>
2.40 </dependency>
2.41 </dependencies>
3.1 --- a/benchmarks/pom.xml Fri May 24 16:45:32 2013 +0200
3.2 +++ b/benchmarks/pom.xml Fri May 24 18:04:55 2013 +0200
3.3 @@ -4,11 +4,11 @@
3.4 <parent>
3.5 <artifactId>bck2brwsr</artifactId>
3.6 <groupId>org.apidesign</groupId>
3.7 - <version>0.6-SNAPSHOT</version>
3.8 + <version>0.8-SNAPSHOT</version>
3.9 </parent>
3.10 <groupId>org.apidesign.bck2brwsr</groupId>
3.11 <artifactId>benchmarks</artifactId>
3.12 - <version>0.6-SNAPSHOT</version>
3.13 + <version>0.8-SNAPSHOT</version>
3.14 <packaging>pom</packaging>
3.15 <name>Performance benchmarks</name>
3.16 <modules>
4.1 --- a/dew/pom.xml Fri May 24 16:45:32 2013 +0200
4.2 +++ b/dew/pom.xml Fri May 24 18:04:55 2013 +0200
4.3 @@ -4,11 +4,11 @@
4.4 <parent>
4.5 <groupId>org.apidesign</groupId>
4.6 <artifactId>bck2brwsr</artifactId>
4.7 - <version>0.6-SNAPSHOT</version>
4.8 + <version>0.8-SNAPSHOT</version>
4.9 </parent>
4.10 <groupId>org.apidesign.bck2brwsr</groupId>
4.11 <artifactId>dew</artifactId>
4.12 - <version>0.6-SNAPSHOT</version>
4.13 + <version>0.8-SNAPSHOT</version>
4.14 <name>Development Environment for Web</name>
4.15 <url>http://maven.apache.org</url>
4.16 <build>
5.1 --- a/ide/editor/pom.xml Fri May 24 16:45:32 2013 +0200
5.2 +++ b/ide/editor/pom.xml Fri May 24 18:04:55 2013 +0200
5.3 @@ -4,12 +4,12 @@
5.4 <parent>
5.5 <artifactId>ide</artifactId>
5.6 <groupId>org.apidesign.bck2brwsr</groupId>
5.7 - <version>0.6-SNAPSHOT</version>
5.8 + <version>0.8-SNAPSHOT</version>
5.9 </parent>
5.10
5.11 <groupId>org.apidesign.bck2brwsr.ide</groupId>
5.12 <artifactId>editor</artifactId>
5.13 - <version>0.6-SNAPSHOT</version>
5.14 + <version>0.8-SNAPSHOT</version>
5.15 <packaging>nbm</packaging>
5.16
5.17 <name>Editor Support for Bck2Brwsr</name>
5.18 @@ -75,7 +75,7 @@
5.19 <dependency>
5.20 <groupId>org.apidesign.bck2brwsr</groupId>
5.21 <artifactId>core</artifactId>
5.22 - <version>0.6-SNAPSHOT</version>
5.23 + <version>0.8-SNAPSHOT</version>
5.24 <type>jar</type>
5.25 <scope>test</scope>
5.26 </dependency>
6.1 --- a/ide/pom.xml Fri May 24 16:45:32 2013 +0200
6.2 +++ b/ide/pom.xml Fri May 24 18:04:55 2013 +0200
6.3 @@ -4,14 +4,26 @@
6.4 <parent>
6.5 <artifactId>bck2brwsr</artifactId>
6.6 <groupId>org.apidesign</groupId>
6.7 - <version>0.6-SNAPSHOT</version>
6.8 + <version>0.8-SNAPSHOT</version>
6.9 </parent>
6.10 <groupId>org.apidesign.bck2brwsr</groupId>
6.11 <artifactId>ide</artifactId>
6.12 - <version>0.6-SNAPSHOT</version>
6.13 + <version>0.8-SNAPSHOT</version>
6.14 <packaging>pom</packaging>
6.15 <name>IDE Support</name>
6.16 <modules>
6.17 <module>editor</module>
6.18 </modules>
6.19 + <build>
6.20 + <plugins>
6.21 + <plugin>
6.22 + <groupId>org.apache.maven.plugins</groupId>
6.23 + <artifactId>maven-deploy-plugin</artifactId>
6.24 + <version>2.7</version>
6.25 + <configuration>
6.26 + <skip>true</skip>
6.27 + </configuration>
6.28 + </plugin>
6.29 + </plugins>
6.30 + </build>
6.31 </project>
7.1 --- a/javaquery/api/pom.xml Fri May 24 16:45:32 2013 +0200
7.2 +++ b/javaquery/api/pom.xml Fri May 24 18:04:55 2013 +0200
7.3 @@ -4,11 +4,11 @@
7.4 <parent>
7.5 <groupId>org.apidesign.bck2brwsr</groupId>
7.6 <artifactId>javaquery</artifactId>
7.7 - <version>0.6-SNAPSHOT</version>
7.8 + <version>0.8-SNAPSHOT</version>
7.9 </parent>
7.10 <groupId>org.apidesign.bck2brwsr</groupId>
7.11 <artifactId>javaquery.api</artifactId>
7.12 - <version>0.6-SNAPSHOT</version>
7.13 + <version>0.8-SNAPSHOT</version>
7.14 <name>JavaQuery API</name>
7.15 <url>http://maven.apache.org</url>
7.16 <build>
7.17 @@ -73,5 +73,11 @@
7.18 <version>${project.version}</version>
7.19 <scope>test</scope>
7.20 </dependency>
7.21 + <dependency>
7.22 + <groupId>${project.groupId}</groupId>
7.23 + <artifactId>launcher.http</artifactId>
7.24 + <version>${project.version}</version>
7.25 + <scope>test</scope>
7.26 + </dependency>
7.27 </dependencies>
7.28 </project>
8.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Fri May 24 16:45:32 2013 +0200
8.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypes.java Fri May 24 18:04:55 2013 +0200
8.3 @@ -54,6 +54,9 @@
8.4 if (value == null) {
8.5 return "null";
8.6 }
8.7 + if (value instanceof Enum) {
8.8 + value = value.toString();
8.9 + }
8.10 if (value instanceof String) {
8.11 return '"' +
8.12 ((String)value).
9.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Fri May 24 16:45:32 2013 +0200
9.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Fri May 24 18:04:55 2013 +0200
9.3 @@ -23,6 +23,7 @@
9.4 import java.io.StringWriter;
9.5 import java.io.Writer;
9.6 import java.lang.annotation.AnnotationTypeMismatchException;
9.7 +import java.lang.annotation.IncompleteAnnotationException;
9.8 import java.lang.reflect.Method;
9.9 import java.util.ArrayList;
9.10 import java.util.Collection;
9.11 @@ -246,7 +247,7 @@
9.12 w.append(type).append("(e));\n");
9.13 } else if (isEnum[0]) {
9.14 w.append(" this.prop_").append(pn);
9.15 - w.append(".add(");
9.16 + w.append(".add(e == null ? null : ");
9.17 w.append(type).append(".valueOf((String)e));\n");
9.18 } else {
9.19 if (isPrimitive(type)) {
9.20 @@ -262,7 +263,7 @@
9.21 } else {
9.22 if (isEnum[0]) {
9.23 w.append(" this.prop_").append(pn);
9.24 - w.append(" = ");
9.25 + w.append(" = ret[" + cnt + "] == null ? null : ");
9.26 w.append(type).append(".valueOf((String)ret[" + cnt + "]);\n");
9.27 } else if (isPrimitive(type)) {
9.28 w.append(" this.prop_").append(pn);
9.29 @@ -1146,7 +1147,8 @@
9.30 String sep = "";
9.31 for (Prprt p : props) {
9.32 w.write(sep);
9.33 - w.append(" sb.append(\"" + p.name() + ": \");\n");
9.34 + w.append(" sb.append('\"').append(\"" + p.name() + "\")");
9.35 + w.append(".append('\"').append(\":\");\n");
9.36 w.append(" sb.append(org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toJSON(prop_");
9.37 w.append(p.name()).append("));\n");
9.38 sep = " sb.append(',');\n";
9.39 @@ -1341,7 +1343,7 @@
9.40 String typeName(ProcessingEnvironment env) {
9.41 try {
9.42 return p.type().getName();
9.43 - } catch (AnnotationTypeMismatchException ex) {
9.44 + } catch (IncompleteAnnotationException | AnnotationTypeMismatchException ex) {
9.45 for (Object v : getAnnoValues(env)) {
9.46 String s = v.toString().replace(" ", "");
9.47 if (s.startsWith("type=") && s.endsWith(".class")) {
10.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java Fri May 24 16:45:32 2013 +0200
10.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/ComputedProperty.java Fri May 24 18:04:55 2013 +0200
10.3 @@ -22,16 +22,11 @@
10.4 import java.lang.annotation.RetentionPolicy;
10.5 import java.lang.annotation.Target;
10.6
10.7 -/** Can be used in classes annotated with {@link Page} annotation to
10.8 - * define a derived property. Value of derived property is based on values
10.9 - * of {@link Property} as enumerated by {@link Page#properties()}.
10.10 - * <p>
10.11 - * The name of the derived property is the name of the method. The arguments
10.12 - * of the method define the property names (from {@link Page#properties()} list)
10.13 - * the value of property depends on.
10.14 - *
10.15 +/**
10.16 + * @deprecated Replaced by new {@link net.java.html.json.ComputedProperty net.java.html.json} API.
10.17 * @author Jaroslav Tulach <jtulach@netbeans.org>
10.18 */
10.19 +@Deprecated
10.20 @Retention(RetentionPolicy.SOURCE)
10.21 @Target(ElementType.METHOD)
10.22 public @interface ComputedProperty {
11.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Model.java Fri May 24 16:45:32 2013 +0200
11.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Model.java Fri May 24 18:04:55 2013 +0200
11.3 @@ -22,19 +22,13 @@
11.4 import java.lang.annotation.RetentionPolicy;
11.5 import java.lang.annotation.Target;
11.6
11.7 -/** Defines a model class named {@link #className()} which contains
11.8 - * defined {@link #properties()}. This class can have methods
11.9 - * annotated by {@link ComputedProperty} which define derived
11.10 - * properties in the model class.
11.11 - * <p>
11.12 - * The {@link #className() generated class}'s <code>toString</code>
11.13 - * converts the state of the object into
11.14 - * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a> format.
11.15 - *
11.16 +/**
11.17 + * @deprecated Replaced by new {@link net.java.html.json.Model net.java.html.json} API.
11.18 * @author Jaroslav Tulach <jtulach@netbeans.org>
11.19 */
11.20 @Retention(RetentionPolicy.SOURCE)
11.21 @Target(ElementType.TYPE)
11.22 +@Deprecated
11.23 public @interface Model {
11.24 /** Name of the model class */
11.25 String className();
12.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnFunction.java Fri May 24 16:45:32 2013 +0200
12.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnFunction.java Fri May 24 18:04:55 2013 +0200
12.3 @@ -22,13 +22,12 @@
12.4 import java.lang.annotation.RetentionPolicy;
12.5 import java.lang.annotation.Target;
12.6
12.7 -/** Methods in class annotated by {@link Model} or {@link Page} can be
12.8 - * annotated by this annotation to signal that they should be available
12.9 - * as functions to users of the model classes.
12.10 - *
12.11 +/**
12.12 + * @deprecated Replaced by new {@link net.java.html.json.Function net.java.html.json} API.
12.13 * @author Jaroslav Tulach <jtulach@netbeans.org>
12.14 */
12.15 @Target(ElementType.METHOD)
12.16 @Retention(RetentionPolicy.SOURCE)
12.17 +@Deprecated
12.18 public @interface OnFunction {
12.19 }
13.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnPropertyChange.java Fri May 24 16:45:32 2013 +0200
13.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnPropertyChange.java Fri May 24 18:04:55 2013 +0200
13.3 @@ -22,13 +22,13 @@
13.4 import java.lang.annotation.RetentionPolicy;
13.5 import java.lang.annotation.Target;
13.6
13.7 -/** Represents a property. Either in a generated model of an HTML
13.8 - * {@link Page} or in a class defined by {@link Model}.
13.9 - *
13.10 +/**
13.11 + * @deprecated Replaced by new {@link net.java.html.json.OnPropertyChange net.java.html.json} API.
13.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
13.13 */
13.14 @Retention(RetentionPolicy.SOURCE)
13.15 @Target(ElementType.METHOD)
13.16 +@Deprecated
13.17 public @interface OnPropertyChange {
13.18 /** Name(s) of the properties. One wishes to observe.
13.19 *
14.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java Fri May 24 16:45:32 2013 +0200
14.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/OnReceive.java Fri May 24 18:04:55 2013 +0200
14.3 @@ -22,54 +22,14 @@
14.4 import java.lang.annotation.RetentionPolicy;
14.5 import java.lang.annotation.Target;
14.6
14.7 -/** Static methods in classes annotated by {@link Page}
14.8 - * can be marked by this annotation to establish a
14.9 - * <a href="http://en.wikipedia.org/wiki/JSON">JSON</a>
14.10 - * communication point.
14.11 - * The associated model page then gets new method to invoke a network
14.12 - * connection. Example follows:
14.13 - *
14.14 - * <pre>
14.15 - * {@link Page @Page}(className="MyModel", xhtml="page.html", properties={
14.16 - * {@link Property @Property}(name = "people", type=Person.class, array=true)
14.17 - * })
14.18 - * class MyModelImpl {
14.19 - * {@link Model @Model}(className="Person", properties={
14.20 - * {@link Property @Property}(name = "firstName", type=String.class),
14.21 - * {@link Property @Property}(name = "lastName", type=String.class)
14.22 - * })
14.23 - * static class PersonImpl {
14.24 - * {@link ComputedProperty @ComputedProperty}
14.25 - * static String fullName(String firstName, String lastName) {
14.26 - * return firstName + " " + lastName;
14.27 - * }
14.28 - * }
14.29 - *
14.30 - * {@link OnReceive @OnReceive}(url = "{protocol}://your.server.com/person/{name}")
14.31 - * static void getANewPerson(MyModel m, Person p) {
14.32 - * {@link Element#alert Element.alert}("Adding " + p.getFullName() + '!');
14.33 - * m.getPeople().add(p);
14.34 - * }
14.35 - *
14.36 - * // the above will generate method <code>getANewPerson</code> in class <code>MyModel</code>.
14.37 - * // with <code>protocol</code> and <code>name</code> arguments
14.38 - * // which asynchronously contacts the server and in case of success calls
14.39 - * // your {@link OnReceive @OnReceive} with parsed in data
14.40 - *
14.41 - * {@link On @On}(event={@link OnEvent#CLICK OnEvent.CLICK}, id="rqst")
14.42 - * static void requestSmith(MyModel m) {
14.43 - * m.getANewPerson("http", "Smith");
14.44 - * }
14.45 - * }
14.46 - * </pre>
14.47 - * When the server returns <code>{ "firstName" : "John", "lastName" : "Smith" }</code>
14.48 - * the browser will show alert message <em>Adding John Smith!</em>.
14.49 - *
14.50 +/**
14.51 + * @deprecated Replaced by new {@link net.java.html.json.OnReceive net.java.html.json} API.
14.52 * @author Jaroslav Tulach <jtulach@netbeans.org>
14.53 * @since 0.6
14.54 */
14.55 @Retention(RetentionPolicy.SOURCE)
14.56 @Target(ElementType.METHOD)
14.57 +@Deprecated
14.58 public @interface OnReceive {
14.59 /** The URL to connect to. Can contain variable names surrounded by '{' and '}'.
14.60 * Those parameters will then become variables of the associated method.
15.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Property.java Fri May 24 16:45:32 2013 +0200
15.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Property.java Fri May 24 18:04:55 2013 +0200
15.3 @@ -22,13 +22,13 @@
15.4 import java.lang.annotation.Target;
15.5 import java.util.List;
15.6
15.7 -/** Represents a property. Either in a generated model of an HTML
15.8 - * {@link Page} or in a class defined by {@link Model}.
15.9 - *
15.10 +/**
15.11 + * @deprecated Replaced by new {@link net.java.html.json.Property net.java.html.json} API.
15.12 * @author Jaroslav Tulach <jtulach@netbeans.org>
15.13 */
15.14 @Retention(RetentionPolicy.SOURCE)
15.15 @Target({})
15.16 +@Deprecated
15.17 public @interface Property {
15.18 /** Name of the property. Will be used to define proper getter and setter
15.19 * in the associated class.
16.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypesTest.java Fri May 24 16:45:32 2013 +0200
16.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/ConvertTypesTest.java Fri May 24 18:04:55 2013 +0200
16.3 @@ -27,17 +27,17 @@
16.4 * @author Jaroslav Tulach <jtulach@netbeans.org>
16.5 */
16.6 public class ConvertTypesTest {
16.7 - @JavaScriptBody(args = { }, body = "var json = new Object();"
16.8 + @JavaScriptBody(args = { "includeSex" }, body = "var json = new Object();"
16.9 + "json.firstName = 'son';\n"
16.10 + "json.lastName = 'dj';\n"
16.11 - + "json.sex = 'MALE';\n"
16.12 + + "if (includeSex) json.sex = 'MALE';\n"
16.13 + "return json;"
16.14 )
16.15 - private static native Object createJSON();
16.16 + private static native Object createJSON(boolean includeSex);
16.17
16.18 @BrwsrTest
16.19 - public void testConvertToPeople() {
16.20 - final Object o = createJSON();
16.21 + public void testConvertToPeople() throws Exception {
16.22 + final Object o = createJSON(true);
16.23
16.24 Person p = new Person(o);
16.25
16.26 @@ -45,6 +45,17 @@
16.27 assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
16.28 assert Sex.MALE.equals(p.getSex()) : "Sex: " + p.getSex();
16.29 }
16.30 +
16.31 + @BrwsrTest
16.32 + public void testConvertToPeopleWithoutSex() throws Exception {
16.33 + final Object o = createJSON(false);
16.34 +
16.35 + Person p = new Person(o);
16.36 +
16.37 + assert "son".equals(p.getFirstName()) : "First name: " + p.getFirstName();
16.38 + assert "dj".equals(p.getLastName()) : "Last name: " + p.getLastName();
16.39 + assert p.getSex() == null : "No sex: " + p.getSex();
16.40 + }
16.41
16.42 @Factory public static Object[] create() {
16.43 return VMTest.create(ConvertTypesTest.class);
17.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Fri May 24 16:45:32 2013 +0200
17.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Fri May 24 18:04:55 2013 +0200
17.3 @@ -70,6 +70,25 @@
17.4 assertEquals(o.getString("sex"), "MALE");
17.5 }
17.6
17.7 + @BrwsrTest public void toJSONInABrowser() throws Throwable {
17.8 + Person p = new Person();
17.9 + p.setSex(Sex.MALE);
17.10 + p.setFirstName("Jarda");
17.11 + p.setLastName("Tulach");
17.12 +
17.13 + Object json;
17.14 + try {
17.15 + json = parseJSON(p.toString());
17.16 + } catch (Throwable ex) {
17.17 + throw new IllegalStateException("Can't parse " + p).initCause(ex);
17.18 + }
17.19 +
17.20 + Person p2 = new Person(json);
17.21 +
17.22 + assert p2.getFirstName().equals(p.getFirstName()) :
17.23 + "Should be the same: " + p.getFirstName() + " != " + p2.getFirstName();
17.24 + }
17.25 +
17.26 @Test public void personWithWildCharactersAndNulls() throws JSONException {
17.27 Person p = new Person();
17.28 p.setFirstName("'\"\n");
17.29 @@ -213,6 +232,9 @@
17.30 @JavaScriptBody(args = { }, body = "return window.document.getElementsByTagName('script').length;")
17.31 private static native int scriptElements();
17.32
17.33 + @JavaScriptBody(args = { "s" }, body = "return window.JSON.parse(s);")
17.34 + private static native Object parseJSON(String s);
17.35 +
17.36 @Http(@Http.Resource(
17.37 content = "{'firstName': 'Sitar', 'sex': 'MALE'}",
17.38 path="/person.json",
18.1 --- a/javaquery/demo-calculator-dynamic/nbactions.xml Fri May 24 16:45:32 2013 +0200
18.2 +++ b/javaquery/demo-calculator-dynamic/nbactions.xml Fri May 24 18:04:55 2013 +0200
18.3 @@ -23,7 +23,7 @@
18.4 <actionName>run</actionName>
18.5 <goals>
18.6 <goal>process-classes</goal>
18.7 - <goal>org.apidesign.bck2brwsr:mojo:0.6-SNAPSHOT:brwsr</goal>
18.8 + <goal>bck2brwsr:brwsr</goal>
18.9 </goals>
18.10 </action>
18.11 </actions>
19.1 --- a/javaquery/demo-calculator-dynamic/pom.xml Fri May 24 16:45:32 2013 +0200
19.2 +++ b/javaquery/demo-calculator-dynamic/pom.xml Fri May 24 18:04:55 2013 +0200
19.3 @@ -4,7 +4,7 @@
19.4
19.5 <groupId>org.apidesign.bck2brwsr</groupId>
19.6 <artifactId>demo.calculator</artifactId>
19.7 - <version>0.6-SNAPSHOT</version>
19.8 + <version>0.8-SNAPSHOT</version>
19.9 <packaging>jar</packaging>
19.10
19.11 <name>JavaQuery Demo - Calculator</name>
19.12 @@ -17,8 +17,8 @@
19.13 <plugins>
19.14 <plugin>
19.15 <groupId>org.apidesign.bck2brwsr</groupId>
19.16 - <artifactId>mojo</artifactId>
19.17 - <version>0.6-SNAPSHOT</version>
19.18 + <artifactId>bck2brwsr-maven-plugin</artifactId>
19.19 + <version>${project.version}</version>
19.20 <executions>
19.21 <execution>
19.22 <goals>
19.23 @@ -93,13 +93,13 @@
19.24 <dependency>
19.25 <groupId>org.apidesign.bck2brwsr</groupId>
19.26 <artifactId>emul</artifactId>
19.27 - <version>0.6-SNAPSHOT</version>
19.28 + <version>${project.version}</version>
19.29 <classifier>rt</classifier>
19.30 </dependency>
19.31 <dependency>
19.32 <groupId>org.apidesign.bck2brwsr</groupId>
19.33 <artifactId>javaquery.api</artifactId>
19.34 - <version>0.6-SNAPSHOT</version>
19.35 + <version>${project.version}</version>
19.36 </dependency>
19.37 <dependency>
19.38 <groupId>org.testng</groupId>
19.39 @@ -112,7 +112,7 @@
19.40 <artifactId>vm4brwsr</artifactId>
19.41 <classifier>js</classifier>
19.42 <type>zip</type>
19.43 - <version>0.6-SNAPSHOT</version>
19.44 + <version>${project.version}</version>
19.45 <scope>provided</scope>
19.46 </dependency>
19.47 </dependencies>
20.1 --- a/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml Fri May 24 16:45:32 2013 +0200
20.2 +++ b/javaquery/demo-calculator-dynamic/src/main/resources/org/apidesign/bck2brwsr/demo/calc/Calculator.xhtml Fri May 24 18:04:55 2013 +0200
20.3 @@ -93,7 +93,7 @@
20.4
20.5 <script src="bck2brwsr.js"></script>
20.6 <script type="text/javascript">
20.7 - var vm = bck2brwsr('demo.calculator-0.5-SNAPSHOT.jar');
20.8 + var vm = bck2brwsr('demo.calculator-0.6.jar');
20.9 vm.loadClass('org.apidesign.bck2brwsr.demo.calc.Calc');
20.10 </script>
20.11
21.1 --- a/javaquery/demo-calculator/nbactions.xml Fri May 24 16:45:32 2013 +0200
21.2 +++ b/javaquery/demo-calculator/nbactions.xml Fri May 24 18:04:55 2013 +0200
21.3 @@ -23,7 +23,7 @@
21.4 <actionName>run</actionName>
21.5 <goals>
21.6 <goal>package</goal>
21.7 - <goal>org.apidesign.bck2brwsr:mojo:0.5-SNAPSHOT:brwsr</goal>
21.8 + <goal>bck2brwsr:brwsr</goal>
21.9 </goals>
21.10 <properties>
21.11 <skipTests>true</skipTests>
22.1 --- a/javaquery/demo-calculator/pom.xml Fri May 24 16:45:32 2013 +0200
22.2 +++ b/javaquery/demo-calculator/pom.xml Fri May 24 18:04:55 2013 +0200
22.3 @@ -4,7 +4,7 @@
22.4
22.5 <groupId>org.apidesign.bck2brwsr</groupId>
22.6 <artifactId>demo.static.calculator</artifactId>
22.7 - <version>0.6-SNAPSHOT</version>
22.8 + <version>0.8-SNAPSHOT</version>
22.9 <packaging>jar</packaging>
22.10
22.11 <name>JavaQuery Demo - Calculator - Static Compilation</name>
22.12 @@ -12,14 +12,14 @@
22.13
22.14 <properties>
22.15 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22.16 - <bck2brwsr.obfuscationlevel>FULL</bck2brwsr.obfuscationlevel>
22.17 + <bck2brwsr.obfuscationlevel>MINIMAL</bck2brwsr.obfuscationlevel>
22.18 </properties>
22.19 <build>
22.20 <plugins>
22.21 <plugin>
22.22 <groupId>org.apidesign.bck2brwsr</groupId>
22.23 - <artifactId>mojo</artifactId>
22.24 - <version>0.6-SNAPSHOT</version>
22.25 + <artifactId>bck2brwsr-maven-plugin</artifactId>
22.26 + <version>${project.version}</version>
22.27 <executions>
22.28 <execution>
22.29 <goals>
22.30 @@ -98,13 +98,13 @@
22.31 <dependency>
22.32 <groupId>org.apidesign.bck2brwsr</groupId>
22.33 <artifactId>emul</artifactId>
22.34 - <version>0.6-SNAPSHOT</version>
22.35 + <version>${project.version}</version>
22.36 <classifier>rt</classifier>
22.37 </dependency>
22.38 <dependency>
22.39 <groupId>org.apidesign.bck2brwsr</groupId>
22.40 <artifactId>javaquery.api</artifactId>
22.41 - <version>0.6-SNAPSHOT</version>
22.42 + <version>${project.version}</version>
22.43 </dependency>
22.44 </dependencies>
22.45 </project>
23.1 --- a/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Fri May 24 16:45:32 2013 +0200
23.2 +++ b/javaquery/demo-calculator/src/main/resources/org/apidesign/bck2brwsr/demo/calc/staticcompilation/Calculator.xhtml Fri May 24 18:04:55 2013 +0200
23.3 @@ -91,7 +91,7 @@
23.4 <div data-bind="text: displayPreview"></div>
23.5 <script src="bck2brwsr.js"></script>
23.6 <script>
23.7 - var vm = bck2brwsr('demo.static.calculator-0.5-SNAPSHOT.jar');
23.8 + var vm = bck2brwsr('demo.static.calculator-0.6.jar');
23.9 vm.loadClass('org.apidesign.bck2brwsr.demo.calc.staticcompilation.Calc');
23.10 </script>
23.11 </body>
24.1 --- a/javaquery/demo-twitter/nbactions.xml Fri May 24 16:45:32 2013 +0200
24.2 +++ b/javaquery/demo-twitter/nbactions.xml Fri May 24 18:04:55 2013 +0200
24.3 @@ -23,7 +23,7 @@
24.4 <actionName>run</actionName>
24.5 <goals>
24.6 <goal>process-classes</goal>
24.7 - <goal>org.apidesign.bck2brwsr:mojo:0.6-SNAPSHOT:brwsr</goal>
24.8 + <goal>bck2brwsr:brwsr</goal>
24.9 </goals>
24.10 </action>
24.11 </actions>
25.1 --- a/javaquery/demo-twitter/pom.xml Fri May 24 16:45:32 2013 +0200
25.2 +++ b/javaquery/demo-twitter/pom.xml Fri May 24 18:04:55 2013 +0200
25.3 @@ -4,12 +4,12 @@
25.4 <parent>
25.5 <artifactId>javaquery</artifactId>
25.6 <groupId>org.apidesign.bck2brwsr</groupId>
25.7 - <version>0.6-SNAPSHOT</version>
25.8 + <version>0.8-SNAPSHOT</version>
25.9 </parent>
25.10
25.11 <groupId>org.apidesign.bck2brwsr</groupId>
25.12 <artifactId>demo-twitter</artifactId>
25.13 - <version>0.6-SNAPSHOT</version>
25.14 + <version>0.8-SNAPSHOT</version>
25.15 <packaging>jar</packaging>
25.16
25.17 <name>Bck2Brwsr's Twttr</name>
25.18 @@ -50,8 +50,8 @@
25.19 <plugins>
25.20 <plugin>
25.21 <groupId>org.apidesign.bck2brwsr</groupId>
25.22 - <artifactId>mojo</artifactId>
25.23 - <version>0.6-SNAPSHOT</version>
25.24 + <artifactId>bck2brwsr-maven-plugin</artifactId>
25.25 + <version>${project.version}</version>
25.26 <executions>
25.27 <execution>
25.28 <goals>
25.29 @@ -89,6 +89,14 @@
25.30 </configuration>
25.31 </plugin>
25.32 <plugin>
25.33 + <groupId>org.apache.maven.plugins</groupId>
25.34 + <artifactId>maven-deploy-plugin</artifactId>
25.35 + <version>2.7</version>
25.36 + <configuration>
25.37 + <skip>true</skip>
25.38 + </configuration>
25.39 + </plugin>
25.40 + <plugin>
25.41 <artifactId>maven-assembly-plugin</artifactId>
25.42 <version>2.4</version>
25.43 <executions>
25.44 @@ -113,13 +121,13 @@
25.45 <dependency>
25.46 <groupId>org.apidesign.bck2brwsr</groupId>
25.47 <artifactId>emul</artifactId>
25.48 - <version>0.6-SNAPSHOT</version>
25.49 + <version>${project.version}</version>
25.50 <classifier>rt</classifier>
25.51 </dependency>
25.52 <dependency>
25.53 <groupId>org.apidesign.bck2brwsr</groupId>
25.54 <artifactId>javaquery.api</artifactId>
25.55 - <version>0.6-SNAPSHOT</version>
25.56 + <version>${project.version}</version>
25.57 </dependency>
25.58 <dependency>
25.59 <groupId>org.testng</groupId>
25.60 @@ -130,8 +138,14 @@
25.61 <dependency>
25.62 <groupId>org.apidesign.bck2brwsr</groupId>
25.63 <artifactId>vmtest</artifactId>
25.64 - <version>0.6-SNAPSHOT</version>
25.65 + <version>${project.version}</version>
25.66 <scope>test</scope>
25.67 </dependency>
25.68 + <dependency>
25.69 + <groupId>org.apidesign.bck2brwsr</groupId>
25.70 + <artifactId>launcher.http</artifactId>
25.71 + <version>${project.version}</version>
25.72 + <scope>runtime</scope>
25.73 + </dependency>
25.74 </dependencies>
25.75 </project>
26.1 --- a/javaquery/demo-twitter/src/main/resources/org/apidesign/bck2brwsr/demo/twitter/index.html Fri May 24 16:45:32 2013 +0200
26.2 +++ b/javaquery/demo-twitter/src/main/resources/org/apidesign/bck2brwsr/demo/twitter/index.html Fri May 24 18:04:55 2013 +0200
26.3 @@ -90,7 +90,7 @@
26.4
26.5 <script src="bck2brwsr.js"></script>
26.6 <script type="text/javascript">
26.7 - var vm = bck2brwsr('demo-twitter-0.6-SNAPSHOT.jar');
26.8 + var vm = bck2brwsr('demo-twitter-0.8-SNAPSHOT.jar');
26.9 vm.loadClass('org.apidesign.bck2brwsr.demo.twitter.TwitterClient');
26.10 </script>
26.11
27.1 --- a/javaquery/pom.xml Fri May 24 16:45:32 2013 +0200
27.2 +++ b/javaquery/pom.xml Fri May 24 18:04:55 2013 +0200
27.3 @@ -4,11 +4,11 @@
27.4 <parent>
27.5 <artifactId>bck2brwsr</artifactId>
27.6 <groupId>org.apidesign</groupId>
27.7 - <version>0.6-SNAPSHOT</version>
27.8 + <version>0.8-SNAPSHOT</version>
27.9 </parent>
27.10 <groupId>org.apidesign.bck2brwsr</groupId>
27.11 <artifactId>javaquery</artifactId>
27.12 - <version>0.6-SNAPSHOT</version>
27.13 + <version>0.8-SNAPSHOT</version>
27.14 <packaging>pom</packaging>
27.15 <name>JavaQuery API and Demo</name>
27.16 <modules>
28.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
28.2 +++ b/launcher/api/pom.xml Fri May 24 18:04:55 2013 +0200
28.3 @@ -0,0 +1,31 @@
28.4 +<?xml version="1.0"?>
28.5 +<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">
28.6 + <modelVersion>4.0.0</modelVersion>
28.7 + <parent>
28.8 + <groupId>org.apidesign.bck2brwsr</groupId>
28.9 + <artifactId>launcher-pom</artifactId>
28.10 + <version>0.8-SNAPSHOT</version>
28.11 + </parent>
28.12 + <groupId>org.apidesign.bck2brwsr</groupId>
28.13 + <artifactId>launcher</artifactId>
28.14 + <version>0.8-SNAPSHOT</version>
28.15 + <name>Launcher API</name>
28.16 + <url>http://maven.apache.org</url>
28.17 + <properties>
28.18 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
28.19 + </properties>
28.20 + <build>
28.21 + <plugins>
28.22 + <plugin>
28.23 + <groupId>org.apache.maven.plugins</groupId>
28.24 + <artifactId>maven-javadoc-plugin</artifactId>
28.25 + <configuration>
28.26 + <subpackages>org.apidesign.bck2brwsr.launcher</subpackages>
28.27 + <skip>false</skip>
28.28 + </configuration>
28.29 + </plugin>
28.30 + </plugins>
28.31 + </build>
28.32 + <dependencies>
28.33 + </dependencies>
28.34 +</project>
29.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
29.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri May 24 18:04:55 2013 +0200
29.3 @@ -0,0 +1,115 @@
29.4 +/**
29.5 + * Back 2 Browser Bytecode Translator
29.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
29.7 + *
29.8 + * This program is free software: you can redistribute it and/or modify
29.9 + * it under the terms of the GNU General Public License as published by
29.10 + * the Free Software Foundation, version 2 of the License.
29.11 + *
29.12 + * This program is distributed in the hope that it will be useful,
29.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29.15 + * GNU General Public License for more details.
29.16 + *
29.17 + * You should have received a copy of the GNU General Public License
29.18 + * along with this program. Look for COPYING file in the top folder.
29.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
29.20 + */
29.21 +package org.apidesign.bck2brwsr.launcher;
29.22 +
29.23 +import java.io.IOException;
29.24 +import java.io.InputStream;
29.25 +import java.util.ArrayList;
29.26 +import java.util.List;
29.27 +import java.util.concurrent.CountDownLatch;
29.28 +import java.util.concurrent.TimeUnit;
29.29 +
29.30 +/** Represents individual method invocation, its context and its result.
29.31 + *
29.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
29.33 + */
29.34 +public final class InvocationContext {
29.35 + final CountDownLatch wait = new CountDownLatch(1);
29.36 + final Class<?> clazz;
29.37 + final String methodName;
29.38 + private final Launcher launcher;
29.39 + private String result;
29.40 + private Throwable exception;
29.41 + String html;
29.42 + final List<Resource> resources = new ArrayList<>();
29.43 +
29.44 + InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
29.45 + this.launcher = launcher;
29.46 + this.clazz = clazz;
29.47 + this.methodName = methodName;
29.48 + }
29.49 +
29.50 + /** An HTML fragment to be available for the execution. Useful primarily when
29.51 + * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
29.52 + * @param html the html fragment
29.53 + */
29.54 + public void setHtmlFragment(String html) {
29.55 + this.html = html;
29.56 + }
29.57 +
29.58 + /** HTTP resource to be available during execution. An invocation may
29.59 + * perform an HTTP query and obtain a resource relative to the page.
29.60 + */
29.61 + public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
29.62 + if (relativePath == null || mimeType == null || content == null || parameters == null) {
29.63 + throw new NullPointerException();
29.64 + }
29.65 + resources.add(new Resource(content, mimeType, relativePath, parameters));
29.66 + }
29.67 +
29.68 + /** Invokes the associated method.
29.69 + * @return the textual result of the invocation
29.70 + */
29.71 + public String invoke() throws IOException {
29.72 + launcher.runMethod(this);
29.73 + return toString();
29.74 + }
29.75 +
29.76 + /** Obtains textual result of the invocation.
29.77 + * @return text representing the exception or result value
29.78 + */
29.79 + @Override
29.80 + public String toString() {
29.81 + if (exception != null) {
29.82 + return exception.toString();
29.83 + }
29.84 + return result;
29.85 + }
29.86 +
29.87 + /**
29.88 + * @param timeOut
29.89 + * @throws InterruptedException
29.90 + */
29.91 + void await(long timeOut) throws InterruptedException {
29.92 + wait.await(timeOut, TimeUnit.MILLISECONDS);
29.93 + }
29.94 +
29.95 + void result(String r, Throwable e) {
29.96 + this.result = r;
29.97 + this.exception = e;
29.98 + wait.countDown();
29.99 + }
29.100 +
29.101 +
29.102 + static final class Resource {
29.103 + final InputStream httpContent;
29.104 + final String httpType;
29.105 + final String httpPath;
29.106 + final String[] parameters;
29.107 +
29.108 + Resource(InputStream httpContent, String httpType, String httpPath,
29.109 + String[] parameters
29.110 + ) {
29.111 + httpContent.mark(Integer.MAX_VALUE);
29.112 + this.httpContent = httpContent;
29.113 + this.httpType = httpType;
29.114 + this.httpPath = httpPath;
29.115 + this.parameters = parameters;
29.116 + }
29.117 + }
29.118 +}
30.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
30.2 +++ b/launcher/api/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Fri May 24 18:04:55 2013 +0200
30.3 @@ -0,0 +1,166 @@
30.4 +/**
30.5 + * Back 2 Browser Bytecode Translator
30.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
30.7 + *
30.8 + * This program is free software: you can redistribute it and/or modify
30.9 + * it under the terms of the GNU General Public License as published by
30.10 + * the Free Software Foundation, version 2 of the License.
30.11 + *
30.12 + * This program is distributed in the hope that it will be useful,
30.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30.15 + * GNU General Public License for more details.
30.16 + *
30.17 + * You should have received a copy of the GNU General Public License
30.18 + * along with this program. Look for COPYING file in the top folder.
30.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
30.20 + */
30.21 +package org.apidesign.bck2brwsr.launcher;
30.22 +
30.23 +import java.io.Closeable;
30.24 +import java.io.File;
30.25 +import java.io.IOException;
30.26 +import java.lang.reflect.Constructor;
30.27 +
30.28 +/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
30.29 + * Either in {@link Launcher#createJavaScript JavaScript engine},
30.30 + * or in {@link Launcher#createBrowser external browser}.
30.31 + * <p>
30.32 + * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
30.33 + * in an external browser served by internal HTTP server.
30.34 + *
30.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
30.36 + */
30.37 +public abstract class Launcher {
30.38 +
30.39 + Launcher() {
30.40 + }
30.41 +
30.42 + /** Initializes the launcher. This may mean starting a web browser or
30.43 + * initializing execution engine.
30.44 + * @throws IOException if something goes wrong
30.45 + */
30.46 + public abstract void initialize() throws IOException;
30.47 +
30.48 + /** Shuts down the launcher.
30.49 + * @throws IOException if something goes wrong
30.50 + */
30.51 + public abstract void shutdown() throws IOException;
30.52 +
30.53 +
30.54 + /** Builds an invocation context. The context can later be customized
30.55 + * and {@link InvocationContext#invoke() invoked}.
30.56 + *
30.57 + * @param clazz the class to execute method from
30.58 + * @param method the method to execute
30.59 + * @return the context pointing to the selected method
30.60 + */
30.61 + public InvocationContext createInvocation(Class<?> clazz, String method) {
30.62 + return new InvocationContext(this, clazz, method);
30.63 + }
30.64 +
30.65 +
30.66 + /** Creates launcher that uses internal JavaScript engine (Rhino).
30.67 + * @return the launcher
30.68 + */
30.69 + public static Launcher createJavaScript() {
30.70 + try {
30.71 + Class<?> c = loadClass("org.apidesign.bck2brwsr.launcher.JSLauncher");
30.72 + return (Launcher) c.newInstance();
30.73 + } catch (Exception ex) {
30.74 + throw new IllegalStateException("Please include org.apidesign.bck2brwsr:launcher.html dependency!", ex);
30.75 + }
30.76 + }
30.77 +
30.78 + /** Creates launcher that is using external browser.
30.79 + *
30.80 + * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
30.81 + * or a string to execute in an external process (with a parameter to the URL)
30.82 + * @return launcher executing in external browser.
30.83 + */
30.84 + public static Launcher createBrowser(String cmd) {
30.85 + String msg = "Trying to create browser '" + cmd + "'";
30.86 + try {
30.87 + Class<?> c;
30.88 + if ("fxbrwsr".equals(cmd)) { // NOI18N
30.89 + msg = "Please include org.apidesign.bck2brwsr:launcher.fx dependency!";
30.90 + c = loadClass("org.apidesign.bck2brwsr.launcher.FXBrwsrLauncher"); // NOI18N
30.91 + } else {
30.92 + msg = "Please include org.apidesign.bck2brwsr:launcher.html dependency!";
30.93 + c = loadClass("org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher"); // NOI18N
30.94 + if ("bck2brwsr".equals(cmd)) { // NOI18N
30.95 + // use default executable
30.96 + cmd = null;
30.97 + }
30.98 + }
30.99 + Constructor<?> cnstr = c.getConstructor(String.class);
30.100 + return (Launcher) cnstr.newInstance(cmd);
30.101 + } catch (Exception ex) {
30.102 + throw new IllegalStateException(msg, ex);
30.103 + }
30.104 + }
30.105 +
30.106 + /** Starts an HTTP server which provides access to classes and resources
30.107 + * available in the <code>classes</code> URL and shows a start page
30.108 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
30.109 + * provide classloader. Opens a browser with URL showing the start page.
30.110 + *
30.111 + * @param classes classloader offering access to classes and resources
30.112 + * @param startpage page to show in the browser
30.113 + * @return interface that allows one to stop the server
30.114 + * @throws IOException if something goes wrong
30.115 + */
30.116 + public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
30.117 + return showURL(null, classes, startpage);
30.118 + }
30.119 + /** Starts an HTTP server which provides access to classes and resources
30.120 + * available in the <code>classes</code> URL and shows a start page
30.121 + * available as {@link ClassLoader#getResource(java.lang.String)} from the
30.122 + * provide classloader. Opens a browser with URL showing the start page.
30.123 + *
30.124 + * @param brwsr name of browser to use or <code>null</code>
30.125 + * @param classes classloader offering access to classes and resources
30.126 + * @param startpage page to show in the browser
30.127 + * @return interface that allows one to stop the server
30.128 + * @throws IOException if something goes wrong
30.129 + * @since 0.7
30.130 + */
30.131 + public static Closeable showURL(String brwsr, ClassLoader classes, String startpage) throws IOException {
30.132 + Launcher l = createBrowser(brwsr);
30.133 + l.addClassLoader(classes);
30.134 + l.showURL(startpage);
30.135 + return (Closeable) l;
30.136 + }
30.137 + /** Starts an HTTP server which provides access to certain directory.
30.138 + * The <code>startpage</code> should be relative location inside the root
30.139 + * directory. Opens a browser with URL showing the start page.
30.140 + *
30.141 + * @param directory the root directory on disk
30.142 + * @param startpage relative path from the root to the page
30.143 + * @exception IOException if something goes wrong.
30.144 + */
30.145 + public static Closeable showDir(File directory, String startpage) throws IOException {
30.146 + Launcher l = createBrowser(null);
30.147 + l.showDirectory(directory, startpage);
30.148 + return (Closeable) l;
30.149 + }
30.150 +
30.151 + abstract InvocationContext runMethod(InvocationContext c) throws IOException;
30.152 +
30.153 +
30.154 + private static Class<?> loadClass(String cn) throws ClassNotFoundException {
30.155 + return Launcher.class.getClassLoader().loadClass(cn);
30.156 + }
30.157 +
30.158 + void showDirectory(File directory, String startpage) throws IOException {
30.159 + throw new UnsupportedOperationException();
30.160 + }
30.161 +
30.162 + void showURL(String startpage) throws IOException {
30.163 + throw new UnsupportedOperationException();
30.164 + }
30.165 +
30.166 + void addClassLoader(ClassLoader classes) {
30.167 + throw new UnsupportedOperationException();
30.168 + }
30.169 +}
31.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
31.2 +++ b/launcher/fx/pom.xml Fri May 24 18:04:55 2013 +0200
31.3 @@ -0,0 +1,58 @@
31.4 +<?xml version="1.0"?>
31.5 +<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">
31.6 + <modelVersion>4.0.0</modelVersion>
31.7 + <parent>
31.8 + <groupId>org.apidesign.bck2brwsr</groupId>
31.9 + <artifactId>launcher-pom</artifactId>
31.10 + <version>0.8-SNAPSHOT</version>
31.11 + </parent>
31.12 + <groupId>org.apidesign.bck2brwsr</groupId>
31.13 + <artifactId>launcher.fx</artifactId>
31.14 + <version>0.8-SNAPSHOT</version>
31.15 + <name>FXBrwsr Launcher</name>
31.16 + <url>http://maven.apache.org</url>
31.17 + <build>
31.18 + <plugins>
31.19 + <plugin>
31.20 + <groupId>org.apache.maven.plugins</groupId>
31.21 + <artifactId>maven-compiler-plugin</artifactId>
31.22 + <version>2.3.2</version>
31.23 + <configuration>
31.24 + <source>1.7</source>
31.25 + <target>1.7</target>
31.26 + </configuration>
31.27 + </plugin>
31.28 + <plugin>
31.29 + <groupId>org.apache.maven.plugins</groupId>
31.30 + <artifactId>maven-javadoc-plugin</artifactId>
31.31 + <configuration>
31.32 + <subpackages>org.apidesign.bck2brwsr.launcher.fx</subpackages>
31.33 + <skip>false</skip>
31.34 + </configuration>
31.35 + </plugin>
31.36 + </plugins>
31.37 + </build>
31.38 + <properties>
31.39 + <jfxrt.jar>${java.home}/lib/jfxrt.jar</jfxrt.jar>
31.40 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
31.41 + </properties>
31.42 + <dependencies>
31.43 + <dependency>
31.44 + <groupId>${project.groupId}</groupId>
31.45 + <artifactId>launcher</artifactId>
31.46 + <version>${project.version}</version>
31.47 + </dependency>
31.48 + <dependency>
31.49 + <groupId>org.glassfish.grizzly</groupId>
31.50 + <artifactId>grizzly-http-server</artifactId>
31.51 + <version>${grizzly.version}</version>
31.52 + </dependency>
31.53 + <dependency>
31.54 + <groupId>com.oracle</groupId>
31.55 + <artifactId>javafx</artifactId>
31.56 + <version>2.2</version>
31.57 + <scope>system</scope>
31.58 + <systemPath>${jfxrt.jar}</systemPath>
31.59 + </dependency>
31.60 + </dependencies>
31.61 +</project>
32.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
32.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/BaseHTTPLauncher.java Fri May 24 18:04:55 2013 +0200
32.3 @@ -0,0 +1,634 @@
32.4 +/**
32.5 + * Back 2 Browser Bytecode Translator
32.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
32.7 + *
32.8 + * This program is free software: you can redistribute it and/or modify
32.9 + * it under the terms of the GNU General Public License as published by
32.10 + * the Free Software Foundation, version 2 of the License.
32.11 + *
32.12 + * This program is distributed in the hope that it will be useful,
32.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32.15 + * GNU General Public License for more details.
32.16 + *
32.17 + * You should have received a copy of the GNU General Public License
32.18 + * along with this program. Look for COPYING file in the top folder.
32.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
32.20 + */
32.21 +package org.apidesign.bck2brwsr.launcher;
32.22 +
32.23 +import java.io.Closeable;
32.24 +import java.io.File;
32.25 +import java.io.IOException;
32.26 +import java.io.InputStream;
32.27 +import java.io.InterruptedIOException;
32.28 +import java.io.OutputStream;
32.29 +import java.io.Reader;
32.30 +import java.io.UnsupportedEncodingException;
32.31 +import java.io.Writer;
32.32 +import java.net.URI;
32.33 +import java.net.URISyntaxException;
32.34 +import java.net.URL;
32.35 +import java.util.ArrayList;
32.36 +import java.util.Arrays;
32.37 +import java.util.Enumeration;
32.38 +import java.util.LinkedHashSet;
32.39 +import java.util.List;
32.40 +import java.util.Set;
32.41 +import java.util.concurrent.BlockingQueue;
32.42 +import java.util.concurrent.Callable;
32.43 +import java.util.concurrent.CountDownLatch;
32.44 +import java.util.concurrent.LinkedBlockingQueue;
32.45 +import java.util.concurrent.TimeUnit;
32.46 +import java.util.logging.Level;
32.47 +import java.util.logging.Logger;
32.48 +import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
32.49 +import org.glassfish.grizzly.PortRange;
32.50 +import org.glassfish.grizzly.http.server.HttpHandler;
32.51 +import org.glassfish.grizzly.http.server.HttpServer;
32.52 +import org.glassfish.grizzly.http.server.NetworkListener;
32.53 +import org.glassfish.grizzly.http.server.Request;
32.54 +import org.glassfish.grizzly.http.server.Response;
32.55 +import org.glassfish.grizzly.http.server.ServerConfiguration;
32.56 +import org.glassfish.grizzly.http.util.HttpStatus;
32.57 +import org.glassfish.grizzly.threadpool.ThreadPoolConfig;
32.58 +
32.59 +/**
32.60 + * Lightweight server to launch Bck2Brwsr applications and tests.
32.61 + * Supports execution in native browser as well as Java's internal
32.62 + * execution engine.
32.63 + */
32.64 +abstract class BaseHTTPLauncher extends Launcher implements Closeable, Callable<HttpServer> {
32.65 + static final Logger LOG = Logger.getLogger(BaseHTTPLauncher.class.getName());
32.66 + private static final InvocationContext END = new InvocationContext(null, null, null);
32.67 + private final Set<ClassLoader> loaders = new LinkedHashSet<>();
32.68 + private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
32.69 + private long timeOut;
32.70 + private final Res resources = new Res();
32.71 + private final String cmd;
32.72 + private Object[] brwsr;
32.73 + private HttpServer server;
32.74 + private CountDownLatch wait;
32.75 +
32.76 + public BaseHTTPLauncher(String cmd) {
32.77 + this.cmd = cmd;
32.78 + addClassLoader(BaseHTTPLauncher.class.getClassLoader());
32.79 + setTimeout(180000);
32.80 + }
32.81 +
32.82 + @Override
32.83 + InvocationContext runMethod(InvocationContext c) throws IOException {
32.84 + loaders.add(c.clazz.getClassLoader());
32.85 + methods.add(c);
32.86 + try {
32.87 + c.await(timeOut);
32.88 + } catch (InterruptedException ex) {
32.89 + throw new IOException(ex);
32.90 + }
32.91 + return c;
32.92 + }
32.93 +
32.94 + public void setTimeout(long ms) {
32.95 + timeOut = ms;
32.96 + }
32.97 +
32.98 + public void addClassLoader(ClassLoader url) {
32.99 + this.loaders.add(url);
32.100 + }
32.101 +
32.102 + ClassLoader[] loaders() {
32.103 + return loaders.toArray(new ClassLoader[loaders.size()]);
32.104 + }
32.105 +
32.106 + public void showURL(String startpage) throws IOException {
32.107 + if (!startpage.startsWith("/")) {
32.108 + startpage = "/" + startpage;
32.109 + }
32.110 + HttpServer s = initServer(".", true);
32.111 + int last = startpage.lastIndexOf('/');
32.112 + String prefix = startpage.substring(0, last);
32.113 + String simpleName = startpage.substring(last);
32.114 + s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
32.115 + server = s;
32.116 + try {
32.117 + launchServerAndBrwsr(s, simpleName);
32.118 + } catch (URISyntaxException | InterruptedException ex) {
32.119 + throw new IOException(ex);
32.120 + }
32.121 + }
32.122 +
32.123 + void showDirectory(File dir, String startpage) throws IOException {
32.124 + if (!startpage.startsWith("/")) {
32.125 + startpage = "/" + startpage;
32.126 + }
32.127 + HttpServer s = initServer(dir.getPath(), false);
32.128 + try {
32.129 + launchServerAndBrwsr(s, startpage);
32.130 + } catch (URISyntaxException | InterruptedException ex) {
32.131 + throw new IOException(ex);
32.132 + }
32.133 + }
32.134 +
32.135 + @Override
32.136 + public void initialize() throws IOException {
32.137 + try {
32.138 + executeInBrowser();
32.139 + } catch (InterruptedException ex) {
32.140 + final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
32.141 + iio.initCause(ex);
32.142 + throw iio;
32.143 + } catch (Exception ex) {
32.144 + if (ex instanceof IOException) {
32.145 + throw (IOException)ex;
32.146 + }
32.147 + if (ex instanceof RuntimeException) {
32.148 + throw (RuntimeException)ex;
32.149 + }
32.150 + throw new IOException(ex);
32.151 + }
32.152 + }
32.153 +
32.154 + private HttpServer initServer(String path, boolean addClasses) throws IOException {
32.155 + HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
32.156 +
32.157 + ThreadPoolConfig fewThreads = ThreadPoolConfig.defaultConfig().copy().
32.158 + setPoolName("Fx/Bck2 Brwsr").
32.159 + setCorePoolSize(1).
32.160 + setMaxPoolSize(5);
32.161 + ThreadPoolConfig oneKernel = ThreadPoolConfig.defaultConfig().copy().
32.162 + setPoolName("Kernel Fx/Bck2").
32.163 + setCorePoolSize(1).
32.164 + setMaxPoolSize(3);
32.165 + for (NetworkListener nl : s.getListeners()) {
32.166 + nl.getTransport().setWorkerThreadPoolConfig(fewThreads);
32.167 + nl.getTransport().setKernelThreadPoolConfig(oneKernel);
32.168 + }
32.169 +
32.170 + final ServerConfiguration conf = s.getServerConfiguration();
32.171 + if (addClasses) {
32.172 + conf.addHttpHandler(new VM(), "/bck2brwsr.js");
32.173 + conf.addHttpHandler(new Classes(resources), "/classes/");
32.174 + }
32.175 + return s;
32.176 + }
32.177 +
32.178 + private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
32.179 + wait = new CountDownLatch(1);
32.180 + server = initServer(".", true);
32.181 + final ServerConfiguration conf = server.getServerConfiguration();
32.182 +
32.183 + class DynamicResourceHandler extends HttpHandler {
32.184 + private final InvocationContext ic;
32.185 + public DynamicResourceHandler(InvocationContext ic) {
32.186 + if (ic == null || ic.resources.isEmpty()) {
32.187 + throw new NullPointerException();
32.188 + }
32.189 + this.ic = ic;
32.190 + for (Resource r : ic.resources) {
32.191 + conf.addHttpHandler(this, r.httpPath);
32.192 + }
32.193 + }
32.194 +
32.195 + public void close() {
32.196 + conf.removeHttpHandler(this);
32.197 + }
32.198 +
32.199 + @Override
32.200 + public void service(Request request, Response response) throws Exception {
32.201 + for (Resource r : ic.resources) {
32.202 + if (r.httpPath.equals(request.getRequestURI())) {
32.203 + LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
32.204 + response.setContentType(r.httpType);
32.205 + r.httpContent.reset();
32.206 + String[] params = null;
32.207 + if (r.parameters.length != 0) {
32.208 + params = new String[r.parameters.length];
32.209 + for (int i = 0; i < r.parameters.length; i++) {
32.210 + params[i] = request.getParameter(r.parameters[i]);
32.211 + if (params[i] == null) {
32.212 + if ("http.method".equals(r.parameters[i])) {
32.213 + params[i] = request.getMethod().toString();
32.214 + } else if ("http.requestBody".equals(r.parameters[i])) {
32.215 + Reader rdr = request.getReader();
32.216 + StringBuilder sb = new StringBuilder();
32.217 + for (;;) {
32.218 + int ch = rdr.read();
32.219 + if (ch == -1) {
32.220 + break;
32.221 + }
32.222 + sb.append((char)ch);
32.223 + }
32.224 + params[i] = sb.toString();
32.225 + }
32.226 + }
32.227 + if (params[i] == null) {
32.228 + params[i] = "null";
32.229 + }
32.230 + }
32.231 + }
32.232 +
32.233 + copyStream(r.httpContent, response.getOutputStream(), null, params);
32.234 + }
32.235 + }
32.236 + }
32.237 + }
32.238 +
32.239 + conf.addHttpHandler(new Page(resources, harnessResource()), "/execute");
32.240 +
32.241 + conf.addHttpHandler(new HttpHandler() {
32.242 + int cnt;
32.243 + List<InvocationContext> cases = new ArrayList<>();
32.244 + DynamicResourceHandler prev;
32.245 + @Override
32.246 + public void service(Request request, Response response) throws Exception {
32.247 + String id = request.getParameter("request");
32.248 + String value = request.getParameter("result");
32.249 + if (value != null && value.indexOf((char)0xC5) != -1) {
32.250 + value = toUTF8(value);
32.251 + }
32.252 +
32.253 +
32.254 + InvocationContext mi = null;
32.255 + int caseNmbr = -1;
32.256 +
32.257 + if (id != null && value != null) {
32.258 + LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
32.259 + value = decodeURL(value);
32.260 + int indx = Integer.parseInt(id);
32.261 + cases.get(indx).result(value, null);
32.262 + if (++indx < cases.size()) {
32.263 + mi = cases.get(indx);
32.264 + LOG.log(Level.INFO, "Re-executing case {0}", indx);
32.265 + caseNmbr = indx;
32.266 + }
32.267 + } else {
32.268 + if (!cases.isEmpty()) {
32.269 + LOG.info("Re-executing test cases");
32.270 + mi = cases.get(0);
32.271 + caseNmbr = 0;
32.272 + }
32.273 + }
32.274 +
32.275 + if (prev != null) {
32.276 + prev.close();
32.277 + prev = null;
32.278 + }
32.279 +
32.280 + if (mi == null) {
32.281 + mi = methods.take();
32.282 + caseNmbr = cnt++;
32.283 + }
32.284 + if (mi == END) {
32.285 + response.getWriter().write("");
32.286 + wait.countDown();
32.287 + cnt = 0;
32.288 + LOG.log(Level.INFO, "End of data reached. Exiting.");
32.289 + return;
32.290 + }
32.291 +
32.292 + if (!mi.resources.isEmpty()) {
32.293 + prev = new DynamicResourceHandler(mi);
32.294 + }
32.295 +
32.296 + cases.add(mi);
32.297 + final String cn = mi.clazz.getName();
32.298 + final String mn = mi.methodName;
32.299 + LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
32.300 + response.getWriter().write("{"
32.301 + + "className: '" + cn + "', "
32.302 + + "methodName: '" + mn + "', "
32.303 + + "request: " + caseNmbr
32.304 + );
32.305 + if (mi.html != null) {
32.306 + response.getWriter().write(", html: '");
32.307 + response.getWriter().write(encodeJSON(mi.html));
32.308 + response.getWriter().write("'");
32.309 + }
32.310 + response.getWriter().write("}");
32.311 + }
32.312 + }, "/data");
32.313 +
32.314 + this.brwsr = launchServerAndBrwsr(server, "/execute");
32.315 + }
32.316 +
32.317 + private static String encodeJSON(String in) {
32.318 + StringBuilder sb = new StringBuilder();
32.319 + for (int i = 0; i < in.length(); i++) {
32.320 + char ch = in.charAt(i);
32.321 + if (ch < 32 || ch == '\'' || ch == '"') {
32.322 + sb.append("\\u");
32.323 + String hs = "0000" + Integer.toHexString(ch);
32.324 + hs = hs.substring(hs.length() - 4);
32.325 + sb.append(hs);
32.326 + } else {
32.327 + sb.append(ch);
32.328 + }
32.329 + }
32.330 + return sb.toString();
32.331 + }
32.332 +
32.333 + @Override
32.334 + public void shutdown() throws IOException {
32.335 + methods.offer(END);
32.336 + for (;;) {
32.337 + int prev = methods.size();
32.338 + try {
32.339 + if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
32.340 + break;
32.341 + }
32.342 + } catch (InterruptedException ex) {
32.343 + throw new IOException(ex);
32.344 + }
32.345 + if (prev == methods.size()) {
32.346 + LOG.log(
32.347 + Level.WARNING,
32.348 + "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
32.349 + methods.size()
32.350 + );
32.351 + break;
32.352 + }
32.353 + LOG.log(Level.INFO,
32.354 + "Timeout, but tests got from {0} to {1}. Trying again.",
32.355 + new Object[]{prev, methods.size()}
32.356 + );
32.357 + }
32.358 + stopServerAndBrwsr(server, brwsr);
32.359 + }
32.360 +
32.361 + static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
32.362 + for (;;) {
32.363 + int ch = is.read();
32.364 + if (ch == -1) {
32.365 + break;
32.366 + }
32.367 + if (ch == '$' && params.length > 0) {
32.368 + int cnt = is.read() - '0';
32.369 + if (baseURL != null && cnt == 'U' - '0') {
32.370 + os.write(baseURL.getBytes("UTF-8"));
32.371 + } else {
32.372 + if (cnt >= 0 && cnt < params.length) {
32.373 + os.write(params[cnt].getBytes("UTF-8"));
32.374 + } else {
32.375 + os.write('$');
32.376 + os.write(cnt + '0');
32.377 + }
32.378 + }
32.379 + } else {
32.380 + os.write(ch);
32.381 + }
32.382 + }
32.383 + }
32.384 +
32.385 + private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
32.386 + server.start();
32.387 + NetworkListener listener = server.getListeners().iterator().next();
32.388 + int port = listener.getPort();
32.389 +
32.390 + URI uri = new URI("http://localhost:" + port + page);
32.391 + return showBrwsr(uri);
32.392 + }
32.393 + private static String toUTF8(String value) throws UnsupportedEncodingException {
32.394 + byte[] arr = new byte[value.length()];
32.395 + for (int i = 0; i < arr.length; i++) {
32.396 + arr[i] = (byte)value.charAt(i);
32.397 + }
32.398 + return new String(arr, "UTF-8");
32.399 + }
32.400 +
32.401 + private static String decodeURL(String s) {
32.402 + for (;;) {
32.403 + int pos = s.indexOf('%');
32.404 + if (pos == -1) {
32.405 + return s;
32.406 + }
32.407 + int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
32.408 + s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
32.409 + }
32.410 + }
32.411 +
32.412 + private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
32.413 + if (brwsr == null) {
32.414 + return;
32.415 + }
32.416 + Process process = (Process)brwsr[0];
32.417 +
32.418 + server.stop();
32.419 + InputStream stdout = process.getInputStream();
32.420 + InputStream stderr = process.getErrorStream();
32.421 + drain("StdOut", stdout);
32.422 + drain("StdErr", stderr);
32.423 + process.destroy();
32.424 + int res;
32.425 + try {
32.426 + res = process.waitFor();
32.427 + } catch (InterruptedException ex) {
32.428 + throw new IOException(ex);
32.429 + }
32.430 + LOG.log(Level.INFO, "Exit code: {0}", res);
32.431 +
32.432 + deleteTree((File)brwsr[1]);
32.433 + }
32.434 +
32.435 + private static void drain(String name, InputStream is) throws IOException {
32.436 + int av = is.available();
32.437 + if (av > 0) {
32.438 + StringBuilder sb = new StringBuilder();
32.439 + sb.append("v== ").append(name).append(" ==v\n");
32.440 + while (av-- > 0) {
32.441 + sb.append((char)is.read());
32.442 + }
32.443 + sb.append("\n^== ").append(name).append(" ==^");
32.444 + LOG.log(Level.INFO, sb.toString());
32.445 + }
32.446 + }
32.447 +
32.448 + private void deleteTree(File file) {
32.449 + if (file == null) {
32.450 + return;
32.451 + }
32.452 + File[] arr = file.listFiles();
32.453 + if (arr != null) {
32.454 + for (File s : arr) {
32.455 + deleteTree(s);
32.456 + }
32.457 + }
32.458 + file.delete();
32.459 + }
32.460 +
32.461 + @Override
32.462 + public HttpServer call() throws Exception {
32.463 + return server;
32.464 + }
32.465 +
32.466 + @Override
32.467 + public void close() throws IOException {
32.468 + shutdown();
32.469 + }
32.470 +
32.471 + protected Object[] showBrwsr(URI uri) throws IOException {
32.472 + LOG.log(Level.INFO, "Showing {0}", uri);
32.473 + if (cmd == null) {
32.474 + try {
32.475 + LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
32.476 + System.getProperty("java.vm.name"),
32.477 + System.getProperty("java.vm.vendor"),
32.478 + System.getProperty("java.vm.version"),
32.479 + });
32.480 + java.awt.Desktop.getDesktop().browse(uri);
32.481 + LOG.log(Level.INFO, "Desktop.browse successfully finished");
32.482 + return null;
32.483 + } catch (UnsupportedOperationException ex) {
32.484 + LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
32.485 + LOG.log(Level.FINE, null, ex);
32.486 + }
32.487 + }
32.488 + {
32.489 + String cmdName = cmd == null ? "xdg-open" : cmd;
32.490 + String[] cmdArr = {
32.491 + cmdName, uri.toString()
32.492 + };
32.493 + LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
32.494 + final Process process = Runtime.getRuntime().exec(cmdArr);
32.495 + return new Object[] { process, null };
32.496 + }
32.497 + }
32.498 +
32.499 + abstract void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException;
32.500 + abstract String harnessResource();
32.501 +
32.502 + class Res {
32.503 + public InputStream get(String resource) throws IOException {
32.504 + URL u = null;
32.505 + for (ClassLoader l : loaders) {
32.506 + Enumeration<URL> en = l.getResources(resource);
32.507 + while (en.hasMoreElements()) {
32.508 + u = en.nextElement();
32.509 + if (u.toExternalForm().matches("^.*emul.*rt\\.jar.*$")) {
32.510 + return u.openStream();
32.511 + }
32.512 + }
32.513 + }
32.514 + if (u != null) {
32.515 + if (u.toExternalForm().contains("rt.jar")) {
32.516 + LOG.log(Level.WARNING, "Fallback to bootclasspath for {0}", u);
32.517 + }
32.518 + return u.openStream();
32.519 + }
32.520 + throw new IOException("Can't find " + resource);
32.521 + }
32.522 + }
32.523 +
32.524 + private static class Page extends HttpHandler {
32.525 + final String resource;
32.526 + private final String[] args;
32.527 + private final Res res;
32.528 +
32.529 + public Page(Res res, String resource, String... args) {
32.530 + this.res = res;
32.531 + this.resource = resource;
32.532 + this.args = args.length == 0 ? new String[] { "$0" } : args;
32.533 + }
32.534 +
32.535 + @Override
32.536 + public void service(Request request, Response response) throws Exception {
32.537 + String r = computePage(request);
32.538 + if (r.startsWith("/")) {
32.539 + r = r.substring(1);
32.540 + }
32.541 + String[] replace = {};
32.542 + if (r.endsWith(".html")) {
32.543 + response.setContentType("text/html");
32.544 + LOG.info("Content type text/html");
32.545 + replace = args;
32.546 + }
32.547 + if (r.endsWith(".xhtml")) {
32.548 + response.setContentType("application/xhtml+xml");
32.549 + LOG.info("Content type application/xhtml+xml");
32.550 + replace = args;
32.551 + }
32.552 + OutputStream os = response.getOutputStream();
32.553 + try (InputStream is = res.get(r)) {
32.554 + copyStream(is, os, request.getRequestURL().toString(), replace);
32.555 + } catch (IOException ex) {
32.556 + response.setDetailMessage(ex.getLocalizedMessage());
32.557 + response.setError();
32.558 + response.setStatus(404);
32.559 + }
32.560 + }
32.561 +
32.562 + protected String computePage(Request request) {
32.563 + String r = resource;
32.564 + if (r == null) {
32.565 + r = request.getHttpHandlerPath();
32.566 + }
32.567 + return r;
32.568 + }
32.569 + }
32.570 +
32.571 + private static class SubTree extends Page {
32.572 +
32.573 + public SubTree(Res res, String resource, String... args) {
32.574 + super(res, resource, args);
32.575 + }
32.576 +
32.577 + @Override
32.578 + protected String computePage(Request request) {
32.579 + return resource + request.getHttpHandlerPath();
32.580 + }
32.581 +
32.582 +
32.583 + }
32.584 +
32.585 + private class VM extends HttpHandler {
32.586 + @Override
32.587 + public void service(Request request, Response response) throws Exception {
32.588 + response.setCharacterEncoding("UTF-8");
32.589 + response.setContentType("text/javascript");
32.590 + StringBuilder sb = new StringBuilder();
32.591 + generateBck2BrwsrJS(sb, BaseHTTPLauncher.this.resources);
32.592 + response.getWriter().write(sb.toString());
32.593 + }
32.594 + }
32.595 +
32.596 + private static class Classes extends HttpHandler {
32.597 + private final Res loader;
32.598 +
32.599 + public Classes(Res loader) {
32.600 + this.loader = loader;
32.601 + }
32.602 +
32.603 + @Override
32.604 + public void service(Request request, Response response) throws Exception {
32.605 + String res = request.getHttpHandlerPath();
32.606 + if (res.startsWith("/")) {
32.607 + res = res.substring(1);
32.608 + }
32.609 + try (InputStream is = loader.get(res)) {
32.610 + response.setContentType("text/javascript");
32.611 + Writer w = response.getWriter();
32.612 + w.append("[");
32.613 + for (int i = 0;; i++) {
32.614 + int b = is.read();
32.615 + if (b == -1) {
32.616 + break;
32.617 + }
32.618 + if (i > 0) {
32.619 + w.append(", ");
32.620 + }
32.621 + if (i % 20 == 0) {
32.622 + w.write("\n");
32.623 + }
32.624 + if (b > 127) {
32.625 + b = b - 256;
32.626 + }
32.627 + w.append(Integer.toString(b));
32.628 + }
32.629 + w.append("\n]");
32.630 + } catch (IOException ex) {
32.631 + response.setStatus(HttpStatus.NOT_FOUND_404);
32.632 + response.setError();
32.633 + response.setDetailMessage(ex.getMessage());
32.634 + }
32.635 + }
32.636 + }
32.637 +}
33.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
33.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/FXBrwsrLauncher.java Fri May 24 18:04:55 2013 +0200
33.3 @@ -0,0 +1,161 @@
33.4 +/**
33.5 + * Back 2 Browser Bytecode Translator
33.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
33.7 + *
33.8 + * This program is free software: you can redistribute it and/or modify
33.9 + * it under the terms of the GNU General Public License as published by
33.10 + * the Free Software Foundation, version 2 of the License.
33.11 + *
33.12 + * This program is distributed in the hope that it will be useful,
33.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33.15 + * GNU General Public License for more details.
33.16 + *
33.17 + * You should have received a copy of the GNU General Public License
33.18 + * along with this program. Look for COPYING file in the top folder.
33.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
33.20 + */
33.21 +package org.apidesign.bck2brwsr.launcher;
33.22 +
33.23 +import org.apidesign.bck2brwsr.launcher.fximpl.FXBrwsr;
33.24 +import java.io.IOException;
33.25 +import java.io.InputStream;
33.26 +import java.lang.reflect.Method;
33.27 +import java.net.URI;
33.28 +import java.net.URL;
33.29 +import java.net.URLClassLoader;
33.30 +import java.util.Enumeration;
33.31 +import java.util.List;
33.32 +
33.33 +import java.util.concurrent.Executors;
33.34 +import java.util.jar.Manifest;
33.35 +import java.util.logging.Level;
33.36 +import java.util.logging.Logger;
33.37 +import javafx.application.Platform;
33.38 +import org.apidesign.bck2brwsr.launcher.fximpl.JVMBridge;
33.39 +
33.40 +/**
33.41 + *
33.42 + * @author Jaroslav Tulach <jtulach@netbeans.org>
33.43 + */
33.44 +final class FXBrwsrLauncher extends BaseHTTPLauncher {
33.45 + private static final Logger LOG = Logger.getLogger(FXBrwsrLauncher.class.getName());
33.46 + static {
33.47 + try {
33.48 + Method m = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
33.49 + m.setAccessible(true);
33.50 + URL l = new URL("file://" + System.getProperty("java.home") + "/lib/jfxrt.jar");
33.51 + LOG.log(Level.INFO, "url : {0}", l);
33.52 + m.invoke(ClassLoader.getSystemClassLoader(), l);
33.53 + } catch (Exception ex) {
33.54 + throw new LinkageError("Can't add jfxrt.jar on the classpath", ex);
33.55 + }
33.56 + }
33.57 +
33.58 + public FXBrwsrLauncher(String ignore) {
33.59 + super(null);
33.60 + }
33.61 +
33.62 + @Override
33.63 + protected Object[] showBrwsr(final URI url) throws IOException {
33.64 + try {
33.65 + LOG.log(Level.INFO, "showBrwsr for {0}", url);
33.66 + JVMBridge.registerClassLoaders(loaders());
33.67 + LOG.info("About to launch WebView");
33.68 + Executors.newSingleThreadExecutor().submit(new Runnable() {
33.69 + @Override
33.70 + public void run() {
33.71 + LOG.log(Level.INFO, "In FX thread. Launching!");
33.72 + try {
33.73 + FXBrwsr.launch(FXBrwsr.class, url.toString());
33.74 + LOG.log(Level.INFO, "Launcher is back. Closing");
33.75 + close();
33.76 + System.exit(0);
33.77 + } catch (Throwable ex) {
33.78 + LOG.log(Level.WARNING, "Error launching Web View", ex);
33.79 + }
33.80 + }
33.81 + });
33.82 + } catch (Throwable ex) {
33.83 + LOG.log(Level.WARNING, "Can't open WebView", ex);
33.84 + }
33.85 + return null;
33.86 + }
33.87 +
33.88 + @Override
33.89 + void generateBck2BrwsrJS(StringBuilder sb, Res loader) throws IOException {
33.90 + sb.append("(function() {\n"
33.91 + + " var impl = this.bck2brwsr;\n"
33.92 + + " this.bck2brwsr = function() { return impl; };\n");
33.93 + if (isDebugged()) {
33.94 + sb.append("var scr = window.document.createElement('script');\n");
33.95 + sb.append("scr.type = 'text/javascript';\n");
33.96 + sb.append("scr.src = 'https://getfirebug.com/firebug-lite.js';\n");
33.97 + sb.append("scr.text = '{ startOpened: true }';\n");
33.98 + sb.append("var head = window.document.getElementsByTagName('head')[0];");
33.99 + sb.append("head.appendChild(scr);\n");
33.100 + sb.append("var html = window.document.getElementsByTagName('html')[0];");
33.101 + sb.append("html.debug = true;\n");
33.102 + }
33.103 +
33.104 + sb.append("})(window);\n");
33.105 + JVMBridge.onBck2BrwsrLoad();
33.106 + }
33.107 +
33.108 + @Override
33.109 + public void close() throws IOException {
33.110 + super.close();
33.111 + Platform.exit();
33.112 + }
33.113 +
33.114 + String harnessResource() {
33.115 + return "org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml";
33.116 + }
33.117 +
33.118 + public static void main(String... args) throws IOException {
33.119 + String startPage = null;
33.120 +
33.121 + final ClassLoader cl = FXBrwsrLauncher.class.getClassLoader();
33.122 + startPage = findStartPage(cl, startPage);
33.123 + if (startPage == null) {
33.124 + throw new NullPointerException("Can't find StartPage tag in manifests!");
33.125 + }
33.126 +
33.127 + Launcher.showURL("fxbrwsr", cl, startPage);
33.128 + }
33.129 +
33.130 + private static String findStartPage(final ClassLoader cl, String startPage) throws IOException {
33.131 + Enumeration<URL> en = cl.getResources("META-INF/MANIFEST.MF");
33.132 + while (en.hasMoreElements()) {
33.133 + URL url = en.nextElement();
33.134 + Manifest mf;
33.135 + try (InputStream is = url.openStream()) {
33.136 + mf = new Manifest(is);
33.137 + }
33.138 + String sp = mf.getMainAttributes().getValue("StartPage");
33.139 + if (sp != null) {
33.140 + startPage = sp;
33.141 + break;
33.142 + }
33.143 + }
33.144 + return startPage;
33.145 + }
33.146 +
33.147 + private static boolean isDebugged() {
33.148 + try {
33.149 + return isDebuggedImpl();
33.150 + } catch (LinkageError e) {
33.151 + return false;
33.152 + }
33.153 + }
33.154 +
33.155 + private static boolean isDebuggedImpl() {
33.156 + java.lang.management.RuntimeMXBean runtime;
33.157 + runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();
33.158 + List<String> args = runtime.getInputArguments();
33.159 + if (args.contains("-Xdebug")) { // NOI18N
33.160 + return true;
33.161 + }
33.162 + return false;
33.163 + }
33.164 +}
34.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
34.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fx/LauncherFX.java Fri May 24 18:04:55 2013 +0200
34.3 @@ -0,0 +1,32 @@
34.4 +/**
34.5 + * Back 2 Browser Bytecode Translator
34.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
34.7 + *
34.8 + * This program is free software: you can redistribute it and/or modify
34.9 + * it under the terms of the GNU General Public License as published by
34.10 + * the Free Software Foundation, version 2 of the License.
34.11 + *
34.12 + * This program is distributed in the hope that it will be useful,
34.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34.15 + * GNU General Public License for more details.
34.16 + *
34.17 + * You should have received a copy of the GNU General Public License
34.18 + * along with this program. Look for COPYING file in the top folder.
34.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
34.20 + */
34.21 +package org.apidesign.bck2brwsr.launcher.fx;
34.22 +
34.23 +import org.apidesign.bck2brwsr.launcher.Launcher;
34.24 +
34.25 +/** This is a launcher for the <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a>
34.26 + * project that is using <b>JavaFX</b>'s WebView to display the browser inside
34.27 + * real Java virtual machine. Use {@link Launcher} methods to access this
34.28 + * functionality via public, supported methods.
34.29 + *
34.30 + * @author Jaroslav Tulach
34.31 + */
34.32 +public final class LauncherFX {
34.33 + private LauncherFX() {
34.34 + }
34.35 +}
35.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
35.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Console.java Fri May 24 18:04:55 2013 +0200
35.3 @@ -0,0 +1,395 @@
35.4 +/**
35.5 + * Back 2 Browser Bytecode Translator
35.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
35.7 + *
35.8 + * This program is free software: you can redistribute it and/or modify
35.9 + * it under the terms of the GNU General Public License as published by
35.10 + * the Free Software Foundation, version 2 of the License.
35.11 + *
35.12 + * This program is distributed in the hope that it will be useful,
35.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35.15 + * GNU General Public License for more details.
35.16 + *
35.17 + * You should have received a copy of the GNU General Public License
35.18 + * along with this program. Look for COPYING file in the top folder.
35.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
35.20 + */
35.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
35.22 +
35.23 +import java.io.IOException;
35.24 +import java.io.InputStream;
35.25 +import java.io.UnsupportedEncodingException;
35.26 +import java.lang.reflect.InvocationTargetException;
35.27 +import java.lang.reflect.Method;
35.28 +import java.lang.reflect.Modifier;
35.29 +import java.net.URL;
35.30 +import java.util.Enumeration;
35.31 +import javafx.scene.web.WebEngine;
35.32 +import netscape.javascript.JSObject;
35.33 +
35.34 +/**
35.35 + *
35.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
35.37 + */
35.38 +public final class Console {
35.39 + public Console() {
35.40 + }
35.41 +
35.42 + private static Object getAttr(Object elem, String attr) {
35.43 + return InvokeJS.CObject.call("getAttr", elem, attr);
35.44 + }
35.45 +
35.46 + private static void setAttr(String id, String attr, Object value) {
35.47 + InvokeJS.CObject.call("setAttrId", id, attr, value);
35.48 + }
35.49 + private static void setAttr(Object id, String attr, Object value) {
35.50 + InvokeJS.CObject.call("setAttr", id, attr, value);
35.51 + }
35.52 +
35.53 + private static void closeWindow() {}
35.54 +
35.55 + private static Object textArea;
35.56 + private static Object statusArea;
35.57 +
35.58 + private static void log(String newText) {
35.59 + if (textArea == null) {
35.60 + return;
35.61 + }
35.62 + String attr = "value";
35.63 + setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
35.64 + setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
35.65 + }
35.66 +
35.67 + private static void beginTest(Case c) {
35.68 + Object[] arr = new Object[2];
35.69 + beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
35.70 + textArea = arr[0];
35.71 + statusArea = arr[1];
35.72 + }
35.73 +
35.74 + private static void finishTest(Case c, Object res) {
35.75 + if ("null".equals(res)) {
35.76 + setAttr(statusArea, "innerHTML", "Success");
35.77 + } else {
35.78 + setAttr(statusArea, "innerHTML", "Result " + res);
35.79 + }
35.80 + statusArea = null;
35.81 + textArea = null;
35.82 + }
35.83 +
35.84 + private static final String BEGIN_TEST =
35.85 + "var ul = window.document.getElementById('bck2brwsr.result');\n"
35.86 + + "var li = window.document.createElement('li');\n"
35.87 + + "var span = window.document.createElement('span');"
35.88 + + "span.innerHTML = test + ' - ';\n"
35.89 + + "var details = window.document.createElement('a');\n"
35.90 + + "details.innerHTML = 'Details';\n"
35.91 + + "details.href = '#';\n"
35.92 + + "var p = window.document.createElement('p');\n"
35.93 + + "var status = window.document.createElement('a');\n"
35.94 + + "status.innerHTML = 'running';"
35.95 + + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
35.96 + + "status.onclick = function() { c.again(arr); }\n"
35.97 + + "var pre = window.document.createElement('textarea');\n"
35.98 + + "pre.cols = 100;"
35.99 + + "pre.rows = 10;"
35.100 + + "li.appendChild(span);\n"
35.101 + + "li.appendChild(status);\n"
35.102 + + "var span = window.document.createElement('span');"
35.103 + + "span.innerHTML = ' ';\n"
35.104 + + "li.appendChild(span);\n"
35.105 + + "li.appendChild(details);\n"
35.106 + + "p.appendChild(pre);\n"
35.107 + + "ul.appendChild(li);\n"
35.108 + + "arr[0] = pre;\n"
35.109 + + "arr[1] = status;\n";
35.110 +
35.111 + private static void beginTest(String test, Case c, Object[] arr) {
35.112 + InvokeJS.CObject.call("beginTest", test, c, arr);
35.113 + }
35.114 +
35.115 + private static final String LOAD_TEXT =
35.116 + "var request = new XMLHttpRequest();\n"
35.117 + + "request.open('GET', url, true);\n"
35.118 + + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
35.119 + + "request.onreadystatechange = function() {\n"
35.120 + + " if (this.readyState!==4) return;\n"
35.121 + + " try {"
35.122 + + " arr[0] = this.responseText;\n"
35.123 + + " callback.run__V();\n"
35.124 + + " } catch (e) { alert(e); }"
35.125 + + "};"
35.126 + + "request.send();";
35.127 + private static void loadText(String url, Runnable callback, String[] arr) throws IOException {
35.128 + InvokeJS.CObject.call("loadText", url, new Run(callback), arr);
35.129 + }
35.130 +
35.131 + public static void runHarness(String url) throws IOException {
35.132 + new Console().harness(url);
35.133 + }
35.134 +
35.135 + public void harness(String url) throws IOException {
35.136 + log("Connecting to " + url);
35.137 + Request r = new Request(url);
35.138 + }
35.139 +
35.140 + private static class Request implements Runnable {
35.141 + private final String[] arr = { null };
35.142 + private final String url;
35.143 + private Case c;
35.144 + private int retries;
35.145 +
35.146 + private Request(String url) throws IOException {
35.147 + this.url = url;
35.148 + loadText(url, this, arr);
35.149 + }
35.150 + private Request(String url, String u) throws IOException {
35.151 + this.url = url;
35.152 + loadText(u, this, arr);
35.153 + }
35.154 +
35.155 + @Override
35.156 + public void run() {
35.157 + try {
35.158 + if (c == null) {
35.159 + String data = arr[0];
35.160 +
35.161 + if (data == null) {
35.162 + log("Some error exiting");
35.163 + closeWindow();
35.164 + return;
35.165 + }
35.166 +
35.167 + if (data.isEmpty()) {
35.168 + log("No data, exiting");
35.169 + closeWindow();
35.170 + return;
35.171 + }
35.172 +
35.173 + c = Case.parseData(data);
35.174 + beginTest(c);
35.175 + log("Got \"" + data + "\"");
35.176 + } else {
35.177 + log("Processing \"" + arr[0] + "\" for " + retries + " time");
35.178 + }
35.179 + Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
35.180 + finishTest(c, result);
35.181 +
35.182 + String u = url + "?request=" + c.getRequestId() + "&result=" + result;
35.183 + new Request(url, u);
35.184 + } catch (Exception ex) {
35.185 + if (ex instanceof InterruptedException) {
35.186 + log("Re-scheduling in 100ms");
35.187 + schedule(this, 100);
35.188 + return;
35.189 + }
35.190 + log(ex.getClass().getName() + ":" + ex.getMessage());
35.191 + }
35.192 + }
35.193 + }
35.194 +
35.195 + private static String encodeURL(String r) throws UnsupportedEncodingException {
35.196 + final String SPECIAL = "%$&+,/:;=?@";
35.197 + StringBuilder sb = new StringBuilder();
35.198 + byte[] utf8 = r.getBytes("UTF-8");
35.199 + for (int i = 0; i < utf8.length; i++) {
35.200 + int ch = utf8[i] & 0xff;
35.201 + if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
35.202 + final String numbers = "0" + Integer.toHexString(ch);
35.203 + sb.append("%").append(numbers.substring(numbers.length() - 2));
35.204 + } else {
35.205 + if (ch == 32) {
35.206 + sb.append("+");
35.207 + } else {
35.208 + sb.append((char)ch);
35.209 + }
35.210 + }
35.211 + }
35.212 + return sb.toString();
35.213 + }
35.214 +
35.215 + static String invoke(String clazz, String method) throws
35.216 + ClassNotFoundException, InvocationTargetException, IllegalAccessException,
35.217 + InstantiationException, InterruptedException {
35.218 + final Object r = new Case(null).invokeMethod(clazz, method);
35.219 + return r == null ? "null" : r.toString().toString();
35.220 + }
35.221 +
35.222 + /** Helper method that inspects the classpath and loads given resource
35.223 + * (usually a class file). Used while running tests in Rhino.
35.224 + *
35.225 + * @param name resource name to find
35.226 + * @return the array of bytes in the given resource
35.227 + * @throws IOException I/O in case something goes wrong
35.228 + */
35.229 + public static byte[] read(String name) throws IOException {
35.230 + URL u = null;
35.231 + Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
35.232 + while (en.hasMoreElements()) {
35.233 + u = en.nextElement();
35.234 + }
35.235 + if (u == null) {
35.236 + throw new IOException("Can't find " + name);
35.237 + }
35.238 + try (InputStream is = u.openStream()) {
35.239 + byte[] arr;
35.240 + arr = new byte[is.available()];
35.241 + int offset = 0;
35.242 + while (offset < arr.length) {
35.243 + int len = is.read(arr, offset, arr.length - offset);
35.244 + if (len == -1) {
35.245 + throw new IOException("Can't read " + name);
35.246 + }
35.247 + offset += len;
35.248 + }
35.249 + return arr;
35.250 + }
35.251 + }
35.252 +
35.253 + private static void turnAssetionStatusOn() {
35.254 + }
35.255 +
35.256 + private static Object schedule(Runnable r, int time) {
35.257 + return InvokeJS.CObject.call("schedule", new Run(r), time);
35.258 + }
35.259 +
35.260 + private static final class Case {
35.261 + private final Object data;
35.262 + private Object inst;
35.263 +
35.264 + private Case(Object data) {
35.265 + this.data = data;
35.266 + }
35.267 +
35.268 + public static Case parseData(String s) {
35.269 + return new Case(toJSON(s));
35.270 + }
35.271 +
35.272 + public String getMethodName() {
35.273 + return (String) value("methodName", data);
35.274 + }
35.275 +
35.276 + public String getClassName() {
35.277 + return (String) value("className", data);
35.278 + }
35.279 +
35.280 + public int getRequestId() {
35.281 + Object v = value("request", data);
35.282 + if (v instanceof Number) {
35.283 + return ((Number)v).intValue();
35.284 + }
35.285 + return Integer.parseInt(v.toString());
35.286 + }
35.287 +
35.288 + public String getHtmlFragment() {
35.289 + return (String) value("html", data);
35.290 + }
35.291 +
35.292 + void again(Object[] arr) {
35.293 + try {
35.294 + textArea = arr[0];
35.295 + statusArea = arr[1];
35.296 + setAttr(textArea, "value", "");
35.297 + runTest();
35.298 + } catch (Exception ex) {
35.299 + log(ex.getClass().getName() + ":" + ex.getMessage());
35.300 + }
35.301 + }
35.302 +
35.303 + private Object runTest() throws IllegalAccessException,
35.304 + IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
35.305 + InvocationTargetException, InstantiationException, InterruptedException {
35.306 + if (this.getHtmlFragment() != null) {
35.307 + setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
35.308 + }
35.309 + log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
35.310 + Object result = invokeMethod(this.getClassName(), this.getMethodName());
35.311 + setAttr("bck2brwsr.fragment", "innerHTML", "");
35.312 + log("Result: " + result);
35.313 + result = encodeURL("" + result);
35.314 + log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
35.315 + return result;
35.316 + }
35.317 +
35.318 + private Object invokeMethod(String clazz, String method)
35.319 + throws ClassNotFoundException, InvocationTargetException,
35.320 + InterruptedException, IllegalAccessException, IllegalArgumentException,
35.321 + InstantiationException {
35.322 + Method found = null;
35.323 + Class<?> c = Class.forName(clazz);
35.324 + for (Method m : c.getMethods()) {
35.325 + if (m.getName().equals(method)) {
35.326 + found = m;
35.327 + }
35.328 + }
35.329 + Object res;
35.330 + if (found != null) {
35.331 + try {
35.332 + if ((found.getModifiers() & Modifier.STATIC) != 0) {
35.333 + res = found.invoke(null);
35.334 + } else {
35.335 + if (inst == null) {
35.336 + inst = c.newInstance();
35.337 + }
35.338 + res = found.invoke(inst);
35.339 + }
35.340 + } catch (Throwable ex) {
35.341 + if (ex instanceof InvocationTargetException) {
35.342 + ex = ((InvocationTargetException) ex).getTargetException();
35.343 + }
35.344 + if (ex instanceof InterruptedException) {
35.345 + throw (InterruptedException)ex;
35.346 + }
35.347 + res = ex.getClass().getName() + ":" + ex.getMessage();
35.348 + }
35.349 + } else {
35.350 + res = "Can't find method " + method + " in " + clazz;
35.351 + }
35.352 + return res;
35.353 + }
35.354 +
35.355 + private static Object toJSON(String s) {
35.356 + return InvokeJS.CObject.call("toJSON", s);
35.357 + }
35.358 +
35.359 + private static Object value(String p, Object d) {
35.360 + return ((JSObject)d).getMember(p);
35.361 + }
35.362 + }
35.363 +
35.364 + private static String safe(String txt) {
35.365 + return "try {" + txt + "} catch (err) { alert(err); }";
35.366 + }
35.367 +
35.368 + static {
35.369 + turnAssetionStatusOn();
35.370 + }
35.371 +
35.372 + private static final class InvokeJS {
35.373 + static final JSObject CObject = initJS();
35.374 +
35.375 + private static JSObject initJS() {
35.376 + WebEngine web = (WebEngine) System.getProperties().get("webEngine");
35.377 + return (JSObject) web.executeScript("(function() {"
35.378 + + "var CObject = {};"
35.379 +
35.380 + + "CObject.getAttr = function(elem, attr) { return elem[attr].toString(); };"
35.381 +
35.382 + + "CObject.setAttrId = function(id, attr, value) { window.document.getElementById(id)[attr] = value; };"
35.383 + + "CObject.setAttr = function(elem, attr, value) { elem[attr] = value; };"
35.384 +
35.385 + + "CObject.beginTest = function(test, c, arr) {" + safe(BEGIN_TEST) + "};"
35.386 +
35.387 + + "CObject.loadText = function(url, callback, arr) {" + safe(LOAD_TEXT.replace("run__V", "run")) + "};"
35.388 +
35.389 + + "CObject.schedule = function(r, time) { return window.setTimeout(function() { r.run(); }, time); };"
35.390 +
35.391 + + "CObject.toJSON = function(s) { return eval('(' + s + ')'); };"
35.392 +
35.393 + + "return CObject;"
35.394 + + "})(this)");
35.395 + }
35.396 + }
35.397 +
35.398 +}
36.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
36.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/FXBrwsr.java Fri May 24 18:04:55 2013 +0200
36.3 @@ -0,0 +1,184 @@
36.4 +/**
36.5 + * Back 2 Browser Bytecode Translator
36.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
36.7 + *
36.8 + * This program is free software: you can redistribute it and/or modify
36.9 + * it under the terms of the GNU General Public License as published by
36.10 + * the Free Software Foundation, version 2 of the License.
36.11 + *
36.12 + * This program is distributed in the hope that it will be useful,
36.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36.15 + * GNU General Public License for more details.
36.16 + *
36.17 + * You should have received a copy of the GNU General Public License
36.18 + * along with this program. Look for COPYING file in the top folder.
36.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
36.20 + */
36.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
36.22 +
36.23 +import java.util.List;
36.24 +import java.util.TooManyListenersException;
36.25 +import java.util.logging.Level;
36.26 +import java.util.logging.Logger;
36.27 +import javafx.application.Application;
36.28 +import javafx.application.Platform;
36.29 +import javafx.beans.value.ChangeListener;
36.30 +import javafx.beans.value.ObservableValue;
36.31 +import javafx.event.ActionEvent;
36.32 +import javafx.event.EventHandler;
36.33 +import javafx.geometry.HPos;
36.34 +import javafx.geometry.Insets;
36.35 +import javafx.geometry.Pos;
36.36 +import javafx.geometry.VPos;
36.37 +import javafx.scene.Node;
36.38 +import javafx.scene.Scene;
36.39 +import javafx.scene.control.Button;
36.40 +import javafx.scene.layout.ColumnConstraints;
36.41 +import javafx.scene.layout.GridPane;
36.42 +import javafx.scene.layout.Pane;
36.43 +import javafx.scene.layout.Priority;
36.44 +import javafx.scene.layout.VBox;
36.45 +import javafx.scene.text.Text;
36.46 +import javafx.scene.web.WebEngine;
36.47 +import javafx.scene.web.WebEvent;
36.48 +import javafx.scene.web.WebView;
36.49 +import javafx.stage.Modality;
36.50 +import javafx.stage.Stage;
36.51 +import netscape.javascript.JSObject;
36.52 +
36.53 +/**
36.54 + * Demonstrates a WebView object accessing a web page.
36.55 + *
36.56 + * @see javafx.scene.web.WebView
36.57 + * @see javafx.scene.web.WebEngine
36.58 + */
36.59 +public class FXBrwsr extends Application {
36.60 + private static final Logger LOG = Logger.getLogger(FXBrwsr.class.getName());
36.61 +
36.62 + @Override
36.63 + public void start(Stage primaryStage) throws Exception {
36.64 + Pane root = new WebViewPane(getParameters().getUnnamed());
36.65 + primaryStage.setScene(new Scene(root, 1024, 768));
36.66 + LOG.info("Showing the stage");
36.67 + primaryStage.show();
36.68 + LOG.log(Level.INFO, "State shown: {0}", primaryStage.isShowing());
36.69 + }
36.70 +
36.71 + /**
36.72 + * Create a resizable WebView pane
36.73 + */
36.74 + private class WebViewPane extends Pane {
36.75 + private final JVMBridge bridge = new JVMBridge();
36.76 +
36.77 + public WebViewPane(List<String> params) {
36.78 + LOG.log(Level.INFO, "Initializing WebView with {0}", params);
36.79 + VBox.setVgrow(this, Priority.ALWAYS);
36.80 + setMaxWidth(Double.MAX_VALUE);
36.81 + setMaxHeight(Double.MAX_VALUE);
36.82 + WebView view = new WebView();
36.83 + view.setMinSize(500, 400);
36.84 + view.setPrefSize(500, 400);
36.85 + final WebEngine eng = view.getEngine();
36.86 + try {
36.87 + JVMBridge.addBck2BrwsrLoad(new InitBck2Brwsr(eng));
36.88 + } catch (TooManyListenersException ex) {
36.89 + LOG.log(Level.SEVERE, null, ex);
36.90 + }
36.91 +
36.92 + if (params.size() > 0) {
36.93 + LOG.log(Level.INFO, "loading page {0}", params.get(0));
36.94 + eng.load(params.get(0));
36.95 + LOG.fine("back from load");
36.96 + }
36.97 + eng.setOnAlert(new EventHandler<WebEvent<String>>() {
36.98 + @Override
36.99 + public void handle(WebEvent<String> t) {
36.100 + final Stage dialogStage = new Stage();
36.101 + dialogStage.initModality(Modality.WINDOW_MODAL);
36.102 + dialogStage.setTitle("Warning");
36.103 + final Button button = new Button("Close");
36.104 + final Text text = new Text(t.getData());
36.105 +
36.106 + VBox box = new VBox();
36.107 + box.setAlignment(Pos.CENTER);
36.108 + box.setSpacing(10);
36.109 + box.setPadding(new Insets(10));
36.110 + box.getChildren().addAll(text, button);
36.111 +
36.112 + dialogStage.setScene(new Scene(box));
36.113 +
36.114 + button.setCancelButton(true);
36.115 + button.setOnAction(new EventHandler<ActionEvent>() {
36.116 + @Override
36.117 + public void handle(ActionEvent t) {
36.118 + dialogStage.close();
36.119 + }
36.120 + });
36.121 +
36.122 + dialogStage.centerOnScreen();
36.123 + dialogStage.showAndWait();
36.124 + }
36.125 + });
36.126 + GridPane grid = new GridPane();
36.127 + grid.setVgap(5);
36.128 + grid.setHgap(5);
36.129 + GridPane.setConstraints(view, 0, 1, 2, 1, HPos.CENTER, VPos.CENTER, Priority.ALWAYS, Priority.ALWAYS);
36.130 + grid.getColumnConstraints().addAll(new ColumnConstraints(100, 100, Double.MAX_VALUE, Priority.ALWAYS, HPos.CENTER, true), new ColumnConstraints(40, 40, 40, Priority.NEVER, HPos.CENTER, true));
36.131 + grid.getChildren().addAll(view);
36.132 + getChildren().add(grid);
36.133 + }
36.134 +
36.135 + boolean initBck2Brwsr(WebEngine webEngine) {
36.136 + JSObject jsobj = (JSObject) webEngine.executeScript("window");
36.137 + LOG.log(Level.FINE, "window: {0}", jsobj);
36.138 + Object prev = jsobj.getMember("bck2brwsr");
36.139 + if ("undefined".equals(prev)) {
36.140 + System.getProperties().put("webEngine", webEngine);
36.141 + jsobj.setMember("bck2brwsr", bridge);
36.142 + return true;
36.143 + }
36.144 + return false;
36.145 + }
36.146 +
36.147 + @Override
36.148 + protected void layoutChildren() {
36.149 + List<Node> managed = getManagedChildren();
36.150 + double width = getWidth();
36.151 + double height = getHeight();
36.152 + double top = getInsets().getTop();
36.153 + double right = getInsets().getRight();
36.154 + double left = getInsets().getLeft();
36.155 + double bottom = getInsets().getBottom();
36.156 + for (int i = 0; i < managed.size(); i++) {
36.157 + Node child = managed.get(i);
36.158 + layoutInArea(child, left, top, width - left - right, height - top - bottom, 0, Insets.EMPTY, true, true, HPos.CENTER, VPos.CENTER);
36.159 + }
36.160 + }
36.161 +
36.162 + private class InitBck2Brwsr implements ChangeListener<Void>, Runnable {
36.163 + private final WebEngine eng;
36.164 +
36.165 + public InitBck2Brwsr(WebEngine eng) {
36.166 + this.eng = eng;
36.167 + }
36.168 +
36.169 + @Override
36.170 + public synchronized void changed(ObservableValue<? extends Void> ov, Void t, Void t1) {
36.171 + Platform.runLater(this);
36.172 + try {
36.173 + wait();
36.174 + } catch (InterruptedException ex) {
36.175 + LOG.log(Level.SEVERE, null, ex);
36.176 + }
36.177 + }
36.178 +
36.179 + @Override
36.180 + public synchronized void run() {
36.181 + initBck2Brwsr(eng);
36.182 + notifyAll();
36.183 + }
36.184 + }
36.185 + }
36.186 +
36.187 +}
37.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
37.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/JVMBridge.java Fri May 24 18:04:55 2013 +0200
37.3 @@ -0,0 +1,64 @@
37.4 +/**
37.5 + * Back 2 Browser Bytecode Translator
37.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
37.7 + *
37.8 + * This program is free software: you can redistribute it and/or modify
37.9 + * it under the terms of the GNU General Public License as published by
37.10 + * the Free Software Foundation, version 2 of the License.
37.11 + *
37.12 + * This program is distributed in the hope that it will be useful,
37.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37.15 + * GNU General Public License for more details.
37.16 + *
37.17 + * You should have received a copy of the GNU General Public License
37.18 + * along with this program. Look for COPYING file in the top folder.
37.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
37.20 + */
37.21 +package org.apidesign.bck2brwsr.launcher.fximpl;
37.22 +
37.23 +import java.util.TooManyListenersException;
37.24 +import javafx.beans.value.ChangeListener;
37.25 +
37.26 +/**
37.27 + *
37.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
37.29 + */
37.30 +public final class JVMBridge {
37.31 + private static ClassLoader[] ldrs;
37.32 + private static ChangeListener<Void> onBck2BrwsrLoad;
37.33 +
37.34 + public static void registerClassLoaders(ClassLoader[] loaders) {
37.35 + ldrs = loaders.clone();
37.36 + }
37.37 +
37.38 + public static void addBck2BrwsrLoad(ChangeListener<Void> l) throws TooManyListenersException {
37.39 + if (onBck2BrwsrLoad != null) {
37.40 + throw new TooManyListenersException();
37.41 + }
37.42 + onBck2BrwsrLoad = l;
37.43 + }
37.44 +
37.45 + public static void onBck2BrwsrLoad() {
37.46 + ChangeListener<Void> l = onBck2BrwsrLoad;
37.47 + if (l != null) {
37.48 + l.changed(null, null, null);
37.49 + }
37.50 + }
37.51 +
37.52 + public Class<?> loadClass(String name) throws ClassNotFoundException {
37.53 + System.err.println("trying to load " + name);
37.54 + ClassNotFoundException ex = null;
37.55 + if (ldrs != null) for (ClassLoader l : ldrs) {
37.56 + try {
37.57 + return Class.forName(name, true, l);
37.58 + } catch (ClassNotFoundException ex2) {
37.59 + ex = ex2;
37.60 + }
37.61 + }
37.62 + if (ex == null) {
37.63 + ex = new ClassNotFoundException("No loaders");
37.64 + }
37.65 + throw ex;
37.66 + }
37.67 +}
38.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
38.2 +++ b/launcher/fx/src/main/java/org/apidesign/bck2brwsr/launcher/fximpl/Run.java Fri May 24 18:04:55 2013 +0200
38.3 @@ -0,0 +1,35 @@
38.4 +/**
38.5 + * Back 2 Browser Bytecode Translator
38.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
38.7 + *
38.8 + * This program is free software: you can redistribute it and/or modify
38.9 + * it under the terms of the GNU General Public License as published by
38.10 + * the Free Software Foundation, version 2 of the License.
38.11 + *
38.12 + * This program is distributed in the hope that it will be useful,
38.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38.15 + * GNU General Public License for more details.
38.16 + *
38.17 + * You should have received a copy of the GNU General Public License
38.18 + * along with this program. Look for COPYING file in the top folder.
38.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
38.20 + */
38.21 +
38.22 +package org.apidesign.bck2brwsr.launcher.fximpl;
38.23 +
38.24 +/**
38.25 + *
38.26 + * @author Jaroslav Tulach <jtulach@netbeans.org>
38.27 + */
38.28 +public final class Run implements Runnable {
38.29 + private final Runnable r;
38.30 + Run(Runnable r) {
38.31 + this.r = r;
38.32 + }
38.33 +
38.34 + @Override
38.35 + public void run() {
38.36 + r.run();
38.37 + }
38.38 +}
39.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
39.2 +++ b/launcher/fx/src/main/resources/org/apidesign/bck2brwsr/launcher/fximpl/harness.xhtml Fri May 24 18:04:55 2013 +0200
39.3 @@ -0,0 +1,52 @@
39.4 +<?xml version="1.0" encoding="UTF-8"?>
39.5 +<!--
39.6 +
39.7 + Back 2 Browser Bytecode Translator
39.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
39.9 +
39.10 + This program is free software: you can redistribute it and/or modify
39.11 + it under the terms of the GNU General Public License as published by
39.12 + the Free Software Foundation, version 2 of the License.
39.13 +
39.14 + This program is distributed in the hope that it will be useful,
39.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
39.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39.17 + GNU General Public License for more details.
39.18 +
39.19 + You should have received a copy of the GNU General Public License
39.20 + along with this program. Look for COPYING file in the top folder.
39.21 + If not, see http://opensource.org/licenses/GPL-2.0.
39.22 +
39.23 +-->
39.24 +<!DOCTYPE html>
39.25 +<html xmlns="http://www.w3.org/1999/xhtml">
39.26 + <head>
39.27 + <title>Bck2Brwsr Harness</title>
39.28 + </head>
39.29 + <body>
39.30 + <script src="/bck2brwsr.js"></script>
39.31 + <script>
39.32 + var vm = bck2brwsr();
39.33 + </script>
39.34 +
39.35 + <h1>Bck2Brwsr Execution Harness</h1>
39.36 +
39.37 + <ul id="bck2brwsr.result" style="width: 100%;" >
39.38 + </ul>
39.39 +
39.40 + <div id="bck2brwsr.fragment"/>
39.41 +
39.42 + <script type="text/javascript">
39.43 + try {
39.44 + (function() {
39.45 + var cls = vm.loadClass('org.apidesign.bck2brwsr.launcher.fximpl.Console');
39.46 + // fxbrwsr mangling
39.47 + var inst = cls.newInstance();
39.48 + inst.harness('$U/../data');
39.49 + })();
39.50 + } catch (err) {
39.51 + alert('Error executing harness: ' + err);
39.52 + }
39.53 + </script>
39.54 + </body>
39.55 +</html>
40.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
40.2 +++ b/launcher/http/pom.xml Fri May 24 18:04:55 2013 +0200
40.3 @@ -0,0 +1,60 @@
40.4 +<?xml version="1.0"?>
40.5 +<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">
40.6 + <modelVersion>4.0.0</modelVersion>
40.7 + <parent>
40.8 + <groupId>org.apidesign.bck2brwsr</groupId>
40.9 + <artifactId>launcher-pom</artifactId>
40.10 + <version>0.8-SNAPSHOT</version>
40.11 + </parent>
40.12 + <groupId>org.apidesign.bck2brwsr</groupId>
40.13 + <artifactId>launcher.http</artifactId>
40.14 + <version>0.8-SNAPSHOT</version>
40.15 + <name>Bck2Brwsr Launcher</name>
40.16 + <url>http://maven.apache.org</url>
40.17 + <build>
40.18 + <plugins>
40.19 + <plugin>
40.20 + <groupId>org.apache.maven.plugins</groupId>
40.21 + <artifactId>maven-compiler-plugin</artifactId>
40.22 + <version>2.3.2</version>
40.23 + <configuration>
40.24 + <source>1.7</source>
40.25 + <target>1.7</target>
40.26 + </configuration>
40.27 + </plugin>
40.28 + <plugin>
40.29 + <groupId>org.apache.maven.plugins</groupId>
40.30 + <artifactId>maven-javadoc-plugin</artifactId>
40.31 + <configuration>
40.32 + <subpackages>org.apidesign.bck2brwsr.launcher.b2b</subpackages>
40.33 + <skip>false</skip>
40.34 + </configuration>
40.35 + </plugin>
40.36 + </plugins>
40.37 + </build>
40.38 + <properties>
40.39 + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
40.40 + </properties>
40.41 + <dependencies>
40.42 + <dependency>
40.43 + <groupId>${project.groupId}</groupId>
40.44 + <artifactId>launcher</artifactId>
40.45 + <version>${project.version}</version>
40.46 + </dependency>
40.47 + <dependency>
40.48 + <groupId>${project.groupId}</groupId>
40.49 + <artifactId>launcher.fx</artifactId>
40.50 + <version>${project.version}</version>
40.51 + </dependency>
40.52 + <dependency>
40.53 + <groupId>org.glassfish.grizzly</groupId>
40.54 + <artifactId>grizzly-http-server</artifactId>
40.55 + <version>${grizzly.version}</version>
40.56 + </dependency>
40.57 + <dependency>
40.58 + <groupId>${project.groupId}</groupId>
40.59 + <artifactId>vm4brwsr</artifactId>
40.60 + <version>${project.version}</version>
40.61 + </dependency>
40.62 + </dependencies>
40.63 +</project>
41.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
41.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Fri May 24 18:04:55 2013 +0200
41.3 @@ -0,0 +1,70 @@
41.4 +/**
41.5 + * Back 2 Browser Bytecode Translator
41.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
41.7 + *
41.8 + * This program is free software: you can redistribute it and/or modify
41.9 + * it under the terms of the GNU General Public License as published by
41.10 + * the Free Software Foundation, version 2 of the License.
41.11 + *
41.12 + * This program is distributed in the hope that it will be useful,
41.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
41.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41.15 + * GNU General Public License for more details.
41.16 + *
41.17 + * You should have received a copy of the GNU General Public License
41.18 + * along with this program. Look for COPYING file in the top folder.
41.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
41.20 + */
41.21 +package org.apidesign.bck2brwsr.launcher;
41.22 +
41.23 +import java.io.IOException;
41.24 +import java.io.InputStream;
41.25 +import org.apidesign.vm4brwsr.Bck2Brwsr;
41.26 +
41.27 +/**
41.28 + * Lightweight server to launch Bck2Brwsr applications and tests.
41.29 + * Supports execution in native browser as well as Java's internal
41.30 + * execution engine.
41.31 + */
41.32 +final class Bck2BrwsrLauncher extends BaseHTTPLauncher {
41.33 +
41.34 + public Bck2BrwsrLauncher(String cmd) {
41.35 + super(cmd);
41.36 + }
41.37 +
41.38 + @Override
41.39 + String harnessResource() {
41.40 + return "org/apidesign/bck2brwsr/launcher/harness.xhtml";
41.41 + }
41.42 +
41.43 + @Override
41.44 + void generateBck2BrwsrJS(StringBuilder sb, final Res loader) throws IOException {
41.45 + class R implements Bck2Brwsr.Resources {
41.46 + @Override
41.47 + public InputStream get(String resource) throws IOException {
41.48 + return loader.get(resource);
41.49 + }
41.50 + }
41.51 +
41.52 + Bck2Brwsr.generate(sb, new R());
41.53 + sb.append(
41.54 + "(function WrapperVM(global) {"
41.55 + + " function ldCls(res) {\n"
41.56 + + " var request = new XMLHttpRequest();\n"
41.57 + + " request.open('GET', '/classes/' + res, false);\n"
41.58 + + " request.send();\n"
41.59 + + " if (request.status !== 200) return null;\n"
41.60 + + " var arr = eval('(' + request.responseText + ')');\n"
41.61 + + " return arr;\n"
41.62 + + " }\n"
41.63 + + " var prevvm = global.bck2brwsr;\n"
41.64 + + " global.bck2brwsr = function() {\n"
41.65 + + " var args = Array.prototype.slice.apply(arguments);\n"
41.66 + + " args.unshift(ldCls);\n"
41.67 + + " return prevvm.apply(null, args);\n"
41.68 + + " };\n"
41.69 + + "})(this);\n"
41.70 + );
41.71 + }
41.72 +
41.73 +}
42.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
42.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Fri May 24 18:04:55 2013 +0200
42.3 @@ -0,0 +1,135 @@
42.4 +/**
42.5 + * Back 2 Browser Bytecode Translator
42.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
42.7 + *
42.8 + * This program is free software: you can redistribute it and/or modify
42.9 + * it under the terms of the GNU General Public License as published by
42.10 + * the Free Software Foundation, version 2 of the License.
42.11 + *
42.12 + * This program is distributed in the hope that it will be useful,
42.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
42.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42.15 + * GNU General Public License for more details.
42.16 + *
42.17 + * You should have received a copy of the GNU General Public License
42.18 + * along with this program. Look for COPYING file in the top folder.
42.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
42.20 + */
42.21 +package org.apidesign.bck2brwsr.launcher;
42.22 +
42.23 +import org.apidesign.bck2brwsr.launcher.impl.Console;
42.24 +import java.io.IOException;
42.25 +import java.io.InputStream;
42.26 +import java.net.URL;
42.27 +import java.util.Enumeration;
42.28 +import java.util.LinkedHashSet;
42.29 +import java.util.Set;
42.30 +import java.util.logging.Level;
42.31 +import java.util.logging.Logger;
42.32 +import javax.script.Invocable;
42.33 +import javax.script.ScriptEngine;
42.34 +import javax.script.ScriptEngineManager;
42.35 +import javax.script.ScriptException;
42.36 +import org.apidesign.vm4brwsr.Bck2Brwsr;
42.37 +
42.38 +/**
42.39 + * Tests execution in Java's internal scripting engine.
42.40 + */
42.41 +final class JSLauncher extends Launcher {
42.42 + private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
42.43 + private Set<ClassLoader> loaders = new LinkedHashSet<>();
42.44 + private final Res resources = new Res();
42.45 + private Invocable code;
42.46 + private StringBuilder codeSeq;
42.47 + private Object console;
42.48 +
42.49 + JSLauncher() {
42.50 + addClassLoader(Bck2Brwsr.class.getClassLoader());
42.51 + }
42.52 +
42.53 + @Override InvocationContext runMethod(InvocationContext mi) {
42.54 + loaders.add(mi.clazz.getClassLoader());
42.55 + try {
42.56 + long time = System.currentTimeMillis();
42.57 + LOG.log(Level.FINE, "Invoking {0}.{1}", new Object[]{mi.clazz.getName(), mi.methodName});
42.58 + String res = code.invokeMethod(
42.59 + console,
42.60 + "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
42.61 + mi.clazz.getName(), mi.methodName).toString();
42.62 + time = System.currentTimeMillis() - time;
42.63 + LOG.log(Level.FINE, "Resut of {0}.{1} = {2} in {3} ms", new Object[]{mi.clazz.getName(), mi.methodName, res, time});
42.64 + mi.result(res, null);
42.65 + } catch (ScriptException | NoSuchMethodException ex) {
42.66 + mi.result(null, ex);
42.67 + }
42.68 + return mi;
42.69 + }
42.70 +
42.71 + public void addClassLoader(ClassLoader url) {
42.72 + this.loaders.add(url);
42.73 + }
42.74 +
42.75 + @Override
42.76 + public void initialize() throws IOException {
42.77 + try {
42.78 + initRhino();
42.79 + } catch (Exception ex) {
42.80 + if (ex instanceof IOException) {
42.81 + throw (IOException)ex;
42.82 + }
42.83 + if (ex instanceof RuntimeException) {
42.84 + throw (RuntimeException)ex;
42.85 + }
42.86 + throw new IOException(ex);
42.87 + }
42.88 + }
42.89 +
42.90 + private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
42.91 + StringBuilder sb = new StringBuilder();
42.92 + Bck2Brwsr.generate(sb, new Res());
42.93 +
42.94 + ScriptEngineManager sem = new ScriptEngineManager();
42.95 + ScriptEngine mach = sem.getEngineByExtension("js");
42.96 +
42.97 + sb.append(
42.98 + "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.impl.Console.read);"
42.99 + + "\nfunction initVM() { return vm; };"
42.100 + + "\n");
42.101 +
42.102 + Object res = mach.eval(sb.toString());
42.103 + if (!(mach instanceof Invocable)) {
42.104 + throw new IOException("It is invocable object: " + res);
42.105 + }
42.106 + code = (Invocable) mach;
42.107 + codeSeq = sb;
42.108 +
42.109 + Object vm = code.invokeFunction("initVM");
42.110 + console = code.invokeMethod(vm, "loadClass", Console.class.getName());
42.111 + }
42.112 +
42.113 + @Override
42.114 + public void shutdown() throws IOException {
42.115 + }
42.116 +
42.117 + @Override
42.118 + public String toString() {
42.119 + return codeSeq.toString();
42.120 + }
42.121 +
42.122 + private class Res implements Bck2Brwsr.Resources {
42.123 + @Override
42.124 + public InputStream get(String resource) throws IOException {
42.125 + for (ClassLoader l : loaders) {
42.126 + URL u = null;
42.127 + Enumeration<URL> en = l.getResources(resource);
42.128 + while (en.hasMoreElements()) {
42.129 + u = en.nextElement();
42.130 + }
42.131 + if (u != null) {
42.132 + return u.openStream();
42.133 + }
42.134 + }
42.135 + throw new IOException("Can't find " + resource);
42.136 + }
42.137 + }
42.138 +}
43.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
43.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/b2b/LauncherBck2Brwsr.java Fri May 24 18:04:55 2013 +0200
43.3 @@ -0,0 +1,33 @@
43.4 +/**
43.5 + * Back 2 Browser Bytecode Translator
43.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
43.7 + *
43.8 + * This program is free software: you can redistribute it and/or modify
43.9 + * it under the terms of the GNU General Public License as published by
43.10 + * the Free Software Foundation, version 2 of the License.
43.11 + *
43.12 + * This program is distributed in the hope that it will be useful,
43.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
43.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43.15 + * GNU General Public License for more details.
43.16 + *
43.17 + * You should have received a copy of the GNU General Public License
43.18 + * along with this program. Look for COPYING file in the top folder.
43.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
43.20 + */
43.21 +package org.apidesign.bck2brwsr.launcher.b2b;
43.22 +
43.23 +import java.awt.Desktop;
43.24 +import org.apidesign.bck2brwsr.launcher.Launcher;
43.25 +
43.26 +/** This is a launcher for the <a href="http://bck2brwsr.apidesign.org">Bck2Brwsr</a>
43.27 + * project that is using {@link Desktop} (or {@link Process}) to display the
43.28 + * external browser in separate process. Use {@link Launcher} methods to access this
43.29 + * functionality via public, supported methods.
43.30 + *
43.31 + * @author Jaroslav Tulach
43.32 + */
43.33 +public final class LauncherBck2Brwsr {
43.34 + private LauncherBck2Brwsr() {
43.35 + }
43.36 +}
44.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
44.2 +++ b/launcher/http/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Fri May 24 18:04:55 2013 +0200
44.3 @@ -0,0 +1,356 @@
44.4 +/**
44.5 + * Back 2 Browser Bytecode Translator
44.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
44.7 + *
44.8 + * This program is free software: you can redistribute it and/or modify
44.9 + * it under the terms of the GNU General Public License as published by
44.10 + * the Free Software Foundation, version 2 of the License.
44.11 + *
44.12 + * This program is distributed in the hope that it will be useful,
44.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
44.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44.15 + * GNU General Public License for more details.
44.16 + *
44.17 + * You should have received a copy of the GNU General Public License
44.18 + * along with this program. Look for COPYING file in the top folder.
44.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
44.20 + */
44.21 +package org.apidesign.bck2brwsr.launcher.impl;
44.22 +
44.23 +import java.io.IOException;
44.24 +import java.io.InputStream;
44.25 +import java.io.UnsupportedEncodingException;
44.26 +import java.lang.reflect.InvocationTargetException;
44.27 +import java.lang.reflect.Method;
44.28 +import java.lang.reflect.Modifier;
44.29 +import java.net.URL;
44.30 +import java.util.Enumeration;
44.31 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
44.32 +
44.33 +/**
44.34 + *
44.35 + * @author Jaroslav Tulach <jtulach@netbeans.org>
44.36 + */
44.37 +public class Console {
44.38 + private Console() {
44.39 + }
44.40 + static {
44.41 + turnAssetionStatusOn();
44.42 + }
44.43 +
44.44 + @JavaScriptBody(args = {"id", "attr"}, body =
44.45 + "return window.document.getElementById(id)[attr].toString();")
44.46 + private static native Object getAttr(String id, String attr);
44.47 + @JavaScriptBody(args = {"elem", "attr"}, body =
44.48 + "return elem[attr].toString();")
44.49 + private static native Object getAttr(Object elem, String attr);
44.50 +
44.51 + @JavaScriptBody(args = {"id", "attr", "value"}, body =
44.52 + "window.document.getElementById(id)[attr] = value;")
44.53 + private static native void setAttr(String id, String attr, Object value);
44.54 + @JavaScriptBody(args = {"elem", "attr", "value"}, body =
44.55 + "elem[attr] = value;")
44.56 + private static native void setAttr(Object id, String attr, Object value);
44.57 +
44.58 + @JavaScriptBody(args = {}, body = "return; window.close();")
44.59 + private static native void closeWindow();
44.60 +
44.61 + private static Object textArea;
44.62 + private static Object statusArea;
44.63 +
44.64 + private static void log(String newText) {
44.65 + if (textArea == null) {
44.66 + return;
44.67 + }
44.68 + String attr = "value";
44.69 + setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
44.70 + setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
44.71 + }
44.72 +
44.73 + private static void beginTest(Case c) {
44.74 + Object[] arr = new Object[2];
44.75 + beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
44.76 + textArea = arr[0];
44.77 + statusArea = arr[1];
44.78 + }
44.79 +
44.80 + private static void finishTest(Case c, Object res) {
44.81 + if ("null".equals(res)) {
44.82 + setAttr(statusArea, "innerHTML", "Success");
44.83 + } else {
44.84 + setAttr(statusArea, "innerHTML", "Result " + res);
44.85 + }
44.86 + statusArea = null;
44.87 + textArea = null;
44.88 + }
44.89 +
44.90 + @JavaScriptBody(args = { "test", "c", "arr" }, body =
44.91 + "var ul = window.document.getElementById('bck2brwsr.result');\n"
44.92 + + "var li = window.document.createElement('li');\n"
44.93 + + "var span = window.document.createElement('span');"
44.94 + + "span.innerHTML = test + ' - ';\n"
44.95 + + "var details = window.document.createElement('a');\n"
44.96 + + "details.innerHTML = 'Details';\n"
44.97 + + "details.href = '#';\n"
44.98 + + "var p = window.document.createElement('p');\n"
44.99 + + "var status = window.document.createElement('a');\n"
44.100 + + "status.innerHTML = 'running';"
44.101 + + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
44.102 + + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
44.103 + + "var pre = window.document.createElement('textarea');\n"
44.104 + + "pre.cols = 100;"
44.105 + + "pre.rows = 10;"
44.106 + + "li.appendChild(span);\n"
44.107 + + "li.appendChild(status);\n"
44.108 + + "var span = window.document.createElement('span');"
44.109 + + "span.innerHTML = ' ';\n"
44.110 + + "li.appendChild(span);\n"
44.111 + + "li.appendChild(details);\n"
44.112 + + "p.appendChild(pre);\n"
44.113 + + "ul.appendChild(li);\n"
44.114 + + "arr[0] = pre;\n"
44.115 + + "arr[1] = status;\n"
44.116 + )
44.117 + private static native void beginTest(String test, Case c, Object[] arr);
44.118 +
44.119 + @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
44.120 + + "var request = new XMLHttpRequest();\n"
44.121 + + "request.open('GET', url, true);\n"
44.122 + + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
44.123 + + "request.onreadystatechange = function() {\n"
44.124 + + " if (this.readyState!==4) return;\n"
44.125 + + " arr[0] = this.responseText;\n"
44.126 + + " callback.run__V();\n"
44.127 + + "};"
44.128 + + "request.send();"
44.129 + )
44.130 + private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
44.131 +
44.132 + public static void harness(String url) throws IOException {
44.133 + log("Connecting to " + url);
44.134 + Request r = new Request(url);
44.135 + }
44.136 +
44.137 + private static class Request implements Runnable {
44.138 + private final String[] arr = { null };
44.139 + private final String url;
44.140 + private Case c;
44.141 + private int retries;
44.142 +
44.143 + private Request(String url) throws IOException {
44.144 + this.url = url;
44.145 + loadText(url, this, arr);
44.146 + }
44.147 + private Request(String url, String u) throws IOException {
44.148 + this.url = url;
44.149 + loadText(u, this, arr);
44.150 + }
44.151 +
44.152 + @Override
44.153 + public void run() {
44.154 + try {
44.155 + if (c == null) {
44.156 + String data = arr[0];
44.157 +
44.158 + if (data == null) {
44.159 + log("Some error exiting");
44.160 + closeWindow();
44.161 + return;
44.162 + }
44.163 +
44.164 + if (data.isEmpty()) {
44.165 + log("No data, exiting");
44.166 + closeWindow();
44.167 + return;
44.168 + }
44.169 +
44.170 + c = Case.parseData(data);
44.171 + beginTest(c);
44.172 + log("Got \"" + data + "\"");
44.173 + } else {
44.174 + log("Processing \"" + arr[0] + "\" for " + retries + " time");
44.175 + }
44.176 + Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
44.177 + finishTest(c, result);
44.178 +
44.179 + String u = url + "?request=" + c.getRequestId() + "&result=" + result;
44.180 + new Request(url, u);
44.181 + } catch (Exception ex) {
44.182 + if (ex instanceof InterruptedException) {
44.183 + log("Re-scheduling in 100ms");
44.184 + schedule(this, 100);
44.185 + return;
44.186 + }
44.187 + log(ex.getClass().getName() + ":" + ex.getMessage());
44.188 + }
44.189 + }
44.190 + }
44.191 +
44.192 + private static String encodeURL(String r) throws UnsupportedEncodingException {
44.193 + final String SPECIAL = "%$&+,/:;=?@";
44.194 + StringBuilder sb = new StringBuilder();
44.195 + byte[] utf8 = r.getBytes("UTF-8");
44.196 + for (int i = 0; i < utf8.length; i++) {
44.197 + int ch = utf8[i] & 0xff;
44.198 + if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
44.199 + final String numbers = "0" + Integer.toHexString(ch);
44.200 + sb.append("%").append(numbers.substring(numbers.length() - 2));
44.201 + } else {
44.202 + if (ch == 32) {
44.203 + sb.append("+");
44.204 + } else {
44.205 + sb.append((char)ch);
44.206 + }
44.207 + }
44.208 + }
44.209 + return sb.toString();
44.210 + }
44.211 +
44.212 + static String invoke(String clazz, String method) throws
44.213 + ClassNotFoundException, InvocationTargetException, IllegalAccessException,
44.214 + InstantiationException, InterruptedException {
44.215 + final Object r = new Case(null).invokeMethod(clazz, method);
44.216 + return r == null ? "null" : r.toString().toString();
44.217 + }
44.218 +
44.219 + /** Helper method that inspects the classpath and loads given resource
44.220 + * (usually a class file). Used while running tests in Rhino.
44.221 + *
44.222 + * @param name resource name to find
44.223 + * @return the array of bytes in the given resource
44.224 + * @throws IOException I/O in case something goes wrong
44.225 + */
44.226 + public static byte[] read(String name) throws IOException {
44.227 + URL u = null;
44.228 + Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
44.229 + while (en.hasMoreElements()) {
44.230 + u = en.nextElement();
44.231 + }
44.232 + if (u == null) {
44.233 + throw new IOException("Can't find " + name);
44.234 + }
44.235 + try (InputStream is = u.openStream()) {
44.236 + byte[] arr;
44.237 + arr = new byte[is.available()];
44.238 + int offset = 0;
44.239 + while (offset < arr.length) {
44.240 + int len = is.read(arr, offset, arr.length - offset);
44.241 + if (len == -1) {
44.242 + throw new IOException("Can't read " + name);
44.243 + }
44.244 + offset += len;
44.245 + }
44.246 + return arr;
44.247 + }
44.248 + }
44.249 +
44.250 + @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
44.251 + private static void turnAssetionStatusOn() {
44.252 + }
44.253 +
44.254 + @JavaScriptBody(args = {"r", "time"}, body =
44.255 + "return window.setTimeout(function() { r.run__V(); }, time);")
44.256 + private static native Object schedule(Runnable r, int time);
44.257 +
44.258 + private static final class Case {
44.259 + private final Object data;
44.260 + private Object inst;
44.261 +
44.262 + private Case(Object data) {
44.263 + this.data = data;
44.264 + }
44.265 +
44.266 + public static Case parseData(String s) {
44.267 + return new Case(toJSON(s));
44.268 + }
44.269 +
44.270 + public String getMethodName() {
44.271 + return value("methodName", data);
44.272 + }
44.273 +
44.274 + public String getClassName() {
44.275 + return value("className", data);
44.276 + }
44.277 +
44.278 + public String getRequestId() {
44.279 + return value("request", data);
44.280 + }
44.281 +
44.282 + public String getHtmlFragment() {
44.283 + return value("html", data);
44.284 + }
44.285 +
44.286 + void again(Object[] arr) {
44.287 + try {
44.288 + textArea = arr[0];
44.289 + statusArea = arr[1];
44.290 + setAttr(textArea, "value", "");
44.291 + runTest();
44.292 + } catch (Exception ex) {
44.293 + log(ex.getClass().getName() + ":" + ex.getMessage());
44.294 + }
44.295 + }
44.296 +
44.297 + private Object runTest() throws IllegalAccessException,
44.298 + IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
44.299 + InvocationTargetException, InstantiationException, InterruptedException {
44.300 + if (this.getHtmlFragment() != null) {
44.301 + setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
44.302 + }
44.303 + log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
44.304 + Object result = invokeMethod(this.getClassName(), this.getMethodName());
44.305 + setAttr("bck2brwsr.fragment", "innerHTML", "");
44.306 + log("Result: " + result);
44.307 + result = encodeURL("" + result);
44.308 + log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
44.309 + return result;
44.310 + }
44.311 +
44.312 + private Object invokeMethod(String clazz, String method)
44.313 + throws ClassNotFoundException, InvocationTargetException,
44.314 + InterruptedException, IllegalAccessException, IllegalArgumentException,
44.315 + InstantiationException {
44.316 + Method found = null;
44.317 + Class<?> c = Class.forName(clazz);
44.318 + for (Method m : c.getMethods()) {
44.319 + if (m.getName().equals(method)) {
44.320 + found = m;
44.321 + }
44.322 + }
44.323 + Object res;
44.324 + if (found != null) {
44.325 + try {
44.326 + if ((found.getModifiers() & Modifier.STATIC) != 0) {
44.327 + res = found.invoke(null);
44.328 + } else {
44.329 + if (inst == null) {
44.330 + inst = c.newInstance();
44.331 + }
44.332 + res = found.invoke(inst);
44.333 + }
44.334 + } catch (Throwable ex) {
44.335 + if (ex instanceof InvocationTargetException) {
44.336 + ex = ((InvocationTargetException) ex).getTargetException();
44.337 + }
44.338 + if (ex instanceof InterruptedException) {
44.339 + throw (InterruptedException)ex;
44.340 + }
44.341 + res = ex.getClass().getName() + ":" + ex.getMessage();
44.342 + }
44.343 + } else {
44.344 + res = "Can't find method " + method + " in " + clazz;
44.345 + }
44.346 + return res;
44.347 + }
44.348 +
44.349 + @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
44.350 + private static native Object toJSON(String s);
44.351 +
44.352 + @JavaScriptBody(args = {"p", "d"}, body =
44.353 + "var v = d[p];\n"
44.354 + + "if (typeof v === 'undefined') return null;\n"
44.355 + + "return v.toString();"
44.356 + )
44.357 + private static native String value(String p, Object d);
44.358 + }
44.359 +}
45.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
45.2 +++ b/launcher/http/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Fri May 24 18:04:55 2013 +0200
45.3 @@ -0,0 +1,43 @@
45.4 +<?xml version="1.0" encoding="UTF-8"?>
45.5 +<!--
45.6 +
45.7 + Back 2 Browser Bytecode Translator
45.8 + Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
45.9 +
45.10 + This program is free software: you can redistribute it and/or modify
45.11 + it under the terms of the GNU General Public License as published by
45.12 + the Free Software Foundation, version 2 of the License.
45.13 +
45.14 + This program is distributed in the hope that it will be useful,
45.15 + but WITHOUT ANY WARRANTY; without even the implied warranty of
45.16 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45.17 + GNU General Public License for more details.
45.18 +
45.19 + You should have received a copy of the GNU General Public License
45.20 + along with this program. Look for COPYING file in the top folder.
45.21 + If not, see http://opensource.org/licenses/GPL-2.0.
45.22 +
45.23 +-->
45.24 +<!DOCTYPE html>
45.25 +<html xmlns="http://www.w3.org/1999/xhtml">
45.26 + <head>
45.27 + <title>Bck2Brwsr Harness</title>
45.28 + </head>
45.29 + <body>
45.30 + <script src="/bck2brwsr.js"></script>
45.31 + <script>
45.32 + var vm = bck2brwsr();
45.33 + </script>
45.34 +
45.35 + <h1>Bck2Brwsr Execution Harness</h1>
45.36 +
45.37 + <ul id="bck2brwsr.result" style="width: 100%;" >
45.38 + </ul>
45.39 +
45.40 + <div id="bck2brwsr.fragment"/>
45.41 +
45.42 + <script type="text/javascript">
45.43 + vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console').harness__VLjava_lang_String_2('$U/../data');
45.44 + </script>
45.45 + </body>
45.46 +</html>
46.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
46.2 +++ b/launcher/pom.xml Fri May 24 18:04:55 2013 +0200
46.3 @@ -0,0 +1,22 @@
46.4 +<?xml version="1.0" encoding="UTF-8"?>
46.5 +<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">
46.6 + <modelVersion>4.0.0</modelVersion>
46.7 + <parent>
46.8 + <artifactId>bck2brwsr</artifactId>
46.9 + <groupId>org.apidesign</groupId>
46.10 + <version>0.8-SNAPSHOT</version>
46.11 + </parent>
46.12 + <groupId>org.apidesign.bck2brwsr</groupId>
46.13 + <artifactId>launcher-pom</artifactId>
46.14 + <version>0.8-SNAPSHOT</version>
46.15 + <packaging>pom</packaging>
46.16 + <name>Launchers</name>
46.17 + <properties>
46.18 + <grizzly.version>2.3.2</grizzly.version>
46.19 + </properties>
46.20 + <modules>
46.21 + <module>api</module>
46.22 + <module>http</module>
46.23 + <module>fx</module>
46.24 + </modules>
46.25 +</project>
46.26 \ No newline at end of file
47.1 --- a/pom.xml Fri May 24 16:45:32 2013 +0200
47.2 +++ b/pom.xml Fri May 24 18:04:55 2013 +0200
47.3 @@ -3,7 +3,7 @@
47.4 <modelVersion>4.0.0</modelVersion>
47.5 <groupId>org.apidesign</groupId>
47.6 <artifactId>bck2brwsr</artifactId>
47.7 - <version>0.6-SNAPSHOT</version>
47.8 + <version>0.8-SNAPSHOT</version>
47.9 <packaging>pom</packaging>
47.10 <name>Back 2 Browser</name>
47.11 <parent>
47.12 @@ -21,6 +21,7 @@
47.13 <module>javaquery</module>
47.14 <module>benchmarks</module>
47.15 <module>ide</module>
47.16 + <module>launcher</module>
47.17 <module>rt</module>
47.18 </modules>
47.19 <licenses>
47.20 @@ -86,7 +87,7 @@
47.21 <exclude>rt/emul/*/src/main/**</exclude>
47.22 <exclude>rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeParser.java</exclude>
47.23 <exclude>rt/archetype/src/main/resources/archetype-resources/**</exclude>
47.24 - <exclude>rt/vmtest/src/test/resources/**</exclude>
47.25 + <exclude>rt/emul/compact/src/test/resources/**</exclude>
47.26 <exclude>dew/src/main/resources/org/apidesign/bck2brwsr/dew/**</exclude>
47.27 <exclude>javaquery/api/src/main/resources/org/apidesign/bck2brwsr/htmlpage/knockout*.js</exclude>
47.28 </excludes>
47.29 @@ -256,4 +257,4 @@
47.30 </dependency>
47.31 </dependencies>
47.32 </dependencyManagement>
47.33 -</project>
47.34 \ No newline at end of file
47.35 +</project>
48.1 --- a/rt/archetype/pom.xml Fri May 24 16:45:32 2013 +0200
48.2 +++ b/rt/archetype/pom.xml Fri May 24 18:04:55 2013 +0200
48.3 @@ -4,11 +4,11 @@
48.4 <parent>
48.5 <artifactId>rt</artifactId>
48.6 <groupId>org.apidesign.bck2brwsr</groupId>
48.7 - <version>0.6-SNAPSHOT</version>
48.8 + <version>0.8-SNAPSHOT</version>
48.9 </parent>
48.10 <groupId>org.apidesign.bck2brwsr</groupId>
48.11 <artifactId>bck2brwsr-archetype-html-sample</artifactId>
48.12 - <version>0.6-SNAPSHOT</version>
48.13 + <version>0.8-SNAPSHOT</version>
48.14 <packaging>jar</packaging>
48.15 <name>Bck2Brwsr Maven Archetype</name>
48.16 <description>
48.17 @@ -17,6 +17,22 @@
48.18 virtual machine.
48.19 </description>
48.20 <build>
48.21 + <resources>
48.22 + <resource>
48.23 + <directory>src/main/resources</directory>
48.24 + <filtering>true</filtering>
48.25 + <includes>
48.26 + <include>**/pom.xml</include>
48.27 + </includes>
48.28 + </resource>
48.29 + <resource>
48.30 + <directory>src/main/resources</directory>
48.31 + <filtering>false</filtering>
48.32 + <excludes>
48.33 + <exclude>**/pom.xml</exclude>
48.34 + </excludes>
48.35 + </resource>
48.36 + </resources>
48.37 <plugins>
48.38 <plugin>
48.39 <groupId>org.apache.maven.plugins</groupId>
48.40 @@ -29,6 +45,15 @@
48.41 </plugin>
48.42 <plugin>
48.43 <groupId>org.apache.maven.plugins</groupId>
48.44 + <artifactId>maven-resources-plugin</artifactId>
48.45 + <version>2.6</version>
48.46 + <configuration>
48.47 + <escapeString>\</escapeString>
48.48 + <target>1.6</target>
48.49 + </configuration>
48.50 + </plugin>
48.51 + <plugin>
48.52 + <groupId>org.apache.maven.plugins</groupId>
48.53 <artifactId>maven-surefire-plugin</artifactId>
48.54 <configuration>
48.55 <skipTests>true</skipTests>
49.1 --- a/rt/archetype/src/main/resources/archetype-resources/nbactions.xml Fri May 24 16:45:32 2013 +0200
49.2 +++ b/rt/archetype/src/main/resources/archetype-resources/nbactions.xml Fri May 24 18:04:55 2013 +0200
49.3 @@ -4,7 +4,7 @@
49.4 <actionName>run</actionName>
49.5 <goals>
49.6 <goal>process-classes</goal>
49.7 - <goal>org.apidesign.bck2brwsr:mojo:0.6-SNAPSHOT:brwsr</goal>
49.8 + <goal>bck2brwsr:brwsr</goal>
49.9 </goals>
49.10 </action>
49.11 </actions>
50.1 --- a/rt/archetype/src/main/resources/archetype-resources/pom.xml Fri May 24 16:45:32 2013 +0200
50.2 +++ b/rt/archetype/src/main/resources/archetype-resources/pom.xml Fri May 24 18:04:55 2013 +0200
50.3 @@ -3,12 +3,12 @@
50.4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
50.5 <modelVersion>4.0.0</modelVersion>
50.6
50.7 - <groupId>${groupId}</groupId>
50.8 - <artifactId>${artifactId}</artifactId>
50.9 - <version>${version}</version>
50.10 + <groupId>\${groupId}</groupId>
50.11 + <artifactId>\${artifactId}</artifactId>
50.12 + <version>\${version}</version>
50.13 <packaging>jar</packaging>
50.14
50.15 - <name>${artifactId}</name>
50.16 + <name>\${artifactId}</name>
50.17
50.18 <repositories>
50.19 <repository>
50.20 @@ -43,8 +43,8 @@
50.21 <plugins>
50.22 <plugin>
50.23 <groupId>org.apidesign.bck2brwsr</groupId>
50.24 - <artifactId>mojo</artifactId>
50.25 - <version>0.6-SNAPSHOT</version>
50.26 + <artifactId>bck2brwsr-maven-plugin</artifactId>
50.27 + <version>${project.version}</version>
50.28 <executions>
50.29 <execution>
50.30 <goals>
50.31 @@ -53,7 +53,7 @@
50.32 </execution>
50.33 </executions>
50.34 <configuration>
50.35 - <startpage>${package.replace('.','/')}/index.html</startpage>
50.36 + <startpage>\${package.replace('.','/')}/index.html</startpage>
50.37 </configuration>
50.38 </plugin>
50.39 <plugin>
50.40 @@ -103,13 +103,13 @@
50.41 <dependency>
50.42 <groupId>org.apidesign.bck2brwsr</groupId>
50.43 <artifactId>emul</artifactId>
50.44 - <version>0.6-SNAPSHOT</version>
50.45 + <version>${project.version}</version>
50.46 <classifier>rt</classifier>
50.47 </dependency>
50.48 <dependency>
50.49 <groupId>org.apidesign.bck2brwsr</groupId>
50.50 <artifactId>javaquery.api</artifactId>
50.51 - <version>0.6-SNAPSHOT</version>
50.52 + <version>${project.version}</version>
50.53 </dependency>
50.54 <dependency>
50.55 <groupId>org.testng</groupId>
50.56 @@ -122,13 +122,13 @@
50.57 <artifactId>vm4brwsr</artifactId>
50.58 <classifier>js</classifier>
50.59 <type>zip</type>
50.60 - <version>0.6-SNAPSHOT</version>
50.61 + <version>${project.version}</version>
50.62 <scope>provided</scope>
50.63 </dependency>
50.64 <dependency>
50.65 <groupId>org.apidesign.bck2brwsr</groupId>
50.66 <artifactId>vmtest</artifactId>
50.67 - <version>0.6-SNAPSHOT</version>
50.68 + <version>${project.version}</version>
50.69 <scope>test</scope>
50.70 </dependency>
50.71 </dependencies>
51.1 --- a/rt/archetype/src/test/java/org/apidesign/bck2brwsr/archetype/ArchetypeVersionTest.java Fri May 24 16:45:32 2013 +0200
51.2 +++ b/rt/archetype/src/test/java/org/apidesign/bck2brwsr/archetype/ArchetypeVersionTest.java Fri May 24 18:04:55 2013 +0200
51.3 @@ -24,7 +24,6 @@
51.4 import javax.xml.xpath.XPathExpression;
51.5 import javax.xml.xpath.XPathFactory;
51.6 import org.testng.annotations.Test;
51.7 -import org.xml.sax.InputSource;
51.8 import static org.testng.Assert.*;
51.9 import org.testng.annotations.BeforeClass;
51.10 import org.w3c.dom.Document;
51.11 @@ -95,9 +94,7 @@
51.12 for (int i = 0; i < goals.getLength(); i++) {
51.13 String s = goals.item(i).getTextContent();
51.14 if (s.contains("bck2brwsr")) {
51.15 - String[] arr = s.split(":");
51.16 - assertEquals(arr.length, 4, "Three :");
51.17 - assertEquals(arr[2], version, "Proper version is used");
51.18 + assertFalse(s.matches(".*bck2brwsr.*[0-9].*"), "No numbers: " + s);
51.19 }
51.20 }
51.21 }
52.1 --- a/rt/core/pom.xml Fri May 24 16:45:32 2013 +0200
52.2 +++ b/rt/core/pom.xml Fri May 24 18:04:55 2013 +0200
52.3 @@ -4,11 +4,11 @@
52.4 <parent>
52.5 <groupId>org.apidesign.bck2brwsr</groupId>
52.6 <artifactId>rt</artifactId>
52.7 - <version>0.6-SNAPSHOT</version>
52.8 + <version>0.8-SNAPSHOT</version>
52.9 </parent>
52.10 <groupId>org.apidesign.bck2brwsr</groupId>
52.11 <artifactId>core</artifactId>
52.12 - <version>0.6-SNAPSHOT</version>
52.13 + <version>0.8-SNAPSHOT</version>
52.14 <name>Bck2Brwsr Native Annotations</name>
52.15 <url>http://maven.apache.org</url>
52.16 <build>
53.1 --- a/rt/emul/brwsrtest/pom.xml Fri May 24 16:45:32 2013 +0200
53.2 +++ b/rt/emul/brwsrtest/pom.xml Fri May 24 18:04:55 2013 +0200
53.3 @@ -4,11 +4,11 @@
53.4 <parent>
53.5 <groupId>org.apidesign.bck2brwsr</groupId>
53.6 <artifactId>emul.pom</artifactId>
53.7 - <version>0.6-SNAPSHOT</version>
53.8 + <version>0.8-SNAPSHOT</version>
53.9 </parent>
53.10 <groupId>org.apidesign.bck2brwsr</groupId>
53.11 <artifactId>brwsrtest</artifactId>
53.12 - <version>0.6-SNAPSHOT</version>
53.13 + <version>0.8-SNAPSHOT</version>
53.14 <name>Tests Inside Real Browser</name>
53.15 <url>http://maven.apache.org</url>
53.16 <properties>
53.17 @@ -42,5 +42,11 @@
53.18 <version>${project.version}</version>
53.19 <scope>test</scope>
53.20 </dependency>
53.21 + <dependency>
53.22 + <groupId>${project.groupId}</groupId>
53.23 + <artifactId>launcher.http</artifactId>
53.24 + <version>${project.version}</version>
53.25 + <scope>provided</scope>
53.26 + </dependency>
53.27 </dependencies>
53.28 </project>
54.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
54.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/BooleanTest.java Fri May 24 18:04:55 2013 +0200
54.3 @@ -0,0 +1,49 @@
54.4 +/**
54.5 + * Back 2 Browser Bytecode Translator
54.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
54.7 + *
54.8 + * This program is free software: you can redistribute it and/or modify
54.9 + * it under the terms of the GNU General Public License as published by
54.10 + * the Free Software Foundation, version 2 of the License.
54.11 + *
54.12 + * This program is distributed in the hope that it will be useful,
54.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
54.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54.15 + * GNU General Public License for more details.
54.16 + *
54.17 + * You should have received a copy of the GNU General Public License
54.18 + * along with this program. Look for COPYING file in the top folder.
54.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
54.20 + */
54.21 +package org.apidesign.bck2brwsr.brwsrtest;
54.22 +
54.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
54.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
54.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
54.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
54.27 +import org.testng.annotations.Factory;
54.28 +
54.29 +/**
54.30 + *
54.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
54.32 + */
54.33 +public class BooleanTest {
54.34 + @JavaScriptBody(args = { "tr" }, body = "return tr ? true : false;")
54.35 + private static native Object trueFalse(boolean tr);
54.36 +
54.37 + @BrwsrTest public void isTrueInstanceOfBoolean() {
54.38 + Object t = trueFalse(true);
54.39 + assert t instanceof Boolean : "Should be boolean: " + t;
54.40 + assert ((boolean)t) : "and is true";
54.41 + }
54.42 +
54.43 + @BrwsrTest public void isFalseInstanceOfBoolean() {
54.44 + Object t = trueFalse(false);
54.45 + assert t instanceof Boolean : "Should be boolean: " + t;
54.46 + assert !((boolean)t) : "and is false: " + t;
54.47 + }
54.48 +
54.49 + @Factory public static Object[] create() {
54.50 + return VMTest.create(BooleanTest.class);
54.51 + }
54.52 +}
55.1 --- a/rt/emul/compact/pom.xml Fri May 24 16:45:32 2013 +0200
55.2 +++ b/rt/emul/compact/pom.xml Fri May 24 18:04:55 2013 +0200
55.3 @@ -4,11 +4,11 @@
55.4 <parent>
55.5 <groupId>org.apidesign.bck2brwsr</groupId>
55.6 <artifactId>emul.pom</artifactId>
55.7 - <version>0.6-SNAPSHOT</version>
55.8 + <version>0.8-SNAPSHOT</version>
55.9 </parent>
55.10 <groupId>org.apidesign.bck2brwsr</groupId>
55.11 <artifactId>emul</artifactId>
55.12 - <version>0.6-SNAPSHOT</version>
55.13 + <version>0.8-SNAPSHOT</version>
55.14 <name>Bck2Brwsr API Profile</name>
55.15 <url>http://maven.apache.org</url>
55.16 <properties>
55.17 @@ -28,6 +28,18 @@
55.18 <scope>test</scope>
55.19 </dependency>
55.20 <dependency>
55.21 + <groupId>${project.groupId}</groupId>
55.22 + <artifactId>launcher.http</artifactId>
55.23 + <version>${project.version}</version>
55.24 + <scope>test</scope>
55.25 + <exclusions>
55.26 + <exclusion>
55.27 + <groupId>com.oracle</groupId>
55.28 + <artifactId>javafx</artifactId>
55.29 + </exclusion>
55.30 + </exclusions>
55.31 + </dependency>
55.32 + <dependency>
55.33 <groupId>org.netbeans.api</groupId>
55.34 <artifactId>org-openide-util-lookup</artifactId>
55.35 <scope>test</scope>
56.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java Fri May 24 16:45:32 2013 +0200
56.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java Fri May 24 18:04:55 2013 +0200
56.3 @@ -30,7 +30,7 @@
56.4 public class ZipCompatibilityTest {
56.5 @Compare
56.6 public String testDemoStaticCalculator() throws IOException {
56.7 - InputStream is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
56.8 + InputStream is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
56.9 ZipArchive zip = ZipArchive.createZip(is);
56.10 final String ts = zip.toString();
56.11 return ts.substring(0, 4096) + ts.hashCode();
57.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipVsJzLibTest.java Fri May 24 16:45:32 2013 +0200
57.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipVsJzLibTest.java Fri May 24 18:04:55 2013 +0200
57.3 @@ -27,10 +27,10 @@
57.4 */
57.5 public class ZipVsJzLibTest {
57.6 @Test public void r() throws IOException {
57.7 - InputStream is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
57.8 + InputStream is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
57.9 ZipArchive zip = ZipArchive.createZip(is);
57.10
57.11 - is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
57.12 + is = getClass().getResourceAsStream("demo.static.calculator-TEST.jar");
57.13 ZipArchive real = ZipArchive.createReal(is);
57.14
57.15 real.assertEquals(zip, "Are they the same?");
58.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
58.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/AssertionTest.java Fri May 24 18:04:55 2013 +0200
58.3 @@ -0,0 +1,43 @@
58.4 +/**
58.5 + * Back 2 Browser Bytecode Translator
58.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
58.7 + *
58.8 + * This program is free software: you can redistribute it and/or modify
58.9 + * it under the terms of the GNU General Public License as published by
58.10 + * the Free Software Foundation, version 2 of the License.
58.11 + *
58.12 + * This program is distributed in the hope that it will be useful,
58.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
58.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58.15 + * GNU General Public License for more details.
58.16 + *
58.17 + * You should have received a copy of the GNU General Public License
58.18 + * along with this program. Look for COPYING file in the top folder.
58.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
58.20 + */
58.21 +package org.apidesign.bck2brwsr.tck;
58.22 +
58.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
58.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
58.25 +import org.testng.annotations.Factory;
58.26 +
58.27 +/**
58.28 + *
58.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
58.30 + */
58.31 +public class AssertionTest {
58.32 +
58.33 + @Compare public Object checkAssert() throws ClassNotFoundException {
58.34 + try {
58.35 + assert false : "Is assertion status on?";
58.36 + return null;
58.37 + } catch (AssertionError ex) {
58.38 + return ex.getClass().getName();
58.39 + }
58.40 + }
58.41 +
58.42 + @Factory
58.43 + public static Object[] create() {
58.44 + return VMTest.create(AssertionTest.class);
58.45 + }
58.46 +}
59.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
59.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java Fri May 24 18:04:55 2013 +0200
59.3 @@ -0,0 +1,57 @@
59.4 +/**
59.5 + * Back 2 Browser Bytecode Translator
59.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
59.7 + *
59.8 + * This program is free software: you can redistribute it and/or modify
59.9 + * it under the terms of the GNU General Public License as published by
59.10 + * the Free Software Foundation, version 2 of the License.
59.11 + *
59.12 + * This program is distributed in the hope that it will be useful,
59.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
59.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
59.15 + * GNU General Public License for more details.
59.16 + *
59.17 + * You should have received a copy of the GNU General Public License
59.18 + * along with this program. Look for COPYING file in the top folder.
59.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
59.20 + */
59.21 +package org.apidesign.bck2brwsr.tck;
59.22 +
59.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
59.24 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
59.25 +import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
59.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
59.27 +import org.testng.annotations.Factory;
59.28 +
59.29 +/**
59.30 + *
59.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
59.32 + */
59.33 +public class BrwsrCheckTest {
59.34 +
59.35 + @BrwsrTest public void assertWindowObjectIsDefined() {
59.36 + assert window() != null : "No window object found!";
59.37 + }
59.38 +
59.39 +
59.40 +
59.41 +
59.42 + @HtmlFragment("<h1 id='hello'>\n"
59.43 + + "Hello!\n"
59.44 + + "</h1>\n")
59.45 + @BrwsrTest public void accessProvidedFragment() {
59.46 + assert getElementById("hello") != null : "Element with 'hello' ID found";
59.47 + }
59.48 +
59.49 + @Factory
59.50 + public static Object[] create() {
59.51 + return VMTest.create(BrwsrCheckTest.class);
59.52 + }
59.53 +
59.54 +
59.55 + @JavaScriptBody(args = {}, body = "return window;")
59.56 + private static native Object window();
59.57 +
59.58 + @JavaScriptBody(args = { "id" }, body = "return window.document.getElementById(id);")
59.59 + private static native Object getElementById(String id);
59.60 +}
60.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
60.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java Fri May 24 18:04:55 2013 +0200
60.3 @@ -0,0 +1,147 @@
60.4 +/**
60.5 + * Back 2 Browser Bytecode Translator
60.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
60.7 + *
60.8 + * This program is free software: you can redistribute it and/or modify
60.9 + * it under the terms of the GNU General Public License as published by
60.10 + * the Free Software Foundation, version 2 of the License.
60.11 + *
60.12 + * This program is distributed in the hope that it will be useful,
60.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
60.15 + * GNU General Public License for more details.
60.16 + *
60.17 + * You should have received a copy of the GNU General Public License
60.18 + * along with this program. Look for COPYING file in the top folder.
60.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
60.20 + */
60.21 +package org.apidesign.bck2brwsr.tck;
60.22 +
60.23 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
60.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
60.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
60.26 +import org.testng.annotations.Factory;
60.27 +
60.28 +/**
60.29 + *
60.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
60.31 + */
60.32 +public class ByteArithmeticTest {
60.33 +
60.34 + private static byte add(byte x, byte y) {
60.35 + return (byte)(x + y);
60.36 + }
60.37 +
60.38 + private static byte sub(byte x, byte y) {
60.39 + return (byte)(x - y);
60.40 + }
60.41 +
60.42 + private static byte mul(byte x, byte y) {
60.43 + return (byte)(x * y);
60.44 + }
60.45 +
60.46 + private static byte div(byte x, byte y) {
60.47 + return (byte)(x / y);
60.48 + }
60.49 +
60.50 + private static byte mod(byte x, byte y) {
60.51 + return (byte)(x % y);
60.52 + }
60.53 +
60.54 + @Compare public byte conversion() {
60.55 + return (byte)123456;
60.56 + }
60.57 +
60.58 + @Compare public byte addOverflow() {
60.59 + return add(Byte.MAX_VALUE, (byte)1);
60.60 + }
60.61 +
60.62 + @Compare public byte subUnderflow() {
60.63 + return sub(Byte.MIN_VALUE, (byte)1);
60.64 + }
60.65 +
60.66 + @Compare public byte addMaxByteAndMaxByte() {
60.67 + return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
60.68 + }
60.69 +
60.70 + @Compare public byte subMinByteAndMinByte() {
60.71 + return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
60.72 + }
60.73 +
60.74 + @Compare public byte multiplyMaxByte() {
60.75 + return mul(Byte.MAX_VALUE, (byte)2);
60.76 + }
60.77 +
60.78 + @Compare public byte multiplyMaxByteAndMaxByte() {
60.79 + return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
60.80 + }
60.81 +
60.82 + @Compare public byte multiplyMinByte() {
60.83 + return mul(Byte.MIN_VALUE, (byte)2);
60.84 + }
60.85 +
60.86 + @Compare public byte multiplyMinByteAndMinByte() {
60.87 + return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
60.88 + }
60.89 +
60.90 + @Compare public byte multiplyPrecision() {
60.91 + return mul((byte)17638, (byte)1103);
60.92 + }
60.93 +
60.94 + @Compare public byte division() {
60.95 + return div((byte)1, (byte)2);
60.96 + }
60.97 +
60.98 + @Compare public byte divisionReminder() {
60.99 + return mod((byte)1, (byte)2);
60.100 + }
60.101 +
60.102 + private static int readShort(byte[] byteCodes, int offset) {
60.103 + int signed = byteCodes[offset];
60.104 + byte b0 = (byte)signed;
60.105 + return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
60.106 + }
60.107 +
60.108 + private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
60.109 + return readShort(byteCodes, offsetInstruction + 1);
60.110 + }
60.111 +
60.112 + @Compare public int readIntArgs255and156() {
60.113 + final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
60.114 +
60.115 + assert arr[1] == -1 : "First byte: " + arr[1];
60.116 + assert arr[2] == -100 : "Second byte: " + arr[2];
60.117 + final int ret = readShortArg(arr, 0);
60.118 + assert ret < 65000: "Value: " + ret;
60.119 + return ret;
60.120 + }
60.121 +
60.122 + @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
60.123 + private static byte[] fill255and156(byte[] arr) {
60.124 + arr[1] = (byte)255;
60.125 + arr[2] = (byte)156;
60.126 + return arr;
60.127 + }
60.128 +
60.129 + @Compare public int readIntArgs255and156JSArray() {
60.130 + final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
60.131 +
60.132 + final int ret = readShortArg(arr, 0);
60.133 + assert ret < 65000: "Value: " + ret;
60.134 + return ret;
60.135 + }
60.136 +
60.137 + @Compare public int readIntArgsMinus1andMinus100() {
60.138 + final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
60.139 +
60.140 + assert arr[1] == -1 : "First byte: " + arr[1];
60.141 + assert arr[2] == -100 : "Second byte: " + arr[2];
60.142 +
60.143 + return readShortArg(arr, 0);
60.144 + }
60.145 +
60.146 + @Factory
60.147 + public static Object[] create() {
60.148 + return VMTest.create(ByteArithmeticTest.class);
60.149 + }
60.150 +}
61.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
61.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Fri May 24 18:04:55 2013 +0200
61.3 @@ -0,0 +1,73 @@
61.4 +/**
61.5 + * Back 2 Browser Bytecode Translator
61.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
61.7 + *
61.8 + * This program is free software: you can redistribute it and/or modify
61.9 + * it under the terms of the GNU General Public License as published by
61.10 + * the Free Software Foundation, version 2 of the License.
61.11 + *
61.12 + * This program is distributed in the hope that it will be useful,
61.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
61.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61.15 + * GNU General Public License for more details.
61.16 + *
61.17 + * You should have received a copy of the GNU General Public License
61.18 + * along with this program. Look for COPYING file in the top folder.
61.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
61.20 + */
61.21 +package org.apidesign.bck2brwsr.tck;
61.22 +
61.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
61.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
61.25 +import org.testng.annotations.Factory;
61.26 +
61.27 +/**
61.28 + *
61.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
61.30 + */
61.31 +public class CloneTest {
61.32 + private int value;
61.33 +
61.34 + @Compare
61.35 + public Object notSupported() throws CloneNotSupportedException {
61.36 + return this.clone();
61.37 + }
61.38 +
61.39 + @Compare public String sameClass() throws CloneNotSupportedException {
61.40 + return new Clnbl().clone().getClass().getName();
61.41 + }
61.42 +
61.43 + @Compare public boolean differentInstance() throws CloneNotSupportedException {
61.44 + Clnbl orig = new Clnbl();
61.45 + return orig == orig.clone();
61.46 + }
61.47 +
61.48 + @Compare public int sameReference() throws CloneNotSupportedException {
61.49 + CloneTest self = this;
61.50 + Clnbl orig = new Clnbl();
61.51 + self.value = 33;
61.52 + orig.ref = self;
61.53 + return ((Clnbl)orig.clone()).ref.value;
61.54 + }
61.55 +
61.56 + @Compare public int sameValue() throws CloneNotSupportedException {
61.57 + Clnbl orig = new Clnbl();
61.58 + orig.value = 10;
61.59 + return ((Clnbl)orig.clone()).value;
61.60 + }
61.61 +
61.62 + @Factory
61.63 + public static Object[] create() {
61.64 + return VMTest.create(CloneTest.class);
61.65 + }
61.66 +
61.67 + public static final class Clnbl implements Cloneable {
61.68 + public CloneTest ref;
61.69 + private int value;
61.70 +
61.71 + @Override
61.72 + public Object clone() throws CloneNotSupportedException {
61.73 + return super.clone();
61.74 + }
61.75 + }
61.76 +}
62.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
62.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java Fri May 24 18:04:55 2013 +0200
62.3 @@ -0,0 +1,103 @@
62.4 +/**
62.5 + * Back 2 Browser Bytecode Translator
62.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
62.7 + *
62.8 + * This program is free software: you can redistribute it and/or modify
62.9 + * it under the terms of the GNU General Public License as published by
62.10 + * the Free Software Foundation, version 2 of the License.
62.11 + *
62.12 + * This program is distributed in the hope that it will be useful,
62.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
62.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
62.15 + * GNU General Public License for more details.
62.16 + *
62.17 + * You should have received a copy of the GNU General Public License
62.18 + * along with this program. Look for COPYING file in the top folder.
62.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
62.20 + */
62.21 +package org.apidesign.bck2brwsr.tck;
62.22 +
62.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
62.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
62.25 +import org.testng.annotations.Factory;
62.26 +
62.27 +/**
62.28 + *
62.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
62.30 + */
62.31 +public class CompareByteArrayTest {
62.32 + @Compare public int byteArraySum() {
62.33 + byte[] arr = createArray();
62.34 + return sumByteArr(arr);
62.35 + }
62.36 +
62.37 + @Compare public int countZeros() {
62.38 + int zeros = 0;
62.39 + for (Byte b : createArray()) {
62.40 + if (b == 0) {
62.41 + zeros++;
62.42 + }
62.43 + }
62.44 + return zeros;
62.45 + }
62.46 +
62.47 + private static int sumByteArr(byte[] arr) {
62.48 + int sum = 0;
62.49 + for (int i = 0; i < arr.length; i++) {
62.50 + sum += arr[i];
62.51 + }
62.52 + return sum;
62.53 + }
62.54 +
62.55 + @Compare public String noOutOfBounds() {
62.56 + return atIndex(1);
62.57 + }
62.58 +
62.59 + @Compare public String outOfBounds() {
62.60 + return atIndex(5);
62.61 + }
62.62 +
62.63 + @Compare public String outOfBoundsMinus() {
62.64 + return atIndex(-1);
62.65 + }
62.66 +
62.67 + @Compare public String toOfBounds() {
62.68 + return toIndex(5);
62.69 + }
62.70 +
62.71 + @Compare public String toOfBoundsMinus() {
62.72 + return toIndex(-1);
62.73 + }
62.74 +
62.75 + @Compare public int multiArrayLength() {
62.76 + int[][] arr = new int[1][0];
62.77 + return arr[0].length;
62.78 + }
62.79 +
62.80 + @Compare public int multiObjectArrayLength() {
62.81 + Object[][] arr = new Object[1][0];
62.82 + return arr[0].length;
62.83 + }
62.84 +
62.85 + private static final int[] arr = { 0, 1, 2 };
62.86 + public static String atIndex(int at) {
62.87 + return "at@" + arr[at];
62.88 + }
62.89 + public static String toIndex(int at) {
62.90 + arr[at] = 10;
62.91 + return "ok";
62.92 + }
62.93 +
62.94 +
62.95 + @Factory
62.96 + public static Object[] create() {
62.97 + return VMTest.create(CompareByteArrayTest.class);
62.98 + }
62.99 +
62.100 + private byte[] createArray() {
62.101 + byte[] arr = new byte[10];
62.102 + arr[5] = 3;
62.103 + arr[7] = 8;
62.104 + return arr;
62.105 + }
62.106 +}
63.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
63.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Fri May 24 18:04:55 2013 +0200
63.3 @@ -0,0 +1,50 @@
63.4 +/**
63.5 + * Back 2 Browser Bytecode Translator
63.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
63.7 + *
63.8 + * This program is free software: you can redistribute it and/or modify
63.9 + * it under the terms of the GNU General Public License as published by
63.10 + * the Free Software Foundation, version 2 of the License.
63.11 + *
63.12 + * This program is distributed in the hope that it will be useful,
63.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
63.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
63.15 + * GNU General Public License for more details.
63.16 + *
63.17 + * You should have received a copy of the GNU General Public License
63.18 + * along with this program. Look for COPYING file in the top folder.
63.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
63.20 + */
63.21 +package org.apidesign.bck2brwsr.tck;
63.22 +
63.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
63.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
63.25 +import org.testng.annotations.Factory;
63.26 +
63.27 +/**
63.28 + *
63.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
63.30 + */
63.31 +public class CompareHashTest {
63.32 + @Compare public int hashOfString() {
63.33 + return "Ahoj".hashCode();
63.34 + }
63.35 +
63.36 + @Compare public int hashRemainsYieldsZero() {
63.37 + Object o = new Object();
63.38 + return o.hashCode() - o.hashCode();
63.39 + }
63.40 +
63.41 + @Compare public int initializeInStatic() {
63.42 + return StaticUse.NON_NULL.hashCode() - StaticUse.NON_NULL.hashCode();
63.43 + }
63.44 +
63.45 + @Compare public int hashOfInt() {
63.46 + return Integer.valueOf(Integer.MAX_VALUE).hashCode();
63.47 + }
63.48 +
63.49 + @Factory
63.50 + public static Object[] create() {
63.51 + return VMTest.create(CompareHashTest.class);
63.52 + }
63.53 +}
64.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
64.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareIntArrayTest.java Fri May 24 18:04:55 2013 +0200
64.3 @@ -0,0 +1,63 @@
64.4 +/**
64.5 + * Back 2 Browser Bytecode Translator
64.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
64.7 + *
64.8 + * This program is free software: you can redistribute it and/or modify
64.9 + * it under the terms of the GNU General Public License as published by
64.10 + * the Free Software Foundation, version 2 of the License.
64.11 + *
64.12 + * This program is distributed in the hope that it will be useful,
64.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
64.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
64.15 + * GNU General Public License for more details.
64.16 + *
64.17 + * You should have received a copy of the GNU General Public License
64.18 + * along with this program. Look for COPYING file in the top folder.
64.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
64.20 + */
64.21 +package org.apidesign.bck2brwsr.tck;
64.22 +
64.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
64.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
64.25 +import org.testng.annotations.Factory;
64.26 +
64.27 +/**
64.28 + *
64.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
64.30 + */
64.31 +public class CompareIntArrayTest {
64.32 + @Compare public int integerArraySum() {
64.33 + int[] arr = createArray();
64.34 + return sumIntArr(arr);
64.35 + }
64.36 +
64.37 + @Compare public int countZeros() {
64.38 + int zeros = 0;
64.39 + for (Integer i : createArray()) {
64.40 + if (i == 0) {
64.41 + zeros++;
64.42 + }
64.43 + }
64.44 + return zeros;
64.45 + }
64.46 +
64.47 + private static int sumIntArr(int[] arr) {
64.48 + int sum = 0;
64.49 + for (int i = 0; i < arr.length; i++) {
64.50 + sum += arr[i];
64.51 + }
64.52 + return sum;
64.53 + }
64.54 +
64.55 + @Factory
64.56 + public static Object[] create() {
64.57 + return VMTest.create(CompareIntArrayTest.class);
64.58 + }
64.59 +
64.60 + private int[] createArray() {
64.61 + int[] arr = new int[10];
64.62 + arr[5] = 3;
64.63 + arr[7] = 8;
64.64 + return arr;
64.65 + }
64.66 +}
65.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
65.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Fri May 24 18:04:55 2013 +0200
65.3 @@ -0,0 +1,174 @@
65.4 +/**
65.5 + * Back 2 Browser Bytecode Translator
65.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
65.7 + *
65.8 + * This program is free software: you can redistribute it and/or modify
65.9 + * it under the terms of the GNU General Public License as published by
65.10 + * the Free Software Foundation, version 2 of the License.
65.11 + *
65.12 + * This program is distributed in the hope that it will be useful,
65.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
65.15 + * GNU General Public License for more details.
65.16 + *
65.17 + * You should have received a copy of the GNU General Public License
65.18 + * along with this program. Look for COPYING file in the top folder.
65.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
65.20 + */
65.21 +package org.apidesign.bck2brwsr.tck;
65.22 +
65.23 +import java.io.UnsupportedEncodingException;
65.24 +import java.net.MalformedURLException;
65.25 +import java.net.URL;
65.26 +import org.apidesign.bck2brwsr.vmtest.Compare;
65.27 +import org.apidesign.bck2brwsr.vmtest.VMTest;
65.28 +import org.testng.annotations.Factory;
65.29 +
65.30 +/**
65.31 + *
65.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
65.33 + */
65.34 +public class CompareStringsTest {
65.35 + @Compare public String firstChar() {
65.36 + return "" + ("Hello".toCharArray()[0]);
65.37 + }
65.38 +
65.39 + @Compare public String classCast() {
65.40 + Object o = firstChar();
65.41 + return String.class.cast(o);
65.42 + }
65.43 +
65.44 + @Compare public String classCastThrown() {
65.45 + Object o = null;
65.46 + return String.class.cast(o);
65.47 + }
65.48 +
65.49 + @Compare public boolean equalToNull() {
65.50 + return "Ahoj".equals(null);
65.51 + }
65.52 +
65.53 + @Compare public int highByteLenght() {
65.54 + byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
65.55 + return new String(arr, 0).length();
65.56 + }
65.57 +
65.58 + @Compare public String highByte() {
65.59 + byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
65.60 + StringBuilder sb = new StringBuilder();
65.61 + sb.append("pref:");
65.62 + sb.append(new String(arr, 0));
65.63 + return sb.toString();
65.64 + }
65.65 +
65.66 + @Compare public static Object compareURLs() throws MalformedURLException {
65.67 + return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
65.68 + }
65.69 +
65.70 + @Compare public String deleteLastTwoCharacters() {
65.71 + StringBuilder sb = new StringBuilder();
65.72 + sb.append("453.0");
65.73 + if (sb.toString().endsWith(".0")) {
65.74 + final int l = sb.length();
65.75 + sb.delete(l - 2, l);
65.76 + }
65.77 + return sb.toString().toString();
65.78 + }
65.79 +
65.80 + @Compare public String nameOfStringClass() throws Exception {
65.81 + return Class.forName("java.lang.String").getName();
65.82 + }
65.83 + @Compare public String nameOfArrayClass() throws Exception {
65.84 + return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
65.85 + }
65.86 +
65.87 + @Compare public String lowerHello() {
65.88 + return "HeLlO".toLowerCase();
65.89 + }
65.90 +
65.91 + @Compare public String lowerA() {
65.92 + return String.valueOf(Character.toLowerCase('A')).toString();
65.93 + }
65.94 + @Compare public String upperHello() {
65.95 + return "hello".toUpperCase();
65.96 + }
65.97 +
65.98 + @Compare public String upperA() {
65.99 + return String.valueOf(Character.toUpperCase('a')).toString();
65.100 + }
65.101 +
65.102 + @Compare public boolean matchRegExp() throws Exception {
65.103 + return "58038503".matches("\\d*");
65.104 + }
65.105 +
65.106 + @Compare public boolean doesNotMatchRegExp() throws Exception {
65.107 + return "58038503GH".matches("\\d*");
65.108 + }
65.109 +
65.110 + @Compare public boolean doesNotMatchRegExpFully() throws Exception {
65.111 + return "Hello".matches("Hell");
65.112 + }
65.113 +
65.114 + @Compare public String emptyCharArray() {
65.115 + char[] arr = new char[10];
65.116 + return new String(arr);
65.117 + }
65.118 +
65.119 + @Compare public String variousCharacterTests() throws Exception {
65.120 + StringBuilder sb = new StringBuilder();
65.121 +
65.122 + sb.append(Character.isUpperCase('a'));
65.123 + sb.append(Character.isUpperCase('A'));
65.124 + sb.append(Character.isLowerCase('a'));
65.125 + sb.append(Character.isLowerCase('A'));
65.126 +
65.127 + sb.append(Character.isLetter('A'));
65.128 + sb.append(Character.isLetterOrDigit('9'));
65.129 + sb.append(Character.isLetterOrDigit('A'));
65.130 + sb.append(Character.isLetter('0'));
65.131 +
65.132 + return sb.toString().toString();
65.133 + }
65.134 +
65.135 + @Compare
65.136 + public String nullFieldInitialized() {
65.137 + NullField nf = new NullField();
65.138 + return ("" + nf.name).toString();
65.139 + }
65.140 + @Compare
65.141 + public String toUTFString() throws UnsupportedEncodingException {
65.142 + byte[] arr = {
65.143 + (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
65.144 + (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
65.145 + (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
65.146 + (byte) -120
65.147 + };
65.148 + return new String(arr, "utf-8");
65.149 + }
65.150 +
65.151 + @Compare
65.152 + public int stringToBytesLenght() throws UnsupportedEncodingException {
65.153 + return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
65.154 + }
65.155 +
65.156 + @Compare public String replaceSeq() {
65.157 + return "Hello World.".replace(".", "!");
65.158 + }
65.159 + @Compare public String replaceSeqAll() {
65.160 + return "Hello World! Hello World.".replace("World", "Jarda");
65.161 + }
65.162 + @Compare public String replaceSeqAA() {
65.163 + String res = "aaa".replace("aa", "b");
65.164 + assert res.equals("ba") : "Expecting ba: " + res;
65.165 + return res;
65.166 + }
65.167 +
65.168 + @Factory
65.169 + public static Object[] create() {
65.170 + return VMTest.create(CompareStringsTest.class);
65.171 + }
65.172 +
65.173 + private static final class NullField {
65.174 +
65.175 + String name;
65.176 + }
65.177 +}
66.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
66.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/DoubleTest.java Fri May 24 18:04:55 2013 +0200
66.3 @@ -0,0 +1,65 @@
66.4 +/**
66.5 + * Back 2 Browser Bytecode Translator
66.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
66.7 + *
66.8 + * This program is free software: you can redistribute it and/or modify
66.9 + * it under the terms of the GNU General Public License as published by
66.10 + * the Free Software Foundation, version 2 of the License.
66.11 + *
66.12 + * This program is distributed in the hope that it will be useful,
66.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
66.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66.15 + * GNU General Public License for more details.
66.16 + *
66.17 + * You should have received a copy of the GNU General Public License
66.18 + * along with this program. Look for COPYING file in the top folder.
66.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
66.20 + */
66.21 +package org.apidesign.bck2brwsr.tck;
66.22 +
66.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
66.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
66.25 +import org.testng.annotations.Factory;
66.26 +
66.27 +/**
66.28 + *
66.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
66.30 + */
66.31 +public class DoubleTest {
66.32 + @Compare public String integerToString() {
66.33 + return toStr(1);
66.34 + }
66.35 +
66.36 + @Compare public String integerAndHalfToString() {
66.37 + return toStr(1.5);
66.38 + }
66.39 +
66.40 + @Compare public double longToAndBack() {
66.41 + return Double.parseDouble(toStr(Long.MAX_VALUE / 10));
66.42 + }
66.43 +
66.44 + @Compare public String negativeIntToString() {
66.45 + return toStr(-10);
66.46 + }
66.47 +
66.48 + @Compare public String negativeIntAndHalfToString() {
66.49 + return toStr(-10.5);
66.50 + }
66.51 +
66.52 + @Compare public double negativeLongAndBack() {
66.53 + return Double.parseDouble(toStr(Long.MIN_VALUE / 10));
66.54 + }
66.55 +
66.56 + @Compare public double canParseExp() {
66.57 + return Double.parseDouble(toStr(1.7976931348623157e+308));
66.58 + }
66.59 +
66.60 + private static String toStr(double d) {
66.61 + return Double.toString(d);
66.62 + }
66.63 +
66.64 + @Factory
66.65 + public static Object[] create() {
66.66 + return VMTest.create(DoubleTest.class);
66.67 + }
66.68 +}
67.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
67.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Fri May 24 18:04:55 2013 +0200
67.3 @@ -0,0 +1,106 @@
67.4 +/**
67.5 + * Back 2 Browser Bytecode Translator
67.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
67.7 + *
67.8 + * This program is free software: you can redistribute it and/or modify
67.9 + * it under the terms of the GNU General Public License as published by
67.10 + * the Free Software Foundation, version 2 of the License.
67.11 + *
67.12 + * This program is distributed in the hope that it will be useful,
67.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
67.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67.15 + * GNU General Public License for more details.
67.16 + *
67.17 + * You should have received a copy of the GNU General Public License
67.18 + * along with this program. Look for COPYING file in the top folder.
67.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
67.20 + */
67.21 +package org.apidesign.bck2brwsr.tck;
67.22 +
67.23 +import java.io.InputStream;
67.24 +import java.net.URL;
67.25 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
67.26 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
67.27 +import org.apidesign.bck2brwsr.vmtest.Http;
67.28 +import org.apidesign.bck2brwsr.vmtest.VMTest;
67.29 +import org.testng.annotations.Factory;
67.30 +
67.31 +/**
67.32 + *
67.33 + * @author Jaroslav Tulach <jtulach@netbeans.org>
67.34 + */
67.35 +public class HttpResourceTest {
67.36 +
67.37 + @Http({
67.38 + @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
67.39 + })
67.40 + @BrwsrTest
67.41 +
67.42 +
67.43 + public String testReadContentViaXHR() throws Exception {
67.44 + String msg = read("/xhr");
67.45 + assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
67.46 + return msg;
67.47 + }
67.48 +
67.49 + @Http({
67.50 + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
67.51 + })
67.52 + @BrwsrTest
67.53 + public String testReadContentViaURL() throws Exception {
67.54 + URL url = new URL("http:/url");
67.55 + String msg = (String) url.getContent();
67.56 + assert "Hello via URL!".equals(msg) : "The message was " + msg;
67.57 + return msg;
67.58 + }
67.59 + @Http({
67.60 + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
67.61 + })
67.62 + @BrwsrTest
67.63 + public String testReadContentViaURLWithStringParam() throws Exception {
67.64 + URL url = new URL("http:/url");
67.65 + String msg = (String) url.getContent(new Class[] { String.class });
67.66 + assert "Hello via URL!".equals(msg) : "The message was " + msg;
67.67 + return msg;
67.68 + }
67.69 +
67.70 + @Http({
67.71 + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
67.72 + })
67.73 + @BrwsrTest
67.74 + public void testReadByte() throws Exception {
67.75 + URL url = new URL("http:/bytes");
67.76 + final Object res = url.getContent(new Class[] { byte[].class });
67.77 + assert res instanceof byte[] : "Expecting byte[]: " + res;
67.78 + byte[] arr = (byte[]) res;
67.79 + assert arr.length == 1 : "One byte " + arr.length;
67.80 + assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
67.81 + }
67.82 +
67.83 + @Http({
67.84 + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
67.85 + })
67.86 + @BrwsrTest
67.87 + public void testReadByteViaInputStream() throws Exception {
67.88 + URL url = new URL("http:/bytes");
67.89 + InputStream is = url.openStream();
67.90 + byte[] arr = new byte[10];
67.91 + int len = is.read(arr);
67.92 + assert len == 1 : "One byte " + len;
67.93 + assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
67.94 + }
67.95 +
67.96 + @JavaScriptBody(args = { "url" }, body =
67.97 + "var req = new XMLHttpRequest();\n"
67.98 + + "req.open('GET', url, false);\n"
67.99 + + "req.send();\n"
67.100 + + "return req.responseText;"
67.101 + )
67.102 + private static native String read(String url);
67.103 +
67.104 +
67.105 + @Factory
67.106 + public static Object[] create() {
67.107 + return VMTest.create(HttpResourceTest.class);
67.108 + }
67.109 +}
68.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
68.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceA.java Fri May 24 18:04:55 2013 +0200
68.3 @@ -0,0 +1,34 @@
68.4 +/**
68.5 + * Back 2 Browser Bytecode Translator
68.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
68.7 + *
68.8 + * This program is free software: you can redistribute it and/or modify
68.9 + * it under the terms of the GNU General Public License as published by
68.10 + * the Free Software Foundation, version 2 of the License.
68.11 + *
68.12 + * This program is distributed in the hope that it will be useful,
68.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
68.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
68.15 + * GNU General Public License for more details.
68.16 + *
68.17 + * You should have received a copy of the GNU General Public License
68.18 + * along with this program. Look for COPYING file in the top folder.
68.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
68.20 + */
68.21 +package org.apidesign.bck2brwsr.tck;
68.22 +
68.23 +/**
68.24 + *
68.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
68.26 + */
68.27 +public class InheritanceA {
68.28 + private String name;
68.29 +
68.30 + public void setA(String n) {
68.31 + this.name = n;
68.32 + }
68.33 +
68.34 + public String getA() {
68.35 + return name;
68.36 + }
68.37 +}
69.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
69.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceB.java Fri May 24 18:04:55 2013 +0200
69.3 @@ -0,0 +1,34 @@
69.4 +/**
69.5 + * Back 2 Browser Bytecode Translator
69.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
69.7 + *
69.8 + * This program is free software: you can redistribute it and/or modify
69.9 + * it under the terms of the GNU General Public License as published by
69.10 + * the Free Software Foundation, version 2 of the License.
69.11 + *
69.12 + * This program is distributed in the hope that it will be useful,
69.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
69.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
69.15 + * GNU General Public License for more details.
69.16 + *
69.17 + * You should have received a copy of the GNU General Public License
69.18 + * along with this program. Look for COPYING file in the top folder.
69.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
69.20 + */
69.21 +package org.apidesign.bck2brwsr.tck;
69.22 +
69.23 +/**
69.24 + *
69.25 + * @author Jaroslav Tulach <jtulach@netbeans.org>
69.26 + */
69.27 +public class InheritanceB extends InheritanceA {
69.28 + private String name;
69.29 +
69.30 + public void setB(String n) {
69.31 + this.name = n;
69.32 + }
69.33 +
69.34 + public String getB() {
69.35 + return name;
69.36 + }
69.37 +}
70.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
70.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceTest.java Fri May 24 18:04:55 2013 +0200
70.3 @@ -0,0 +1,41 @@
70.4 +/**
70.5 + * Back 2 Browser Bytecode Translator
70.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
70.7 + *
70.8 + * This program is free software: you can redistribute it and/or modify
70.9 + * it under the terms of the GNU General Public License as published by
70.10 + * the Free Software Foundation, version 2 of the License.
70.11 + *
70.12 + * This program is distributed in the hope that it will be useful,
70.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
70.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
70.15 + * GNU General Public License for more details.
70.16 + *
70.17 + * You should have received a copy of the GNU General Public License
70.18 + * along with this program. Look for COPYING file in the top folder.
70.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
70.20 + */
70.21 +package org.apidesign.bck2brwsr.tck;
70.22 +
70.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
70.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
70.25 +import org.testng.annotations.Factory;
70.26 +
70.27 +/**
70.28 + *
70.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
70.30 + */
70.31 +public class InheritanceTest {
70.32 +
70.33 + @Compare public String checkFieldsIndependent() throws ClassNotFoundException {
70.34 + InheritanceB ib = new InheritanceB();
70.35 + ib.setA("A");
70.36 + ib.setB("B");
70.37 + return "A: " + ib.getA() + " B: " + ib.getB();
70.38 + }
70.39 +
70.40 + @Factory
70.41 + public static Object[] create() {
70.42 + return VMTest.create(InheritanceTest.class);
70.43 + }
70.44 +}
71.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
71.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Fri May 24 18:04:55 2013 +0200
71.3 @@ -0,0 +1,166 @@
71.4 +/**
71.5 + * Back 2 Browser Bytecode Translator
71.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
71.7 + *
71.8 + * This program is free software: you can redistribute it and/or modify
71.9 + * it under the terms of the GNU General Public License as published by
71.10 + * the Free Software Foundation, version 2 of the License.
71.11 + *
71.12 + * This program is distributed in the hope that it will be useful,
71.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
71.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
71.15 + * GNU General Public License for more details.
71.16 + *
71.17 + * You should have received a copy of the GNU General Public License
71.18 + * along with this program. Look for COPYING file in the top folder.
71.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
71.20 + */
71.21 +package org.apidesign.bck2brwsr.tck;
71.22 +
71.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
71.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
71.25 +import org.testng.annotations.Factory;
71.26 +
71.27 +/**
71.28 + *
71.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
71.30 + */
71.31 +public class IntegerArithmeticTest {
71.32 +
71.33 + private static int add(int x, int y) {
71.34 + return x + y;
71.35 + }
71.36 +
71.37 + private static int sub(int x, int y) {
71.38 + return x - y;
71.39 + }
71.40 +
71.41 + private static int mul(int x, int y) {
71.42 + return x * y;
71.43 + }
71.44 +
71.45 + private static int div(int x, int y) {
71.46 + return x / y;
71.47 + }
71.48 +
71.49 + private static int mod(int x, int y) {
71.50 + return x % y;
71.51 + }
71.52 +
71.53 + private static int neg(int x) {
71.54 + return (-x);
71.55 + }
71.56 +
71.57 + private static float fadd(float x, float y) {
71.58 + return x + y;
71.59 + }
71.60 +
71.61 + private static double dadd(double x, double y) {
71.62 + return x + y;
71.63 + }
71.64 +
71.65 + @Compare public int addOverflow() {
71.66 + return add(Integer.MAX_VALUE, 1);
71.67 + }
71.68 +
71.69 + @Compare public int subUnderflow() {
71.70 + return sub(Integer.MIN_VALUE, 1);
71.71 + }
71.72 +
71.73 + @Compare public int addMaxIntAndMaxInt() {
71.74 + return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
71.75 + }
71.76 +
71.77 + @Compare public int subMinIntAndMinInt() {
71.78 + return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
71.79 + }
71.80 +
71.81 + @Compare public int multiplyMaxInt() {
71.82 + return mul(Integer.MAX_VALUE, 2);
71.83 + }
71.84 +
71.85 + @Compare public int multiplyMaxIntAndMaxInt() {
71.86 + return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
71.87 + }
71.88 +
71.89 + @Compare public int multiplyMinInt() {
71.90 + return mul(Integer.MIN_VALUE, 2);
71.91 + }
71.92 +
71.93 + @Compare public int multiplyMinIntAndMinInt() {
71.94 + return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
71.95 + }
71.96 +
71.97 + @Compare public int multiplyPrecision() {
71.98 + return mul(119106029, 1103515245);
71.99 + }
71.100 +
71.101 + @Compare public int division() {
71.102 + return div(1, 2);
71.103 + }
71.104 +
71.105 + @Compare public int divisionReminder() {
71.106 + return mod(1, 2);
71.107 + }
71.108 +
71.109 + @Compare public int negativeDivision() {
71.110 + return div(-7, 3);
71.111 + }
71.112 +
71.113 + @Compare public int negativeDivisionReminder() {
71.114 + return mod(-7, 3);
71.115 + }
71.116 +
71.117 + @Compare public int conversionFromFloat() {
71.118 + return (int) fadd(-2, -0.6f);
71.119 + }
71.120 +
71.121 + @Compare public int conversionFromDouble() {
71.122 + return (int) dadd(-2, -0.6);
71.123 + }
71.124 +
71.125 + @Compare public boolean divByZeroThrowsArithmeticException() {
71.126 + try {
71.127 + div(1, 0);
71.128 + return false;
71.129 + } catch (final ArithmeticException e) {
71.130 + return true;
71.131 + }
71.132 + }
71.133 +
71.134 + @Compare public boolean modByZeroThrowsArithmeticException() {
71.135 + try {
71.136 + mod(1, 0);
71.137 + return false;
71.138 + } catch (final ArithmeticException e) {
71.139 + return true;
71.140 + }
71.141 + }
71.142 +
71.143 + @Compare public int negate() {
71.144 + return neg(123456);
71.145 + }
71.146 +
71.147 + @Compare public int negateMaxInt() {
71.148 + return neg(Integer.MAX_VALUE);
71.149 + }
71.150 +
71.151 + @Compare public int negateMinInt() {
71.152 + return neg(Integer.MIN_VALUE);
71.153 + }
71.154 +
71.155 + @Compare public int sumTwoDimensions() {
71.156 + int[][] matrix = createMatrix(4, 3);
71.157 + matrix[0][0] += 10;
71.158 + return matrix[0][0];
71.159 + }
71.160 +
71.161 + static int[][] createMatrix(int x, int y) {
71.162 + return new int[x][y];
71.163 + }
71.164 +
71.165 + @Factory
71.166 + public static Object[] create() {
71.167 + return VMTest.create(IntegerArithmeticTest.class);
71.168 + }
71.169 +}
72.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
72.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Fri May 24 18:04:55 2013 +0200
72.3 @@ -0,0 +1,376 @@
72.4 +/**
72.5 + * Back 2 Browser Bytecode Translator
72.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
72.7 + *
72.8 + * This program is free software: you can redistribute it and/or modify
72.9 + * it under the terms of the GNU General Public License as published by
72.10 + * the Free Software Foundation, version 2 of the License.
72.11 + *
72.12 + * This program is distributed in the hope that it will be useful,
72.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
72.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
72.15 + * GNU General Public License for more details.
72.16 + *
72.17 + * You should have received a copy of the GNU General Public License
72.18 + * along with this program. Look for COPYING file in the top folder.
72.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
72.20 + */
72.21 +package org.apidesign.bck2brwsr.tck;
72.22 +
72.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
72.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
72.25 +import org.testng.annotations.Factory;
72.26 +
72.27 +/**
72.28 + *
72.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
72.30 + */
72.31 +public class LongArithmeticTest {
72.32 +
72.33 + private static long add(long x, long y) {
72.34 + return (x + y);
72.35 + }
72.36 +
72.37 + private static long sub(long x, long y) {
72.38 + return (x - y);
72.39 + }
72.40 +
72.41 + private static long mul(long x, long y) {
72.42 + return (x * y);
72.43 + }
72.44 +
72.45 + private static long div(long x, long y) {
72.46 + return (x / y);
72.47 + }
72.48 +
72.49 + private static long mod(long x, long y) {
72.50 + return (x % y);
72.51 + }
72.52 +
72.53 + private static long neg(long x) {
72.54 + return (-x);
72.55 + }
72.56 +
72.57 + private static long shl(long x, int b) {
72.58 + return (x << b);
72.59 + }
72.60 +
72.61 + private static long shr(long x, int b) {
72.62 + return (x >> b);
72.63 + }
72.64 +
72.65 + private static long ushr(long x, int b) {
72.66 + return (x >>> b);
72.67 + }
72.68 +
72.69 + private static long and(long x, long y) {
72.70 + return (x & y);
72.71 + }
72.72 +
72.73 + private static long or(long x, long y) {
72.74 + return (x | y);
72.75 + }
72.76 +
72.77 + private static long xor(long x, long y) {
72.78 + return (x ^ y);
72.79 + }
72.80 +
72.81 + private static float fadd(float x, float y) {
72.82 + return x + y;
72.83 + }
72.84 +
72.85 + private static double dadd(double x, double y) {
72.86 + return x + y;
72.87 + }
72.88 +
72.89 + public static int compare(long x, long y, int zero) {
72.90 + final int xyResult = compareL(x, y, zero);
72.91 + final int yxResult = compareL(y, x, zero);
72.92 +
72.93 + return ((xyResult + yxResult) == 0) ? xyResult : -2;
72.94 + }
72.95 +
72.96 + private static int compareL(long x, long y, int zero) {
72.97 + int result = -2;
72.98 + int trueCount = 0;
72.99 +
72.100 + x += zero;
72.101 + if (x == y) {
72.102 + result = 0;
72.103 + ++trueCount;
72.104 + }
72.105 +
72.106 + x += zero;
72.107 + if (x < y) {
72.108 + result = -1;
72.109 + ++trueCount;
72.110 + }
72.111 +
72.112 + x += zero;
72.113 + if (x > y) {
72.114 + result = 1;
72.115 + ++trueCount;
72.116 + }
72.117 +
72.118 + return (trueCount == 1) ? result : -2;
72.119 + }
72.120 +
72.121 + @Compare public long conversion() {
72.122 + return Long.MAX_VALUE;
72.123 + }
72.124 +
72.125 + @Compare public long negate1() {
72.126 + return neg(0x00fa37d7763e0ca1l);
72.127 + }
72.128 +
72.129 + @Compare public long negate2() {
72.130 + return neg(0x80fa37d7763e0ca1l);
72.131 + }
72.132 +
72.133 + @Compare public long negate3() {
72.134 + return neg(0xfffffffffffffeddl);
72.135 + }
72.136 +
72.137 + @Compare public long addOverflow() {
72.138 + return add(Long.MAX_VALUE, 1l);
72.139 + }
72.140 +
72.141 + @Compare public long subUnderflow() {
72.142 + return sub(Long.MIN_VALUE, 1l);
72.143 + }
72.144 +
72.145 + @Compare public long addMaxLongAndMaxLong() {
72.146 + return add(Long.MAX_VALUE, Long.MAX_VALUE);
72.147 + }
72.148 +
72.149 + @Compare public long subMinLongAndMinLong() {
72.150 + return sub(Long.MIN_VALUE, Long.MIN_VALUE);
72.151 + }
72.152 +
72.153 + @Compare public long subMinLongAndMaxLong() {
72.154 + return sub(Long.MIN_VALUE, Long.MAX_VALUE);
72.155 + }
72.156 +
72.157 + @Compare public long multiplyMaxLong() {
72.158 + return mul(Long.MAX_VALUE, 2l);
72.159 + }
72.160 +
72.161 + @Compare public long multiplyMaxLongAndMaxLong() {
72.162 + return mul(Long.MAX_VALUE, Long.MAX_VALUE);
72.163 + }
72.164 +
72.165 + @Compare public long multiplyMinLong() {
72.166 + return mul(Long.MIN_VALUE, 2l);
72.167 + }
72.168 +
72.169 + @Compare public long multiplyMinLongAndMinLong() {
72.170 + return mul(Long.MIN_VALUE, Long.MIN_VALUE);
72.171 + }
72.172 +
72.173 + @Compare public long multiplyPrecision() {
72.174 + return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
72.175 + }
72.176 +
72.177 + @Compare public long divideSmallPositiveNumbers() {
72.178 + return div(0xabcdef, 0x123);
72.179 + }
72.180 +
72.181 + @Compare public long divideSmallNegativeNumbers() {
72.182 + return div(-0xabcdef, -0x123);
72.183 + }
72.184 +
72.185 + @Compare public long divideSmallMixedNumbers() {
72.186 + return div(0xabcdef, -0x123);
72.187 + }
72.188 +
72.189 + @Compare public long dividePositiveNumbersOneDigitDenom() {
72.190 + return div(0xabcdef0102ffffl, 0x654);
72.191 + }
72.192 +
72.193 + @Compare public long divideNegativeNumbersOneDigitDenom() {
72.194 + return div(-0xabcdef0102ffffl, -0x654);
72.195 + }
72.196 +
72.197 + @Compare public long divideMixedNumbersOneDigitDenom() {
72.198 + return div(-0xabcdef0102ffffl, 0x654);
72.199 + }
72.200 +
72.201 + @Compare public long dividePositiveNumbersMultiDigitDenom() {
72.202 + return div(0x7ffefc003322aabbl, 0x89ab1000l);
72.203 + }
72.204 +
72.205 + @Compare public long divideNegativeNumbersMultiDigitDenom() {
72.206 + return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
72.207 + }
72.208 +
72.209 + @Compare public long divideMixedNumbersMultiDigitDenom() {
72.210 + return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
72.211 + }
72.212 +
72.213 + @Compare public long divideWithOverflow() {
72.214 + return div(0x8000fffe0000l, 0x8000ffffl);
72.215 + }
72.216 +
72.217 + @Compare public long divideWithCorrection() {
72.218 + return div(0x7fff800000000000l, 0x800000000001l);
72.219 + }
72.220 +
72.221 + @Compare public long moduloSmallPositiveNumbers() {
72.222 + return mod(0xabcdef, 0x123);
72.223 + }
72.224 +
72.225 + @Compare public long moduloSmallNegativeNumbers() {
72.226 + return mod(-0xabcdef, -0x123);
72.227 + }
72.228 +
72.229 + @Compare public long moduloSmallMixedNumbers() {
72.230 + return mod(0xabcdef, -0x123);
72.231 + }
72.232 +
72.233 + @Compare public long moduloPositiveNumbersOneDigitDenom() {
72.234 + return mod(0xabcdef0102ffffl, 0x654);
72.235 + }
72.236 +
72.237 + @Compare public long moduloNegativeNumbersOneDigitDenom() {
72.238 + return mod(-0xabcdef0102ffffl, -0x654);
72.239 + }
72.240 +
72.241 + @Compare public long moduloMixedNumbersOneDigitDenom() {
72.242 + return mod(-0xabcdef0102ffffl, 0x654);
72.243 + }
72.244 +
72.245 + @Compare public long moduloPositiveNumbersMultiDigitDenom() {
72.246 + return mod(0x7ffefc003322aabbl, 0x89ab1000l);
72.247 + }
72.248 +
72.249 + @Compare public long moduloNegativeNumbersMultiDigitDenom() {
72.250 + return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
72.251 + }
72.252 +
72.253 + @Compare public long moduloMixedNumbersMultiDigitDenom() {
72.254 + return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
72.255 + }
72.256 +
72.257 + @Compare public long moduloWithOverflow() {
72.258 + return mod(0x8000fffe0000l, 0x8000ffffl);
72.259 + }
72.260 +
72.261 + @Compare public long moduloWithCorrection() {
72.262 + return mod(0x7fff800000000000l, 0x800000000001l);
72.263 + }
72.264 +
72.265 + @Compare public long conversionFromFloatPositive() {
72.266 + return (long) fadd(2, 0.6f);
72.267 + }
72.268 +
72.269 + @Compare public long conversionFromFloatNegative() {
72.270 + return (long) fadd(-2, -0.6f);
72.271 + }
72.272 +
72.273 + @Compare public long conversionFromDoublePositive() {
72.274 + return (long) dadd(0x20ffff0000L, 0.6);
72.275 + }
72.276 +
72.277 + @Compare public long conversionFromDoubleNegative() {
72.278 + return (long) dadd(-0x20ffff0000L, -0.6);
72.279 + }
72.280 +
72.281 + @Compare public boolean divByZeroThrowsArithmeticException() {
72.282 + try {
72.283 + div(1, 0);
72.284 + return false;
72.285 + } catch (final ArithmeticException e) {
72.286 + return true;
72.287 + }
72.288 + }
72.289 +
72.290 + @Compare public boolean modByZeroThrowsArithmeticException() {
72.291 + try {
72.292 + mod(1, 0);
72.293 + return false;
72.294 + } catch (final ArithmeticException e) {
72.295 + return true;
72.296 + }
72.297 + }
72.298 +
72.299 + @Compare public long shiftL1() {
72.300 + return shl(0x00fa37d7763e0ca1l, 5);
72.301 + }
72.302 +
72.303 + @Compare public long shiftL2() {
72.304 + return shl(0x00fa37d7763e0ca1l, 32);
72.305 + }
72.306 +
72.307 + @Compare public long shiftL3() {
72.308 + return shl(0x00fa37d7763e0ca1l, 45);
72.309 + }
72.310 +
72.311 + @Compare public long shiftR1() {
72.312 + return shr(0x00fa37d7763e0ca1l, 5);
72.313 + }
72.314 +
72.315 + @Compare public long shiftR2() {
72.316 + return shr(0x00fa37d7763e0ca1l, 32);
72.317 + }
72.318 +
72.319 + @Compare public long shiftR3() {
72.320 + return shr(0x00fa37d7763e0ca1l, 45);
72.321 + }
72.322 +
72.323 + @Compare public long uShiftR1() {
72.324 + return ushr(0x00fa37d7763e0ca1l, 5);
72.325 + }
72.326 +
72.327 + @Compare public long uShiftR2() {
72.328 + return ushr(0x00fa37d7763e0ca1l, 45);
72.329 + }
72.330 +
72.331 + @Compare public long uShiftR3() {
72.332 + return ushr(0xf0fa37d7763e0ca1l, 5);
72.333 + }
72.334 +
72.335 + @Compare public long uShiftR4() {
72.336 + return ushr(0xf0fa37d7763e0ca1l, 45);
72.337 + }
72.338 +
72.339 + @Compare public long and1() {
72.340 + return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
72.341 + }
72.342 +
72.343 + @Compare public long or1() {
72.344 + return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
72.345 + }
72.346 +
72.347 + @Compare public long xor1() {
72.348 + return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
72.349 + }
72.350 +
72.351 + @Compare public long xor2() {
72.352 + return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
72.353 + }
72.354 +
72.355 + @Compare public long xor3() {
72.356 + return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
72.357 + }
72.358 +
72.359 + @Compare public int compareSameNumbers() {
72.360 + return compare(0x0000000000000000l, 0x0000000000000000l, 0);
72.361 + }
72.362 +
72.363 + @Compare public int comparePositiveNumbers() {
72.364 + return compare(0x0000000000200000l, 0x0000000010000000l, 0);
72.365 + }
72.366 +
72.367 + @Compare public int compareNegativeNumbers() {
72.368 + return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
72.369 + }
72.370 +
72.371 + @Compare public int compareMixedNumbers() {
72.372 + return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
72.373 + }
72.374 +
72.375 + @Factory
72.376 + public static Object[] create() {
72.377 + return VMTest.create(LongArithmeticTest.class);
72.378 + }
72.379 +}
73.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
73.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java Fri May 24 18:04:55 2013 +0200
73.3 @@ -0,0 +1,161 @@
73.4 +/**
73.5 + * Back 2 Browser Bytecode Translator
73.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
73.7 + *
73.8 + * This program is free software: you can redistribute it and/or modify
73.9 + * it under the terms of the GNU General Public License as published by
73.10 + * the Free Software Foundation, version 2 of the License.
73.11 + *
73.12 + * This program is distributed in the hope that it will be useful,
73.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
73.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
73.15 + * GNU General Public License for more details.
73.16 + *
73.17 + * You should have received a copy of the GNU General Public License
73.18 + * along with this program. Look for COPYING file in the top folder.
73.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
73.20 + */
73.21 +package org.apidesign.bck2brwsr.tck;
73.22 +
73.23 +import java.lang.reflect.Array;
73.24 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
73.25 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
73.26 +import org.apidesign.bck2brwsr.vmtest.Compare;
73.27 +import org.apidesign.bck2brwsr.vmtest.VMTest;
73.28 +import org.testng.annotations.Factory;
73.29 +
73.30 +/**
73.31 + *
73.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
73.33 + */
73.34 +public class ReflectionArrayTest {
73.35 + @Compare public int lengthOfStringArray() {
73.36 + String[] arr = (String[]) Array.newInstance(String.class, 10);
73.37 + return arr.length;
73.38 + }
73.39 +
73.40 + @Compare public int reflectiveLengthOfStringArray() {
73.41 + Object arr = Array.newInstance(String.class, 10);
73.42 + return Array.getLength(arr);
73.43 + }
73.44 +
73.45 + @Compare public int reflectiveLengthOneNonArray() {
73.46 + Object arr = "non-array";
73.47 + return Array.getLength(arr);
73.48 + }
73.49 +
73.50 + @Compare public String compTypeOfStringArray() {
73.51 + String[] arr = (String[]) Array.newInstance(String.class, 10);
73.52 + return arr.getClass().getComponentType().getName();
73.53 + }
73.54 +
73.55 + @Compare public Object negativeArrayExcp() {
73.56 + return Array.newInstance(String.class, -5);
73.57 + }
73.58 +
73.59 + @Compare public int lengthOfIntArray() {
73.60 + int[] arr = (int[]) Array.newInstance(Integer.TYPE, 10);
73.61 + return arr.length;
73.62 + }
73.63 +
73.64 + @Compare public int reflectiveLengthOfIntArray() {
73.65 + Object arr = Array.newInstance(Integer.TYPE, 10);
73.66 + return Array.getLength(arr);
73.67 + }
73.68 +
73.69 + @Compare public String compTypeOfIntArray() {
73.70 + int[] arr = (int[]) Array.newInstance(int.class, 10);
73.71 + return arr.getClass().getComponentType().getName();
73.72 + }
73.73 +
73.74 + @Compare public Object intNegativeArrayExcp() {
73.75 + return Array.newInstance(int.class, -5);
73.76 + }
73.77 +
73.78 + @Compare public Integer verifyAutobox() {
73.79 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.80 + return (Integer) Array.get(arr, 0);
73.81 + }
73.82 + @Compare public String verifyObjectArray() {
73.83 + String[] arr = (String[]) Array.newInstance(String.class, 5);
73.84 + Array.set(arr, 0, "Hello");
73.85 + return (String) Array.get(arr, 0);
73.86 + }
73.87 + @Compare public int verifyInt() {
73.88 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.89 + return Array.getInt(arr, 0);
73.90 + }
73.91 + @Compare public long verifyConvertToLong() {
73.92 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.93 + return Array.getLong(arr, 0);
73.94 + }
73.95 +
73.96 + @Compare public Object verifySetIntToObject() {
73.97 + try {
73.98 + Object[] arr = (Object[]) Array.newInstance(Object.class, 5);
73.99 + Array.setInt(arr, 0, 10);
73.100 + return Array.get(arr, 0);
73.101 + } catch (Exception exception) {
73.102 + return exception.getClass().getName();
73.103 + }
73.104 + }
73.105 + @Compare public long verifySetShort() {
73.106 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.107 + Array.setShort(arr, 0, (short)10);
73.108 + return Array.getLong(arr, 0);
73.109 + }
73.110 + @Compare public long verifyCantSetLong() {
73.111 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.112 + Array.setLong(arr, 0, 10);
73.113 + return Array.getLong(arr, 0);
73.114 + }
73.115 + @Compare public float verifyLongToFloat() {
73.116 + Object arr = Array.newInstance(float.class, 5);
73.117 + Array.setLong(arr, 0, 10);
73.118 + return Array.getFloat(arr, 0);
73.119 + }
73.120 +
73.121 + @Compare public double verifyConvertToDouble() {
73.122 + int[] arr = (int[]) Array.newInstance(int.class, 5);
73.123 + return Array.getDouble(arr, 0);
73.124 + }
73.125 +
73.126 + @Compare public int multiIntArray() {
73.127 + int[][][] arr = (int[][][]) Array.newInstance(int.class, 3, 3, 3);
73.128 + return arr[0][1][2] + 5 + arr[2][2][0];
73.129 + }
73.130 +
73.131 + @Compare public String multiIntArrayCompType() {
73.132 + return Array.newInstance(int.class, 3, 3, 3).getClass().getName();
73.133 + }
73.134 +
73.135 + @JavaScriptBody(args = {}, body = "return [1, 2];")
73.136 + private static native Object crtarr();
73.137 +
73.138 + @JavaScriptBody(args = {}, body = "return new Object();")
73.139 + private static native Object newobj();
73.140 +
73.141 + @BrwsrTest
73.142 + public static void toStringArray() {
73.143 + final Object arr = crtarr();
73.144 + final Object real = new Object[2];
73.145 + assert arr instanceof Object[] : "Any array is Java array: " + arr;
73.146 + assert arr.getClass() == real.getClass() : "Same classes " + arr + " and " + real.getClass();
73.147 + final String str = arr.toString();
73.148 + assert str != null;
73.149 + assert str.startsWith("[Ljava.lang.Object;@") : str;
73.150 + }
73.151 +
73.152 + @BrwsrTest
73.153 + public static void objectToString() {
73.154 + String s = newobj().toString();
73.155 + assert s != null : "Some string computed";
73.156 + assert s.startsWith("java.lang.Object@") : "Regular object toString(): " + s;
73.157 + }
73.158 +
73.159 +
73.160 + @Factory
73.161 + public static Object[] create() {
73.162 + return VMTest.create(ReflectionArrayTest.class);
73.163 + }
73.164 +}
74.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
74.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Fri May 24 18:04:55 2013 +0200
74.3 @@ -0,0 +1,272 @@
74.4 +/**
74.5 + * Back 2 Browser Bytecode Translator
74.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
74.7 + *
74.8 + * This program is free software: you can redistribute it and/or modify
74.9 + * it under the terms of the GNU General Public License as published by
74.10 + * the Free Software Foundation, version 2 of the License.
74.11 + *
74.12 + * This program is distributed in the hope that it will be useful,
74.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
74.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74.15 + * GNU General Public License for more details.
74.16 + *
74.17 + * You should have received a copy of the GNU General Public License
74.18 + * along with this program. Look for COPYING file in the top folder.
74.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
74.20 + */
74.21 +package org.apidesign.bck2brwsr.tck;
74.22 +
74.23 +import java.lang.annotation.Retention;
74.24 +import java.lang.annotation.RetentionPolicy;
74.25 +import java.lang.reflect.Method;
74.26 +import java.util.Arrays;
74.27 +import java.util.Collections;
74.28 +import java.util.List;
74.29 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
74.30 +import org.apidesign.bck2brwsr.vmtest.Compare;
74.31 +import org.apidesign.bck2brwsr.vmtest.VMTest;
74.32 +import org.testng.annotations.Factory;
74.33 +
74.34 +/**
74.35 + *
74.36 + * @author Jaroslav Tulach <jtulach@netbeans.org>
74.37 + */
74.38 +public class ReflectionTest {
74.39 + @Compare public boolean nonNullThis() {
74.40 + return this == null;
74.41 + }
74.42 +
74.43 + @Compare public String intType() {
74.44 + return Integer.TYPE.toString();
74.45 + }
74.46 +
74.47 + @Compare public String voidType() throws Exception {
74.48 + return void.class.toString();
74.49 + }
74.50 +
74.51 + @Compare public String longClass() {
74.52 + return long.class.toString();
74.53 + }
74.54 +
74.55 + @Compare public boolean isRunnableInterface() {
74.56 + return Runnable.class.isInterface();
74.57 + }
74.58 +
74.59 + @Compare public boolean isAssignableToPrimitiveType() {
74.60 + return boolean.class.isAssignableFrom(Runnable.class);
74.61 + }
74.62 +
74.63 + @Compare public boolean isAssignableFromPrimitiveType() {
74.64 + return Runnable.class.isAssignableFrom(boolean.class);
74.65 + }
74.66 +
74.67 + @Compare public boolean isAssignableLongFromInt() {
74.68 + return long.class.isAssignableFrom(int.class);
74.69 + }
74.70 +
74.71 + @Compare public boolean isAssignableIntFromLong() {
74.72 + return int.class.isAssignableFrom(long.class);
74.73 + }
74.74 +
74.75 + @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
74.76 + return Runnable.class.getMethod("run").getName();
74.77 + }
74.78 +
74.79 + @Compare public String namesOfMethods() {
74.80 + StringBuilder sb = new StringBuilder();
74.81 + String[] arr = new String[20];
74.82 + int i = 0;
74.83 + for (Method m : StaticUse.class.getMethods()) {
74.84 + arr[i++] = m.getName();
74.85 + }
74.86 + for (String s : sort(arr, i)) {
74.87 + sb.append(s).append("\n");
74.88 + }
74.89 + return sb.toString();
74.90 + }
74.91 +
74.92 + @Compare public String namesOfDeclaringClassesOfMethods() {
74.93 + StringBuilder sb = new StringBuilder();
74.94 + String[] arr = new String[20];
74.95 + int i = 0;
74.96 + for (Method m : StaticUse.class.getMethods()) {
74.97 + arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
74.98 + }
74.99 + for (String s : sort(arr, i)) {
74.100 + sb.append(s).append("\n");
74.101 + }
74.102 + return sb.toString();
74.103 + }
74.104 +
74.105 + @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
74.106 + StaticUse.class.getMethod("instanceMethod").invoke(null);
74.107 + return "should not happen";
74.108 + }
74.109 +
74.110 + @Compare public String classCastException() {
74.111 + try {
74.112 + Integer i = (Integer)StaticUseSub.getNonNull();
74.113 + return "" + i.intValue();
74.114 + } catch (ClassCastException ex) {
74.115 + return ex.getClass().getName();
74.116 + }
74.117 + }
74.118 +
74.119 + @Compare public String methodThatThrowsException() throws Exception {
74.120 + StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
74.121 + return "should not happen";
74.122 + }
74.123 +
74.124 + @Compare public Object voidReturnType() throws Exception {
74.125 + return StaticUse.class.getMethod("instanceMethod").getReturnType();
74.126 + }
74.127 +
74.128 + @Retention(RetentionPolicy.RUNTIME)
74.129 + @interface Ann {
74.130 + }
74.131 +
74.132 + @Compare public String annoClass() throws Exception {
74.133 + Retention r = Ann.class.getAnnotation(Retention.class);
74.134 + assert r != null : "Annotation is present";
74.135 + assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
74.136 + return r.annotationType().getName();
74.137 + }
74.138 +
74.139 + @Compare public boolean isAnnotation() {
74.140 + return Ann.class.isAnnotation();
74.141 + }
74.142 + @Compare public boolean isNotAnnotation() {
74.143 + return String.class.isAnnotation();
74.144 + }
74.145 + @Compare public boolean isNotAnnotationEnum() {
74.146 + return E.class.isAnnotation();
74.147 + }
74.148 + enum E { A, B };
74.149 + @Compare public boolean isEnum() {
74.150 + return E.A.getClass().isEnum();
74.151 + }
74.152 +
74.153 + @Compare public boolean isNotEnum() {
74.154 + return "".getClass().isEnum();
74.155 + }
74.156 +
74.157 + @Compare public String newInstanceFails() throws InstantiationException {
74.158 + try {
74.159 + return "success: " + StaticUseSub.class.newInstance();
74.160 + } catch (IllegalAccessException ex) {
74.161 + return ex.getClass().getName();
74.162 + }
74.163 + }
74.164 +
74.165 + @Compare public String paramTypes() throws Exception {
74.166 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
74.167 + final Class[] pt = plus.getParameterTypes();
74.168 + return pt[0].getName();
74.169 + }
74.170 + @Compare public String paramTypesNotFound() throws Exception {
74.171 + return StaticUse.class.getMethod("plus", int.class, double.class).toString();
74.172 + }
74.173 + @Compare public int methodWithArgs() throws Exception {
74.174 + Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
74.175 + return (Integer)plus.invoke(null, 2, 3);
74.176 + }
74.177 +
74.178 + @Compare public String classGetNameForByte() {
74.179 + return byte.class.getName();
74.180 + }
74.181 + @Compare public String classGetNameForBaseObject() {
74.182 + return newObject().getClass().getName();
74.183 + }
74.184 + @Compare public String classGetNameForJavaObject() {
74.185 + return new Object().getClass().getName();
74.186 + }
74.187 + @Compare public String classGetNameForObjectArray() {
74.188 + return (new Object[3]).getClass().getName();
74.189 + }
74.190 + @Compare public String classGetNameForSimpleIntArray() {
74.191 + return (new int[3]).getClass().getName();
74.192 + }
74.193 + @Compare public boolean sameClassGetNameForSimpleCharArray() {
74.194 + return (new char[3]).getClass() == (new char[34]).getClass();
74.195 + }
74.196 + @Compare public String classGetNameForMultiIntArray() {
74.197 + return (new int[3][4][5][6][7][8][9]).getClass().getName();
74.198 + }
74.199 + @Compare public String classGetNameForMultiIntArrayInner() {
74.200 + final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
74.201 + int[][][][][][] subarr = arr[0];
74.202 + int[][][][][] subsubarr = subarr[0];
74.203 + return subsubarr.getClass().getName();
74.204 + }
74.205 + @Compare public String classGetNameForMultiStringArray() {
74.206 + return (new String[3][4][5][6][7][8][9]).getClass().getName();
74.207 + }
74.208 +
74.209 + @Compare public String classForByte() throws Exception {
74.210 + return Class.forName("[Z").getName();
74.211 + }
74.212 +
74.213 + @Compare public String classForUnknownArray() {
74.214 + try {
74.215 + return Class.forName("[W").getName();
74.216 + } catch (Exception ex) {
74.217 + return ex.getClass().getName();
74.218 + }
74.219 + }
74.220 +
74.221 + @Compare public String classForUnknownDeepArray() {
74.222 + try {
74.223 + return Class.forName("[[[[[W").getName();
74.224 + } catch (Exception ex) {
74.225 + return ex.getClass().getName();
74.226 + }
74.227 + }
74.228 +
74.229 + @Compare public String componentGetNameForObjectArray() {
74.230 + return (new Object[3]).getClass().getComponentType().getName();
74.231 + }
74.232 + @Compare public boolean sameComponentGetNameForObjectArray() {
74.233 + return (new Object[3]).getClass().getComponentType() == Object.class;
74.234 + }
74.235 + @Compare public String componentGetNameForSimpleIntArray() {
74.236 + return (new int[3]).getClass().getComponentType().getName();
74.237 + }
74.238 + @Compare public String componentGetNameForMultiIntArray() {
74.239 + return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
74.240 + }
74.241 + @Compare public String componentGetNameForMultiStringArray() {
74.242 + Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
74.243 + StringBuilder sb = new StringBuilder();
74.244 + for (;;) {
74.245 + sb.append(c.getName()).append("\n");
74.246 + c = c.getComponentType();
74.247 + if (c == null) {
74.248 + break;
74.249 + }
74.250 + }
74.251 + return sb.toString();
74.252 + }
74.253 +
74.254 + @Compare public boolean isArray() {
74.255 + return new Object[0].getClass().isArray();
74.256 + }
74.257 +
74.258 + @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
74.259 + private static String[] sort(String[] arr, int len) {
74.260 + List<String> list = Arrays.asList(arr).subList(0, len);
74.261 + Collections.sort(list);
74.262 + return list.toArray(new String[0]);
74.263 + }
74.264 +
74.265 + @JavaScriptBody(args = {}, body = "return new Object();")
74.266 + private static Object newObject() {
74.267 + return new Object();
74.268 + }
74.269 +
74.270 + @Factory
74.271 + public static Object[] create() {
74.272 + return VMTest.create(ReflectionTest.class);
74.273 + }
74.274 +
74.275 +}
75.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
75.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Fri May 24 18:04:55 2013 +0200
75.3 @@ -0,0 +1,45 @@
75.4 +/**
75.5 + * Back 2 Browser Bytecode Translator
75.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
75.7 + *
75.8 + * This program is free software: you can redistribute it and/or modify
75.9 + * it under the terms of the GNU General Public License as published by
75.10 + * the Free Software Foundation, version 2 of the License.
75.11 + *
75.12 + * This program is distributed in the hope that it will be useful,
75.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
75.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
75.15 + * GNU General Public License for more details.
75.16 + *
75.17 + * You should have received a copy of the GNU General Public License
75.18 + * along with this program. Look for COPYING file in the top folder.
75.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
75.20 + */
75.21 +package org.apidesign.bck2brwsr.tck;
75.22 +
75.23 +import java.io.InputStream;
75.24 +import org.apidesign.bck2brwsr.vmtest.Compare;
75.25 +import org.apidesign.bck2brwsr.vmtest.VMTest;
75.26 +import org.testng.annotations.Factory;
75.27 +
75.28 +/**
75.29 + *
75.30 + * @author Jaroslav Tulach <jtulach@netbeans.org>
75.31 + */
75.32 +public class ResourcesTest {
75.33 +
75.34 + @Compare public String readResourceAsStream() throws Exception {
75.35 + InputStream is = getClass().getResourceAsStream("Resources.txt");
75.36 + byte[] b = new byte[30];
75.37 + int len = is.read(b);
75.38 + StringBuilder sb = new StringBuilder();
75.39 + for (int i = 0; i < len; i++) {
75.40 + sb.append((char)b[i]);
75.41 + }
75.42 + return sb.toString();
75.43 + }
75.44 +
75.45 + @Factory public static Object[] create() {
75.46 + return VMTest.create(ResourcesTest.class);
75.47 + }
75.48 +}
76.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
76.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java Fri May 24 18:04:55 2013 +0200
76.3 @@ -0,0 +1,102 @@
76.4 +/**
76.5 + * Back 2 Browser Bytecode Translator
76.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
76.7 + *
76.8 + * This program is free software: you can redistribute it and/or modify
76.9 + * it under the terms of the GNU General Public License as published by
76.10 + * the Free Software Foundation, version 2 of the License.
76.11 + *
76.12 + * This program is distributed in the hope that it will be useful,
76.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
76.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
76.15 + * GNU General Public License for more details.
76.16 + *
76.17 + * You should have received a copy of the GNU General Public License
76.18 + * along with this program. Look for COPYING file in the top folder.
76.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
76.20 + */
76.21 +package org.apidesign.bck2brwsr.tck;
76.22 +
76.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
76.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
76.25 +import org.testng.annotations.Factory;
76.26 +
76.27 +/**
76.28 + *
76.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
76.30 + */
76.31 +public class ShortArithmeticTest {
76.32 +
76.33 + private static short add(short x, short y) {
76.34 + return (short)(x + y);
76.35 + }
76.36 +
76.37 + private static short sub(short x, short y) {
76.38 + return (short)(x - y);
76.39 + }
76.40 +
76.41 + private static short mul(short x, short y) {
76.42 + return (short)(x * y);
76.43 + }
76.44 +
76.45 + private static short div(short x, short y) {
76.46 + return (short)(x / y);
76.47 + }
76.48 +
76.49 + private static short mod(short x, short y) {
76.50 + return (short)(x % y);
76.51 + }
76.52 +
76.53 + @Compare public short conversion() {
76.54 + return (short)123456;
76.55 + }
76.56 +
76.57 + @Compare public short addOverflow() {
76.58 + return add(Short.MAX_VALUE, (short)1);
76.59 + }
76.60 +
76.61 + @Compare public short subUnderflow() {
76.62 + return sub(Short.MIN_VALUE, (short)1);
76.63 + }
76.64 +
76.65 + @Compare public short addMaxShortAndMaxShort() {
76.66 + return add(Short.MAX_VALUE, Short.MAX_VALUE);
76.67 + }
76.68 +
76.69 + @Compare public short subMinShortAndMinShort() {
76.70 + return sub(Short.MIN_VALUE, Short.MIN_VALUE);
76.71 + }
76.72 +
76.73 + @Compare public short multiplyMaxShort() {
76.74 + return mul(Short.MAX_VALUE, (short)2);
76.75 + }
76.76 +
76.77 + @Compare public short multiplyMaxShortAndMaxShort() {
76.78 + return mul(Short.MAX_VALUE, Short.MAX_VALUE);
76.79 + }
76.80 +
76.81 + @Compare public short multiplyMinShort() {
76.82 + return mul(Short.MIN_VALUE, (short)2);
76.83 + }
76.84 +
76.85 + @Compare public short multiplyMinShortAndMinShort() {
76.86 + return mul(Short.MIN_VALUE, Short.MIN_VALUE);
76.87 + }
76.88 +
76.89 + @Compare public short multiplyPrecision() {
76.90 + return mul((short)17638, (short)1103);
76.91 + }
76.92 +
76.93 + @Compare public short division() {
76.94 + return div((short)1, (short)2);
76.95 + }
76.96 +
76.97 + @Compare public short divisionReminder() {
76.98 + return mod((short)1, (short)2);
76.99 + }
76.100 +
76.101 + @Factory
76.102 + public static Object[] create() {
76.103 + return VMTest.create(ShortArithmeticTest.class);
76.104 + }
76.105 +}
77.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
77.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Fri May 24 18:04:55 2013 +0200
77.3 @@ -0,0 +1,39 @@
77.4 +/**
77.5 + * Back 2 Browser Bytecode Translator
77.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
77.7 + *
77.8 + * This program is free software: you can redistribute it and/or modify
77.9 + * it under the terms of the GNU General Public License as published by
77.10 + * the Free Software Foundation, version 2 of the License.
77.11 + *
77.12 + * This program is distributed in the hope that it will be useful,
77.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
77.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
77.15 + * GNU General Public License for more details.
77.16 + *
77.17 + * You should have received a copy of the GNU General Public License
77.18 + * along with this program. Look for COPYING file in the top folder.
77.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
77.20 + */
77.21 +package org.apidesign.bck2brwsr.tck;
77.22 +
77.23 +class StaticUse {
77.24 + public static final Object NON_NULL = new Object();
77.25 + public static int cnt;
77.26 + static {
77.27 + if (cnt++ != 0) {
77.28 + throw new IllegalStateException("Multiple initialization of a <cinit>");
77.29 + }
77.30 + }
77.31 +
77.32 + StaticUse() {
77.33 + }
77.34 +
77.35 + public void instanceMethod() {
77.36 + throw new IllegalStateException();
77.37 + }
77.38 +
77.39 + public static int plus(int a, int b) {
77.40 + return a + b;
77.41 + }
77.42 +}
78.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
78.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java Fri May 24 18:04:55 2013 +0200
78.3 @@ -0,0 +1,30 @@
78.4 +/**
78.5 + * Back 2 Browser Bytecode Translator
78.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
78.7 + *
78.8 + * This program is free software: you can redistribute it and/or modify
78.9 + * it under the terms of the GNU General Public License as published by
78.10 + * the Free Software Foundation, version 2 of the License.
78.11 + *
78.12 + * This program is distributed in the hope that it will be useful,
78.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
78.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
78.15 + * GNU General Public License for more details.
78.16 + *
78.17 + * You should have received a copy of the GNU General Public License
78.18 + * along with this program. Look for COPYING file in the top folder.
78.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
78.20 + */
78.21 +package org.apidesign.bck2brwsr.tck;
78.22 +
78.23 +public class StaticUseSub extends StaticUse {
78.24 + private StaticUseSub() {
78.25 + }
78.26 +
78.27 + public static Object getNonNull() {
78.28 + return NON_NULL;
78.29 + }
78.30 + static Object getNull() {
78.31 + return null;
78.32 + }
78.33 +}
79.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
79.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java Fri May 24 18:04:55 2013 +0200
79.3 @@ -0,0 +1,45 @@
79.4 +/**
79.5 + * Back 2 Browser Bytecode Translator
79.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
79.7 + *
79.8 + * This program is free software: you can redistribute it and/or modify
79.9 + * it under the terms of the GNU General Public License as published by
79.10 + * the Free Software Foundation, version 2 of the License.
79.11 + *
79.12 + * This program is distributed in the hope that it will be useful,
79.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
79.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
79.15 + * GNU General Public License for more details.
79.16 + *
79.17 + * You should have received a copy of the GNU General Public License
79.18 + * along with this program. Look for COPYING file in the top folder.
79.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
79.20 + */
79.21 +package org.apidesign.bck2brwsr.tck;
79.22 +
79.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
79.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
79.25 +import org.testng.annotations.Factory;
79.26 +
79.27 +/**
79.28 + *
79.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
79.30 + */
79.31 +public class StaticUseSubTest {
79.32 + @Compare public String staticFieldInitializationInSuperClass() throws Exception {
79.33 + Object ret = StaticUseSub.getNonNull();
79.34 + return ret.getClass().getName();
79.35 + }
79.36 +
79.37 + @Compare public String isNullPointerTheSame() throws Exception {
79.38 + try {
79.39 + return StaticUseSub.getNull().getClass().toString();
79.40 + } catch (NullPointerException ex) {
79.41 + return ex.getClass().getName();
79.42 + }
79.43 + }
79.44 +
79.45 + @Factory public static Object[] create() {
79.46 + return VMTest.create(StaticUseSubTest.class);
79.47 + }
79.48 +}
80.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
80.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CRC32Test.java Fri May 24 18:04:55 2013 +0200
80.3 @@ -0,0 +1,41 @@
80.4 +/**
80.5 + * Back 2 Browser Bytecode Translator
80.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
80.7 + *
80.8 + * This program is free software: you can redistribute it and/or modify
80.9 + * it under the terms of the GNU General Public License as published by
80.10 + * the Free Software Foundation, version 2 of the License.
80.11 + *
80.12 + * This program is distributed in the hope that it will be useful,
80.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
80.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80.15 + * GNU General Public License for more details.
80.16 + *
80.17 + * You should have received a copy of the GNU General Public License
80.18 + * along with this program. Look for COPYING file in the top folder.
80.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
80.20 + */
80.21 +package org.apidesign.bck2brwsr.vmtest.impl;
80.22 +
80.23 +import java.io.UnsupportedEncodingException;
80.24 +import java.util.zip.CRC32;
80.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
80.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
80.27 +import org.testng.annotations.Factory;
80.28 +
80.29 +/**
80.30 + *
80.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
80.32 + */
80.33 +public class CRC32Test {
80.34 +
80.35 + @Compare public long crc1() throws UnsupportedEncodingException {
80.36 + CRC32 crc = new CRC32();
80.37 + crc.update("Hello World!".getBytes("UTF-8"));
80.38 + return crc.getValue();
80.39 + }
80.40 +
80.41 + @Factory public static Object[] create() {
80.42 + return VMTest.create(CRC32Test.class);
80.43 + }
80.44 +}
81.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
81.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CallMeTwiceTest.java Fri May 24 18:04:55 2013 +0200
81.3 @@ -0,0 +1,43 @@
81.4 +/**
81.5 + * Back 2 Browser Bytecode Translator
81.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
81.7 + *
81.8 + * This program is free software: you can redistribute it and/or modify
81.9 + * it under the terms of the GNU General Public License as published by
81.10 + * the Free Software Foundation, version 2 of the License.
81.11 + *
81.12 + * This program is distributed in the hope that it will be useful,
81.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
81.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
81.15 + * GNU General Public License for more details.
81.16 + *
81.17 + * You should have received a copy of the GNU General Public License
81.18 + * along with this program. Look for COPYING file in the top folder.
81.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
81.20 + */
81.21 +package org.apidesign.bck2brwsr.vmtest.impl;
81.22 +
81.23 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
81.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
81.25 +import org.testng.annotations.Factory;
81.26 +
81.27 +/**
81.28 + *
81.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
81.30 + */
81.31 +public class CallMeTwiceTest {
81.32 + int cnt;
81.33 +
81.34 + @BrwsrTest public void callMeTwice() throws InterruptedException {
81.35 + if (cnt++ == 0) {
81.36 + throw new InterruptedException();
81.37 + }
81.38 + int prevCnt = cnt;
81.39 + cnt = 0;
81.40 + assert prevCnt == 2 : "We need to receive two calls " + prevCnt;
81.41 + }
81.42 +
81.43 + @Factory public static Object[] create() {
81.44 + return VMTest.create(CallMeTwiceTest.class);
81.45 + }
81.46 +}
82.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
82.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipEntryTest.java Fri May 24 18:04:55 2013 +0200
82.3 @@ -0,0 +1,67 @@
82.4 +/**
82.5 + * Back 2 Browser Bytecode Translator
82.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
82.7 + *
82.8 + * This program is free software: you can redistribute it and/or modify
82.9 + * it under the terms of the GNU General Public License as published by
82.10 + * the Free Software Foundation, version 2 of the License.
82.11 + *
82.12 + * This program is distributed in the hope that it will be useful,
82.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
82.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82.15 + * GNU General Public License for more details.
82.16 + *
82.17 + * You should have received a copy of the GNU General Public License
82.18 + * along with this program. Look for COPYING file in the top folder.
82.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
82.20 + */
82.21 +package org.apidesign.bck2brwsr.vmtest.impl;
82.22 +
82.23 +import java.io.ByteArrayInputStream;
82.24 +import java.io.IOException;
82.25 +import java.io.InputStream;
82.26 +import org.apidesign.bck2brwsr.emul.zip.FastJar;
82.27 +import org.testng.annotations.Test;
82.28 +import static org.testng.Assert.*;
82.29 +
82.30 +/**
82.31 + *
82.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
82.33 + */
82.34 +@GenerateZip(name = "five.zip", contents = {
82.35 + "1.txt", "one",
82.36 + "2.txt", "duo",
82.37 + "3.txt", "three",
82.38 + "4.txt", "four",
82.39 + "5.txt", "five"
82.40 +})
82.41 +public class ZipEntryTest {
82.42 + @Test
82.43 + public void readEntriesEffectively() throws IOException {
82.44 + InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip");
82.45 + byte[] arr = new byte[is.available()];
82.46 + int len = is.read(arr);
82.47 + assertEquals(len, arr.length, "Read fully");
82.48 +
82.49 + FastJar fj = new FastJar(arr);
82.50 + FastJar.Entry[] entrs = fj.list();
82.51 +
82.52 + assertEquals(5, entrs.length, "Five entries");
82.53 +
82.54 + for (int i = 1; i <= 5; i++) {
82.55 + FastJar.Entry en = entrs[i - 1];
82.56 + assertEquals(en.name, i + ".txt");
82.57 +// assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read");
82.58 + }
82.59 +
82.60 + assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK");
82.61 + assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK");
82.62 + }
82.63 +
82.64 + private static void assertContent(String exp, InputStream is, String msg) throws IOException {
82.65 + byte[] arr = new byte[512];
82.66 + int len = is.read(arr);
82.67 + String s = new String(arr, 0, len);
82.68 + assertEquals(exp, s, msg);
82.69 + }
82.70 +}
83.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
83.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Fri May 24 18:04:55 2013 +0200
83.3 @@ -0,0 +1,108 @@
83.4 +/**
83.5 + * Back 2 Browser Bytecode Translator
83.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
83.7 + *
83.8 + * This program is free software: you can redistribute it and/or modify
83.9 + * it under the terms of the GNU General Public License as published by
83.10 + * the Free Software Foundation, version 2 of the License.
83.11 + *
83.12 + * This program is distributed in the hope that it will be useful,
83.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
83.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
83.15 + * GNU General Public License for more details.
83.16 + *
83.17 + * You should have received a copy of the GNU General Public License
83.18 + * along with this program. Look for COPYING file in the top folder.
83.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
83.20 + */
83.21 +package org.apidesign.bck2brwsr.vmtest.impl;
83.22 +
83.23 +import java.io.IOException;
83.24 +import java.io.InputStream;
83.25 +import java.util.Objects;
83.26 +import java.util.zip.ZipEntry;
83.27 +import java.util.zip.ZipInputStream;
83.28 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
83.29 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
83.30 +import org.apidesign.bck2brwsr.vmtest.Compare;
83.31 +import org.apidesign.bck2brwsr.vmtest.Http;
83.32 +import org.apidesign.bck2brwsr.vmtest.VMTest;
83.33 +import org.testng.annotations.Factory;
83.34 +
83.35 +/**
83.36 + *
83.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
83.38 + */
83.39 +@GenerateZip(name = "readAnEntry.zip", contents = {
83.40 + "my/main/file.txt", "Hello World!"
83.41 +})
83.42 +public class ZipFileTest {
83.43 +
83.44 + @Compare public String readAnEntry() throws IOException {
83.45 + InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip");
83.46 + ZipInputStream zip = new ZipInputStream(is);
83.47 + ZipEntry entry = zip.getNextEntry();
83.48 + assertEquals(entry.getName(), "my/main/file.txt", "Correct entry");
83.49 +
83.50 + byte[] arr = new byte[4096];
83.51 + int len = zip.read(arr);
83.52 +
83.53 + assertEquals(zip.getNextEntry(), null, "No next entry");
83.54 +
83.55 + final String ret = new String(arr, 0, len, "UTF-8");
83.56 + return ret;
83.57 + }
83.58 +
83.59 + @JavaScriptBody(args = { "res", "path" }, body =
83.60 + "var myvm = bck2brwsr.apply(null, path);\n"
83.61 + + "var cls = myvm.loadClass('java.lang.String');\n"
83.62 + + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
83.63 + )
83.64 + private static native Object loadVMResource(String res, String...path);
83.65 +
83.66 + @Http({
83.67 + @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
83.68 + })
83.69 + @BrwsrTest public void canVmLoadResourceFromZip() throws IOException {
83.70 + Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
83.71 + assert res instanceof InputStream : "Got array of bytes: " + res;
83.72 + InputStream is = (InputStream)res;
83.73 +
83.74 + byte[] arr = new byte[4096];
83.75 + int len = is.read(arr);
83.76 +
83.77 + final String ret = new String(arr, 0, len, "UTF-8");
83.78 +
83.79 + assertEquals(ret, "Hello World!", "Can read the bytes");
83.80 + }
83.81 +
83.82 + @GenerateZip(name = "cpattr.zip", contents = {
83.83 + "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
83.84 + + "Created-By: hand\n"
83.85 + + "Class-Path: realJar.jar\n\n\n"
83.86 + })
83.87 + @Http({
83.88 + @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
83.89 + @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
83.90 + })
83.91 + @BrwsrTest public void understandsClassPathAttr() throws IOException {
83.92 + Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
83.93 + assert res instanceof InputStream : "Got array of bytes: " + res;
83.94 + InputStream is = (InputStream)res;
83.95 +
83.96 + byte[] arr = new byte[4096];
83.97 + int len = is.read(arr);
83.98 +
83.99 + final String ret = new String(arr, 0, len, "UTF-8");
83.100 +
83.101 + assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
83.102 + }
83.103 +
83.104 + private static void assertEquals(Object real, Object exp, String msg) {
83.105 + assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
83.106 + }
83.107 +
83.108 + @Factory public static Object[] create() {
83.109 + return VMTest.create(ZipFileTest.class);
83.110 + }
83.111 +}
84.1 Binary file rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/compact/tck/demo.static.calculator-0.3-SNAPSHOT.jar has changed
85.1 Binary file rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/compact/tck/demo.static.calculator-TEST.jar has changed
86.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
86.2 +++ b/rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/tck/0xfe Fri May 24 18:04:55 2013 +0200
86.3 @@ -0,0 +1,1 @@
86.4 +þ
86.5 \ No newline at end of file
87.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
87.2 +++ b/rt/emul/compact/src/test/resources/org/apidesign/bck2brwsr/tck/Resources.txt Fri May 24 18:04:55 2013 +0200
87.3 @@ -0,0 +1,1 @@
87.4 +Ahoj
88.1 --- a/rt/emul/mini/pom.xml Fri May 24 16:45:32 2013 +0200
88.2 +++ b/rt/emul/mini/pom.xml Fri May 24 18:04:55 2013 +0200
88.3 @@ -4,11 +4,11 @@
88.4 <parent>
88.5 <groupId>org.apidesign.bck2brwsr</groupId>
88.6 <artifactId>emul.pom</artifactId>
88.7 - <version>0.6-SNAPSHOT</version>
88.8 + <version>0.8-SNAPSHOT</version>
88.9 </parent>
88.10 <groupId>org.apidesign.bck2brwsr</groupId>
88.11 <artifactId>emul.mini</artifactId>
88.12 - <version>0.6-SNAPSHOT</version>
88.13 + <version>0.8-SNAPSHOT</version>
88.14 <name>Minimal API Profile</name>
88.15 <url>http://maven.apache.org</url>
88.16 <properties>
88.17 @@ -18,7 +18,7 @@
88.18 <dependency>
88.19 <groupId>org.apidesign.bck2brwsr</groupId>
88.20 <artifactId>core</artifactId>
88.21 - <version>0.6-SNAPSHOT</version>
88.22 + <version>0.8-SNAPSHOT</version>
88.23 <type>jar</type>
88.24 </dependency>
88.25 <dependency>
89.1 --- a/rt/emul/mini/src/main/java/java/lang/Boolean.java Fri May 24 16:45:32 2013 +0200
89.2 +++ b/rt/emul/mini/src/main/java/java/lang/Boolean.java Fri May 24 18:04:55 2013 +0200
89.3 @@ -26,6 +26,7 @@
89.4 package java.lang;
89.5
89.6 import org.apidesign.bck2brwsr.core.JavaScriptBody;
89.7 +import org.apidesign.bck2brwsr.core.JavaScriptPrototype;
89.8
89.9 /**
89.10 * The Boolean class wraps a value of the primitive type
89.11 @@ -42,6 +43,7 @@
89.12 * @author Arthur van Hoff
89.13 * @since JDK1.0
89.14 */
89.15 +@JavaScriptPrototype(container = "Boolean.prototype", prototype = "new Boolean")
89.16 public final class Boolean implements java.io.Serializable,
89.17 Comparable<Boolean>
89.18 {
89.19 @@ -129,6 +131,7 @@
89.20 *
89.21 * @return the primitive {@code boolean} value of this object.
89.22 */
89.23 + @JavaScriptBody(args = {}, body = "return this.valueOf();")
89.24 public boolean booleanValue() {
89.25 return value;
89.26 }
89.27 @@ -287,7 +290,7 @@
89.28 }
89.29 @JavaScriptBody(args = { }, body =
89.30 "vm.java_lang_Boolean(false)" +
89.31 - ".valueOf = function() { return this._value(); };"
89.32 + ".valueOf = function() { return this._value() ? true : false; };"
89.33 )
89.34 private native static void initValueOf();
89.35 }
90.1 --- a/rt/emul/mini/src/main/java/java/lang/Throwable.java Fri May 24 16:45:32 2013 +0200
90.2 +++ b/rt/emul/mini/src/main/java/java/lang/Throwable.java Fri May 24 18:04:55 2013 +0200
90.3 @@ -1085,4 +1085,22 @@
90.4 // else
90.5 // return suppressedExceptions.toArray(EMPTY_THROWABLE_ARRAY);
90.6 }
90.7 +
90.8 + private static Object bck2BrwsrCnvrt(Object o) {
90.9 + if (o instanceof Throwable) {
90.10 + return o;
90.11 + }
90.12 + final String msg = msg(o);
90.13 + if (msg == null || msg.startsWith("TypeError")) {
90.14 + return new NullPointerException(msg);
90.15 + }
90.16 + return new Throwable(msg);
90.17 + }
90.18 +
90.19 + @JavaScriptBody(args = { "o" }, body = "return o ? o.toString() : null;")
90.20 + private static native String msg(Object o);
90.21 +
90.22 + @JavaScriptOnly(name = "bck2BrwsrCnvrt", value = "c.bck2BrwsrCnvrt__Ljava_lang_Object_2Ljava_lang_Object_2")
90.23 + private static void bck2BrwsrCnvrtVM() {
90.24 + }
90.25 }
91.1 --- a/rt/emul/pom.xml Fri May 24 16:45:32 2013 +0200
91.2 +++ b/rt/emul/pom.xml Fri May 24 18:04:55 2013 +0200
91.3 @@ -4,11 +4,11 @@
91.4 <parent>
91.5 <groupId>org.apidesign.bck2brwsr</groupId>
91.6 <artifactId>rt</artifactId>
91.7 - <version>0.6-SNAPSHOT</version>
91.8 + <version>0.8-SNAPSHOT</version>
91.9 </parent>
91.10 <groupId>org.apidesign.bck2brwsr</groupId>
91.11 <artifactId>emul.pom</artifactId>
91.12 - <version>0.6-SNAPSHOT</version>
91.13 + <version>0.8-SNAPSHOT</version>
91.14 <packaging>pom</packaging>
91.15 <name>Emulation of Core Libraries</name>
91.16 <modules>
92.1 --- a/rt/launcher/pom.xml Fri May 24 16:45:32 2013 +0200
92.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
92.3 @@ -1,56 +0,0 @@
92.4 -<?xml version="1.0"?>
92.5 -<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">
92.6 - <modelVersion>4.0.0</modelVersion>
92.7 - <parent>
92.8 - <groupId>org.apidesign.bck2brwsr</groupId>
92.9 - <artifactId>rt</artifactId>
92.10 - <version>0.6-SNAPSHOT</version>
92.11 - </parent>
92.12 - <groupId>org.apidesign.bck2brwsr</groupId>
92.13 - <artifactId>launcher</artifactId>
92.14 - <version>0.6-SNAPSHOT</version>
92.15 - <name>Bck2Brwsr Launcher</name>
92.16 - <url>http://maven.apache.org</url>
92.17 - <build>
92.18 - <plugins>
92.19 - <plugin>
92.20 - <groupId>org.apache.maven.plugins</groupId>
92.21 - <artifactId>maven-compiler-plugin</artifactId>
92.22 - <version>2.3.2</version>
92.23 - <configuration>
92.24 - <source>1.7</source>
92.25 - <target>1.7</target>
92.26 - </configuration>
92.27 - </plugin>
92.28 - <plugin>
92.29 - <groupId>org.apache.maven.plugins</groupId>
92.30 - <artifactId>maven-javadoc-plugin</artifactId>
92.31 - <configuration>
92.32 - <excludePackageNames>org.apidesign.bck2brwsr.launcher.impl</excludePackageNames>
92.33 - <skip>false</skip>
92.34 - </configuration>
92.35 - </plugin>
92.36 - </plugins>
92.37 - </build>
92.38 - <properties>
92.39 - <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
92.40 - </properties>
92.41 - <dependencies>
92.42 - <dependency>
92.43 - <groupId>junit</groupId>
92.44 - <artifactId>junit</artifactId>
92.45 - <version>3.8.1</version>
92.46 - <scope>test</scope>
92.47 - </dependency>
92.48 - <dependency>
92.49 - <groupId>org.glassfish.grizzly</groupId>
92.50 - <artifactId>grizzly-http-server</artifactId>
92.51 - <version>2.2.19</version>
92.52 - </dependency>
92.53 - <dependency>
92.54 - <groupId>${project.groupId}</groupId>
92.55 - <artifactId>vm4brwsr</artifactId>
92.56 - <version>${project.version}</version>
92.57 - </dependency>
92.58 - </dependencies>
92.59 -</project>
93.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Fri May 24 16:45:32 2013 +0200
93.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
93.3 @@ -1,602 +0,0 @@
93.4 -/**
93.5 - * Back 2 Browser Bytecode Translator
93.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
93.7 - *
93.8 - * This program is free software: you can redistribute it and/or modify
93.9 - * it under the terms of the GNU General Public License as published by
93.10 - * the Free Software Foundation, version 2 of the License.
93.11 - *
93.12 - * This program is distributed in the hope that it will be useful,
93.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
93.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
93.15 - * GNU General Public License for more details.
93.16 - *
93.17 - * You should have received a copy of the GNU General Public License
93.18 - * along with this program. Look for COPYING file in the top folder.
93.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
93.20 - */
93.21 -package org.apidesign.bck2brwsr.launcher;
93.22 -
93.23 -import java.io.Closeable;
93.24 -import java.io.File;
93.25 -import java.io.IOException;
93.26 -import java.io.InputStream;
93.27 -import java.io.InterruptedIOException;
93.28 -import java.io.OutputStream;
93.29 -import java.io.UnsupportedEncodingException;
93.30 -import java.io.Writer;
93.31 -import java.net.URI;
93.32 -import java.net.URISyntaxException;
93.33 -import java.net.URL;
93.34 -import java.util.ArrayList;
93.35 -import java.util.Arrays;
93.36 -import java.util.Enumeration;
93.37 -import java.util.LinkedHashSet;
93.38 -import java.util.List;
93.39 -import java.util.Set;
93.40 -import java.util.concurrent.BlockingQueue;
93.41 -import java.util.concurrent.CountDownLatch;
93.42 -import java.util.concurrent.LinkedBlockingQueue;
93.43 -import java.util.concurrent.TimeUnit;
93.44 -import java.util.logging.Level;
93.45 -import java.util.logging.Logger;
93.46 -import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
93.47 -import org.apidesign.vm4brwsr.Bck2Brwsr;
93.48 -import org.glassfish.grizzly.PortRange;
93.49 -import org.glassfish.grizzly.http.server.HttpHandler;
93.50 -import org.glassfish.grizzly.http.server.HttpServer;
93.51 -import org.glassfish.grizzly.http.server.NetworkListener;
93.52 -import org.glassfish.grizzly.http.server.Request;
93.53 -import org.glassfish.grizzly.http.server.Response;
93.54 -import org.glassfish.grizzly.http.server.ServerConfiguration;
93.55 -import org.glassfish.grizzly.http.util.HttpStatus;
93.56 -
93.57 -/**
93.58 - * Lightweight server to launch Bck2Brwsr applications and tests.
93.59 - * Supports execution in native browser as well as Java's internal
93.60 - * execution engine.
93.61 - */
93.62 -final class Bck2BrwsrLauncher extends Launcher implements Closeable {
93.63 - private static final Logger LOG = Logger.getLogger(Bck2BrwsrLauncher.class.getName());
93.64 - private static final InvocationContext END = new InvocationContext(null, null, null);
93.65 - private final Set<ClassLoader> loaders = new LinkedHashSet<>();
93.66 - private final BlockingQueue<InvocationContext> methods = new LinkedBlockingQueue<>();
93.67 - private long timeOut;
93.68 - private final Res resources = new Res();
93.69 - private final String cmd;
93.70 - private Object[] brwsr;
93.71 - private HttpServer server;
93.72 - private CountDownLatch wait;
93.73 -
93.74 - public Bck2BrwsrLauncher(String cmd) {
93.75 - this.cmd = cmd;
93.76 - }
93.77 -
93.78 - @Override
93.79 - InvocationContext runMethod(InvocationContext c) throws IOException {
93.80 - loaders.add(c.clazz.getClassLoader());
93.81 - methods.add(c);
93.82 - try {
93.83 - c.await(timeOut);
93.84 - } catch (InterruptedException ex) {
93.85 - throw new IOException(ex);
93.86 - }
93.87 - return c;
93.88 - }
93.89 -
93.90 - public void setTimeout(long ms) {
93.91 - timeOut = ms;
93.92 - }
93.93 -
93.94 - public void addClassLoader(ClassLoader url) {
93.95 - this.loaders.add(url);
93.96 - }
93.97 -
93.98 - public void showURL(String startpage) throws IOException {
93.99 - if (!startpage.startsWith("/")) {
93.100 - startpage = "/" + startpage;
93.101 - }
93.102 - HttpServer s = initServer(".", true);
93.103 - int last = startpage.lastIndexOf('/');
93.104 - String prefix = startpage.substring(0, last);
93.105 - String simpleName = startpage.substring(last);
93.106 - s.getServerConfiguration().addHttpHandler(new SubTree(resources, prefix), "/");
93.107 - try {
93.108 - launchServerAndBrwsr(s, simpleName);
93.109 - } catch (URISyntaxException | InterruptedException ex) {
93.110 - throw new IOException(ex);
93.111 - }
93.112 - }
93.113 -
93.114 - void showDirectory(File dir, String startpage) throws IOException {
93.115 - if (!startpage.startsWith("/")) {
93.116 - startpage = "/" + startpage;
93.117 - }
93.118 - HttpServer s = initServer(dir.getPath(), false);
93.119 - try {
93.120 - launchServerAndBrwsr(s, startpage);
93.121 - } catch (URISyntaxException | InterruptedException ex) {
93.122 - throw new IOException(ex);
93.123 - }
93.124 - }
93.125 -
93.126 - @Override
93.127 - public void initialize() throws IOException {
93.128 - try {
93.129 - executeInBrowser();
93.130 - } catch (InterruptedException ex) {
93.131 - final InterruptedIOException iio = new InterruptedIOException(ex.getMessage());
93.132 - iio.initCause(ex);
93.133 - throw iio;
93.134 - } catch (Exception ex) {
93.135 - if (ex instanceof IOException) {
93.136 - throw (IOException)ex;
93.137 - }
93.138 - if (ex instanceof RuntimeException) {
93.139 - throw (RuntimeException)ex;
93.140 - }
93.141 - throw new IOException(ex);
93.142 - }
93.143 - }
93.144 -
93.145 - private HttpServer initServer(String path, boolean addClasses) throws IOException {
93.146 - HttpServer s = HttpServer.createSimpleServer(path, new PortRange(8080, 65535));
93.147 -
93.148 - final ServerConfiguration conf = s.getServerConfiguration();
93.149 - if (addClasses) {
93.150 - conf.addHttpHandler(new VM(resources), "/bck2brwsr.js");
93.151 - conf.addHttpHandler(new Classes(resources), "/classes/");
93.152 - }
93.153 - return s;
93.154 - }
93.155 -
93.156 - private void executeInBrowser() throws InterruptedException, URISyntaxException, IOException {
93.157 - wait = new CountDownLatch(1);
93.158 - server = initServer(".", true);
93.159 - final ServerConfiguration conf = server.getServerConfiguration();
93.160 -
93.161 - class DynamicResourceHandler extends HttpHandler {
93.162 - private final InvocationContext ic;
93.163 - public DynamicResourceHandler(InvocationContext ic) {
93.164 - if (ic == null || ic.resources.isEmpty()) {
93.165 - throw new NullPointerException();
93.166 - }
93.167 - this.ic = ic;
93.168 - for (Resource r : ic.resources) {
93.169 - conf.addHttpHandler(this, r.httpPath);
93.170 - }
93.171 - }
93.172 -
93.173 - public void close() {
93.174 - conf.removeHttpHandler(this);
93.175 - }
93.176 -
93.177 - @Override
93.178 - public void service(Request request, Response response) throws Exception {
93.179 - for (Resource r : ic.resources) {
93.180 - if (r.httpPath.equals(request.getRequestURI())) {
93.181 - LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
93.182 - response.setContentType(r.httpType);
93.183 - r.httpContent.reset();
93.184 - String[] params = null;
93.185 - if (r.parameters.length != 0) {
93.186 - params = new String[r.parameters.length];
93.187 - for (int i = 0; i < r.parameters.length; i++) {
93.188 - params[i] = request.getParameter(r.parameters[i]);
93.189 - }
93.190 - }
93.191 -
93.192 - copyStream(r.httpContent, response.getOutputStream(), null, params);
93.193 - }
93.194 - }
93.195 - }
93.196 - }
93.197 -
93.198 - conf.addHttpHandler(new Page(resources,
93.199 - "org/apidesign/bck2brwsr/launcher/harness.xhtml"
93.200 - ), "/execute");
93.201 -
93.202 - conf.addHttpHandler(new HttpHandler() {
93.203 - int cnt;
93.204 - List<InvocationContext> cases = new ArrayList<>();
93.205 - DynamicResourceHandler prev;
93.206 - @Override
93.207 - public void service(Request request, Response response) throws Exception {
93.208 - String id = request.getParameter("request");
93.209 - String value = request.getParameter("result");
93.210 - if (value != null && value.indexOf((char)0xC5) != -1) {
93.211 - value = toUTF8(value);
93.212 - }
93.213 -
93.214 -
93.215 - InvocationContext mi = null;
93.216 - int caseNmbr = -1;
93.217 -
93.218 - if (id != null && value != null) {
93.219 - LOG.log(Level.INFO, "Received result for case {0} = {1}", new Object[]{id, value});
93.220 - value = decodeURL(value);
93.221 - int indx = Integer.parseInt(id);
93.222 - cases.get(indx).result(value, null);
93.223 - if (++indx < cases.size()) {
93.224 - mi = cases.get(indx);
93.225 - LOG.log(Level.INFO, "Re-executing case {0}", indx);
93.226 - caseNmbr = indx;
93.227 - }
93.228 - } else {
93.229 - if (!cases.isEmpty()) {
93.230 - LOG.info("Re-executing test cases");
93.231 - mi = cases.get(0);
93.232 - caseNmbr = 0;
93.233 - }
93.234 - }
93.235 -
93.236 - if (prev != null) {
93.237 - prev.close();
93.238 - prev = null;
93.239 - }
93.240 -
93.241 - if (mi == null) {
93.242 - mi = methods.take();
93.243 - caseNmbr = cnt++;
93.244 - }
93.245 - if (mi == END) {
93.246 - response.getWriter().write("");
93.247 - wait.countDown();
93.248 - cnt = 0;
93.249 - LOG.log(Level.INFO, "End of data reached. Exiting.");
93.250 - return;
93.251 - }
93.252 -
93.253 - if (!mi.resources.isEmpty()) {
93.254 - prev = new DynamicResourceHandler(mi);
93.255 - }
93.256 -
93.257 - cases.add(mi);
93.258 - final String cn = mi.clazz.getName();
93.259 - final String mn = mi.methodName;
93.260 - LOG.log(Level.INFO, "Request for {0} case. Sending {1}.{2}", new Object[]{caseNmbr, cn, mn});
93.261 - response.getWriter().write("{"
93.262 - + "className: '" + cn + "', "
93.263 - + "methodName: '" + mn + "', "
93.264 - + "request: " + caseNmbr
93.265 - );
93.266 - if (mi.html != null) {
93.267 - response.getWriter().write(", html: '");
93.268 - response.getWriter().write(encodeJSON(mi.html));
93.269 - response.getWriter().write("'");
93.270 - }
93.271 - response.getWriter().write("}");
93.272 - }
93.273 - }, "/data");
93.274 -
93.275 - this.brwsr = launchServerAndBrwsr(server, "/execute");
93.276 - }
93.277 -
93.278 - private static String encodeJSON(String in) {
93.279 - StringBuilder sb = new StringBuilder();
93.280 - for (int i = 0; i < in.length(); i++) {
93.281 - char ch = in.charAt(i);
93.282 - if (ch < 32 || ch == '\'' || ch == '"') {
93.283 - sb.append("\\u");
93.284 - String hs = "0000" + Integer.toHexString(ch);
93.285 - hs = hs.substring(hs.length() - 4);
93.286 - sb.append(hs);
93.287 - } else {
93.288 - sb.append(ch);
93.289 - }
93.290 - }
93.291 - return sb.toString();
93.292 - }
93.293 -
93.294 - @Override
93.295 - public void shutdown() throws IOException {
93.296 - methods.offer(END);
93.297 - for (;;) {
93.298 - int prev = methods.size();
93.299 - try {
93.300 - if (wait != null && wait.await(timeOut, TimeUnit.MILLISECONDS)) {
93.301 - break;
93.302 - }
93.303 - } catch (InterruptedException ex) {
93.304 - throw new IOException(ex);
93.305 - }
93.306 - if (prev == methods.size()) {
93.307 - LOG.log(
93.308 - Level.WARNING,
93.309 - "Timeout and no test has been executed meanwhile (at {0}). Giving up.",
93.310 - methods.size()
93.311 - );
93.312 - break;
93.313 - }
93.314 - LOG.log(Level.INFO,
93.315 - "Timeout, but tests got from {0} to {1}. Trying again.",
93.316 - new Object[]{prev, methods.size()}
93.317 - );
93.318 - }
93.319 - stopServerAndBrwsr(server, brwsr);
93.320 - }
93.321 -
93.322 - static void copyStream(InputStream is, OutputStream os, String baseURL, String... params) throws IOException {
93.323 - for (;;) {
93.324 - int ch = is.read();
93.325 - if (ch == -1) {
93.326 - break;
93.327 - }
93.328 - if (ch == '$' && params.length > 0) {
93.329 - int cnt = is.read() - '0';
93.330 - if (baseURL != null && cnt == 'U' - '0') {
93.331 - os.write(baseURL.getBytes("UTF-8"));
93.332 - } else {
93.333 - if (cnt >= 0 && cnt < params.length) {
93.334 - os.write(params[cnt].getBytes("UTF-8"));
93.335 - } else {
93.336 - os.write('$');
93.337 - os.write(cnt + '0');
93.338 - }
93.339 - }
93.340 - } else {
93.341 - os.write(ch);
93.342 - }
93.343 - }
93.344 - }
93.345 -
93.346 - private Object[] launchServerAndBrwsr(HttpServer server, final String page) throws IOException, URISyntaxException, InterruptedException {
93.347 - server.start();
93.348 - NetworkListener listener = server.getListeners().iterator().next();
93.349 - int port = listener.getPort();
93.350 -
93.351 - URI uri = new URI("http://localhost:" + port + page);
93.352 - LOG.log(Level.INFO, "Showing {0}", uri);
93.353 - if (cmd == null) {
93.354 - try {
93.355 - LOG.log(Level.INFO, "Trying Desktop.browse on {0} {2} by {1}", new Object[] {
93.356 - System.getProperty("java.vm.name"),
93.357 - System.getProperty("java.vm.vendor"),
93.358 - System.getProperty("java.vm.version"),
93.359 - });
93.360 - java.awt.Desktop.getDesktop().browse(uri);
93.361 - LOG.log(Level.INFO, "Desktop.browse successfully finished");
93.362 - return null;
93.363 - } catch (UnsupportedOperationException ex) {
93.364 - LOG.log(Level.INFO, "Desktop.browse not supported: {0}", ex.getMessage());
93.365 - LOG.log(Level.FINE, null, ex);
93.366 - }
93.367 - }
93.368 - {
93.369 - String cmdName = cmd == null ? "xdg-open" : cmd;
93.370 - String[] cmdArr = {
93.371 - cmdName, uri.toString()
93.372 - };
93.373 - LOG.log(Level.INFO, "Launching {0}", Arrays.toString(cmdArr));
93.374 - final Process process = Runtime.getRuntime().exec(cmdArr);
93.375 - return new Object[] { process, null };
93.376 - }
93.377 - }
93.378 - private static String toUTF8(String value) throws UnsupportedEncodingException {
93.379 - byte[] arr = new byte[value.length()];
93.380 - for (int i = 0; i < arr.length; i++) {
93.381 - arr[i] = (byte)value.charAt(i);
93.382 - }
93.383 - return new String(arr, "UTF-8");
93.384 - }
93.385 -
93.386 - private static String decodeURL(String s) {
93.387 - for (;;) {
93.388 - int pos = s.indexOf('%');
93.389 - if (pos == -1) {
93.390 - return s;
93.391 - }
93.392 - int i = Integer.parseInt(s.substring(pos + 1, pos + 2), 16);
93.393 - s = s.substring(0, pos) + (char)i + s.substring(pos + 2);
93.394 - }
93.395 - }
93.396 -
93.397 - private void stopServerAndBrwsr(HttpServer server, Object[] brwsr) throws IOException {
93.398 - if (brwsr == null) {
93.399 - return;
93.400 - }
93.401 - Process process = (Process)brwsr[0];
93.402 -
93.403 - server.stop();
93.404 - InputStream stdout = process.getInputStream();
93.405 - InputStream stderr = process.getErrorStream();
93.406 - drain("StdOut", stdout);
93.407 - drain("StdErr", stderr);
93.408 - process.destroy();
93.409 - int res;
93.410 - try {
93.411 - res = process.waitFor();
93.412 - } catch (InterruptedException ex) {
93.413 - throw new IOException(ex);
93.414 - }
93.415 - LOG.log(Level.INFO, "Exit code: {0}", res);
93.416 -
93.417 - deleteTree((File)brwsr[1]);
93.418 - }
93.419 -
93.420 - private static void drain(String name, InputStream is) throws IOException {
93.421 - int av = is.available();
93.422 - if (av > 0) {
93.423 - StringBuilder sb = new StringBuilder();
93.424 - sb.append("v== ").append(name).append(" ==v\n");
93.425 - while (av-- > 0) {
93.426 - sb.append((char)is.read());
93.427 - }
93.428 - sb.append("\n^== ").append(name).append(" ==^");
93.429 - LOG.log(Level.INFO, sb.toString());
93.430 - }
93.431 - }
93.432 -
93.433 - private void deleteTree(File file) {
93.434 - if (file == null) {
93.435 - return;
93.436 - }
93.437 - File[] arr = file.listFiles();
93.438 - if (arr != null) {
93.439 - for (File s : arr) {
93.440 - deleteTree(s);
93.441 - }
93.442 - }
93.443 - file.delete();
93.444 - }
93.445 -
93.446 - @Override
93.447 - public void close() throws IOException {
93.448 - shutdown();
93.449 - }
93.450 -
93.451 - private class Res implements Bck2Brwsr.Resources {
93.452 - @Override
93.453 - public InputStream get(String resource) throws IOException {
93.454 - for (ClassLoader l : loaders) {
93.455 - URL u = null;
93.456 - Enumeration<URL> en = l.getResources(resource);
93.457 - while (en.hasMoreElements()) {
93.458 - u = en.nextElement();
93.459 - }
93.460 - if (u != null) {
93.461 - return u.openStream();
93.462 - }
93.463 - }
93.464 - throw new IOException("Can't find " + resource);
93.465 - }
93.466 - }
93.467 -
93.468 - private static class Page extends HttpHandler {
93.469 - final String resource;
93.470 - private final String[] args;
93.471 - private final Res res;
93.472 -
93.473 - public Page(Res res, String resource, String... args) {
93.474 - this.res = res;
93.475 - this.resource = resource;
93.476 - this.args = args.length == 0 ? new String[] { "$0" } : args;
93.477 - }
93.478 -
93.479 - @Override
93.480 - public void service(Request request, Response response) throws Exception {
93.481 - String r = computePage(request);
93.482 - if (r.startsWith("/")) {
93.483 - r = r.substring(1);
93.484 - }
93.485 - String[] replace = {};
93.486 - if (r.endsWith(".html")) {
93.487 - response.setContentType("text/html");
93.488 - LOG.info("Content type text/html");
93.489 - replace = args;
93.490 - }
93.491 - if (r.endsWith(".xhtml")) {
93.492 - response.setContentType("application/xhtml+xml");
93.493 - LOG.info("Content type application/xhtml+xml");
93.494 - replace = args;
93.495 - }
93.496 - OutputStream os = response.getOutputStream();
93.497 - try (InputStream is = res.get(r)) {
93.498 - copyStream(is, os, request.getRequestURL().toString(), replace);
93.499 - } catch (IOException ex) {
93.500 - response.setDetailMessage(ex.getLocalizedMessage());
93.501 - response.setError();
93.502 - response.setStatus(404);
93.503 - }
93.504 - }
93.505 -
93.506 - protected String computePage(Request request) {
93.507 - String r = resource;
93.508 - if (r == null) {
93.509 - r = request.getHttpHandlerPath();
93.510 - }
93.511 - return r;
93.512 - }
93.513 - }
93.514 -
93.515 - private static class SubTree extends Page {
93.516 -
93.517 - public SubTree(Res res, String resource, String... args) {
93.518 - super(res, resource, args);
93.519 - }
93.520 -
93.521 - @Override
93.522 - protected String computePage(Request request) {
93.523 - return resource + request.getHttpHandlerPath();
93.524 - }
93.525 -
93.526 -
93.527 - }
93.528 -
93.529 - private static class VM extends HttpHandler {
93.530 - private final String bck2brwsr;
93.531 -
93.532 - public VM(Res loader) throws IOException {
93.533 - StringBuilder sb = new StringBuilder();
93.534 - Bck2Brwsr.generate(sb, loader);
93.535 - sb.append(
93.536 - "(function WrapperVM(global) {"
93.537 - + " function ldCls(res) {\n"
93.538 - + " var request = new XMLHttpRequest();\n"
93.539 - + " request.open('GET', '/classes/' + res, false);\n"
93.540 - + " request.send();\n"
93.541 - + " if (request.status !== 200) return null;\n"
93.542 - + " var arr = eval('(' + request.responseText + ')');\n"
93.543 - + " return arr;\n"
93.544 - + " }\n"
93.545 - + " var prevvm = global.bck2brwsr;\n"
93.546 - + " global.bck2brwsr = function() {\n"
93.547 - + " var args = Array.prototype.slice.apply(arguments);\n"
93.548 - + " args.unshift(ldCls);\n"
93.549 - + " return prevvm.apply(null, args);\n"
93.550 - + " };\n"
93.551 - + "})(this);\n"
93.552 - );
93.553 - this.bck2brwsr = sb.toString();
93.554 - }
93.555 -
93.556 - @Override
93.557 - public void service(Request request, Response response) throws Exception {
93.558 - response.setCharacterEncoding("UTF-8");
93.559 - response.setContentType("text/javascript");
93.560 - response.getWriter().write(bck2brwsr);
93.561 - }
93.562 - }
93.563 -
93.564 - private static class Classes extends HttpHandler {
93.565 - private final Res loader;
93.566 -
93.567 - public Classes(Res loader) {
93.568 - this.loader = loader;
93.569 - }
93.570 -
93.571 - @Override
93.572 - public void service(Request request, Response response) throws Exception {
93.573 - String res = request.getHttpHandlerPath();
93.574 - if (res.startsWith("/")) {
93.575 - res = res.substring(1);
93.576 - }
93.577 - try (InputStream is = loader.get(res)) {
93.578 - response.setContentType("text/javascript");
93.579 - Writer w = response.getWriter();
93.580 - w.append("[");
93.581 - for (int i = 0;; i++) {
93.582 - int b = is.read();
93.583 - if (b == -1) {
93.584 - break;
93.585 - }
93.586 - if (i > 0) {
93.587 - w.append(", ");
93.588 - }
93.589 - if (i % 20 == 0) {
93.590 - w.write("\n");
93.591 - }
93.592 - if (b > 127) {
93.593 - b = b - 256;
93.594 - }
93.595 - w.append(Integer.toString(b));
93.596 - }
93.597 - w.append("\n]");
93.598 - } catch (IOException ex) {
93.599 - response.setStatus(HttpStatus.NOT_FOUND_404);
93.600 - response.setError();
93.601 - response.setDetailMessage(ex.getMessage());
93.602 - }
93.603 - }
93.604 - }
93.605 -}
94.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Fri May 24 16:45:32 2013 +0200
94.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
94.3 @@ -1,115 +0,0 @@
94.4 -/**
94.5 - * Back 2 Browser Bytecode Translator
94.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
94.7 - *
94.8 - * This program is free software: you can redistribute it and/or modify
94.9 - * it under the terms of the GNU General Public License as published by
94.10 - * the Free Software Foundation, version 2 of the License.
94.11 - *
94.12 - * This program is distributed in the hope that it will be useful,
94.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
94.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
94.15 - * GNU General Public License for more details.
94.16 - *
94.17 - * You should have received a copy of the GNU General Public License
94.18 - * along with this program. Look for COPYING file in the top folder.
94.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
94.20 - */
94.21 -package org.apidesign.bck2brwsr.launcher;
94.22 -
94.23 -import java.io.IOException;
94.24 -import java.io.InputStream;
94.25 -import java.util.ArrayList;
94.26 -import java.util.List;
94.27 -import java.util.concurrent.CountDownLatch;
94.28 -import java.util.concurrent.TimeUnit;
94.29 -
94.30 -/** Represents individual method invocation, its context and its result.
94.31 - *
94.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
94.33 - */
94.34 -public final class InvocationContext {
94.35 - final CountDownLatch wait = new CountDownLatch(1);
94.36 - final Class<?> clazz;
94.37 - final String methodName;
94.38 - private final Launcher launcher;
94.39 - private String result;
94.40 - private Throwable exception;
94.41 - String html;
94.42 - final List<Resource> resources = new ArrayList<>();
94.43 -
94.44 - InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
94.45 - this.launcher = launcher;
94.46 - this.clazz = clazz;
94.47 - this.methodName = methodName;
94.48 - }
94.49 -
94.50 - /** An HTML fragment to be available for the execution. Useful primarily when
94.51 - * executing in a browser via {@link Launcher#createBrowser(java.lang.String)}.
94.52 - * @param html the html fragment
94.53 - */
94.54 - public void setHtmlFragment(String html) {
94.55 - this.html = html;
94.56 - }
94.57 -
94.58 - /** HTTP resource to be available during execution. An invocation may
94.59 - * perform an HTTP query and obtain a resource relative to the page.
94.60 - */
94.61 - public void addHttpResource(String relativePath, String mimeType, String[] parameters, InputStream content) {
94.62 - if (relativePath == null || mimeType == null || content == null || parameters == null) {
94.63 - throw new NullPointerException();
94.64 - }
94.65 - resources.add(new Resource(content, mimeType, relativePath, parameters));
94.66 - }
94.67 -
94.68 - /** Invokes the associated method.
94.69 - * @return the textual result of the invocation
94.70 - */
94.71 - public String invoke() throws IOException {
94.72 - launcher.runMethod(this);
94.73 - return toString();
94.74 - }
94.75 -
94.76 - /** Obtains textual result of the invocation.
94.77 - * @return text representing the exception or result value
94.78 - */
94.79 - @Override
94.80 - public String toString() {
94.81 - if (exception != null) {
94.82 - return exception.toString();
94.83 - }
94.84 - return result;
94.85 - }
94.86 -
94.87 - /**
94.88 - * @param timeOut
94.89 - * @throws InterruptedException
94.90 - */
94.91 - void await(long timeOut) throws InterruptedException {
94.92 - wait.await(timeOut, TimeUnit.MILLISECONDS);
94.93 - }
94.94 -
94.95 - void result(String r, Throwable e) {
94.96 - this.result = r;
94.97 - this.exception = e;
94.98 - wait.countDown();
94.99 - }
94.100 -
94.101 -
94.102 - static final class Resource {
94.103 - final InputStream httpContent;
94.104 - final String httpType;
94.105 - final String httpPath;
94.106 - final String[] parameters;
94.107 -
94.108 - Resource(InputStream httpContent, String httpType, String httpPath,
94.109 - String[] parameters
94.110 - ) {
94.111 - httpContent.mark(Integer.MAX_VALUE);
94.112 - this.httpContent = httpContent;
94.113 - this.httpType = httpType;
94.114 - this.httpPath = httpPath;
94.115 - this.parameters = parameters;
94.116 - }
94.117 - }
94.118 -}
95.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/JSLauncher.java Fri May 24 16:45:32 2013 +0200
95.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
95.3 @@ -1,132 +0,0 @@
95.4 -/**
95.5 - * Back 2 Browser Bytecode Translator
95.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
95.7 - *
95.8 - * This program is free software: you can redistribute it and/or modify
95.9 - * it under the terms of the GNU General Public License as published by
95.10 - * the Free Software Foundation, version 2 of the License.
95.11 - *
95.12 - * This program is distributed in the hope that it will be useful,
95.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
95.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95.15 - * GNU General Public License for more details.
95.16 - *
95.17 - * You should have received a copy of the GNU General Public License
95.18 - * along with this program. Look for COPYING file in the top folder.
95.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
95.20 - */
95.21 -package org.apidesign.bck2brwsr.launcher;
95.22 -
95.23 -import org.apidesign.bck2brwsr.launcher.impl.Console;
95.24 -import java.io.IOException;
95.25 -import java.io.InputStream;
95.26 -import java.net.URL;
95.27 -import java.util.Enumeration;
95.28 -import java.util.LinkedHashSet;
95.29 -import java.util.Set;
95.30 -import java.util.logging.Level;
95.31 -import java.util.logging.Logger;
95.32 -import javax.script.Invocable;
95.33 -import javax.script.ScriptEngine;
95.34 -import javax.script.ScriptEngineManager;
95.35 -import javax.script.ScriptException;
95.36 -import org.apidesign.vm4brwsr.Bck2Brwsr;
95.37 -
95.38 -/**
95.39 - * Tests execution in Java's internal scripting engine.
95.40 - */
95.41 -final class JSLauncher extends Launcher {
95.42 - private static final Logger LOG = Logger.getLogger(JSLauncher.class.getName());
95.43 - private Set<ClassLoader> loaders = new LinkedHashSet<>();
95.44 - private final Res resources = new Res();
95.45 - private Invocable code;
95.46 - private StringBuilder codeSeq;
95.47 - private Object console;
95.48 -
95.49 -
95.50 - @Override InvocationContext runMethod(InvocationContext mi) {
95.51 - loaders.add(mi.clazz.getClassLoader());
95.52 - try {
95.53 - long time = System.currentTimeMillis();
95.54 - LOG.log(Level.FINE, "Invoking {0}.{1}", new Object[]{mi.clazz.getName(), mi.methodName});
95.55 - String res = code.invokeMethod(
95.56 - console,
95.57 - "invoke__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2",
95.58 - mi.clazz.getName(), mi.methodName).toString();
95.59 - time = System.currentTimeMillis() - time;
95.60 - LOG.log(Level.FINE, "Resut of {0}.{1} = {2} in {3} ms", new Object[]{mi.clazz.getName(), mi.methodName, res, time});
95.61 - mi.result(res, null);
95.62 - } catch (ScriptException | NoSuchMethodException ex) {
95.63 - mi.result(null, ex);
95.64 - }
95.65 - return mi;
95.66 - }
95.67 -
95.68 - public void addClassLoader(ClassLoader url) {
95.69 - this.loaders.add(url);
95.70 - }
95.71 -
95.72 - @Override
95.73 - public void initialize() throws IOException {
95.74 - try {
95.75 - initRhino();
95.76 - } catch (Exception ex) {
95.77 - if (ex instanceof IOException) {
95.78 - throw (IOException)ex;
95.79 - }
95.80 - if (ex instanceof RuntimeException) {
95.81 - throw (RuntimeException)ex;
95.82 - }
95.83 - throw new IOException(ex);
95.84 - }
95.85 - }
95.86 -
95.87 - private void initRhino() throws IOException, ScriptException, NoSuchMethodException {
95.88 - StringBuilder sb = new StringBuilder();
95.89 - Bck2Brwsr.generate(sb, new Res());
95.90 -
95.91 - ScriptEngineManager sem = new ScriptEngineManager();
95.92 - ScriptEngine mach = sem.getEngineByExtension("js");
95.93 -
95.94 - sb.append(
95.95 - "\nvar vm = new bck2brwsr(org.apidesign.bck2brwsr.launcher.impl.Console.read);"
95.96 - + "\nfunction initVM() { return vm; };"
95.97 - + "\n");
95.98 -
95.99 - Object res = mach.eval(sb.toString());
95.100 - if (!(mach instanceof Invocable)) {
95.101 - throw new IOException("It is invocable object: " + res);
95.102 - }
95.103 - code = (Invocable) mach;
95.104 - codeSeq = sb;
95.105 -
95.106 - Object vm = code.invokeFunction("initVM");
95.107 - console = code.invokeMethod(vm, "loadClass", Console.class.getName());
95.108 - }
95.109 -
95.110 - @Override
95.111 - public void shutdown() throws IOException {
95.112 - }
95.113 -
95.114 - @Override
95.115 - public String toString() {
95.116 - return codeSeq.toString();
95.117 - }
95.118 -
95.119 - private class Res implements Bck2Brwsr.Resources {
95.120 - @Override
95.121 - public InputStream get(String resource) throws IOException {
95.122 - for (ClassLoader l : loaders) {
95.123 - URL u = null;
95.124 - Enumeration<URL> en = l.getResources(resource);
95.125 - while (en.hasMoreElements()) {
95.126 - u = en.nextElement();
95.127 - }
95.128 - if (u != null) {
95.129 - return u.openStream();
95.130 - }
95.131 - }
95.132 - throw new IOException("Can't find " + resource);
95.133 - }
95.134 - }
95.135 -}
96.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Launcher.java Fri May 24 16:45:32 2013 +0200
96.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
96.3 @@ -1,116 +0,0 @@
96.4 -/**
96.5 - * Back 2 Browser Bytecode Translator
96.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
96.7 - *
96.8 - * This program is free software: you can redistribute it and/or modify
96.9 - * it under the terms of the GNU General Public License as published by
96.10 - * the Free Software Foundation, version 2 of the License.
96.11 - *
96.12 - * This program is distributed in the hope that it will be useful,
96.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
96.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96.15 - * GNU General Public License for more details.
96.16 - *
96.17 - * You should have received a copy of the GNU General Public License
96.18 - * along with this program. Look for COPYING file in the top folder.
96.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
96.20 - */
96.21 -package org.apidesign.bck2brwsr.launcher;
96.22 -
96.23 -import java.io.Closeable;
96.24 -import java.io.File;
96.25 -import java.io.IOException;
96.26 -import org.apidesign.vm4brwsr.Bck2Brwsr;
96.27 -
96.28 -/** An abstraction for executing tests in a Bck2Brwsr virtual machine.
96.29 - * Either in {@linkm Launcher#createJavaScript JavaScript engine},
96.30 - * or in {@linkm Launcher#createBrowser external browser}.
96.31 - * <p>
96.32 - * There also are methods to {@link #showDir(java.io.File, java.lang.String) display pages}
96.33 - * in an external browser served by internal HTTP server.
96.34 - *
96.35 - * @author Jaroslav Tulach <jtulach@netbeans.org>
96.36 - */
96.37 -public abstract class Launcher {
96.38 -
96.39 - Launcher() {
96.40 - }
96.41 -
96.42 - /** Initializes the launcher. This may mean starting a web browser or
96.43 - * initializing execution engine.
96.44 - * @throws IOException if something goes wrong
96.45 - */
96.46 - public abstract void initialize() throws IOException;
96.47 -
96.48 - /** Shuts down the launcher.
96.49 - * @throws IOException if something goes wrong
96.50 - */
96.51 - public abstract void shutdown() throws IOException;
96.52 -
96.53 -
96.54 - /** Builds an invocation context. The context can later be customized
96.55 - * and {@link InvocationContext#invoke() invoked}.
96.56 - *
96.57 - * @param clazz the class to execute method from
96.58 - * @param method the method to execute
96.59 - * @return the context pointing to the selected method
96.60 - */
96.61 - public InvocationContext createInvocation(Class<?> clazz, String method) {
96.62 - return new InvocationContext(this, clazz, method);
96.63 - }
96.64 -
96.65 -
96.66 - /** Creates launcher that uses internal JavaScript engine (Rhino).
96.67 - * @return the launcher
96.68 - */
96.69 - public static Launcher createJavaScript() {
96.70 - final JSLauncher l = new JSLauncher();
96.71 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
96.72 - return l;
96.73 - }
96.74 -
96.75 - /** Creates launcher that is using external browser.
96.76 - *
96.77 - * @param cmd <code>null</code> to use <code>java.awt.Desktop</code> to show the launcher
96.78 - * or a string to execute in an external process (with a parameter to the URL)
96.79 - * @return launcher executing in external browser.
96.80 - */
96.81 - public static Launcher createBrowser(String cmd) {
96.82 - final Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(cmd);
96.83 - l.addClassLoader(Bck2Brwsr.class.getClassLoader());
96.84 - l.setTimeout(180000);
96.85 - return l;
96.86 - }
96.87 -
96.88 - /** Starts an HTTP server which provides access to classes and resources
96.89 - * available in the <code>classes</code> URL and shows a start page
96.90 - * available as {@link ClassLoader#getResource(java.lang.String)} from the
96.91 - * provide classloader. Opens a browser with URL showing the start page.
96.92 - *
96.93 - * @param classes classloader offering access to classes and resources
96.94 - * @param startpage page to show in the browser
96.95 - * @return interface that allows one to stop the server
96.96 - * @throws IOException if something goes wrong
96.97 - */
96.98 - public static Closeable showURL(ClassLoader classes, String startpage) throws IOException {
96.99 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
96.100 - l.addClassLoader(classes);
96.101 - l.showURL(startpage);
96.102 - return l;
96.103 - }
96.104 - /** Starts an HTTP server which provides access to certain directory.
96.105 - * The <code>startpage</code> should be relative location inside the root
96.106 - * directory. Opens a browser with URL showing the start page.
96.107 - *
96.108 - * @param directory the root directory on disk
96.109 - * @param startpage relative path from the root to the page
96.110 - * @exception IOException if something goes wrong.
96.111 - */
96.112 - public static Closeable showDir(File directory, String startpage) throws IOException {
96.113 - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(null);
96.114 - l.showDirectory(directory, startpage);
96.115 - return l;
96.116 - }
96.117 -
96.118 - abstract InvocationContext runMethod(InvocationContext c) throws IOException;
96.119 -}
97.1 --- a/rt/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/impl/Console.java Fri May 24 16:45:32 2013 +0200
97.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
97.3 @@ -1,356 +0,0 @@
97.4 -/**
97.5 - * Back 2 Browser Bytecode Translator
97.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
97.7 - *
97.8 - * This program is free software: you can redistribute it and/or modify
97.9 - * it under the terms of the GNU General Public License as published by
97.10 - * the Free Software Foundation, version 2 of the License.
97.11 - *
97.12 - * This program is distributed in the hope that it will be useful,
97.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
97.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
97.15 - * GNU General Public License for more details.
97.16 - *
97.17 - * You should have received a copy of the GNU General Public License
97.18 - * along with this program. Look for COPYING file in the top folder.
97.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
97.20 - */
97.21 -package org.apidesign.bck2brwsr.launcher.impl;
97.22 -
97.23 -import java.io.IOException;
97.24 -import java.io.InputStream;
97.25 -import java.io.UnsupportedEncodingException;
97.26 -import java.lang.reflect.InvocationTargetException;
97.27 -import java.lang.reflect.Method;
97.28 -import java.lang.reflect.Modifier;
97.29 -import java.net.URL;
97.30 -import java.util.Enumeration;
97.31 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
97.32 -
97.33 -/**
97.34 - *
97.35 - * @author Jaroslav Tulach <jtulach@netbeans.org>
97.36 - */
97.37 -public class Console {
97.38 - private Console() {
97.39 - }
97.40 - static {
97.41 - turnAssetionStatusOn();
97.42 - }
97.43 -
97.44 - @JavaScriptBody(args = {"id", "attr"}, body =
97.45 - "return window.document.getElementById(id)[attr].toString();")
97.46 - private static native Object getAttr(String id, String attr);
97.47 - @JavaScriptBody(args = {"elem", "attr"}, body =
97.48 - "return elem[attr].toString();")
97.49 - private static native Object getAttr(Object elem, String attr);
97.50 -
97.51 - @JavaScriptBody(args = {"id", "attr", "value"}, body =
97.52 - "window.document.getElementById(id)[attr] = value;")
97.53 - private static native void setAttr(String id, String attr, Object value);
97.54 - @JavaScriptBody(args = {"elem", "attr", "value"}, body =
97.55 - "elem[attr] = value;")
97.56 - private static native void setAttr(Object id, String attr, Object value);
97.57 -
97.58 - @JavaScriptBody(args = {}, body = "return; window.close();")
97.59 - private static native void closeWindow();
97.60 -
97.61 - private static Object textArea;
97.62 - private static Object statusArea;
97.63 -
97.64 - private static void log(String newText) {
97.65 - if (textArea == null) {
97.66 - return;
97.67 - }
97.68 - String attr = "value";
97.69 - setAttr(textArea, attr, getAttr(textArea, attr) + "\n" + newText);
97.70 - setAttr(textArea, "scrollTop", getAttr(textArea, "scrollHeight"));
97.71 - }
97.72 -
97.73 - private static void beginTest(Case c) {
97.74 - Object[] arr = new Object[2];
97.75 - beginTest(c.getClassName() + "." + c.getMethodName(), c, arr);
97.76 - textArea = arr[0];
97.77 - statusArea = arr[1];
97.78 - }
97.79 -
97.80 - private static void finishTest(Case c, Object res) {
97.81 - if ("null".equals(res)) {
97.82 - setAttr(statusArea, "innerHTML", "Success");
97.83 - } else {
97.84 - setAttr(statusArea, "innerHTML", "Result " + res);
97.85 - }
97.86 - statusArea = null;
97.87 - textArea = null;
97.88 - }
97.89 -
97.90 - @JavaScriptBody(args = { "test", "c", "arr" }, body =
97.91 - "var ul = window.document.getElementById('bck2brwsr.result');\n"
97.92 - + "var li = window.document.createElement('li');\n"
97.93 - + "var span = window.document.createElement('span');"
97.94 - + "span.innerHTML = test + ' - ';\n"
97.95 - + "var details = window.document.createElement('a');\n"
97.96 - + "details.innerHTML = 'Details';\n"
97.97 - + "details.href = '#';\n"
97.98 - + "var p = window.document.createElement('p');\n"
97.99 - + "var status = window.document.createElement('a');\n"
97.100 - + "status.innerHTML = 'running';"
97.101 - + "details.onclick = function() { li.appendChild(p); li.removeChild(details); status.innerHTML = 'Run Again'; status.href = '#'; };\n"
97.102 - + "status.onclick = function() { c.again__V_3Ljava_lang_Object_2(arr); }\n"
97.103 - + "var pre = window.document.createElement('textarea');\n"
97.104 - + "pre.cols = 100;"
97.105 - + "pre.rows = 10;"
97.106 - + "li.appendChild(span);\n"
97.107 - + "li.appendChild(status);\n"
97.108 - + "var span = window.document.createElement('span');"
97.109 - + "span.innerHTML = ' ';\n"
97.110 - + "li.appendChild(span);\n"
97.111 - + "li.appendChild(details);\n"
97.112 - + "p.appendChild(pre);\n"
97.113 - + "ul.appendChild(li);\n"
97.114 - + "arr[0] = pre;\n"
97.115 - + "arr[1] = status;\n"
97.116 - )
97.117 - private static native void beginTest(String test, Case c, Object[] arr);
97.118 -
97.119 - @JavaScriptBody(args = { "url", "callback", "arr" }, body = ""
97.120 - + "var request = new XMLHttpRequest();\n"
97.121 - + "request.open('GET', url, true);\n"
97.122 - + "request.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');\n"
97.123 - + "request.onreadystatechange = function() {\n"
97.124 - + " if (this.readyState!==4) return;\n"
97.125 - + " arr[0] = this.responseText;\n"
97.126 - + " callback.run__V();\n"
97.127 - + "};"
97.128 - + "request.send();"
97.129 - )
97.130 - private static native void loadText(String url, Runnable callback, String[] arr) throws IOException;
97.131 -
97.132 - public static void harness(String url) throws IOException {
97.133 - log("Connecting to " + url);
97.134 - Request r = new Request(url);
97.135 - }
97.136 -
97.137 - private static class Request implements Runnable {
97.138 - private final String[] arr = { null };
97.139 - private final String url;
97.140 - private Case c;
97.141 - private int retries;
97.142 -
97.143 - private Request(String url) throws IOException {
97.144 - this.url = url;
97.145 - loadText(url, this, arr);
97.146 - }
97.147 - private Request(String url, String u) throws IOException {
97.148 - this.url = url;
97.149 - loadText(u, this, arr);
97.150 - }
97.151 -
97.152 - @Override
97.153 - public void run() {
97.154 - try {
97.155 - if (c == null) {
97.156 - String data = arr[0];
97.157 -
97.158 - if (data == null) {
97.159 - log("Some error exiting");
97.160 - closeWindow();
97.161 - return;
97.162 - }
97.163 -
97.164 - if (data.isEmpty()) {
97.165 - log("No data, exiting");
97.166 - closeWindow();
97.167 - return;
97.168 - }
97.169 -
97.170 - c = Case.parseData(data);
97.171 - beginTest(c);
97.172 - log("Got \"" + data + "\"");
97.173 - } else {
97.174 - log("Processing \"" + arr[0] + "\" for " + retries + " time");
97.175 - }
97.176 - Object result = retries++ >= 10 ? "java.lang.InterruptedException:timeout" : c.runTest();
97.177 - finishTest(c, result);
97.178 -
97.179 - String u = url + "?request=" + c.getRequestId() + "&result=" + result;
97.180 - new Request(url, u);
97.181 - } catch (Exception ex) {
97.182 - if (ex instanceof InterruptedException) {
97.183 - log("Re-scheduling in 100ms");
97.184 - schedule(this, 100);
97.185 - return;
97.186 - }
97.187 - log(ex.getClass().getName() + ":" + ex.getMessage());
97.188 - }
97.189 - }
97.190 - }
97.191 -
97.192 - private static String encodeURL(String r) throws UnsupportedEncodingException {
97.193 - final String SPECIAL = "%$&+,/:;=?@";
97.194 - StringBuilder sb = new StringBuilder();
97.195 - byte[] utf8 = r.getBytes("UTF-8");
97.196 - for (int i = 0; i < utf8.length; i++) {
97.197 - int ch = utf8[i] & 0xff;
97.198 - if (ch < 32 || ch > 127 || SPECIAL.indexOf(ch) >= 0) {
97.199 - final String numbers = "0" + Integer.toHexString(ch);
97.200 - sb.append("%").append(numbers.substring(numbers.length() - 2));
97.201 - } else {
97.202 - if (ch == 32) {
97.203 - sb.append("+");
97.204 - } else {
97.205 - sb.append((char)ch);
97.206 - }
97.207 - }
97.208 - }
97.209 - return sb.toString();
97.210 - }
97.211 -
97.212 - static String invoke(String clazz, String method) throws
97.213 - ClassNotFoundException, InvocationTargetException, IllegalAccessException,
97.214 - InstantiationException, InterruptedException {
97.215 - final Object r = new Case(null).invokeMethod(clazz, method);
97.216 - return r == null ? "null" : r.toString().toString();
97.217 - }
97.218 -
97.219 - /** Helper method that inspects the classpath and loads given resource
97.220 - * (usually a class file). Used while running tests in Rhino.
97.221 - *
97.222 - * @param name resource name to find
97.223 - * @return the array of bytes in the given resource
97.224 - * @throws IOException I/O in case something goes wrong
97.225 - */
97.226 - public static byte[] read(String name) throws IOException {
97.227 - URL u = null;
97.228 - Enumeration<URL> en = Console.class.getClassLoader().getResources(name);
97.229 - while (en.hasMoreElements()) {
97.230 - u = en.nextElement();
97.231 - }
97.232 - if (u == null) {
97.233 - throw new IOException("Can't find " + name);
97.234 - }
97.235 - try (InputStream is = u.openStream()) {
97.236 - byte[] arr;
97.237 - arr = new byte[is.available()];
97.238 - int offset = 0;
97.239 - while (offset < arr.length) {
97.240 - int len = is.read(arr, offset, arr.length - offset);
97.241 - if (len == -1) {
97.242 - throw new IOException("Can't read " + name);
97.243 - }
97.244 - offset += len;
97.245 - }
97.246 - return arr;
97.247 - }
97.248 - }
97.249 -
97.250 - @JavaScriptBody(args = {}, body = "vm.desiredAssertionStatus = true;")
97.251 - private static void turnAssetionStatusOn() {
97.252 - }
97.253 -
97.254 - @JavaScriptBody(args = {"r", "time"}, body =
97.255 - "return window.setTimeout(function() { r.run__V(); }, time);")
97.256 - private static native Object schedule(Runnable r, int time);
97.257 -
97.258 - private static final class Case {
97.259 - private final Object data;
97.260 - private Object inst;
97.261 -
97.262 - private Case(Object data) {
97.263 - this.data = data;
97.264 - }
97.265 -
97.266 - public static Case parseData(String s) {
97.267 - return new Case(toJSON(s));
97.268 - }
97.269 -
97.270 - public String getMethodName() {
97.271 - return value("methodName", data);
97.272 - }
97.273 -
97.274 - public String getClassName() {
97.275 - return value("className", data);
97.276 - }
97.277 -
97.278 - public String getRequestId() {
97.279 - return value("request", data);
97.280 - }
97.281 -
97.282 - public String getHtmlFragment() {
97.283 - return value("html", data);
97.284 - }
97.285 -
97.286 - void again(Object[] arr) {
97.287 - try {
97.288 - textArea = arr[0];
97.289 - statusArea = arr[1];
97.290 - setAttr(textArea, "value", "");
97.291 - runTest();
97.292 - } catch (Exception ex) {
97.293 - log(ex.getClass().getName() + ":" + ex.getMessage());
97.294 - }
97.295 - }
97.296 -
97.297 - private Object runTest() throws IllegalAccessException,
97.298 - IllegalArgumentException, ClassNotFoundException, UnsupportedEncodingException,
97.299 - InvocationTargetException, InstantiationException, InterruptedException {
97.300 - if (this.getHtmlFragment() != null) {
97.301 - setAttr("bck2brwsr.fragment", "innerHTML", this.getHtmlFragment());
97.302 - }
97.303 - log("Invoking " + this.getClassName() + '.' + this.getMethodName() + " as request: " + this.getRequestId());
97.304 - Object result = invokeMethod(this.getClassName(), this.getMethodName());
97.305 - setAttr("bck2brwsr.fragment", "innerHTML", "");
97.306 - log("Result: " + result);
97.307 - result = encodeURL("" + result);
97.308 - log("Sending back: ...?request=" + this.getRequestId() + "&result=" + result);
97.309 - return result;
97.310 - }
97.311 -
97.312 - private Object invokeMethod(String clazz, String method)
97.313 - throws ClassNotFoundException, InvocationTargetException,
97.314 - InterruptedException, IllegalAccessException, IllegalArgumentException,
97.315 - InstantiationException {
97.316 - Method found = null;
97.317 - Class<?> c = Class.forName(clazz);
97.318 - for (Method m : c.getMethods()) {
97.319 - if (m.getName().equals(method)) {
97.320 - found = m;
97.321 - }
97.322 - }
97.323 - Object res;
97.324 - if (found != null) {
97.325 - try {
97.326 - if ((found.getModifiers() & Modifier.STATIC) != 0) {
97.327 - res = found.invoke(null);
97.328 - } else {
97.329 - if (inst == null) {
97.330 - inst = c.newInstance();
97.331 - }
97.332 - res = found.invoke(inst);
97.333 - }
97.334 - } catch (Throwable ex) {
97.335 - if (ex instanceof InvocationTargetException) {
97.336 - ex = ((InvocationTargetException) ex).getTargetException();
97.337 - }
97.338 - if (ex instanceof InterruptedException) {
97.339 - throw (InterruptedException)ex;
97.340 - }
97.341 - res = ex.getClass().getName() + ":" + ex.getMessage();
97.342 - }
97.343 - } else {
97.344 - res = "Can't find method " + method + " in " + clazz;
97.345 - }
97.346 - return res;
97.347 - }
97.348 -
97.349 - @JavaScriptBody(args = "s", body = "return eval('(' + s + ')');")
97.350 - private static native Object toJSON(String s);
97.351 -
97.352 - @JavaScriptBody(args = {"p", "d"}, body =
97.353 - "var v = d[p];\n"
97.354 - + "if (typeof v === 'undefined') return null;\n"
97.355 - + "return v.toString();"
97.356 - )
97.357 - private static native String value(String p, Object d);
97.358 - }
97.359 -}
98.1 --- a/rt/launcher/src/main/resources/org/apidesign/bck2brwsr/launcher/harness.xhtml Fri May 24 16:45:32 2013 +0200
98.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
98.3 @@ -1,43 +0,0 @@
98.4 -<?xml version="1.0" encoding="UTF-8"?>
98.5 -<!--
98.6 -
98.7 - Back 2 Browser Bytecode Translator
98.8 - Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
98.9 -
98.10 - This program is free software: you can redistribute it and/or modify
98.11 - it under the terms of the GNU General Public License as published by
98.12 - the Free Software Foundation, version 2 of the License.
98.13 -
98.14 - This program is distributed in the hope that it will be useful,
98.15 - but WITHOUT ANY WARRANTY; without even the implied warranty of
98.16 - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98.17 - GNU General Public License for more details.
98.18 -
98.19 - You should have received a copy of the GNU General Public License
98.20 - along with this program. Look for COPYING file in the top folder.
98.21 - If not, see http://opensource.org/licenses/GPL-2.0.
98.22 -
98.23 --->
98.24 -<!DOCTYPE html>
98.25 -<html xmlns="http://www.w3.org/1999/xhtml">
98.26 - <head>
98.27 - <title>Bck2Brwsr Harness</title>
98.28 - </head>
98.29 - <body>
98.30 - <script src="/bck2brwsr.js"></script>
98.31 - <script>
98.32 - var vm = bck2brwsr();
98.33 - </script>
98.34 -
98.35 - <h1>Bck2Brwsr Execution Harness</h1>
98.36 -
98.37 - <ul id="bck2brwsr.result" style="width: 100%;" >
98.38 - </ul>
98.39 -
98.40 - <div id="bck2brwsr.fragment"/>
98.41 -
98.42 - <script type="text/javascript">
98.43 - vm.loadClass('org.apidesign.bck2brwsr.launcher.impl.Console').harness__VLjava_lang_String_2('$U/../data');
98.44 - </script>
98.45 - </body>
98.46 -</html>
99.1 --- a/rt/mojo/pom.xml Fri May 24 16:45:32 2013 +0200
99.2 +++ b/rt/mojo/pom.xml Fri May 24 18:04:55 2013 +0200
99.3 @@ -4,14 +4,14 @@
99.4 <parent>
99.5 <groupId>org.apidesign.bck2brwsr</groupId>
99.6 <artifactId>rt</artifactId>
99.7 - <version>0.6-SNAPSHOT</version>
99.8 + <version>0.8-SNAPSHOT</version>
99.9 </parent>
99.10 <groupId>org.apidesign.bck2brwsr</groupId>
99.11 - <artifactId>mojo</artifactId>
99.12 - <version>0.6-SNAPSHOT</version>
99.13 + <artifactId>bck2brwsr-maven-plugin</artifactId>
99.14 + <version>0.8-SNAPSHOT</version>
99.15 <packaging>maven-plugin</packaging>
99.16 - <name>Bck2Brwsr Maven Plugins</name>
99.17 - <url>http://maven.apache.org</url>
99.18 + <name>Bck2Brwsr Maven Plugin</name>
99.19 + <url>http://bck2brwsr.apidesign.org/</url>
99.20 <build>
99.21 <plugins>
99.22 <plugin>
99.23 @@ -81,5 +81,15 @@
99.24 <artifactId>launcher</artifactId>
99.25 <version>${project.version}</version>
99.26 </dependency>
99.27 + <dependency>
99.28 + <groupId>${project.groupId}</groupId>
99.29 + <artifactId>launcher.http</artifactId>
99.30 + <version>${project.version}</version>
99.31 + </dependency>
99.32 + <dependency>
99.33 + <groupId>${project.groupId}</groupId>
99.34 + <artifactId>launcher.fx</artifactId>
99.35 + <version>${project.version}</version>
99.36 + </dependency>
99.37 </dependencies>
99.38 </project>
100.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrswrMojo.java Fri May 24 16:45:32 2013 +0200
100.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
100.3 @@ -1,99 +0,0 @@
100.4 -/**
100.5 - * Back 2 Browser Bytecode Translator
100.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
100.7 - *
100.8 - * This program is free software: you can redistribute it and/or modify
100.9 - * it under the terms of the GNU General Public License as published by
100.10 - * the Free Software Foundation, version 2 of the License.
100.11 - *
100.12 - * This program is distributed in the hope that it will be useful,
100.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
100.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
100.15 - * GNU General Public License for more details.
100.16 - *
100.17 - * You should have received a copy of the GNU General Public License
100.18 - * along with this program. Look for COPYING file in the top folder.
100.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
100.20 - */
100.21 -package org.apidesign.bck2brwsr.mojo;
100.22 -
100.23 -import java.io.Closeable;
100.24 -import org.apache.maven.plugin.AbstractMojo;
100.25 -
100.26 -import java.io.File;
100.27 -import java.io.IOException;
100.28 -import java.net.MalformedURLException;
100.29 -import java.net.URL;
100.30 -import java.net.URLClassLoader;
100.31 -import java.util.ArrayList;
100.32 -import java.util.Collection;
100.33 -import java.util.List;
100.34 -import org.apache.maven.artifact.Artifact;
100.35 -import org.apache.maven.plugin.MojoExecutionException;
100.36 -import org.apache.maven.plugins.annotations.LifecyclePhase;
100.37 -import org.apache.maven.plugins.annotations.Mojo;
100.38 -import org.apache.maven.plugins.annotations.Parameter;
100.39 -import org.apache.maven.project.MavenProject;
100.40 -import org.apidesign.bck2brwsr.launcher.Launcher;
100.41 -
100.42 -/** Executes given HTML page in a browser. */
100.43 -@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
100.44 -public class BrswrMojo extends AbstractMojo {
100.45 - public BrswrMojo() {
100.46 - }
100.47 - /** Resource to show as initial page */
100.48 - @Parameter
100.49 - private String startpage;
100.50 -
100.51 - @Parameter(defaultValue="${project}")
100.52 - private MavenProject prj;
100.53 -
100.54 - /** Root of the class files */
100.55 - @Parameter(defaultValue="${project.build.directory}/classes")
100.56 - private File classes;
100.57 -
100.58 - /** Root of all pages, and files, etc. */
100.59 - @Parameter
100.60 - private File directory;
100.61 -
100.62 - @Override
100.63 - public void execute() throws MojoExecutionException {
100.64 - if (startpage == null) {
100.65 - throw new MojoExecutionException("You have to provide a start page");
100.66 - }
100.67 -
100.68 - try {
100.69 - Closeable httpServer;
100.70 - if (directory != null) {
100.71 - httpServer = Launcher.showDir(directory, startpage);
100.72 - } else {
100.73 - URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
100.74 - try {
100.75 - httpServer = Launcher.showURL(url, startpage());
100.76 - } catch (Exception ex) {
100.77 - throw new MojoExecutionException("Can't open " + startpage(), ex);
100.78 - }
100.79 - }
100.80 - System.in.read();
100.81 - httpServer.close();
100.82 - } catch (IOException ex) {
100.83 - throw new MojoExecutionException("Can't show the browser", ex);
100.84 - }
100.85 - }
100.86 -
100.87 - private String startpage() {
100.88 - return startpage;
100.89 - }
100.90 -
100.91 - private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
100.92 - List<URL> arr = new ArrayList<URL>();
100.93 - arr.add(root.toURI().toURL());
100.94 - for (Artifact a : deps) {
100.95 - final File f = a.getFile();
100.96 - if (f != null) {
100.97 - arr.add(f.toURI().toURL());
100.98 - }
100.99 - }
100.100 - return new URLClassLoader(arr.toArray(new URL[0]), BrswrMojo.class.getClassLoader());
100.101 - }
100.102 -}
101.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
101.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java Fri May 24 18:04:55 2013 +0200
101.3 @@ -0,0 +1,108 @@
101.4 +/**
101.5 + * Back 2 Browser Bytecode Translator
101.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
101.7 + *
101.8 + * This program is free software: you can redistribute it and/or modify
101.9 + * it under the terms of the GNU General Public License as published by
101.10 + * the Free Software Foundation, version 2 of the License.
101.11 + *
101.12 + * This program is distributed in the hope that it will be useful,
101.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
101.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101.15 + * GNU General Public License for more details.
101.16 + *
101.17 + * You should have received a copy of the GNU General Public License
101.18 + * along with this program. Look for COPYING file in the top folder.
101.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
101.20 + */
101.21 +package org.apidesign.bck2brwsr.mojo;
101.22 +
101.23 +import java.io.Closeable;
101.24 +import org.apache.maven.plugin.AbstractMojo;
101.25 +
101.26 +import java.io.File;
101.27 +import java.io.IOException;
101.28 +import java.net.MalformedURLException;
101.29 +import java.net.URL;
101.30 +import java.net.URLClassLoader;
101.31 +import java.util.ArrayList;
101.32 +import java.util.Collection;
101.33 +import java.util.List;
101.34 +import org.apache.maven.artifact.Artifact;
101.35 +import org.apache.maven.plugin.MojoExecutionException;
101.36 +import org.apache.maven.plugins.annotations.LifecyclePhase;
101.37 +import org.apache.maven.plugins.annotations.Mojo;
101.38 +import org.apache.maven.plugins.annotations.Parameter;
101.39 +import org.apache.maven.project.MavenProject;
101.40 +import org.apidesign.bck2brwsr.launcher.Launcher;
101.41 +
101.42 +/** Executes given HTML page in a browser. */
101.43 +@Mojo(name="brwsr", defaultPhase=LifecyclePhase.NONE)
101.44 +public class BrwsrMojo extends AbstractMojo {
101.45 + public BrwsrMojo() {
101.46 + }
101.47 +
101.48 + /** The identification of a launcher to use. Known values <code>fxbrwsr</code>,
101.49 + * <code>bck2brwsr</code>, or
101.50 + * name of an external process to execute.
101.51 + */
101.52 + @Parameter
101.53 + private String launcher;
101.54 +
101.55 +
101.56 + /** Resource to show as initial page */
101.57 + @Parameter
101.58 + private String startpage;
101.59 +
101.60 + @Parameter(defaultValue="${project}")
101.61 + private MavenProject prj;
101.62 +
101.63 + /** Root of the class files */
101.64 + @Parameter(defaultValue="${project.build.directory}/classes")
101.65 + private File classes;
101.66 +
101.67 + /** Root of all pages, and files, etc. */
101.68 + @Parameter
101.69 + private File directory;
101.70 +
101.71 + @Override
101.72 + public void execute() throws MojoExecutionException {
101.73 + if (startpage == null) {
101.74 + throw new MojoExecutionException("You have to provide a start page");
101.75 + }
101.76 +
101.77 + try {
101.78 + Closeable httpServer;
101.79 + if (directory != null) {
101.80 + httpServer = Launcher.showDir(directory, startpage);
101.81 + } else {
101.82 + URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
101.83 + try {
101.84 + httpServer = Launcher.showURL(launcher, url, startpage());
101.85 + } catch (Exception ex) {
101.86 + throw new MojoExecutionException("Can't open " + startpage(), ex);
101.87 + }
101.88 + }
101.89 + System.in.read();
101.90 + httpServer.close();
101.91 + } catch (IOException ex) {
101.92 + throw new MojoExecutionException("Can't show the browser", ex);
101.93 + }
101.94 + }
101.95 +
101.96 + private String startpage() {
101.97 + return startpage;
101.98 + }
101.99 +
101.100 + private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
101.101 + List<URL> arr = new ArrayList<URL>();
101.102 + arr.add(root.toURI().toURL());
101.103 + for (Artifact a : deps) {
101.104 + final File f = a.getFile();
101.105 + if (f != null) {
101.106 + arr.add(f.toURI().toURL());
101.107 + }
101.108 + }
101.109 + return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
101.110 + }
101.111 +}
102.1 --- a/rt/pom.xml Fri May 24 16:45:32 2013 +0200
102.2 +++ b/rt/pom.xml Fri May 24 18:04:55 2013 +0200
102.3 @@ -3,18 +3,17 @@
102.4 <modelVersion>4.0.0</modelVersion>
102.5 <groupId>org.apidesign.bck2brwsr</groupId>
102.6 <artifactId>rt</artifactId>
102.7 - <version>0.6-SNAPSHOT</version>
102.8 + <version>0.8-SNAPSHOT</version>
102.9 <packaging>pom</packaging>
102.10 <name>Bck2Brwsr Runtime</name>
102.11 <parent>
102.12 <groupId>org.apidesign</groupId>
102.13 <artifactId>bck2brwsr</artifactId>
102.14 - <version>0.6-SNAPSHOT</version>
102.15 + <version>0.8-SNAPSHOT</version>
102.16 </parent>
102.17 <modules>
102.18 <module>core</module>
102.19 <module>emul</module>
102.20 - <module>launcher</module>
102.21 <module>archetype</module>
102.22 <module>mojo</module>
102.23 <module>vm</module>
103.1 --- a/rt/vm/pom.xml Fri May 24 16:45:32 2013 +0200
103.2 +++ b/rt/vm/pom.xml Fri May 24 18:04:55 2013 +0200
103.3 @@ -3,12 +3,12 @@
103.4 <parent>
103.5 <groupId>org.apidesign.bck2brwsr</groupId>
103.6 <artifactId>rt</artifactId>
103.7 - <version>0.6-SNAPSHOT</version>
103.8 + <version>0.8-SNAPSHOT</version>
103.9 </parent>
103.10
103.11 <groupId>org.apidesign.bck2brwsr</groupId>
103.12 <artifactId>vm4brwsr</artifactId>
103.13 - <version>0.6-SNAPSHOT</version>
103.14 + <version>0.8-SNAPSHOT</version>
103.15 <packaging>jar</packaging>
103.16
103.17 <name>Virtual Machine for Browser</name>
103.18 @@ -86,6 +86,7 @@
103.19 <configuration>
103.20 <executable>java</executable>
103.21 <arguments>
103.22 + <argument>-Dskip.if.exists=true</argument>
103.23 <argument>-cp</argument>
103.24 <classpath />
103.25 <argument>org.apidesign.vm4brwsr.Main</argument>
104.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri May 24 16:45:32 2013 +0200
104.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java Fri May 24 18:04:55 2013 +0200
104.3 @@ -1814,20 +1814,11 @@
104.4 if (e.catch_cpx != 0) { //not finally
104.5 final String classInternalName = jc.getClassName(e.catch_cpx);
104.6 addReference(classInternalName);
104.7 - if ("java/lang/Throwable".equals(classInternalName)) {
104.8 - out.append("if (e['$instOf_java_lang_Throwable']) {");
104.9 - out.append(" var stA0 = e;");
104.10 - out.append("} else {");
104.11 - out.append(" var stA0 = vm.java_lang_Throwable(true);");
104.12 - out.append(" vm.java_lang_Throwable['cons__VLjava_lang_String_2'].call(stA0, e.toString());");
104.13 - out.append("}");
104.14 - goTo(out, current, e.handler_pc, topMostLabel);
104.15 - } else {
104.16 - out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
104.17 - out.append("var stA0 = e;");
104.18 - goTo(out, current, e.handler_pc, topMostLabel);
104.19 - out.append("}\n");
104.20 - }
104.21 + out.append("e = vm.java_lang_Throwable(false).bck2BrwsrCnvrt(e);");
104.22 + out.append("if (e['$instOf_" + classInternalName.replace('/', '_') + "']) {");
104.23 + out.append("var stA0 = e;");
104.24 + goTo(out, current, e.handler_pc, topMostLabel);
104.25 + out.append("}\n");
104.26 } else {
104.27 finallyPC = e.handler_pc;
104.28 }
105.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Fri May 24 16:45:32 2013 +0200
105.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Main.java Fri May 24 18:04:55 2013 +0200
105.3 @@ -97,7 +97,14 @@
105.4 collectClasses(classes, mainClassLoader, args[i]);
105.5 }
105.6 }
105.7 - try (Writer w = new BufferedWriter(new FileWriter(generateTo))) {
105.8 +
105.9 + File gt = new File(generateTo);
105.10 + if (Boolean.getBoolean("skip.if.exists") && gt.isFile()) {
105.11 + System.err.println("Skipping as " + gt + " exists.");
105.12 + System.exit(0);
105.13 + }
105.14 +
105.15 + try (Writer w = new BufferedWriter(new FileWriter(gt))) {
105.16 Bck2Brwsr.newCompiler().
105.17 extension(createExtension).
105.18 obfuscation(obfLevel).
106.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Fri May 24 16:45:32 2013 +0200
106.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/NumberTest.java Fri May 24 18:04:55 2013 +0200
106.3 @@ -194,14 +194,14 @@
106.4
106.5 @Test public void valueOfLongBooleanTrue() throws Exception {
106.6 assertExec("Can we call JavaScripts valueOf on Boolean?",
106.7 - Numbers.class, "seven__DI",
106.8 - Double.valueOf(1), 31
106.9 + Numbers.class, "bseven__ZI",
106.10 + true, 31
106.11 );
106.12 }
106.13 @Test public void valueOfLongBooleanFalse() throws Exception {
106.14 assertExec("Can we call JavaScripts valueOf on Boolean?",
106.15 - Numbers.class, "seven__DI",
106.16 - Double.valueOf(0), 30
106.17 + Numbers.class, "bseven__ZI",
106.18 + false, 30
106.19 );
106.20 }
106.21
107.1 --- a/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Fri May 24 16:45:32 2013 +0200
107.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/Numbers.java Fri May 24 18:04:55 2013 +0200
107.3 @@ -84,10 +84,20 @@
107.4 default: throw new IllegalStateException();
107.5 }
107.6 }
107.7 + static boolean bseven(int todo) {
107.8 + switch (todo) {
107.9 + case 30: return bvalueOf(Boolean.FALSE);
107.10 + case 31: return bvalueOf(Boolean.TRUE);
107.11 + default: throw new IllegalStateException();
107.12 + }
107.13 + }
107.14
107.15 @JavaScriptBody(args = {}, body = "return 7;")
107.16 private static native Number sevenNew();
107.17 +
107.18 + @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
107.19 + private static native double valueOf(Object o);
107.20
107.21 @JavaScriptBody(args = { "o" }, body = "return o.valueOf();")
107.22 - private static native double valueOf(Object o);
107.23 + private static native boolean bvalueOf(Object o);
107.24 }
108.1 --- a/rt/vmtest/pom.xml Fri May 24 16:45:32 2013 +0200
108.2 +++ b/rt/vmtest/pom.xml Fri May 24 18:04:55 2013 +0200
108.3 @@ -4,11 +4,11 @@
108.4 <parent>
108.5 <groupId>org.apidesign.bck2brwsr</groupId>
108.6 <artifactId>rt</artifactId>
108.7 - <version>0.6-SNAPSHOT</version>
108.8 + <version>0.8-SNAPSHOT</version>
108.9 </parent>
108.10 <groupId>org.apidesign.bck2brwsr</groupId>
108.11 <artifactId>vmtest</artifactId>
108.12 - <version>0.6-SNAPSHOT</version>
108.13 + <version>0.8-SNAPSHOT</version>
108.14
108.15 <name>VM Testing APIs</name>
108.16 <url>http://bck2brwsr.apidesign.org</url>
108.17 @@ -50,18 +50,6 @@
108.18 </dependency>
108.19 <dependency>
108.20 <groupId>${project.groupId}</groupId>
108.21 - <artifactId>vm4brwsr</artifactId>
108.22 - <version>${project.version}</version>
108.23 - <type>jar</type>
108.24 - </dependency>
108.25 - <dependency>
108.26 - <groupId>${project.groupId}</groupId>
108.27 - <artifactId>emul.mini</artifactId>
108.28 - <version>${project.version}</version>
108.29 - <scope>test</scope>
108.30 - </dependency>
108.31 - <dependency>
108.32 - <groupId>${project.groupId}</groupId>
108.33 <artifactId>launcher</artifactId>
108.34 <version>${project.version}</version>
108.35 </dependency>
109.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Fri May 24 16:45:32 2013 +0200
109.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Fri May 24 18:04:55 2013 +0200
109.3 @@ -17,6 +17,9 @@
109.4 */
109.5 package org.apidesign.bck2brwsr.vmtest;
109.6
109.7 +import java.util.ArrayList;
109.8 +import java.util.Arrays;
109.9 +import java.util.List;
109.10 import org.apidesign.bck2brwsr.launcher.Launcher;
109.11 import org.apidesign.bck2brwsr.vmtest.impl.CompareCase;
109.12 import org.testng.annotations.Factory;
109.13 @@ -34,6 +37,9 @@
109.14 * @author Jaroslav Tulach <jtulach@netbeans.org>
109.15 */
109.16 public final class VMTest {
109.17 + private final List<Class> classes = new ArrayList<>();
109.18 + private final List<String> launcher = new ArrayList<>();
109.19 +
109.20 private VMTest() {
109.21 }
109.22
109.23 @@ -47,10 +53,66 @@
109.24 * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
109.25 * browser}.
109.26 *
109.27 - * @param clazz the class to inspect
109.28 + * @param clazz the class (or classes) to inspect
109.29 * @return the set of created tests
109.30 */
109.31 - public static Object[] create(Class<?> clazz) {
109.32 - return CompareCase.create(clazz);
109.33 + public static Object[] create(Class clazz) {
109.34 + return newTests().withClasses(clazz).build();
109.35 + }
109.36 +
109.37 + /** Creates new builder for test execution. Continue with methods
109.38 + * like {@link #withClasses(java.lang.Class[])} or {@link #withLaunchers(java.lang.String[])}.
109.39 + * Finish the process by calling {@link #build()}.
109.40 + *
109.41 + * @return new instance of a builder
109.42 + * @since 0.7
109.43 + */
109.44 + public static VMTest newTests() {
109.45 + return new VMTest();
109.46 + }
109.47 +
109.48 + /** Adds class (or classes) to the test execution. The classes are inspected
109.49 + * to contain methods annotated by
109.50 + * {@link Compare} or {@link BrwsrTest}. Appropriate set of TestNG test
109.51 + * cases is then created.
109.52 + * <p>
109.53 + * Each {@link Compare} instance runs the test in different virtual
109.54 + * machine and at the end they compare the results.
109.55 + * <p>
109.56 + * Each {@link BrwsrTest} annotated method is executed once in {@link Launcher started
109.57 + * browser}.
109.58 + *
109.59 + * @param classes one or more classes to inspect
109.60 + * @since 0.7
109.61 + */
109.62 + public final VMTest withClasses(Class... classes) {
109.63 + this.classes.addAll(Arrays.asList(classes));
109.64 + return this;
109.65 + }
109.66 +
109.67 + /** Adds list of launchers that should be used to execute tests defined
109.68 + * by {@link Compare} and {@link BrwsrTest} annotations. This value
109.69 + * can be overrided by using <code>vmtest.brwsrs</code> property.
109.70 + * List of supported launchers is available in the documentation of
109.71 + * {@link Launcher}.
109.72 + *
109.73 + * @param launcher names of one or more launchers to use for the execution
109.74 + * of tests
109.75 + * @since 0.7
109.76 + */
109.77 + public final VMTest withLaunchers(String... launcher) {
109.78 + this.launcher.addAll(Arrays.asList(launcher));
109.79 + return this;
109.80 + }
109.81 +
109.82 + /** Assembles the provided information into the final array of tests.
109.83 + * @return array of TestNG tests
109.84 + * @since 0.7
109.85 + */
109.86 + public final Object[] build() {
109.87 + return CompareCase.create(
109.88 + launcher.toArray(new String[0]),
109.89 + classes.toArray(new Class[0])
109.90 + );
109.91 }
109.92 }
110.1 --- a/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Fri May 24 16:45:32 2013 +0200
110.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Fri May 24 18:04:55 2013 +0200
110.3 @@ -53,26 +53,25 @@
110.4 * @param clazz the class to inspect
110.5 * @return the set of created tests
110.6 */
110.7 - public static Object[] create(Class<?> clazz) {
110.8 - Method[] arr = clazz.getMethods();
110.9 + public static Object[] create(String[] brwsr, Class[] classes) {
110.10 List<Object> ret = new ArrayList<>();
110.11
110.12 final LaunchSetup l = LaunchSetup.INSTANCE;
110.13 ret.add(l);
110.14
110.15 - String[] brwsr;
110.16 {
110.17 String p = System.getProperty("vmtest.brwsrs");
110.18 if (p != null) {
110.19 brwsr = p.split(",");
110.20 - } else {
110.21 - brwsr = new String[0];
110.22 }
110.23 }
110.24
110.25 - for (Method m : arr) {
110.26 - registerCompareCases(m, l, ret, brwsr);
110.27 - registerBrwsrCases(m, l, ret, brwsr);
110.28 + for (Class clazz : classes) {
110.29 + Method[] arr = clazz.getMethods();
110.30 + for (Method m : arr) {
110.31 + registerCompareCases(m, l, ret, brwsr);
110.32 + registerBrwsrCases(m, l, ret, brwsr);
110.33 + }
110.34 }
110.35 return ret.toArray();
110.36 }
111.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/AssertionTest.java Fri May 24 16:45:32 2013 +0200
111.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
111.3 @@ -1,43 +0,0 @@
111.4 -/**
111.5 - * Back 2 Browser Bytecode Translator
111.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
111.7 - *
111.8 - * This program is free software: you can redistribute it and/or modify
111.9 - * it under the terms of the GNU General Public License as published by
111.10 - * the Free Software Foundation, version 2 of the License.
111.11 - *
111.12 - * This program is distributed in the hope that it will be useful,
111.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
111.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
111.15 - * GNU General Public License for more details.
111.16 - *
111.17 - * You should have received a copy of the GNU General Public License
111.18 - * along with this program. Look for COPYING file in the top folder.
111.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
111.20 - */
111.21 -package org.apidesign.bck2brwsr.tck;
111.22 -
111.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
111.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
111.25 -import org.testng.annotations.Factory;
111.26 -
111.27 -/**
111.28 - *
111.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
111.30 - */
111.31 -public class AssertionTest {
111.32 -
111.33 - @Compare public Object checkAssert() throws ClassNotFoundException {
111.34 - try {
111.35 - assert false : "Is assertion status on?";
111.36 - return null;
111.37 - } catch (AssertionError ex) {
111.38 - return ex.getClass().getName();
111.39 - }
111.40 - }
111.41 -
111.42 - @Factory
111.43 - public static Object[] create() {
111.44 - return VMTest.create(AssertionTest.class);
111.45 - }
111.46 -}
112.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/BrwsrCheckTest.java Fri May 24 16:45:32 2013 +0200
112.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
112.3 @@ -1,57 +0,0 @@
112.4 -/**
112.5 - * Back 2 Browser Bytecode Translator
112.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
112.7 - *
112.8 - * This program is free software: you can redistribute it and/or modify
112.9 - * it under the terms of the GNU General Public License as published by
112.10 - * the Free Software Foundation, version 2 of the License.
112.11 - *
112.12 - * This program is distributed in the hope that it will be useful,
112.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
112.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
112.15 - * GNU General Public License for more details.
112.16 - *
112.17 - * You should have received a copy of the GNU General Public License
112.18 - * along with this program. Look for COPYING file in the top folder.
112.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
112.20 - */
112.21 -package org.apidesign.bck2brwsr.tck;
112.22 -
112.23 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
112.24 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
112.25 -import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
112.26 -import org.apidesign.bck2brwsr.vmtest.VMTest;
112.27 -import org.testng.annotations.Factory;
112.28 -
112.29 -/**
112.30 - *
112.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
112.32 - */
112.33 -public class BrwsrCheckTest {
112.34 -
112.35 - @BrwsrTest public void assertWindowObjectIsDefined() {
112.36 - assert window() != null : "No window object found!";
112.37 - }
112.38 -
112.39 -
112.40 -
112.41 -
112.42 - @HtmlFragment("<h1 id='hello'>\n"
112.43 - + "Hello!\n"
112.44 - + "</h1>\n")
112.45 - @BrwsrTest public void accessProvidedFragment() {
112.46 - assert getElementById("hello") != null : "Element with 'hello' ID found";
112.47 - }
112.48 -
112.49 - @Factory
112.50 - public static Object[] create() {
112.51 - return VMTest.create(BrwsrCheckTest.class);
112.52 - }
112.53 -
112.54 -
112.55 - @JavaScriptBody(args = {}, body = "return window;")
112.56 - private static native Object window();
112.57 -
112.58 - @JavaScriptBody(args = { "id" }, body = "return window.document.getElementById(id);")
112.59 - private static native Object getElementById(String id);
112.60 -}
113.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ByteArithmeticTest.java Fri May 24 16:45:32 2013 +0200
113.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
113.3 @@ -1,147 +0,0 @@
113.4 -/**
113.5 - * Back 2 Browser Bytecode Translator
113.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
113.7 - *
113.8 - * This program is free software: you can redistribute it and/or modify
113.9 - * it under the terms of the GNU General Public License as published by
113.10 - * the Free Software Foundation, version 2 of the License.
113.11 - *
113.12 - * This program is distributed in the hope that it will be useful,
113.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
113.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
113.15 - * GNU General Public License for more details.
113.16 - *
113.17 - * You should have received a copy of the GNU General Public License
113.18 - * along with this program. Look for COPYING file in the top folder.
113.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
113.20 - */
113.21 -package org.apidesign.bck2brwsr.tck;
113.22 -
113.23 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
113.24 -import org.apidesign.bck2brwsr.vmtest.Compare;
113.25 -import org.apidesign.bck2brwsr.vmtest.VMTest;
113.26 -import org.testng.annotations.Factory;
113.27 -
113.28 -/**
113.29 - *
113.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
113.31 - */
113.32 -public class ByteArithmeticTest {
113.33 -
113.34 - private static byte add(byte x, byte y) {
113.35 - return (byte)(x + y);
113.36 - }
113.37 -
113.38 - private static byte sub(byte x, byte y) {
113.39 - return (byte)(x - y);
113.40 - }
113.41 -
113.42 - private static byte mul(byte x, byte y) {
113.43 - return (byte)(x * y);
113.44 - }
113.45 -
113.46 - private static byte div(byte x, byte y) {
113.47 - return (byte)(x / y);
113.48 - }
113.49 -
113.50 - private static byte mod(byte x, byte y) {
113.51 - return (byte)(x % y);
113.52 - }
113.53 -
113.54 - @Compare public byte conversion() {
113.55 - return (byte)123456;
113.56 - }
113.57 -
113.58 - @Compare public byte addOverflow() {
113.59 - return add(Byte.MAX_VALUE, (byte)1);
113.60 - }
113.61 -
113.62 - @Compare public byte subUnderflow() {
113.63 - return sub(Byte.MIN_VALUE, (byte)1);
113.64 - }
113.65 -
113.66 - @Compare public byte addMaxByteAndMaxByte() {
113.67 - return add(Byte.MAX_VALUE, Byte.MAX_VALUE);
113.68 - }
113.69 -
113.70 - @Compare public byte subMinByteAndMinByte() {
113.71 - return sub(Byte.MIN_VALUE, Byte.MIN_VALUE);
113.72 - }
113.73 -
113.74 - @Compare public byte multiplyMaxByte() {
113.75 - return mul(Byte.MAX_VALUE, (byte)2);
113.76 - }
113.77 -
113.78 - @Compare public byte multiplyMaxByteAndMaxByte() {
113.79 - return mul(Byte.MAX_VALUE, Byte.MAX_VALUE);
113.80 - }
113.81 -
113.82 - @Compare public byte multiplyMinByte() {
113.83 - return mul(Byte.MIN_VALUE, (byte)2);
113.84 - }
113.85 -
113.86 - @Compare public byte multiplyMinByteAndMinByte() {
113.87 - return mul(Byte.MIN_VALUE, Byte.MIN_VALUE);
113.88 - }
113.89 -
113.90 - @Compare public byte multiplyPrecision() {
113.91 - return mul((byte)17638, (byte)1103);
113.92 - }
113.93 -
113.94 - @Compare public byte division() {
113.95 - return div((byte)1, (byte)2);
113.96 - }
113.97 -
113.98 - @Compare public byte divisionReminder() {
113.99 - return mod((byte)1, (byte)2);
113.100 - }
113.101 -
113.102 - private static int readShort(byte[] byteCodes, int offset) {
113.103 - int signed = byteCodes[offset];
113.104 - byte b0 = (byte)signed;
113.105 - return (b0 << 8) | (byteCodes[offset + 1] & 0xff);
113.106 - }
113.107 -
113.108 - private static int readShortArg(byte[] byteCodes, int offsetInstruction) {
113.109 - return readShort(byteCodes, offsetInstruction + 1);
113.110 - }
113.111 -
113.112 - @Compare public int readIntArgs255and156() {
113.113 - final byte[] arr = new byte[] { (byte)0, (byte)255, (byte)156 };
113.114 -
113.115 - assert arr[1] == -1 : "First byte: " + arr[1];
113.116 - assert arr[2] == -100 : "Second byte: " + arr[2];
113.117 - final int ret = readShortArg(arr, 0);
113.118 - assert ret < 65000: "Value: " + ret;
113.119 - return ret;
113.120 - }
113.121 -
113.122 - @JavaScriptBody(args = { "arr" }, body = "arr[1] = 255; arr[2] = 156; return arr;")
113.123 - private static byte[] fill255and156(byte[] arr) {
113.124 - arr[1] = (byte)255;
113.125 - arr[2] = (byte)156;
113.126 - return arr;
113.127 - }
113.128 -
113.129 - @Compare public int readIntArgs255and156JSArray() {
113.130 - final byte[] arr = fill255and156(new byte[] { 0, 0, 0 });
113.131 -
113.132 - final int ret = readShortArg(arr, 0);
113.133 - assert ret < 65000: "Value: " + ret;
113.134 - return ret;
113.135 - }
113.136 -
113.137 - @Compare public int readIntArgsMinus1andMinus100() {
113.138 - final byte[] arr = new byte[] { (byte)0, (byte)-1, (byte)-100 };
113.139 -
113.140 - assert arr[1] == -1 : "First byte: " + arr[1];
113.141 - assert arr[2] == -100 : "Second byte: " + arr[2];
113.142 -
113.143 - return readShortArg(arr, 0);
113.144 - }
113.145 -
113.146 - @Factory
113.147 - public static Object[] create() {
113.148 - return VMTest.create(ByteArithmeticTest.class);
113.149 - }
113.150 -}
114.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CloneTest.java Fri May 24 16:45:32 2013 +0200
114.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
114.3 @@ -1,73 +0,0 @@
114.4 -/**
114.5 - * Back 2 Browser Bytecode Translator
114.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
114.7 - *
114.8 - * This program is free software: you can redistribute it and/or modify
114.9 - * it under the terms of the GNU General Public License as published by
114.10 - * the Free Software Foundation, version 2 of the License.
114.11 - *
114.12 - * This program is distributed in the hope that it will be useful,
114.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
114.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
114.15 - * GNU General Public License for more details.
114.16 - *
114.17 - * You should have received a copy of the GNU General Public License
114.18 - * along with this program. Look for COPYING file in the top folder.
114.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
114.20 - */
114.21 -package org.apidesign.bck2brwsr.tck;
114.22 -
114.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
114.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
114.25 -import org.testng.annotations.Factory;
114.26 -
114.27 -/**
114.28 - *
114.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
114.30 - */
114.31 -public class CloneTest {
114.32 - private int value;
114.33 -
114.34 - @Compare
114.35 - public Object notSupported() throws CloneNotSupportedException {
114.36 - return this.clone();
114.37 - }
114.38 -
114.39 - @Compare public String sameClass() throws CloneNotSupportedException {
114.40 - return new Clnbl().clone().getClass().getName();
114.41 - }
114.42 -
114.43 - @Compare public boolean differentInstance() throws CloneNotSupportedException {
114.44 - Clnbl orig = new Clnbl();
114.45 - return orig == orig.clone();
114.46 - }
114.47 -
114.48 - @Compare public int sameReference() throws CloneNotSupportedException {
114.49 - CloneTest self = this;
114.50 - Clnbl orig = new Clnbl();
114.51 - self.value = 33;
114.52 - orig.ref = self;
114.53 - return ((Clnbl)orig.clone()).ref.value;
114.54 - }
114.55 -
114.56 - @Compare public int sameValue() throws CloneNotSupportedException {
114.57 - Clnbl orig = new Clnbl();
114.58 - orig.value = 10;
114.59 - return ((Clnbl)orig.clone()).value;
114.60 - }
114.61 -
114.62 - @Factory
114.63 - public static Object[] create() {
114.64 - return VMTest.create(CloneTest.class);
114.65 - }
114.66 -
114.67 - public static final class Clnbl implements Cloneable {
114.68 - public CloneTest ref;
114.69 - private int value;
114.70 -
114.71 - @Override
114.72 - public Object clone() throws CloneNotSupportedException {
114.73 - return super.clone();
114.74 - }
114.75 - }
114.76 -}
115.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareByteArrayTest.java Fri May 24 16:45:32 2013 +0200
115.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
115.3 @@ -1,103 +0,0 @@
115.4 -/**
115.5 - * Back 2 Browser Bytecode Translator
115.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
115.7 - *
115.8 - * This program is free software: you can redistribute it and/or modify
115.9 - * it under the terms of the GNU General Public License as published by
115.10 - * the Free Software Foundation, version 2 of the License.
115.11 - *
115.12 - * This program is distributed in the hope that it will be useful,
115.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
115.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
115.15 - * GNU General Public License for more details.
115.16 - *
115.17 - * You should have received a copy of the GNU General Public License
115.18 - * along with this program. Look for COPYING file in the top folder.
115.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
115.20 - */
115.21 -package org.apidesign.bck2brwsr.tck;
115.22 -
115.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
115.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
115.25 -import org.testng.annotations.Factory;
115.26 -
115.27 -/**
115.28 - *
115.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
115.30 - */
115.31 -public class CompareByteArrayTest {
115.32 - @Compare public int byteArraySum() {
115.33 - byte[] arr = createArray();
115.34 - return sumByteArr(arr);
115.35 - }
115.36 -
115.37 - @Compare public int countZeros() {
115.38 - int zeros = 0;
115.39 - for (Byte b : createArray()) {
115.40 - if (b == 0) {
115.41 - zeros++;
115.42 - }
115.43 - }
115.44 - return zeros;
115.45 - }
115.46 -
115.47 - private static int sumByteArr(byte[] arr) {
115.48 - int sum = 0;
115.49 - for (int i = 0; i < arr.length; i++) {
115.50 - sum += arr[i];
115.51 - }
115.52 - return sum;
115.53 - }
115.54 -
115.55 - @Compare public String noOutOfBounds() {
115.56 - return atIndex(1);
115.57 - }
115.58 -
115.59 - @Compare public String outOfBounds() {
115.60 - return atIndex(5);
115.61 - }
115.62 -
115.63 - @Compare public String outOfBoundsMinus() {
115.64 - return atIndex(-1);
115.65 - }
115.66 -
115.67 - @Compare public String toOfBounds() {
115.68 - return toIndex(5);
115.69 - }
115.70 -
115.71 - @Compare public String toOfBoundsMinus() {
115.72 - return toIndex(-1);
115.73 - }
115.74 -
115.75 - @Compare public int multiArrayLength() {
115.76 - int[][] arr = new int[1][0];
115.77 - return arr[0].length;
115.78 - }
115.79 -
115.80 - @Compare public int multiObjectArrayLength() {
115.81 - Object[][] arr = new Object[1][0];
115.82 - return arr[0].length;
115.83 - }
115.84 -
115.85 - private static final int[] arr = { 0, 1, 2 };
115.86 - public static String atIndex(int at) {
115.87 - return "at@" + arr[at];
115.88 - }
115.89 - public static String toIndex(int at) {
115.90 - arr[at] = 10;
115.91 - return "ok";
115.92 - }
115.93 -
115.94 -
115.95 - @Factory
115.96 - public static Object[] create() {
115.97 - return VMTest.create(CompareByteArrayTest.class);
115.98 - }
115.99 -
115.100 - private byte[] createArray() {
115.101 - byte[] arr = new byte[10];
115.102 - arr[5] = 3;
115.103 - arr[7] = 8;
115.104 - return arr;
115.105 - }
115.106 -}
116.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareHashTest.java Fri May 24 16:45:32 2013 +0200
116.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
116.3 @@ -1,50 +0,0 @@
116.4 -/**
116.5 - * Back 2 Browser Bytecode Translator
116.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
116.7 - *
116.8 - * This program is free software: you can redistribute it and/or modify
116.9 - * it under the terms of the GNU General Public License as published by
116.10 - * the Free Software Foundation, version 2 of the License.
116.11 - *
116.12 - * This program is distributed in the hope that it will be useful,
116.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
116.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
116.15 - * GNU General Public License for more details.
116.16 - *
116.17 - * You should have received a copy of the GNU General Public License
116.18 - * along with this program. Look for COPYING file in the top folder.
116.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
116.20 - */
116.21 -package org.apidesign.bck2brwsr.tck;
116.22 -
116.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
116.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
116.25 -import org.testng.annotations.Factory;
116.26 -
116.27 -/**
116.28 - *
116.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
116.30 - */
116.31 -public class CompareHashTest {
116.32 - @Compare public int hashOfString() {
116.33 - return "Ahoj".hashCode();
116.34 - }
116.35 -
116.36 - @Compare public int hashRemainsYieldsZero() {
116.37 - Object o = new Object();
116.38 - return o.hashCode() - o.hashCode();
116.39 - }
116.40 -
116.41 - @Compare public int initializeInStatic() {
116.42 - return StaticUse.NON_NULL.hashCode() - StaticUse.NON_NULL.hashCode();
116.43 - }
116.44 -
116.45 - @Compare public int hashOfInt() {
116.46 - return Integer.valueOf(Integer.MAX_VALUE).hashCode();
116.47 - }
116.48 -
116.49 - @Factory
116.50 - public static Object[] create() {
116.51 - return VMTest.create(CompareHashTest.class);
116.52 - }
116.53 -}
117.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareIntArrayTest.java Fri May 24 16:45:32 2013 +0200
117.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
117.3 @@ -1,63 +0,0 @@
117.4 -/**
117.5 - * Back 2 Browser Bytecode Translator
117.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
117.7 - *
117.8 - * This program is free software: you can redistribute it and/or modify
117.9 - * it under the terms of the GNU General Public License as published by
117.10 - * the Free Software Foundation, version 2 of the License.
117.11 - *
117.12 - * This program is distributed in the hope that it will be useful,
117.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
117.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
117.15 - * GNU General Public License for more details.
117.16 - *
117.17 - * You should have received a copy of the GNU General Public License
117.18 - * along with this program. Look for COPYING file in the top folder.
117.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
117.20 - */
117.21 -package org.apidesign.bck2brwsr.tck;
117.22 -
117.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
117.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
117.25 -import org.testng.annotations.Factory;
117.26 -
117.27 -/**
117.28 - *
117.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
117.30 - */
117.31 -public class CompareIntArrayTest {
117.32 - @Compare public int integerArraySum() {
117.33 - int[] arr = createArray();
117.34 - return sumIntArr(arr);
117.35 - }
117.36 -
117.37 - @Compare public int countZeros() {
117.38 - int zeros = 0;
117.39 - for (Integer i : createArray()) {
117.40 - if (i == 0) {
117.41 - zeros++;
117.42 - }
117.43 - }
117.44 - return zeros;
117.45 - }
117.46 -
117.47 - private static int sumIntArr(int[] arr) {
117.48 - int sum = 0;
117.49 - for (int i = 0; i < arr.length; i++) {
117.50 - sum += arr[i];
117.51 - }
117.52 - return sum;
117.53 - }
117.54 -
117.55 - @Factory
117.56 - public static Object[] create() {
117.57 - return VMTest.create(CompareIntArrayTest.class);
117.58 - }
117.59 -
117.60 - private int[] createArray() {
117.61 - int[] arr = new int[10];
117.62 - arr[5] = 3;
117.63 - arr[7] = 8;
117.64 - return arr;
117.65 - }
117.66 -}
118.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/CompareStringsTest.java Fri May 24 16:45:32 2013 +0200
118.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
118.3 @@ -1,174 +0,0 @@
118.4 -/**
118.5 - * Back 2 Browser Bytecode Translator
118.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
118.7 - *
118.8 - * This program is free software: you can redistribute it and/or modify
118.9 - * it under the terms of the GNU General Public License as published by
118.10 - * the Free Software Foundation, version 2 of the License.
118.11 - *
118.12 - * This program is distributed in the hope that it will be useful,
118.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
118.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
118.15 - * GNU General Public License for more details.
118.16 - *
118.17 - * You should have received a copy of the GNU General Public License
118.18 - * along with this program. Look for COPYING file in the top folder.
118.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
118.20 - */
118.21 -package org.apidesign.bck2brwsr.tck;
118.22 -
118.23 -import java.io.UnsupportedEncodingException;
118.24 -import java.net.MalformedURLException;
118.25 -import java.net.URL;
118.26 -import org.apidesign.bck2brwsr.vmtest.Compare;
118.27 -import org.apidesign.bck2brwsr.vmtest.VMTest;
118.28 -import org.testng.annotations.Factory;
118.29 -
118.30 -/**
118.31 - *
118.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
118.33 - */
118.34 -public class CompareStringsTest {
118.35 - @Compare public String firstChar() {
118.36 - return "" + ("Hello".toCharArray()[0]);
118.37 - }
118.38 -
118.39 - @Compare public String classCast() {
118.40 - Object o = firstChar();
118.41 - return String.class.cast(o);
118.42 - }
118.43 -
118.44 - @Compare public String classCastThrown() {
118.45 - Object o = null;
118.46 - return String.class.cast(o);
118.47 - }
118.48 -
118.49 - @Compare public boolean equalToNull() {
118.50 - return "Ahoj".equals(null);
118.51 - }
118.52 -
118.53 - @Compare public int highByteLenght() {
118.54 - byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
118.55 - return new String(arr, 0).length();
118.56 - }
118.57 -
118.58 - @Compare public String highByte() {
118.59 - byte[] arr= { 77,97,110,105,102,101,115,116,45,86,101,114,115,105,111,110 };
118.60 - StringBuilder sb = new StringBuilder();
118.61 - sb.append("pref:");
118.62 - sb.append(new String(arr, 0));
118.63 - return sb.toString();
118.64 - }
118.65 -
118.66 - @Compare public static Object compareURLs() throws MalformedURLException {
118.67 - return new URL("http://apidesign.org:8080/wiki/").toExternalForm().toString();
118.68 - }
118.69 -
118.70 - @Compare public String deleteLastTwoCharacters() {
118.71 - StringBuilder sb = new StringBuilder();
118.72 - sb.append("453.0");
118.73 - if (sb.toString().endsWith(".0")) {
118.74 - final int l = sb.length();
118.75 - sb.delete(l - 2, l);
118.76 - }
118.77 - return sb.toString().toString();
118.78 - }
118.79 -
118.80 - @Compare public String nameOfStringClass() throws Exception {
118.81 - return Class.forName("java.lang.String").getName();
118.82 - }
118.83 - @Compare public String nameOfArrayClass() throws Exception {
118.84 - return Class.forName("org.apidesign.bck2brwsr.tck.CompareHashTest").getName();
118.85 - }
118.86 -
118.87 - @Compare public String lowerHello() {
118.88 - return "HeLlO".toLowerCase();
118.89 - }
118.90 -
118.91 - @Compare public String lowerA() {
118.92 - return String.valueOf(Character.toLowerCase('A')).toString();
118.93 - }
118.94 - @Compare public String upperHello() {
118.95 - return "hello".toUpperCase();
118.96 - }
118.97 -
118.98 - @Compare public String upperA() {
118.99 - return String.valueOf(Character.toUpperCase('a')).toString();
118.100 - }
118.101 -
118.102 - @Compare public boolean matchRegExp() throws Exception {
118.103 - return "58038503".matches("\\d*");
118.104 - }
118.105 -
118.106 - @Compare public boolean doesNotMatchRegExp() throws Exception {
118.107 - return "58038503GH".matches("\\d*");
118.108 - }
118.109 -
118.110 - @Compare public boolean doesNotMatchRegExpFully() throws Exception {
118.111 - return "Hello".matches("Hell");
118.112 - }
118.113 -
118.114 - @Compare public String emptyCharArray() {
118.115 - char[] arr = new char[10];
118.116 - return new String(arr);
118.117 - }
118.118 -
118.119 - @Compare public String variousCharacterTests() throws Exception {
118.120 - StringBuilder sb = new StringBuilder();
118.121 -
118.122 - sb.append(Character.isUpperCase('a'));
118.123 - sb.append(Character.isUpperCase('A'));
118.124 - sb.append(Character.isLowerCase('a'));
118.125 - sb.append(Character.isLowerCase('A'));
118.126 -
118.127 - sb.append(Character.isLetter('A'));
118.128 - sb.append(Character.isLetterOrDigit('9'));
118.129 - sb.append(Character.isLetterOrDigit('A'));
118.130 - sb.append(Character.isLetter('0'));
118.131 -
118.132 - return sb.toString().toString();
118.133 - }
118.134 -
118.135 - @Compare
118.136 - public String nullFieldInitialized() {
118.137 - NullField nf = new NullField();
118.138 - return ("" + nf.name).toString();
118.139 - }
118.140 - @Compare
118.141 - public String toUTFString() throws UnsupportedEncodingException {
118.142 - byte[] arr = {
118.143 - (byte) -59, (byte) -67, (byte) 108, (byte) 117, (byte) -59, (byte) -91,
118.144 - (byte) 111, (byte) 117, (byte) -60, (byte) -115, (byte) 107, (byte) -61,
118.145 - (byte) -67, (byte) 32, (byte) 107, (byte) -59, (byte) -81, (byte) -59,
118.146 - (byte) -120
118.147 - };
118.148 - return new String(arr, "utf-8");
118.149 - }
118.150 -
118.151 - @Compare
118.152 - public int stringToBytesLenght() throws UnsupportedEncodingException {
118.153 - return "\u017dlu\u0165ou\u010dk\u00fd k\u016f\u0148".getBytes("utf8").length;
118.154 - }
118.155 -
118.156 - @Compare public String replaceSeq() {
118.157 - return "Hello World.".replace(".", "!");
118.158 - }
118.159 - @Compare public String replaceSeqAll() {
118.160 - return "Hello World! Hello World.".replace("World", "Jarda");
118.161 - }
118.162 - @Compare public String replaceSeqAA() {
118.163 - String res = "aaa".replace("aa", "b");
118.164 - assert res.equals("ba") : "Expecting ba: " + res;
118.165 - return res;
118.166 - }
118.167 -
118.168 - @Factory
118.169 - public static Object[] create() {
118.170 - return VMTest.create(CompareStringsTest.class);
118.171 - }
118.172 -
118.173 - private static final class NullField {
118.174 -
118.175 - String name;
118.176 - }
118.177 -}
119.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/DoubleTest.java Fri May 24 16:45:32 2013 +0200
119.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
119.3 @@ -1,65 +0,0 @@
119.4 -/**
119.5 - * Back 2 Browser Bytecode Translator
119.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
119.7 - *
119.8 - * This program is free software: you can redistribute it and/or modify
119.9 - * it under the terms of the GNU General Public License as published by
119.10 - * the Free Software Foundation, version 2 of the License.
119.11 - *
119.12 - * This program is distributed in the hope that it will be useful,
119.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
119.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
119.15 - * GNU General Public License for more details.
119.16 - *
119.17 - * You should have received a copy of the GNU General Public License
119.18 - * along with this program. Look for COPYING file in the top folder.
119.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
119.20 - */
119.21 -package org.apidesign.bck2brwsr.tck;
119.22 -
119.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
119.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
119.25 -import org.testng.annotations.Factory;
119.26 -
119.27 -/**
119.28 - *
119.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
119.30 - */
119.31 -public class DoubleTest {
119.32 - @Compare public String integerToString() {
119.33 - return toStr(1);
119.34 - }
119.35 -
119.36 - @Compare public String integerAndHalfToString() {
119.37 - return toStr(1.5);
119.38 - }
119.39 -
119.40 - @Compare public double longToAndBack() {
119.41 - return Double.parseDouble(toStr(Long.MAX_VALUE / 10));
119.42 - }
119.43 -
119.44 - @Compare public String negativeIntToString() {
119.45 - return toStr(-10);
119.46 - }
119.47 -
119.48 - @Compare public String negativeIntAndHalfToString() {
119.49 - return toStr(-10.5);
119.50 - }
119.51 -
119.52 - @Compare public double negativeLongAndBack() {
119.53 - return Double.parseDouble(toStr(Long.MIN_VALUE / 10));
119.54 - }
119.55 -
119.56 - @Compare public double canParseExp() {
119.57 - return Double.parseDouble(toStr(1.7976931348623157e+308));
119.58 - }
119.59 -
119.60 - private static String toStr(double d) {
119.61 - return Double.toString(d);
119.62 - }
119.63 -
119.64 - @Factory
119.65 - public static Object[] create() {
119.66 - return VMTest.create(DoubleTest.class);
119.67 - }
119.68 -}
120.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Fri May 24 16:45:32 2013 +0200
120.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
120.3 @@ -1,106 +0,0 @@
120.4 -/**
120.5 - * Back 2 Browser Bytecode Translator
120.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
120.7 - *
120.8 - * This program is free software: you can redistribute it and/or modify
120.9 - * it under the terms of the GNU General Public License as published by
120.10 - * the Free Software Foundation, version 2 of the License.
120.11 - *
120.12 - * This program is distributed in the hope that it will be useful,
120.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
120.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
120.15 - * GNU General Public License for more details.
120.16 - *
120.17 - * You should have received a copy of the GNU General Public License
120.18 - * along with this program. Look for COPYING file in the top folder.
120.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
120.20 - */
120.21 -package org.apidesign.bck2brwsr.tck;
120.22 -
120.23 -import java.io.InputStream;
120.24 -import java.net.URL;
120.25 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
120.26 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
120.27 -import org.apidesign.bck2brwsr.vmtest.Http;
120.28 -import org.apidesign.bck2brwsr.vmtest.VMTest;
120.29 -import org.testng.annotations.Factory;
120.30 -
120.31 -/**
120.32 - *
120.33 - * @author Jaroslav Tulach <jtulach@netbeans.org>
120.34 - */
120.35 -public class HttpResourceTest {
120.36 -
120.37 - @Http({
120.38 - @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
120.39 - })
120.40 - @BrwsrTest
120.41 -
120.42 -
120.43 - public String testReadContentViaXHR() throws Exception {
120.44 - String msg = read("/xhr");
120.45 - assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
120.46 - return msg;
120.47 - }
120.48 -
120.49 - @Http({
120.50 - @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
120.51 - })
120.52 - @BrwsrTest
120.53 - public String testReadContentViaURL() throws Exception {
120.54 - URL url = new URL("http:/url");
120.55 - String msg = (String) url.getContent();
120.56 - assert "Hello via URL!".equals(msg) : "The message was " + msg;
120.57 - return msg;
120.58 - }
120.59 - @Http({
120.60 - @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
120.61 - })
120.62 - @BrwsrTest
120.63 - public String testReadContentViaURLWithStringParam() throws Exception {
120.64 - URL url = new URL("http:/url");
120.65 - String msg = (String) url.getContent(new Class[] { String.class });
120.66 - assert "Hello via URL!".equals(msg) : "The message was " + msg;
120.67 - return msg;
120.68 - }
120.69 -
120.70 - @Http({
120.71 - @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
120.72 - })
120.73 - @BrwsrTest
120.74 - public void testReadByte() throws Exception {
120.75 - URL url = new URL("http:/bytes");
120.76 - final Object res = url.getContent(new Class[] { byte[].class });
120.77 - assert res instanceof byte[] : "Expecting byte[]: " + res;
120.78 - byte[] arr = (byte[]) res;
120.79 - assert arr.length == 1 : "One byte " + arr.length;
120.80 - assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
120.81 - }
120.82 -
120.83 - @Http({
120.84 - @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
120.85 - })
120.86 - @BrwsrTest
120.87 - public void testReadByteViaInputStream() throws Exception {
120.88 - URL url = new URL("http:/bytes");
120.89 - InputStream is = url.openStream();
120.90 - byte[] arr = new byte[10];
120.91 - int len = is.read(arr);
120.92 - assert len == 1 : "One byte " + len;
120.93 - assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
120.94 - }
120.95 -
120.96 - @JavaScriptBody(args = { "url" }, body =
120.97 - "var req = new XMLHttpRequest();\n"
120.98 - + "req.open('GET', url, false);\n"
120.99 - + "req.send();\n"
120.100 - + "return req.responseText;"
120.101 - )
120.102 - private static native String read(String url);
120.103 -
120.104 -
120.105 - @Factory
120.106 - public static Object[] create() {
120.107 - return VMTest.create(HttpResourceTest.class);
120.108 - }
120.109 -}
121.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceA.java Fri May 24 16:45:32 2013 +0200
121.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
121.3 @@ -1,34 +0,0 @@
121.4 -/**
121.5 - * Back 2 Browser Bytecode Translator
121.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
121.7 - *
121.8 - * This program is free software: you can redistribute it and/or modify
121.9 - * it under the terms of the GNU General Public License as published by
121.10 - * the Free Software Foundation, version 2 of the License.
121.11 - *
121.12 - * This program is distributed in the hope that it will be useful,
121.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
121.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
121.15 - * GNU General Public License for more details.
121.16 - *
121.17 - * You should have received a copy of the GNU General Public License
121.18 - * along with this program. Look for COPYING file in the top folder.
121.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
121.20 - */
121.21 -package org.apidesign.bck2brwsr.tck;
121.22 -
121.23 -/**
121.24 - *
121.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
121.26 - */
121.27 -public class InheritanceA {
121.28 - private String name;
121.29 -
121.30 - public void setA(String n) {
121.31 - this.name = n;
121.32 - }
121.33 -
121.34 - public String getA() {
121.35 - return name;
121.36 - }
121.37 -}
122.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceB.java Fri May 24 16:45:32 2013 +0200
122.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
122.3 @@ -1,34 +0,0 @@
122.4 -/**
122.5 - * Back 2 Browser Bytecode Translator
122.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
122.7 - *
122.8 - * This program is free software: you can redistribute it and/or modify
122.9 - * it under the terms of the GNU General Public License as published by
122.10 - * the Free Software Foundation, version 2 of the License.
122.11 - *
122.12 - * This program is distributed in the hope that it will be useful,
122.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
122.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
122.15 - * GNU General Public License for more details.
122.16 - *
122.17 - * You should have received a copy of the GNU General Public License
122.18 - * along with this program. Look for COPYING file in the top folder.
122.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
122.20 - */
122.21 -package org.apidesign.bck2brwsr.tck;
122.22 -
122.23 -/**
122.24 - *
122.25 - * @author Jaroslav Tulach <jtulach@netbeans.org>
122.26 - */
122.27 -public class InheritanceB extends InheritanceA {
122.28 - private String name;
122.29 -
122.30 - public void setB(String n) {
122.31 - this.name = n;
122.32 - }
122.33 -
122.34 - public String getB() {
122.35 - return name;
122.36 - }
122.37 -}
123.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/InheritanceTest.java Fri May 24 16:45:32 2013 +0200
123.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
123.3 @@ -1,41 +0,0 @@
123.4 -/**
123.5 - * Back 2 Browser Bytecode Translator
123.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
123.7 - *
123.8 - * This program is free software: you can redistribute it and/or modify
123.9 - * it under the terms of the GNU General Public License as published by
123.10 - * the Free Software Foundation, version 2 of the License.
123.11 - *
123.12 - * This program is distributed in the hope that it will be useful,
123.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
123.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123.15 - * GNU General Public License for more details.
123.16 - *
123.17 - * You should have received a copy of the GNU General Public License
123.18 - * along with this program. Look for COPYING file in the top folder.
123.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
123.20 - */
123.21 -package org.apidesign.bck2brwsr.tck;
123.22 -
123.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
123.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
123.25 -import org.testng.annotations.Factory;
123.26 -
123.27 -/**
123.28 - *
123.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
123.30 - */
123.31 -public class InheritanceTest {
123.32 -
123.33 - @Compare public String checkFieldsIndependent() throws ClassNotFoundException {
123.34 - InheritanceB ib = new InheritanceB();
123.35 - ib.setA("A");
123.36 - ib.setB("B");
123.37 - return "A: " + ib.getA() + " B: " + ib.getB();
123.38 - }
123.39 -
123.40 - @Factory
123.41 - public static Object[] create() {
123.42 - return VMTest.create(InheritanceTest.class);
123.43 - }
123.44 -}
124.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/IntegerArithmeticTest.java Fri May 24 16:45:32 2013 +0200
124.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
124.3 @@ -1,166 +0,0 @@
124.4 -/**
124.5 - * Back 2 Browser Bytecode Translator
124.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
124.7 - *
124.8 - * This program is free software: you can redistribute it and/or modify
124.9 - * it under the terms of the GNU General Public License as published by
124.10 - * the Free Software Foundation, version 2 of the License.
124.11 - *
124.12 - * This program is distributed in the hope that it will be useful,
124.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
124.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124.15 - * GNU General Public License for more details.
124.16 - *
124.17 - * You should have received a copy of the GNU General Public License
124.18 - * along with this program. Look for COPYING file in the top folder.
124.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
124.20 - */
124.21 -package org.apidesign.bck2brwsr.tck;
124.22 -
124.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
124.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
124.25 -import org.testng.annotations.Factory;
124.26 -
124.27 -/**
124.28 - *
124.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
124.30 - */
124.31 -public class IntegerArithmeticTest {
124.32 -
124.33 - private static int add(int x, int y) {
124.34 - return x + y;
124.35 - }
124.36 -
124.37 - private static int sub(int x, int y) {
124.38 - return x - y;
124.39 - }
124.40 -
124.41 - private static int mul(int x, int y) {
124.42 - return x * y;
124.43 - }
124.44 -
124.45 - private static int div(int x, int y) {
124.46 - return x / y;
124.47 - }
124.48 -
124.49 - private static int mod(int x, int y) {
124.50 - return x % y;
124.51 - }
124.52 -
124.53 - private static int neg(int x) {
124.54 - return (-x);
124.55 - }
124.56 -
124.57 - private static float fadd(float x, float y) {
124.58 - return x + y;
124.59 - }
124.60 -
124.61 - private static double dadd(double x, double y) {
124.62 - return x + y;
124.63 - }
124.64 -
124.65 - @Compare public int addOverflow() {
124.66 - return add(Integer.MAX_VALUE, 1);
124.67 - }
124.68 -
124.69 - @Compare public int subUnderflow() {
124.70 - return sub(Integer.MIN_VALUE, 1);
124.71 - }
124.72 -
124.73 - @Compare public int addMaxIntAndMaxInt() {
124.74 - return add(Integer.MAX_VALUE, Integer.MAX_VALUE);
124.75 - }
124.76 -
124.77 - @Compare public int subMinIntAndMinInt() {
124.78 - return sub(Integer.MIN_VALUE, Integer.MIN_VALUE);
124.79 - }
124.80 -
124.81 - @Compare public int multiplyMaxInt() {
124.82 - return mul(Integer.MAX_VALUE, 2);
124.83 - }
124.84 -
124.85 - @Compare public int multiplyMaxIntAndMaxInt() {
124.86 - return mul(Integer.MAX_VALUE, Integer.MAX_VALUE);
124.87 - }
124.88 -
124.89 - @Compare public int multiplyMinInt() {
124.90 - return mul(Integer.MIN_VALUE, 2);
124.91 - }
124.92 -
124.93 - @Compare public int multiplyMinIntAndMinInt() {
124.94 - return mul(Integer.MIN_VALUE, Integer.MIN_VALUE);
124.95 - }
124.96 -
124.97 - @Compare public int multiplyPrecision() {
124.98 - return mul(119106029, 1103515245);
124.99 - }
124.100 -
124.101 - @Compare public int division() {
124.102 - return div(1, 2);
124.103 - }
124.104 -
124.105 - @Compare public int divisionReminder() {
124.106 - return mod(1, 2);
124.107 - }
124.108 -
124.109 - @Compare public int negativeDivision() {
124.110 - return div(-7, 3);
124.111 - }
124.112 -
124.113 - @Compare public int negativeDivisionReminder() {
124.114 - return mod(-7, 3);
124.115 - }
124.116 -
124.117 - @Compare public int conversionFromFloat() {
124.118 - return (int) fadd(-2, -0.6f);
124.119 - }
124.120 -
124.121 - @Compare public int conversionFromDouble() {
124.122 - return (int) dadd(-2, -0.6);
124.123 - }
124.124 -
124.125 - @Compare public boolean divByZeroThrowsArithmeticException() {
124.126 - try {
124.127 - div(1, 0);
124.128 - return false;
124.129 - } catch (final ArithmeticException e) {
124.130 - return true;
124.131 - }
124.132 - }
124.133 -
124.134 - @Compare public boolean modByZeroThrowsArithmeticException() {
124.135 - try {
124.136 - mod(1, 0);
124.137 - return false;
124.138 - } catch (final ArithmeticException e) {
124.139 - return true;
124.140 - }
124.141 - }
124.142 -
124.143 - @Compare public int negate() {
124.144 - return neg(123456);
124.145 - }
124.146 -
124.147 - @Compare public int negateMaxInt() {
124.148 - return neg(Integer.MAX_VALUE);
124.149 - }
124.150 -
124.151 - @Compare public int negateMinInt() {
124.152 - return neg(Integer.MIN_VALUE);
124.153 - }
124.154 -
124.155 - @Compare public int sumTwoDimensions() {
124.156 - int[][] matrix = createMatrix(4, 3);
124.157 - matrix[0][0] += 10;
124.158 - return matrix[0][0];
124.159 - }
124.160 -
124.161 - static int[][] createMatrix(int x, int y) {
124.162 - return new int[x][y];
124.163 - }
124.164 -
124.165 - @Factory
124.166 - public static Object[] create() {
124.167 - return VMTest.create(IntegerArithmeticTest.class);
124.168 - }
124.169 -}
125.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java Fri May 24 16:45:32 2013 +0200
125.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
125.3 @@ -1,376 +0,0 @@
125.4 -/**
125.5 - * Back 2 Browser Bytecode Translator
125.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
125.7 - *
125.8 - * This program is free software: you can redistribute it and/or modify
125.9 - * it under the terms of the GNU General Public License as published by
125.10 - * the Free Software Foundation, version 2 of the License.
125.11 - *
125.12 - * This program is distributed in the hope that it will be useful,
125.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
125.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
125.15 - * GNU General Public License for more details.
125.16 - *
125.17 - * You should have received a copy of the GNU General Public License
125.18 - * along with this program. Look for COPYING file in the top folder.
125.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
125.20 - */
125.21 -package org.apidesign.bck2brwsr.tck;
125.22 -
125.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
125.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
125.25 -import org.testng.annotations.Factory;
125.26 -
125.27 -/**
125.28 - *
125.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
125.30 - */
125.31 -public class LongArithmeticTest {
125.32 -
125.33 - private static long add(long x, long y) {
125.34 - return (x + y);
125.35 - }
125.36 -
125.37 - private static long sub(long x, long y) {
125.38 - return (x - y);
125.39 - }
125.40 -
125.41 - private static long mul(long x, long y) {
125.42 - return (x * y);
125.43 - }
125.44 -
125.45 - private static long div(long x, long y) {
125.46 - return (x / y);
125.47 - }
125.48 -
125.49 - private static long mod(long x, long y) {
125.50 - return (x % y);
125.51 - }
125.52 -
125.53 - private static long neg(long x) {
125.54 - return (-x);
125.55 - }
125.56 -
125.57 - private static long shl(long x, int b) {
125.58 - return (x << b);
125.59 - }
125.60 -
125.61 - private static long shr(long x, int b) {
125.62 - return (x >> b);
125.63 - }
125.64 -
125.65 - private static long ushr(long x, int b) {
125.66 - return (x >>> b);
125.67 - }
125.68 -
125.69 - private static long and(long x, long y) {
125.70 - return (x & y);
125.71 - }
125.72 -
125.73 - private static long or(long x, long y) {
125.74 - return (x | y);
125.75 - }
125.76 -
125.77 - private static long xor(long x, long y) {
125.78 - return (x ^ y);
125.79 - }
125.80 -
125.81 - private static float fadd(float x, float y) {
125.82 - return x + y;
125.83 - }
125.84 -
125.85 - private static double dadd(double x, double y) {
125.86 - return x + y;
125.87 - }
125.88 -
125.89 - public static int compare(long x, long y, int zero) {
125.90 - final int xyResult = compareL(x, y, zero);
125.91 - final int yxResult = compareL(y, x, zero);
125.92 -
125.93 - return ((xyResult + yxResult) == 0) ? xyResult : -2;
125.94 - }
125.95 -
125.96 - private static int compareL(long x, long y, int zero) {
125.97 - int result = -2;
125.98 - int trueCount = 0;
125.99 -
125.100 - x += zero;
125.101 - if (x == y) {
125.102 - result = 0;
125.103 - ++trueCount;
125.104 - }
125.105 -
125.106 - x += zero;
125.107 - if (x < y) {
125.108 - result = -1;
125.109 - ++trueCount;
125.110 - }
125.111 -
125.112 - x += zero;
125.113 - if (x > y) {
125.114 - result = 1;
125.115 - ++trueCount;
125.116 - }
125.117 -
125.118 - return (trueCount == 1) ? result : -2;
125.119 - }
125.120 -
125.121 - @Compare public long conversion() {
125.122 - return Long.MAX_VALUE;
125.123 - }
125.124 -
125.125 - @Compare public long negate1() {
125.126 - return neg(0x00fa37d7763e0ca1l);
125.127 - }
125.128 -
125.129 - @Compare public long negate2() {
125.130 - return neg(0x80fa37d7763e0ca1l);
125.131 - }
125.132 -
125.133 - @Compare public long negate3() {
125.134 - return neg(0xfffffffffffffeddl);
125.135 - }
125.136 -
125.137 - @Compare public long addOverflow() {
125.138 - return add(Long.MAX_VALUE, 1l);
125.139 - }
125.140 -
125.141 - @Compare public long subUnderflow() {
125.142 - return sub(Long.MIN_VALUE, 1l);
125.143 - }
125.144 -
125.145 - @Compare public long addMaxLongAndMaxLong() {
125.146 - return add(Long.MAX_VALUE, Long.MAX_VALUE);
125.147 - }
125.148 -
125.149 - @Compare public long subMinLongAndMinLong() {
125.150 - return sub(Long.MIN_VALUE, Long.MIN_VALUE);
125.151 - }
125.152 -
125.153 - @Compare public long subMinLongAndMaxLong() {
125.154 - return sub(Long.MIN_VALUE, Long.MAX_VALUE);
125.155 - }
125.156 -
125.157 - @Compare public long multiplyMaxLong() {
125.158 - return mul(Long.MAX_VALUE, 2l);
125.159 - }
125.160 -
125.161 - @Compare public long multiplyMaxLongAndMaxLong() {
125.162 - return mul(Long.MAX_VALUE, Long.MAX_VALUE);
125.163 - }
125.164 -
125.165 - @Compare public long multiplyMinLong() {
125.166 - return mul(Long.MIN_VALUE, 2l);
125.167 - }
125.168 -
125.169 - @Compare public long multiplyMinLongAndMinLong() {
125.170 - return mul(Long.MIN_VALUE, Long.MIN_VALUE);
125.171 - }
125.172 -
125.173 - @Compare public long multiplyPrecision() {
125.174 - return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
125.175 - }
125.176 -
125.177 - @Compare public long divideSmallPositiveNumbers() {
125.178 - return div(0xabcdef, 0x123);
125.179 - }
125.180 -
125.181 - @Compare public long divideSmallNegativeNumbers() {
125.182 - return div(-0xabcdef, -0x123);
125.183 - }
125.184 -
125.185 - @Compare public long divideSmallMixedNumbers() {
125.186 - return div(0xabcdef, -0x123);
125.187 - }
125.188 -
125.189 - @Compare public long dividePositiveNumbersOneDigitDenom() {
125.190 - return div(0xabcdef0102ffffl, 0x654);
125.191 - }
125.192 -
125.193 - @Compare public long divideNegativeNumbersOneDigitDenom() {
125.194 - return div(-0xabcdef0102ffffl, -0x654);
125.195 - }
125.196 -
125.197 - @Compare public long divideMixedNumbersOneDigitDenom() {
125.198 - return div(-0xabcdef0102ffffl, 0x654);
125.199 - }
125.200 -
125.201 - @Compare public long dividePositiveNumbersMultiDigitDenom() {
125.202 - return div(0x7ffefc003322aabbl, 0x89ab1000l);
125.203 - }
125.204 -
125.205 - @Compare public long divideNegativeNumbersMultiDigitDenom() {
125.206 - return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
125.207 - }
125.208 -
125.209 - @Compare public long divideMixedNumbersMultiDigitDenom() {
125.210 - return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
125.211 - }
125.212 -
125.213 - @Compare public long divideWithOverflow() {
125.214 - return div(0x8000fffe0000l, 0x8000ffffl);
125.215 - }
125.216 -
125.217 - @Compare public long divideWithCorrection() {
125.218 - return div(0x7fff800000000000l, 0x800000000001l);
125.219 - }
125.220 -
125.221 - @Compare public long moduloSmallPositiveNumbers() {
125.222 - return mod(0xabcdef, 0x123);
125.223 - }
125.224 -
125.225 - @Compare public long moduloSmallNegativeNumbers() {
125.226 - return mod(-0xabcdef, -0x123);
125.227 - }
125.228 -
125.229 - @Compare public long moduloSmallMixedNumbers() {
125.230 - return mod(0xabcdef, -0x123);
125.231 - }
125.232 -
125.233 - @Compare public long moduloPositiveNumbersOneDigitDenom() {
125.234 - return mod(0xabcdef0102ffffl, 0x654);
125.235 - }
125.236 -
125.237 - @Compare public long moduloNegativeNumbersOneDigitDenom() {
125.238 - return mod(-0xabcdef0102ffffl, -0x654);
125.239 - }
125.240 -
125.241 - @Compare public long moduloMixedNumbersOneDigitDenom() {
125.242 - return mod(-0xabcdef0102ffffl, 0x654);
125.243 - }
125.244 -
125.245 - @Compare public long moduloPositiveNumbersMultiDigitDenom() {
125.246 - return mod(0x7ffefc003322aabbl, 0x89ab1000l);
125.247 - }
125.248 -
125.249 - @Compare public long moduloNegativeNumbersMultiDigitDenom() {
125.250 - return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
125.251 - }
125.252 -
125.253 - @Compare public long moduloMixedNumbersMultiDigitDenom() {
125.254 - return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
125.255 - }
125.256 -
125.257 - @Compare public long moduloWithOverflow() {
125.258 - return mod(0x8000fffe0000l, 0x8000ffffl);
125.259 - }
125.260 -
125.261 - @Compare public long moduloWithCorrection() {
125.262 - return mod(0x7fff800000000000l, 0x800000000001l);
125.263 - }
125.264 -
125.265 - @Compare public long conversionFromFloatPositive() {
125.266 - return (long) fadd(2, 0.6f);
125.267 - }
125.268 -
125.269 - @Compare public long conversionFromFloatNegative() {
125.270 - return (long) fadd(-2, -0.6f);
125.271 - }
125.272 -
125.273 - @Compare public long conversionFromDoublePositive() {
125.274 - return (long) dadd(0x20ffff0000L, 0.6);
125.275 - }
125.276 -
125.277 - @Compare public long conversionFromDoubleNegative() {
125.278 - return (long) dadd(-0x20ffff0000L, -0.6);
125.279 - }
125.280 -
125.281 - @Compare public boolean divByZeroThrowsArithmeticException() {
125.282 - try {
125.283 - div(1, 0);
125.284 - return false;
125.285 - } catch (final ArithmeticException e) {
125.286 - return true;
125.287 - }
125.288 - }
125.289 -
125.290 - @Compare public boolean modByZeroThrowsArithmeticException() {
125.291 - try {
125.292 - mod(1, 0);
125.293 - return false;
125.294 - } catch (final ArithmeticException e) {
125.295 - return true;
125.296 - }
125.297 - }
125.298 -
125.299 - @Compare public long shiftL1() {
125.300 - return shl(0x00fa37d7763e0ca1l, 5);
125.301 - }
125.302 -
125.303 - @Compare public long shiftL2() {
125.304 - return shl(0x00fa37d7763e0ca1l, 32);
125.305 - }
125.306 -
125.307 - @Compare public long shiftL3() {
125.308 - return shl(0x00fa37d7763e0ca1l, 45);
125.309 - }
125.310 -
125.311 - @Compare public long shiftR1() {
125.312 - return shr(0x00fa37d7763e0ca1l, 5);
125.313 - }
125.314 -
125.315 - @Compare public long shiftR2() {
125.316 - return shr(0x00fa37d7763e0ca1l, 32);
125.317 - }
125.318 -
125.319 - @Compare public long shiftR3() {
125.320 - return shr(0x00fa37d7763e0ca1l, 45);
125.321 - }
125.322 -
125.323 - @Compare public long uShiftR1() {
125.324 - return ushr(0x00fa37d7763e0ca1l, 5);
125.325 - }
125.326 -
125.327 - @Compare public long uShiftR2() {
125.328 - return ushr(0x00fa37d7763e0ca1l, 45);
125.329 - }
125.330 -
125.331 - @Compare public long uShiftR3() {
125.332 - return ushr(0xf0fa37d7763e0ca1l, 5);
125.333 - }
125.334 -
125.335 - @Compare public long uShiftR4() {
125.336 - return ushr(0xf0fa37d7763e0ca1l, 45);
125.337 - }
125.338 -
125.339 - @Compare public long and1() {
125.340 - return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
125.341 - }
125.342 -
125.343 - @Compare public long or1() {
125.344 - return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
125.345 - }
125.346 -
125.347 - @Compare public long xor1() {
125.348 - return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
125.349 - }
125.350 -
125.351 - @Compare public long xor2() {
125.352 - return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
125.353 - }
125.354 -
125.355 - @Compare public long xor3() {
125.356 - return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
125.357 - }
125.358 -
125.359 - @Compare public int compareSameNumbers() {
125.360 - return compare(0x0000000000000000l, 0x0000000000000000l, 0);
125.361 - }
125.362 -
125.363 - @Compare public int comparePositiveNumbers() {
125.364 - return compare(0x0000000000200000l, 0x0000000010000000l, 0);
125.365 - }
125.366 -
125.367 - @Compare public int compareNegativeNumbers() {
125.368 - return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
125.369 - }
125.370 -
125.371 - @Compare public int compareMixedNumbers() {
125.372 - return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
125.373 - }
125.374 -
125.375 - @Factory
125.376 - public static Object[] create() {
125.377 - return VMTest.create(LongArithmeticTest.class);
125.378 - }
125.379 -}
126.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionArrayTest.java Fri May 24 16:45:32 2013 +0200
126.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
126.3 @@ -1,161 +0,0 @@
126.4 -/**
126.5 - * Back 2 Browser Bytecode Translator
126.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
126.7 - *
126.8 - * This program is free software: you can redistribute it and/or modify
126.9 - * it under the terms of the GNU General Public License as published by
126.10 - * the Free Software Foundation, version 2 of the License.
126.11 - *
126.12 - * This program is distributed in the hope that it will be useful,
126.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
126.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
126.15 - * GNU General Public License for more details.
126.16 - *
126.17 - * You should have received a copy of the GNU General Public License
126.18 - * along with this program. Look for COPYING file in the top folder.
126.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
126.20 - */
126.21 -package org.apidesign.bck2brwsr.tck;
126.22 -
126.23 -import java.lang.reflect.Array;
126.24 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
126.25 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
126.26 -import org.apidesign.bck2brwsr.vmtest.Compare;
126.27 -import org.apidesign.bck2brwsr.vmtest.VMTest;
126.28 -import org.testng.annotations.Factory;
126.29 -
126.30 -/**
126.31 - *
126.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
126.33 - */
126.34 -public class ReflectionArrayTest {
126.35 - @Compare public int lengthOfStringArray() {
126.36 - String[] arr = (String[]) Array.newInstance(String.class, 10);
126.37 - return arr.length;
126.38 - }
126.39 -
126.40 - @Compare public int reflectiveLengthOfStringArray() {
126.41 - Object arr = Array.newInstance(String.class, 10);
126.42 - return Array.getLength(arr);
126.43 - }
126.44 -
126.45 - @Compare public int reflectiveLengthOneNonArray() {
126.46 - Object arr = "non-array";
126.47 - return Array.getLength(arr);
126.48 - }
126.49 -
126.50 - @Compare public String compTypeOfStringArray() {
126.51 - String[] arr = (String[]) Array.newInstance(String.class, 10);
126.52 - return arr.getClass().getComponentType().getName();
126.53 - }
126.54 -
126.55 - @Compare public Object negativeArrayExcp() {
126.56 - return Array.newInstance(String.class, -5);
126.57 - }
126.58 -
126.59 - @Compare public int lengthOfIntArray() {
126.60 - int[] arr = (int[]) Array.newInstance(Integer.TYPE, 10);
126.61 - return arr.length;
126.62 - }
126.63 -
126.64 - @Compare public int reflectiveLengthOfIntArray() {
126.65 - Object arr = Array.newInstance(Integer.TYPE, 10);
126.66 - return Array.getLength(arr);
126.67 - }
126.68 -
126.69 - @Compare public String compTypeOfIntArray() {
126.70 - int[] arr = (int[]) Array.newInstance(int.class, 10);
126.71 - return arr.getClass().getComponentType().getName();
126.72 - }
126.73 -
126.74 - @Compare public Object intNegativeArrayExcp() {
126.75 - return Array.newInstance(int.class, -5);
126.76 - }
126.77 -
126.78 - @Compare public Integer verifyAutobox() {
126.79 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.80 - return (Integer) Array.get(arr, 0);
126.81 - }
126.82 - @Compare public String verifyObjectArray() {
126.83 - String[] arr = (String[]) Array.newInstance(String.class, 5);
126.84 - Array.set(arr, 0, "Hello");
126.85 - return (String) Array.get(arr, 0);
126.86 - }
126.87 - @Compare public int verifyInt() {
126.88 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.89 - return Array.getInt(arr, 0);
126.90 - }
126.91 - @Compare public long verifyConvertToLong() {
126.92 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.93 - return Array.getLong(arr, 0);
126.94 - }
126.95 -
126.96 - @Compare public Object verifySetIntToObject() {
126.97 - try {
126.98 - Object[] arr = (Object[]) Array.newInstance(Object.class, 5);
126.99 - Array.setInt(arr, 0, 10);
126.100 - return Array.get(arr, 0);
126.101 - } catch (Exception exception) {
126.102 - return exception.getClass().getName();
126.103 - }
126.104 - }
126.105 - @Compare public long verifySetShort() {
126.106 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.107 - Array.setShort(arr, 0, (short)10);
126.108 - return Array.getLong(arr, 0);
126.109 - }
126.110 - @Compare public long verifyCantSetLong() {
126.111 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.112 - Array.setLong(arr, 0, 10);
126.113 - return Array.getLong(arr, 0);
126.114 - }
126.115 - @Compare public float verifyLongToFloat() {
126.116 - Object arr = Array.newInstance(float.class, 5);
126.117 - Array.setLong(arr, 0, 10);
126.118 - return Array.getFloat(arr, 0);
126.119 - }
126.120 -
126.121 - @Compare public double verifyConvertToDouble() {
126.122 - int[] arr = (int[]) Array.newInstance(int.class, 5);
126.123 - return Array.getDouble(arr, 0);
126.124 - }
126.125 -
126.126 - @Compare public int multiIntArray() {
126.127 - int[][][] arr = (int[][][]) Array.newInstance(int.class, 3, 3, 3);
126.128 - return arr[0][1][2] + 5 + arr[2][2][0];
126.129 - }
126.130 -
126.131 - @Compare public String multiIntArrayCompType() {
126.132 - return Array.newInstance(int.class, 3, 3, 3).getClass().getName();
126.133 - }
126.134 -
126.135 - @JavaScriptBody(args = {}, body = "return [1, 2];")
126.136 - private static native Object crtarr();
126.137 -
126.138 - @JavaScriptBody(args = {}, body = "return new Object();")
126.139 - private static native Object newobj();
126.140 -
126.141 - @BrwsrTest
126.142 - public static void toStringArray() {
126.143 - final Object arr = crtarr();
126.144 - final Object real = new Object[2];
126.145 - assert arr instanceof Object[] : "Any array is Java array: " + arr;
126.146 - assert arr.getClass() == real.getClass() : "Same classes " + arr + " and " + real.getClass();
126.147 - final String str = arr.toString();
126.148 - assert str != null;
126.149 - assert str.startsWith("[Ljava.lang.Object;@") : str;
126.150 - }
126.151 -
126.152 - @BrwsrTest
126.153 - public static void objectToString() {
126.154 - String s = newobj().toString();
126.155 - assert s != null : "Some string computed";
126.156 - assert s.startsWith("java.lang.Object@") : "Regular object toString(): " + s;
126.157 - }
126.158 -
126.159 -
126.160 - @Factory
126.161 - public static Object[] create() {
126.162 - return VMTest.create(ReflectionArrayTest.class);
126.163 - }
126.164 -}
127.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ReflectionTest.java Fri May 24 16:45:32 2013 +0200
127.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
127.3 @@ -1,272 +0,0 @@
127.4 -/**
127.5 - * Back 2 Browser Bytecode Translator
127.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
127.7 - *
127.8 - * This program is free software: you can redistribute it and/or modify
127.9 - * it under the terms of the GNU General Public License as published by
127.10 - * the Free Software Foundation, version 2 of the License.
127.11 - *
127.12 - * This program is distributed in the hope that it will be useful,
127.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
127.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
127.15 - * GNU General Public License for more details.
127.16 - *
127.17 - * You should have received a copy of the GNU General Public License
127.18 - * along with this program. Look for COPYING file in the top folder.
127.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
127.20 - */
127.21 -package org.apidesign.bck2brwsr.tck;
127.22 -
127.23 -import java.lang.annotation.Retention;
127.24 -import java.lang.annotation.RetentionPolicy;
127.25 -import java.lang.reflect.Method;
127.26 -import java.util.Arrays;
127.27 -import java.util.Collections;
127.28 -import java.util.List;
127.29 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
127.30 -import org.apidesign.bck2brwsr.vmtest.Compare;
127.31 -import org.apidesign.bck2brwsr.vmtest.VMTest;
127.32 -import org.testng.annotations.Factory;
127.33 -
127.34 -/**
127.35 - *
127.36 - * @author Jaroslav Tulach <jtulach@netbeans.org>
127.37 - */
127.38 -public class ReflectionTest {
127.39 - @Compare public boolean nonNullThis() {
127.40 - return this == null;
127.41 - }
127.42 -
127.43 - @Compare public String intType() {
127.44 - return Integer.TYPE.toString();
127.45 - }
127.46 -
127.47 - @Compare public String voidType() throws Exception {
127.48 - return void.class.toString();
127.49 - }
127.50 -
127.51 - @Compare public String longClass() {
127.52 - return long.class.toString();
127.53 - }
127.54 -
127.55 - @Compare public boolean isRunnableInterface() {
127.56 - return Runnable.class.isInterface();
127.57 - }
127.58 -
127.59 - @Compare public boolean isAssignableToPrimitiveType() {
127.60 - return boolean.class.isAssignableFrom(Runnable.class);
127.61 - }
127.62 -
127.63 - @Compare public boolean isAssignableFromPrimitiveType() {
127.64 - return Runnable.class.isAssignableFrom(boolean.class);
127.65 - }
127.66 -
127.67 - @Compare public boolean isAssignableLongFromInt() {
127.68 - return long.class.isAssignableFrom(int.class);
127.69 - }
127.70 -
127.71 - @Compare public boolean isAssignableIntFromLong() {
127.72 - return int.class.isAssignableFrom(long.class);
127.73 - }
127.74 -
127.75 - @Compare public String isRunnableHasRunMethod() throws NoSuchMethodException {
127.76 - return Runnable.class.getMethod("run").getName();
127.77 - }
127.78 -
127.79 - @Compare public String namesOfMethods() {
127.80 - StringBuilder sb = new StringBuilder();
127.81 - String[] arr = new String[20];
127.82 - int i = 0;
127.83 - for (Method m : StaticUse.class.getMethods()) {
127.84 - arr[i++] = m.getName();
127.85 - }
127.86 - for (String s : sort(arr, i)) {
127.87 - sb.append(s).append("\n");
127.88 - }
127.89 - return sb.toString();
127.90 - }
127.91 -
127.92 - @Compare public String namesOfDeclaringClassesOfMethods() {
127.93 - StringBuilder sb = new StringBuilder();
127.94 - String[] arr = new String[20];
127.95 - int i = 0;
127.96 - for (Method m : StaticUse.class.getMethods()) {
127.97 - arr[i++] = m.getName() + "@" + m.getDeclaringClass().getName();
127.98 - }
127.99 - for (String s : sort(arr, i)) {
127.100 - sb.append(s).append("\n");
127.101 - }
127.102 - return sb.toString();
127.103 - }
127.104 -
127.105 - @Compare public String cannotCallNonStaticMethodWithNull() throws Exception {
127.106 - StaticUse.class.getMethod("instanceMethod").invoke(null);
127.107 - return "should not happen";
127.108 - }
127.109 -
127.110 - @Compare public String classCastException() {
127.111 - try {
127.112 - Integer i = (Integer)StaticUseSub.getNonNull();
127.113 - return "" + i.intValue();
127.114 - } catch (ClassCastException ex) {
127.115 - return ex.getClass().getName();
127.116 - }
127.117 - }
127.118 -
127.119 - @Compare public String methodThatThrowsException() throws Exception {
127.120 - StaticUse.class.getMethod("instanceMethod").invoke(new StaticUse());
127.121 - return "should not happen";
127.122 - }
127.123 -
127.124 - @Compare public Object voidReturnType() throws Exception {
127.125 - return StaticUse.class.getMethod("instanceMethod").getReturnType();
127.126 - }
127.127 -
127.128 - @Retention(RetentionPolicy.RUNTIME)
127.129 - @interface Ann {
127.130 - }
127.131 -
127.132 - @Compare public String annoClass() throws Exception {
127.133 - Retention r = Ann.class.getAnnotation(Retention.class);
127.134 - assert r != null : "Annotation is present";
127.135 - assert r.value() == RetentionPolicy.RUNTIME : "Policy value is OK: " + r.value();
127.136 - return r.annotationType().getName();
127.137 - }
127.138 -
127.139 - @Compare public boolean isAnnotation() {
127.140 - return Ann.class.isAnnotation();
127.141 - }
127.142 - @Compare public boolean isNotAnnotation() {
127.143 - return String.class.isAnnotation();
127.144 - }
127.145 - @Compare public boolean isNotAnnotationEnum() {
127.146 - return E.class.isAnnotation();
127.147 - }
127.148 - enum E { A, B };
127.149 - @Compare public boolean isEnum() {
127.150 - return E.A.getClass().isEnum();
127.151 - }
127.152 -
127.153 - @Compare public boolean isNotEnum() {
127.154 - return "".getClass().isEnum();
127.155 - }
127.156 -
127.157 - @Compare public String newInstanceFails() throws InstantiationException {
127.158 - try {
127.159 - return "success: " + StaticUseSub.class.newInstance();
127.160 - } catch (IllegalAccessException ex) {
127.161 - return ex.getClass().getName();
127.162 - }
127.163 - }
127.164 -
127.165 - @Compare public String paramTypes() throws Exception {
127.166 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
127.167 - final Class[] pt = plus.getParameterTypes();
127.168 - return pt[0].getName();
127.169 - }
127.170 - @Compare public String paramTypesNotFound() throws Exception {
127.171 - return StaticUse.class.getMethod("plus", int.class, double.class).toString();
127.172 - }
127.173 - @Compare public int methodWithArgs() throws Exception {
127.174 - Method plus = StaticUse.class.getMethod("plus", int.class, Integer.TYPE);
127.175 - return (Integer)plus.invoke(null, 2, 3);
127.176 - }
127.177 -
127.178 - @Compare public String classGetNameForByte() {
127.179 - return byte.class.getName();
127.180 - }
127.181 - @Compare public String classGetNameForBaseObject() {
127.182 - return newObject().getClass().getName();
127.183 - }
127.184 - @Compare public String classGetNameForJavaObject() {
127.185 - return new Object().getClass().getName();
127.186 - }
127.187 - @Compare public String classGetNameForObjectArray() {
127.188 - return (new Object[3]).getClass().getName();
127.189 - }
127.190 - @Compare public String classGetNameForSimpleIntArray() {
127.191 - return (new int[3]).getClass().getName();
127.192 - }
127.193 - @Compare public boolean sameClassGetNameForSimpleCharArray() {
127.194 - return (new char[3]).getClass() == (new char[34]).getClass();
127.195 - }
127.196 - @Compare public String classGetNameForMultiIntArray() {
127.197 - return (new int[3][4][5][6][7][8][9]).getClass().getName();
127.198 - }
127.199 - @Compare public String classGetNameForMultiIntArrayInner() {
127.200 - final int[][][][][][][] arr = new int[3][4][5][6][7][8][9];
127.201 - int[][][][][][] subarr = arr[0];
127.202 - int[][][][][] subsubarr = subarr[0];
127.203 - return subsubarr.getClass().getName();
127.204 - }
127.205 - @Compare public String classGetNameForMultiStringArray() {
127.206 - return (new String[3][4][5][6][7][8][9]).getClass().getName();
127.207 - }
127.208 -
127.209 - @Compare public String classForByte() throws Exception {
127.210 - return Class.forName("[Z").getName();
127.211 - }
127.212 -
127.213 - @Compare public String classForUnknownArray() {
127.214 - try {
127.215 - return Class.forName("[W").getName();
127.216 - } catch (Exception ex) {
127.217 - return ex.getClass().getName();
127.218 - }
127.219 - }
127.220 -
127.221 - @Compare public String classForUnknownDeepArray() {
127.222 - try {
127.223 - return Class.forName("[[[[[W").getName();
127.224 - } catch (Exception ex) {
127.225 - return ex.getClass().getName();
127.226 - }
127.227 - }
127.228 -
127.229 - @Compare public String componentGetNameForObjectArray() {
127.230 - return (new Object[3]).getClass().getComponentType().getName();
127.231 - }
127.232 - @Compare public boolean sameComponentGetNameForObjectArray() {
127.233 - return (new Object[3]).getClass().getComponentType() == Object.class;
127.234 - }
127.235 - @Compare public String componentGetNameForSimpleIntArray() {
127.236 - return (new int[3]).getClass().getComponentType().getName();
127.237 - }
127.238 - @Compare public String componentGetNameForMultiIntArray() {
127.239 - return (new int[3][4][5][6][7][8][9]).getClass().getComponentType().getName();
127.240 - }
127.241 - @Compare public String componentGetNameForMultiStringArray() {
127.242 - Class<?> c = (new String[3][4][5][6][7][8][9]).getClass();
127.243 - StringBuilder sb = new StringBuilder();
127.244 - for (;;) {
127.245 - sb.append(c.getName()).append("\n");
127.246 - c = c.getComponentType();
127.247 - if (c == null) {
127.248 - break;
127.249 - }
127.250 - }
127.251 - return sb.toString();
127.252 - }
127.253 -
127.254 - @Compare public boolean isArray() {
127.255 - return new Object[0].getClass().isArray();
127.256 - }
127.257 -
127.258 - @JavaScriptBody(args = { "arr", "len" }, body="var a = arr.slice(0, len); a.sort(); return a;")
127.259 - private static String[] sort(String[] arr, int len) {
127.260 - List<String> list = Arrays.asList(arr).subList(0, len);
127.261 - Collections.sort(list);
127.262 - return list.toArray(new String[0]);
127.263 - }
127.264 -
127.265 - @JavaScriptBody(args = {}, body = "return new Object();")
127.266 - private static Object newObject() {
127.267 - return new Object();
127.268 - }
127.269 -
127.270 - @Factory
127.271 - public static Object[] create() {
127.272 - return VMTest.create(ReflectionTest.class);
127.273 - }
127.274 -
127.275 -}
128.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ResourcesTest.java Fri May 24 16:45:32 2013 +0200
128.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
128.3 @@ -1,45 +0,0 @@
128.4 -/**
128.5 - * Back 2 Browser Bytecode Translator
128.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
128.7 - *
128.8 - * This program is free software: you can redistribute it and/or modify
128.9 - * it under the terms of the GNU General Public License as published by
128.10 - * the Free Software Foundation, version 2 of the License.
128.11 - *
128.12 - * This program is distributed in the hope that it will be useful,
128.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
128.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
128.15 - * GNU General Public License for more details.
128.16 - *
128.17 - * You should have received a copy of the GNU General Public License
128.18 - * along with this program. Look for COPYING file in the top folder.
128.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
128.20 - */
128.21 -package org.apidesign.bck2brwsr.tck;
128.22 -
128.23 -import java.io.InputStream;
128.24 -import org.apidesign.bck2brwsr.vmtest.Compare;
128.25 -import org.apidesign.bck2brwsr.vmtest.VMTest;
128.26 -import org.testng.annotations.Factory;
128.27 -
128.28 -/**
128.29 - *
128.30 - * @author Jaroslav Tulach <jtulach@netbeans.org>
128.31 - */
128.32 -public class ResourcesTest {
128.33 -
128.34 - @Compare public String readResourceAsStream() throws Exception {
128.35 - InputStream is = getClass().getResourceAsStream("Resources.txt");
128.36 - byte[] b = new byte[30];
128.37 - int len = is.read(b);
128.38 - StringBuilder sb = new StringBuilder();
128.39 - for (int i = 0; i < len; i++) {
128.40 - sb.append((char)b[i]);
128.41 - }
128.42 - return sb.toString();
128.43 - }
128.44 -
128.45 - @Factory public static Object[] create() {
128.46 - return VMTest.create(ResourcesTest.class);
128.47 - }
128.48 -}
129.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/ShortArithmeticTest.java Fri May 24 16:45:32 2013 +0200
129.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
129.3 @@ -1,102 +0,0 @@
129.4 -/**
129.5 - * Back 2 Browser Bytecode Translator
129.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
129.7 - *
129.8 - * This program is free software: you can redistribute it and/or modify
129.9 - * it under the terms of the GNU General Public License as published by
129.10 - * the Free Software Foundation, version 2 of the License.
129.11 - *
129.12 - * This program is distributed in the hope that it will be useful,
129.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
129.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
129.15 - * GNU General Public License for more details.
129.16 - *
129.17 - * You should have received a copy of the GNU General Public License
129.18 - * along with this program. Look for COPYING file in the top folder.
129.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
129.20 - */
129.21 -package org.apidesign.bck2brwsr.tck;
129.22 -
129.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
129.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
129.25 -import org.testng.annotations.Factory;
129.26 -
129.27 -/**
129.28 - *
129.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
129.30 - */
129.31 -public class ShortArithmeticTest {
129.32 -
129.33 - private static short add(short x, short y) {
129.34 - return (short)(x + y);
129.35 - }
129.36 -
129.37 - private static short sub(short x, short y) {
129.38 - return (short)(x - y);
129.39 - }
129.40 -
129.41 - private static short mul(short x, short y) {
129.42 - return (short)(x * y);
129.43 - }
129.44 -
129.45 - private static short div(short x, short y) {
129.46 - return (short)(x / y);
129.47 - }
129.48 -
129.49 - private static short mod(short x, short y) {
129.50 - return (short)(x % y);
129.51 - }
129.52 -
129.53 - @Compare public short conversion() {
129.54 - return (short)123456;
129.55 - }
129.56 -
129.57 - @Compare public short addOverflow() {
129.58 - return add(Short.MAX_VALUE, (short)1);
129.59 - }
129.60 -
129.61 - @Compare public short subUnderflow() {
129.62 - return sub(Short.MIN_VALUE, (short)1);
129.63 - }
129.64 -
129.65 - @Compare public short addMaxShortAndMaxShort() {
129.66 - return add(Short.MAX_VALUE, Short.MAX_VALUE);
129.67 - }
129.68 -
129.69 - @Compare public short subMinShortAndMinShort() {
129.70 - return sub(Short.MIN_VALUE, Short.MIN_VALUE);
129.71 - }
129.72 -
129.73 - @Compare public short multiplyMaxShort() {
129.74 - return mul(Short.MAX_VALUE, (short)2);
129.75 - }
129.76 -
129.77 - @Compare public short multiplyMaxShortAndMaxShort() {
129.78 - return mul(Short.MAX_VALUE, Short.MAX_VALUE);
129.79 - }
129.80 -
129.81 - @Compare public short multiplyMinShort() {
129.82 - return mul(Short.MIN_VALUE, (short)2);
129.83 - }
129.84 -
129.85 - @Compare public short multiplyMinShortAndMinShort() {
129.86 - return mul(Short.MIN_VALUE, Short.MIN_VALUE);
129.87 - }
129.88 -
129.89 - @Compare public short multiplyPrecision() {
129.90 - return mul((short)17638, (short)1103);
129.91 - }
129.92 -
129.93 - @Compare public short division() {
129.94 - return div((short)1, (short)2);
129.95 - }
129.96 -
129.97 - @Compare public short divisionReminder() {
129.98 - return mod((short)1, (short)2);
129.99 - }
129.100 -
129.101 - @Factory
129.102 - public static Object[] create() {
129.103 - return VMTest.create(ShortArithmeticTest.class);
129.104 - }
129.105 -}
130.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUse.java Fri May 24 16:45:32 2013 +0200
130.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
130.3 @@ -1,39 +0,0 @@
130.4 -/**
130.5 - * Back 2 Browser Bytecode Translator
130.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
130.7 - *
130.8 - * This program is free software: you can redistribute it and/or modify
130.9 - * it under the terms of the GNU General Public License as published by
130.10 - * the Free Software Foundation, version 2 of the License.
130.11 - *
130.12 - * This program is distributed in the hope that it will be useful,
130.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
130.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130.15 - * GNU General Public License for more details.
130.16 - *
130.17 - * You should have received a copy of the GNU General Public License
130.18 - * along with this program. Look for COPYING file in the top folder.
130.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
130.20 - */
130.21 -package org.apidesign.bck2brwsr.tck;
130.22 -
130.23 -class StaticUse {
130.24 - public static final Object NON_NULL = new Object();
130.25 - public static int cnt;
130.26 - static {
130.27 - if (cnt++ != 0) {
130.28 - throw new IllegalStateException("Multiple initialization of a <cinit>");
130.29 - }
130.30 - }
130.31 -
130.32 - StaticUse() {
130.33 - }
130.34 -
130.35 - public void instanceMethod() {
130.36 - throw new IllegalStateException();
130.37 - }
130.38 -
130.39 - public static int plus(int a, int b) {
130.40 - return a + b;
130.41 - }
130.42 -}
131.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSub.java Fri May 24 16:45:32 2013 +0200
131.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
131.3 @@ -1,27 +0,0 @@
131.4 -/**
131.5 - * Back 2 Browser Bytecode Translator
131.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
131.7 - *
131.8 - * This program is free software: you can redistribute it and/or modify
131.9 - * it under the terms of the GNU General Public License as published by
131.10 - * the Free Software Foundation, version 2 of the License.
131.11 - *
131.12 - * This program is distributed in the hope that it will be useful,
131.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
131.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131.15 - * GNU General Public License for more details.
131.16 - *
131.17 - * You should have received a copy of the GNU General Public License
131.18 - * along with this program. Look for COPYING file in the top folder.
131.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
131.20 - */
131.21 -package org.apidesign.bck2brwsr.tck;
131.22 -
131.23 -public class StaticUseSub extends StaticUse {
131.24 - private StaticUseSub() {
131.25 - }
131.26 -
131.27 - public static Object getNonNull() {
131.28 - return NON_NULL;
131.29 - }
131.30 -}
132.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/StaticUseSubTest.java Fri May 24 16:45:32 2013 +0200
132.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
132.3 @@ -1,37 +0,0 @@
132.4 -/**
132.5 - * Back 2 Browser Bytecode Translator
132.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
132.7 - *
132.8 - * This program is free software: you can redistribute it and/or modify
132.9 - * it under the terms of the GNU General Public License as published by
132.10 - * the Free Software Foundation, version 2 of the License.
132.11 - *
132.12 - * This program is distributed in the hope that it will be useful,
132.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
132.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132.15 - * GNU General Public License for more details.
132.16 - *
132.17 - * You should have received a copy of the GNU General Public License
132.18 - * along with this program. Look for COPYING file in the top folder.
132.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
132.20 - */
132.21 -package org.apidesign.bck2brwsr.tck;
132.22 -
132.23 -import org.apidesign.bck2brwsr.vmtest.Compare;
132.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
132.25 -import org.testng.annotations.Factory;
132.26 -
132.27 -/**
132.28 - *
132.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
132.30 - */
132.31 -public class StaticUseSubTest {
132.32 - @Compare public String staticFieldInitializationInSuperClass() throws Exception {
132.33 - Object ret = StaticUseSub.getNonNull();
132.34 - return ret.getClass().getName();
132.35 - }
132.36 -
132.37 - @Factory public static Object[] create() {
132.38 - return VMTest.create(StaticUseSubTest.class);
132.39 - }
132.40 -}
133.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CRC32Test.java Fri May 24 16:45:32 2013 +0200
133.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
133.3 @@ -1,41 +0,0 @@
133.4 -/**
133.5 - * Back 2 Browser Bytecode Translator
133.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
133.7 - *
133.8 - * This program is free software: you can redistribute it and/or modify
133.9 - * it under the terms of the GNU General Public License as published by
133.10 - * the Free Software Foundation, version 2 of the License.
133.11 - *
133.12 - * This program is distributed in the hope that it will be useful,
133.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
133.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133.15 - * GNU General Public License for more details.
133.16 - *
133.17 - * You should have received a copy of the GNU General Public License
133.18 - * along with this program. Look for COPYING file in the top folder.
133.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
133.20 - */
133.21 -package org.apidesign.bck2brwsr.vmtest.impl;
133.22 -
133.23 -import java.io.UnsupportedEncodingException;
133.24 -import java.util.zip.CRC32;
133.25 -import org.apidesign.bck2brwsr.vmtest.Compare;
133.26 -import org.apidesign.bck2brwsr.vmtest.VMTest;
133.27 -import org.testng.annotations.Factory;
133.28 -
133.29 -/**
133.30 - *
133.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
133.32 - */
133.33 -public class CRC32Test {
133.34 -
133.35 - @Compare public long crc1() throws UnsupportedEncodingException {
133.36 - CRC32 crc = new CRC32();
133.37 - crc.update("Hello World!".getBytes("UTF-8"));
133.38 - return crc.getValue();
133.39 - }
133.40 -
133.41 - @Factory public static Object[] create() {
133.42 - return VMTest.create(CRC32Test.class);
133.43 - }
133.44 -}
134.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/CallMeTwiceTest.java Fri May 24 16:45:32 2013 +0200
134.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
134.3 @@ -1,43 +0,0 @@
134.4 -/**
134.5 - * Back 2 Browser Bytecode Translator
134.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
134.7 - *
134.8 - * This program is free software: you can redistribute it and/or modify
134.9 - * it under the terms of the GNU General Public License as published by
134.10 - * the Free Software Foundation, version 2 of the License.
134.11 - *
134.12 - * This program is distributed in the hope that it will be useful,
134.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
134.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134.15 - * GNU General Public License for more details.
134.16 - *
134.17 - * You should have received a copy of the GNU General Public License
134.18 - * along with this program. Look for COPYING file in the top folder.
134.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
134.20 - */
134.21 -package org.apidesign.bck2brwsr.vmtest.impl;
134.22 -
134.23 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
134.24 -import org.apidesign.bck2brwsr.vmtest.VMTest;
134.25 -import org.testng.annotations.Factory;
134.26 -
134.27 -/**
134.28 - *
134.29 - * @author Jaroslav Tulach <jtulach@netbeans.org>
134.30 - */
134.31 -public class CallMeTwiceTest {
134.32 - int cnt;
134.33 -
134.34 - @BrwsrTest public void callMeTwice() throws InterruptedException {
134.35 - if (cnt++ == 0) {
134.36 - throw new InterruptedException();
134.37 - }
134.38 - int prevCnt = cnt;
134.39 - cnt = 0;
134.40 - assert prevCnt == 2 : "We need to receive two calls " + prevCnt;
134.41 - }
134.42 -
134.43 - @Factory public static Object[] create() {
134.44 - return VMTest.create(CallMeTwiceTest.class);
134.45 - }
134.46 -}
135.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipEntryTest.java Fri May 24 16:45:32 2013 +0200
135.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
135.3 @@ -1,67 +0,0 @@
135.4 -/**
135.5 - * Back 2 Browser Bytecode Translator
135.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
135.7 - *
135.8 - * This program is free software: you can redistribute it and/or modify
135.9 - * it under the terms of the GNU General Public License as published by
135.10 - * the Free Software Foundation, version 2 of the License.
135.11 - *
135.12 - * This program is distributed in the hope that it will be useful,
135.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
135.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
135.15 - * GNU General Public License for more details.
135.16 - *
135.17 - * You should have received a copy of the GNU General Public License
135.18 - * along with this program. Look for COPYING file in the top folder.
135.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
135.20 - */
135.21 -package org.apidesign.bck2brwsr.vmtest.impl;
135.22 -
135.23 -import java.io.ByteArrayInputStream;
135.24 -import java.io.IOException;
135.25 -import java.io.InputStream;
135.26 -import org.apidesign.bck2brwsr.emul.zip.FastJar;
135.27 -import org.testng.annotations.Test;
135.28 -import static org.testng.Assert.*;
135.29 -
135.30 -/**
135.31 - *
135.32 - * @author Jaroslav Tulach <jtulach@netbeans.org>
135.33 - */
135.34 -@GenerateZip(name = "five.zip", contents = {
135.35 - "1.txt", "one",
135.36 - "2.txt", "duo",
135.37 - "3.txt", "three",
135.38 - "4.txt", "four",
135.39 - "5.txt", "five"
135.40 -})
135.41 -public class ZipEntryTest {
135.42 - @Test
135.43 - public void readEntriesEffectively() throws IOException {
135.44 - InputStream is = ZipEntryTest.class.getResourceAsStream("five.zip");
135.45 - byte[] arr = new byte[is.available()];
135.46 - int len = is.read(arr);
135.47 - assertEquals(len, arr.length, "Read fully");
135.48 -
135.49 - FastJar fj = new FastJar(arr);
135.50 - FastJar.Entry[] entrs = fj.list();
135.51 -
135.52 - assertEquals(5, entrs.length, "Five entries");
135.53 -
135.54 - for (int i = 1; i <= 5; i++) {
135.55 - FastJar.Entry en = entrs[i - 1];
135.56 - assertEquals(en.name, i + ".txt");
135.57 -// assertEquals(cis.cnt, 0, "Content of the file should be skipped, not read");
135.58 - }
135.59 -
135.60 - assertContent("three", fj.getInputStream(entrs[3 - 1]), "read OK");
135.61 - assertContent("five", fj.getInputStream(entrs[5 - 1]), "read OK");
135.62 - }
135.63 -
135.64 - private static void assertContent(String exp, InputStream is, String msg) throws IOException {
135.65 - byte[] arr = new byte[512];
135.66 - int len = is.read(arr);
135.67 - String s = new String(arr, 0, len);
135.68 - assertEquals(exp, s, msg);
135.69 - }
135.70 -}
136.1 --- a/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Fri May 24 16:45:32 2013 +0200
136.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
136.3 @@ -1,108 +0,0 @@
136.4 -/**
136.5 - * Back 2 Browser Bytecode Translator
136.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
136.7 - *
136.8 - * This program is free software: you can redistribute it and/or modify
136.9 - * it under the terms of the GNU General Public License as published by
136.10 - * the Free Software Foundation, version 2 of the License.
136.11 - *
136.12 - * This program is distributed in the hope that it will be useful,
136.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
136.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136.15 - * GNU General Public License for more details.
136.16 - *
136.17 - * You should have received a copy of the GNU General Public License
136.18 - * along with this program. Look for COPYING file in the top folder.
136.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
136.20 - */
136.21 -package org.apidesign.bck2brwsr.vmtest.impl;
136.22 -
136.23 -import java.io.IOException;
136.24 -import java.io.InputStream;
136.25 -import java.util.Objects;
136.26 -import java.util.zip.ZipEntry;
136.27 -import java.util.zip.ZipInputStream;
136.28 -import org.apidesign.bck2brwsr.core.JavaScriptBody;
136.29 -import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
136.30 -import org.apidesign.bck2brwsr.vmtest.Compare;
136.31 -import org.apidesign.bck2brwsr.vmtest.Http;
136.32 -import org.apidesign.bck2brwsr.vmtest.VMTest;
136.33 -import org.testng.annotations.Factory;
136.34 -
136.35 -/**
136.36 - *
136.37 - * @author Jaroslav Tulach <jtulach@netbeans.org>
136.38 - */
136.39 -@GenerateZip(name = "readAnEntry.zip", contents = {
136.40 - "my/main/file.txt", "Hello World!"
136.41 -})
136.42 -public class ZipFileTest {
136.43 -
136.44 - @Compare public String readAnEntry() throws IOException {
136.45 - InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip");
136.46 - ZipInputStream zip = new ZipInputStream(is);
136.47 - ZipEntry entry = zip.getNextEntry();
136.48 - assertEquals(entry.getName(), "my/main/file.txt", "Correct entry");
136.49 -
136.50 - byte[] arr = new byte[4096];
136.51 - int len = zip.read(arr);
136.52 -
136.53 - assertEquals(zip.getNextEntry(), null, "No next entry");
136.54 -
136.55 - final String ret = new String(arr, 0, len, "UTF-8");
136.56 - return ret;
136.57 - }
136.58 -
136.59 - @JavaScriptBody(args = { "res", "path" }, body =
136.60 - "var myvm = bck2brwsr.apply(null, path);\n"
136.61 - + "var cls = myvm.loadClass('java.lang.String');\n"
136.62 - + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
136.63 - )
136.64 - private static native Object loadVMResource(String res, String...path);
136.65 -
136.66 - @Http({
136.67 - @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
136.68 - })
136.69 - @BrwsrTest public void canVmLoadResourceFromZip() throws IOException {
136.70 - Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
136.71 - assert res instanceof InputStream : "Got array of bytes: " + res;
136.72 - InputStream is = (InputStream)res;
136.73 -
136.74 - byte[] arr = new byte[4096];
136.75 - int len = is.read(arr);
136.76 -
136.77 - final String ret = new String(arr, 0, len, "UTF-8");
136.78 -
136.79 - assertEquals(ret, "Hello World!", "Can read the bytes");
136.80 - }
136.81 -
136.82 - @GenerateZip(name = "cpattr.zip", contents = {
136.83 - "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
136.84 - + "Created-By: hand\n"
136.85 - + "Class-Path: realJar.jar\n\n\n"
136.86 - })
136.87 - @Http({
136.88 - @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
136.89 - @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
136.90 - })
136.91 - @BrwsrTest public void understandsClassPathAttr() throws IOException {
136.92 - Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
136.93 - assert res instanceof InputStream : "Got array of bytes: " + res;
136.94 - InputStream is = (InputStream)res;
136.95 -
136.96 - byte[] arr = new byte[4096];
136.97 - int len = is.read(arr);
136.98 -
136.99 - final String ret = new String(arr, 0, len, "UTF-8");
136.100 -
136.101 - assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
136.102 - }
136.103 -
136.104 - private static void assertEquals(Object real, Object exp, String msg) {
136.105 - assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
136.106 - }
136.107 -
136.108 - @Factory public static Object[] create() {
136.109 - return VMTest.create(ZipFileTest.class);
136.110 - }
136.111 -}
137.1 --- a/rt/vmtest/src/test/resources/org/apidesign/bck2brwsr/tck/0xfe Fri May 24 16:45:32 2013 +0200
137.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
137.3 @@ -1,1 +0,0 @@
137.4 -þ
137.5 \ No newline at end of file
138.1 --- a/rt/vmtest/src/test/resources/org/apidesign/bck2brwsr/tck/Resources.txt Fri May 24 16:45:32 2013 +0200
138.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
138.3 @@ -1,1 +0,0 @@
138.4 -Ahoj