Test can specify multiple HTTP resources emul
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 05 Feb 2013 08:48:23 +0100
branchemul
changeset 6675866e89ef568
parent 666 8338ab1991e6
child 670 3026d9c844f0
Test can specify multiple HTTP resources
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java
launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/HttpResource.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java
vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java
vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java
vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Tue Feb 05 08:27:42 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java	Tue Feb 05 08:48:23 2013 +0100
     1.3 @@ -39,6 +39,7 @@
     1.4  import java.util.concurrent.TimeUnit;
     1.5  import java.util.logging.Level;
     1.6  import java.util.logging.Logger;
     1.7 +import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource;
     1.8  import org.apidesign.vm4brwsr.Bck2Brwsr;
     1.9  import org.glassfish.grizzly.PortRange;
    1.10  import org.glassfish.grizzly.http.server.HttpHandler;
    1.11 @@ -152,11 +153,13 @@
    1.12          class DynamicResourceHandler extends HttpHandler {
    1.13              private final InvocationContext ic;
    1.14              public DynamicResourceHandler(InvocationContext ic) {
    1.15 -                if (ic == null || ic.httpPath == null) {
    1.16 +                if (ic == null || ic.resources.isEmpty()) {
    1.17                      throw new NullPointerException();
    1.18                  }
    1.19                  this.ic = ic;
    1.20 -                conf.addHttpHandler(this, ic.httpPath);
    1.21 +                for (Resource r : ic.resources) {
    1.22 +                    conf.addHttpHandler(this, r.httpPath);
    1.23 +                }
    1.24              }
    1.25  
    1.26              public void close() {
    1.27 @@ -165,10 +168,12 @@
    1.28              
    1.29              @Override
    1.30              public void service(Request request, Response response) throws Exception {
    1.31 -                if (ic.httpPath.equals(request.getRequestURI())) {
    1.32 -                    LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
    1.33 -                    response.setContentType(ic.httpType);
    1.34 -                    copyStream(ic.httpContent, response.getOutputStream(), null);
    1.35 +                for (Resource r : ic.resources) {
    1.36 +                    if (r.httpPath.equals(request.getRequestURI())) {
    1.37 +                        LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI());
    1.38 +                        response.setContentType(r.httpType);
    1.39 +                        copyStream(r.httpContent, response.getOutputStream(), null);
    1.40 +                    }
    1.41                  }
    1.42              }
    1.43          }
    1.44 @@ -206,7 +211,7 @@
    1.45                      return;
    1.46                  }
    1.47                  
    1.48 -                if (mi.httpPath != null) {
    1.49 +                if (!mi.resources.isEmpty()) {
    1.50                      prev = new DynamicResourceHandler(mi);
    1.51                  }
    1.52                  
     2.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Tue Feb 05 08:27:42 2013 +0100
     2.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java	Tue Feb 05 08:48:23 2013 +0100
     2.3 @@ -19,6 +19,8 @@
     2.4  
     2.5  import java.io.IOException;
     2.6  import java.io.InputStream;
     2.7 +import java.util.ArrayList;
     2.8 +import java.util.List;
     2.9  import java.util.concurrent.CountDownLatch;
    2.10  import java.util.concurrent.TimeUnit;
    2.11  
    2.12 @@ -34,9 +36,7 @@
    2.13      private String result;
    2.14      private Throwable exception;
    2.15      String html;
    2.16 -    InputStream httpContent;
    2.17 -    String httpType;
    2.18 -    String httpPath;
    2.19 +    final List<Resource> resources = new ArrayList<>();
    2.20  
    2.21      InvocationContext(Launcher launcher, Class<?> clazz, String methodName) {
    2.22          this.launcher = launcher;
    2.23 @@ -55,13 +55,11 @@
    2.24      /** HTTP resource to be available during execution. An invocation may
    2.25       * perform an HTTP query and obtain a resource relative to the page.
    2.26       */
    2.27 -    public void setHttpResource(String relativePath, String mimeType, InputStream content) {
    2.28 +    public void addHttpResource(String relativePath, String mimeType, InputStream content) {
    2.29          if (relativePath == null || mimeType == null || content == null) {
    2.30              throw new NullPointerException();
    2.31          }
    2.32 -        this.httpPath = relativePath;
    2.33 -        this.httpType = mimeType;
    2.34 -        this.httpContent = content;
    2.35 +        resources.add(new Resource(content, mimeType, relativePath));
    2.36      }
    2.37      
    2.38      /** Invokes the associated method. 
    2.39 @@ -97,5 +95,16 @@
    2.40          wait.countDown();
    2.41      }
    2.42  
    2.43 -    
    2.44 +
    2.45 +    static final class Resource {
    2.46 +        final InputStream httpContent;
    2.47 +        final String httpType;
    2.48 +        final String httpPath;
    2.49 +
    2.50 +        Resource(InputStream httpContent, String httpType, String httpPath) {
    2.51 +            this.httpContent = httpContent;
    2.52 +            this.httpType = httpType;
    2.53 +            this.httpPath = httpPath;
    2.54 +        }
    2.55 +    }
    2.56  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java	Tue Feb 05 08:48:23 2013 +0100
     3.3 @@ -0,0 +1,56 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.bck2brwsr.vmtest;
    3.22 +
    3.23 +import java.lang.annotation.ElementType;
    3.24 +import java.lang.annotation.Retention;
    3.25 +import java.lang.annotation.RetentionPolicy;
    3.26 +import java.lang.annotation.Target;
    3.27 +
    3.28 +/**
    3.29 + * Exposes HTTP page or pages to the running {@link BrwsrTest}, so it can access under
    3.30 + * the relative path.
    3.31 + *
    3.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.33 + */
    3.34 +@Retention(RetentionPolicy.RUNTIME)
    3.35 +@Target({ElementType.METHOD, ElementType.TYPE})
    3.36 +public @interface Http {
    3.37 +    /** Set of pages to make available */
    3.38 +    public Resource[] value();
    3.39 +    
    3.40 +    /** Exposes an HTTP page to the running {@link BrwsrTest}, so it can access
    3.41 +     * under the relative path.
    3.42 +     *
    3.43 +     * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.44 +     */
    3.45 +    @Retention(RetentionPolicy.RUNTIME)
    3.46 +    @Target({})
    3.47 +    public @interface Resource {
    3.48 +        /** path on the server that the test can use to access the exposed resource */
    3.49 +        String path();
    3.50 +        /** the content of the HttpResource */
    3.51 +        String content();
    3.52 +        /** resource relative to the class that should be used instead of <code>content</code>.
    3.53 +         * Leave content equal to empty string.
    3.54 +         */
    3.55 +        String resource() default "";
    3.56 +        /** mime type of the resource */
    3.57 +        String mimeType();
    3.58 +    }
    3.59 +}
     4.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/HttpResource.java	Tue Feb 05 08:27:42 2013 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,43 +0,0 @@
     4.4 -/**
     4.5 - * Back 2 Browser Bytecode Translator
     4.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 - *
     4.8 - * This program is free software: you can redistribute it and/or modify
     4.9 - * it under the terms of the GNU General Public License as published by
    4.10 - * the Free Software Foundation, version 2 of the License.
    4.11 - *
    4.12 - * This program is distributed in the hope that it will be useful,
    4.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 - * GNU General Public License for more details.
    4.16 - *
    4.17 - * You should have received a copy of the GNU General Public License
    4.18 - * along with this program. Look for COPYING file in the top folder.
    4.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 - */
    4.21 -package org.apidesign.bck2brwsr.vmtest;
    4.22 -
    4.23 -import java.lang.annotation.ElementType;
    4.24 -import java.lang.annotation.Retention;
    4.25 -import java.lang.annotation.RetentionPolicy;
    4.26 -import java.lang.annotation.Target;
    4.27 -
    4.28 -/** Exposes an HTTP page to the running {@link BrwsrTest}, so it can access
    4.29 - * under the relative path.
    4.30 - *
    4.31 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.32 - */
    4.33 -@Retention(RetentionPolicy.RUNTIME)
    4.34 -@Target({ ElementType.METHOD, ElementType.TYPE})
    4.35 -public @interface HttpResource {
    4.36 -    /** path on the server that the test can use to access the exposed resource */
    4.37 -    String path();
    4.38 -    /** the content of the HttpResource */
    4.39 -    String content();
    4.40 -    /** resource relative to the class that should be used instead of <code>content</code>.
    4.41 -     * Leave content equal to empty string.
    4.42 -     */
    4.43 -    String resource() default "";
    4.44 -    /** mime type of the resource */
    4.45 -    String mimeType();
    4.46 -}
     5.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Tue Feb 05 08:27:42 2013 +0100
     5.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java	Tue Feb 05 08:48:23 2013 +0100
     5.3 @@ -28,7 +28,7 @@
     5.4  import org.apidesign.bck2brwsr.launcher.Launcher;
     5.5  import org.apidesign.bck2brwsr.launcher.InvocationContext;
     5.6  import org.apidesign.bck2brwsr.vmtest.HtmlFragment;
     5.7 -import org.apidesign.bck2brwsr.vmtest.HttpResource;
     5.8 +import org.apidesign.bck2brwsr.vmtest.Http;
     5.9  import org.testng.ITest;
    5.10  import org.testng.annotations.Test;
    5.11  
    5.12 @@ -42,10 +42,10 @@
    5.13      private final String type;
    5.14      private final boolean fail;
    5.15      private final HtmlFragment html;
    5.16 -    private final HttpResource http;
    5.17 +    private final Http.Resource[] http;
    5.18      Object value;
    5.19  
    5.20 -    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, HttpResource http) {
    5.21 +    Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) {
    5.22          this.l = l;
    5.23          this.m = m;
    5.24          this.type = type;
    5.25 @@ -62,12 +62,14 @@
    5.26                  c.setHtmlFragment(html.value());
    5.27              }
    5.28              if (http != null) {
    5.29 -                if (!http.content().isEmpty()) {
    5.30 -                    InputStream is = new ByteArrayInputStream(http.content().getBytes("UTF-8"));
    5.31 -                    c.setHttpResource(http.path(), http.mimeType(), is);
    5.32 -                } else {
    5.33 -                    InputStream is = m.getDeclaringClass().getResourceAsStream(http.resource());
    5.34 -                    c.setHttpResource(http.path(), http.mimeType(), is);
    5.35 +                for (Http.Resource r : http) {
    5.36 +                    if (!r.content().isEmpty()) {
    5.37 +                        InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8"));
    5.38 +                        c.addHttpResource(r.path(), r.mimeType(), is);
    5.39 +                    } else {
    5.40 +                        InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource());
    5.41 +                        c.addHttpResource(r.path(), r.mimeType(), is);
    5.42 +                    }
    5.43                  }
    5.44              }
    5.45              String res = c.invoke();
     6.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java	Tue Feb 05 08:27:42 2013 +0100
     6.2 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java	Tue Feb 05 08:48:23 2013 +0100
     6.3 @@ -135,9 +135,15 @@
     6.4          if (f == null) {
     6.5              f = m.getDeclaringClass().getAnnotation(HtmlFragment.class);
     6.6          }
     6.7 -        HttpResource r = m.getAnnotation(HttpResource.class);
     6.8 -        if (r == null) {
     6.9 -            r = m.getDeclaringClass().getAnnotation(HttpResource.class);
    6.10 +        Http.Resource[] r = {};
    6.11 +        Http h = m.getAnnotation(Http.class);
    6.12 +        if (h == null) {
    6.13 +            h = m.getDeclaringClass().getAnnotation(Http.class);
    6.14 +            if (h != null) {
    6.15 +                r = h.value();
    6.16 +            }
    6.17 +        } else {
    6.18 +            r = h.value();
    6.19          }
    6.20          if (brwsr.length == 0) {
    6.21              final Launcher s = l.brwsr(null);
     7.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java	Tue Feb 05 08:27:42 2013 +0100
     7.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java	Tue Feb 05 08:48:23 2013 +0100
     7.3 @@ -21,7 +21,7 @@
     7.4  import java.net.URL;
     7.5  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     7.6  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
     7.7 -import org.apidesign.bck2brwsr.vmtest.HttpResource;
     7.8 +import org.apidesign.bck2brwsr.vmtest.Http;
     7.9  import org.apidesign.bck2brwsr.vmtest.VMTest;
    7.10  import org.testng.annotations.Factory;
    7.11  
    7.12 @@ -31,15 +31,21 @@
    7.13   */
    7.14  public class HttpResourceTest {
    7.15      
    7.16 -    @HttpResource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
    7.17 +    @Http({
    7.18 +        @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain")
    7.19 +    })
    7.20      @BrwsrTest
    7.21 +    
    7.22 +    
    7.23      public String testReadContentViaXHR() throws Exception {
    7.24          String msg = read("/xhr");
    7.25          assert "Hello Brwsr!".equals(msg) : "The message was " + msg;
    7.26          return msg;
    7.27      }
    7.28  
    7.29 -    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    7.30 +    @Http({
    7.31 +        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    7.32 +    })
    7.33      @BrwsrTest
    7.34      public String testReadContentViaURL() throws Exception {
    7.35          URL url = new URL("http:/url");
    7.36 @@ -47,7 +53,9 @@
    7.37          assert "Hello via URL!".equals(msg) : "The message was " + msg;
    7.38          return msg;
    7.39      }
    7.40 -    @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    7.41 +    @Http({
    7.42 +        @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain")
    7.43 +    })
    7.44      @BrwsrTest
    7.45      public String testReadContentViaURLWithStringParam() throws Exception {
    7.46          URL url = new URL("http:/url");
    7.47 @@ -56,7 +64,9 @@
    7.48          return msg;
    7.49      }
    7.50  
    7.51 -    @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    7.52 +    @Http({
    7.53 +        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    7.54 +    })
    7.55      @BrwsrTest
    7.56      public void testReadByte() throws Exception {
    7.57          URL url = new URL("http:/bytes");
    7.58 @@ -67,7 +77,9 @@
    7.59          assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]);
    7.60      }
    7.61  
    7.62 -    @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    7.63 +    @Http({
    7.64 +        @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary")
    7.65 +    })
    7.66      @BrwsrTest
    7.67      public void testReadByteViaInputStream() throws Exception {
    7.68          URL url = new URL("http:/bytes");
     8.1 --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Tue Feb 05 08:27:42 2013 +0100
     8.2 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Tue Feb 05 08:48:23 2013 +0100
     8.3 @@ -25,7 +25,7 @@
     8.4  import org.apidesign.bck2brwsr.core.JavaScriptBody;
     8.5  import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
     8.6  import org.apidesign.bck2brwsr.vmtest.Compare;
     8.7 -import org.apidesign.bck2brwsr.vmtest.HttpResource;
     8.8 +import org.apidesign.bck2brwsr.vmtest.Http;
     8.9  import org.apidesign.bck2brwsr.vmtest.VMTest;
    8.10  import org.testng.annotations.Factory;
    8.11  
    8.12 @@ -60,7 +60,9 @@
    8.13      )
    8.14      private static native Object loadVMResource(String res, String...path);
    8.15  
    8.16 -    @HttpResource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
    8.17 +    @Http({
    8.18 +        @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
    8.19 +    })
    8.20      @BrwsrTest  public void canVmLoadResourceFromZip() throws IOException {
    8.21          Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
    8.22          assert res instanceof InputStream : "Got array of bytes: " + res;