# HG changeset patch # User Jaroslav Tulach # Date 1360050503 -3600 # Node ID 5866e89ef5685e790eced68470276a4f8b41f555 # Parent 8338ab1991e6a7687233015f981072f969f2a60a Test can specify multiple HTTP resources diff -r 8338ab1991e6 -r 5866e89ef568 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Feb 05 08:27:42 2013 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/Bck2BrwsrLauncher.java Tue Feb 05 08:48:23 2013 +0100 @@ -39,6 +39,7 @@ import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; +import org.apidesign.bck2brwsr.launcher.InvocationContext.Resource; import org.apidesign.vm4brwsr.Bck2Brwsr; import org.glassfish.grizzly.PortRange; import org.glassfish.grizzly.http.server.HttpHandler; @@ -152,11 +153,13 @@ class DynamicResourceHandler extends HttpHandler { private final InvocationContext ic; public DynamicResourceHandler(InvocationContext ic) { - if (ic == null || ic.httpPath == null) { + if (ic == null || ic.resources.isEmpty()) { throw new NullPointerException(); } this.ic = ic; - conf.addHttpHandler(this, ic.httpPath); + for (Resource r : ic.resources) { + conf.addHttpHandler(this, r.httpPath); + } } public void close() { @@ -165,10 +168,12 @@ @Override public void service(Request request, Response response) throws Exception { - if (ic.httpPath.equals(request.getRequestURI())) { - LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI()); - response.setContentType(ic.httpType); - copyStream(ic.httpContent, response.getOutputStream(), null); + for (Resource r : ic.resources) { + if (r.httpPath.equals(request.getRequestURI())) { + LOG.log(Level.INFO, "Serving HttpResource for {0}", request.getRequestURI()); + response.setContentType(r.httpType); + copyStream(r.httpContent, response.getOutputStream(), null); + } } } } @@ -206,7 +211,7 @@ return; } - if (mi.httpPath != null) { + if (!mi.resources.isEmpty()) { prev = new DynamicResourceHandler(mi); } diff -r 8338ab1991e6 -r 5866e89ef568 launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java --- a/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Tue Feb 05 08:27:42 2013 +0100 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/launcher/InvocationContext.java Tue Feb 05 08:48:23 2013 +0100 @@ -19,6 +19,8 @@ import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -34,9 +36,7 @@ private String result; private Throwable exception; String html; - InputStream httpContent; - String httpType; - String httpPath; + final List resources = new ArrayList<>(); InvocationContext(Launcher launcher, Class clazz, String methodName) { this.launcher = launcher; @@ -55,13 +55,11 @@ /** HTTP resource to be available during execution. An invocation may * perform an HTTP query and obtain a resource relative to the page. */ - public void setHttpResource(String relativePath, String mimeType, InputStream content) { + public void addHttpResource(String relativePath, String mimeType, InputStream content) { if (relativePath == null || mimeType == null || content == null) { throw new NullPointerException(); } - this.httpPath = relativePath; - this.httpType = mimeType; - this.httpContent = content; + resources.add(new Resource(content, mimeType, relativePath)); } /** Invokes the associated method. @@ -97,5 +95,16 @@ wait.countDown(); } - + + static final class Resource { + final InputStream httpContent; + final String httpType; + final String httpPath; + + Resource(InputStream httpContent, String httpType, String httpPath) { + this.httpContent = httpContent; + this.httpType = httpType; + this.httpPath = httpPath; + } + } } diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Http.java Tue Feb 05 08:48:23 2013 +0100 @@ -0,0 +1,56 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.vmtest; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Exposes HTTP page or pages to the running {@link BrwsrTest}, so it can access under + * the relative path. + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.TYPE}) +public @interface Http { + /** Set of pages to make available */ + public Resource[] value(); + + /** Exposes an HTTP page to the running {@link BrwsrTest}, so it can access + * under the relative path. + * + * @author Jaroslav Tulach + */ + @Retention(RetentionPolicy.RUNTIME) + @Target({}) + public @interface Resource { + /** path on the server that the test can use to access the exposed resource */ + String path(); + /** the content of the HttpResource */ + String content(); + /** resource relative to the class that should be used instead of content. + * Leave content equal to empty string. + */ + String resource() default ""; + /** mime type of the resource */ + String mimeType(); + } +} diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/HttpResource.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/HttpResource.java Tue Feb 05 08:27:42 2013 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/** - * Back 2 Browser Bytecode Translator - * Copyright (C) 2012 Jaroslav Tulach - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, version 2 of the License. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. Look for COPYING file in the top folder. - * If not, see http://opensource.org/licenses/GPL-2.0. - */ -package org.apidesign.bck2brwsr.vmtest; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** Exposes an HTTP page to the running {@link BrwsrTest}, so it can access - * under the relative path. - * - * @author Jaroslav Tulach - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ ElementType.METHOD, ElementType.TYPE}) -public @interface HttpResource { - /** path on the server that the test can use to access the exposed resource */ - String path(); - /** the content of the HttpResource */ - String content(); - /** resource relative to the class that should be used instead of content. - * Leave content equal to empty string. - */ - String resource() default ""; - /** mime type of the resource */ - String mimeType(); -} diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Tue Feb 05 08:27:42 2013 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/Bck2BrwsrCase.java Tue Feb 05 08:48:23 2013 +0100 @@ -28,7 +28,7 @@ import org.apidesign.bck2brwsr.launcher.Launcher; import org.apidesign.bck2brwsr.launcher.InvocationContext; import org.apidesign.bck2brwsr.vmtest.HtmlFragment; -import org.apidesign.bck2brwsr.vmtest.HttpResource; +import org.apidesign.bck2brwsr.vmtest.Http; import org.testng.ITest; import org.testng.annotations.Test; @@ -42,10 +42,10 @@ private final String type; private final boolean fail; private final HtmlFragment html; - private final HttpResource http; + private final Http.Resource[] http; Object value; - Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, HttpResource http) { + Bck2BrwsrCase(Method m, String type, Launcher l, boolean fail, HtmlFragment html, Http.Resource[] http) { this.l = l; this.m = m; this.type = type; @@ -62,12 +62,14 @@ c.setHtmlFragment(html.value()); } if (http != null) { - if (!http.content().isEmpty()) { - InputStream is = new ByteArrayInputStream(http.content().getBytes("UTF-8")); - c.setHttpResource(http.path(), http.mimeType(), is); - } else { - InputStream is = m.getDeclaringClass().getResourceAsStream(http.resource()); - c.setHttpResource(http.path(), http.mimeType(), is); + for (Http.Resource r : http) { + if (!r.content().isEmpty()) { + InputStream is = new ByteArrayInputStream(r.content().getBytes("UTF-8")); + c.addHttpResource(r.path(), r.mimeType(), is); + } else { + InputStream is = m.getDeclaringClass().getResourceAsStream(r.resource()); + c.addHttpResource(r.path(), r.mimeType(), is); + } } } String res = c.invoke(); diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Tue Feb 05 08:27:42 2013 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/CompareCase.java Tue Feb 05 08:48:23 2013 +0100 @@ -135,9 +135,15 @@ if (f == null) { f = m.getDeclaringClass().getAnnotation(HtmlFragment.class); } - HttpResource r = m.getAnnotation(HttpResource.class); - if (r == null) { - r = m.getDeclaringClass().getAnnotation(HttpResource.class); + Http.Resource[] r = {}; + Http h = m.getAnnotation(Http.class); + if (h == null) { + h = m.getDeclaringClass().getAnnotation(Http.class); + if (h != null) { + r = h.value(); + } + } else { + r = h.value(); } if (brwsr.length == 0) { final Launcher s = l.brwsr(null); diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Tue Feb 05 08:27:42 2013 +0100 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/tck/HttpResourceTest.java Tue Feb 05 08:48:23 2013 +0100 @@ -21,7 +21,7 @@ import java.net.URL; import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.vmtest.BrwsrTest; -import org.apidesign.bck2brwsr.vmtest.HttpResource; +import org.apidesign.bck2brwsr.vmtest.Http; import org.apidesign.bck2brwsr.vmtest.VMTest; import org.testng.annotations.Factory; @@ -31,15 +31,21 @@ */ public class HttpResourceTest { - @HttpResource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain") + @Http({ + @Http.Resource(path = "/xhr", content = "Hello Brwsr!", mimeType = "text/plain") + }) @BrwsrTest + + public String testReadContentViaXHR() throws Exception { String msg = read("/xhr"); assert "Hello Brwsr!".equals(msg) : "The message was " + msg; return msg; } - @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") + @Http({ + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") + }) @BrwsrTest public String testReadContentViaURL() throws Exception { URL url = new URL("http:/url"); @@ -47,7 +53,9 @@ assert "Hello via URL!".equals(msg) : "The message was " + msg; return msg; } - @HttpResource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") + @Http({ + @Http.Resource(path = "/url", content = "Hello via URL!", mimeType = "text/plain") + }) @BrwsrTest public String testReadContentViaURLWithStringParam() throws Exception { URL url = new URL("http:/url"); @@ -56,7 +64,9 @@ return msg; } - @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") + @Http({ + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") + }) @BrwsrTest public void testReadByte() throws Exception { URL url = new URL("http:/bytes"); @@ -67,7 +77,9 @@ assert arr[0] == 0xfe : "It is 0xfe: " + Integer.toHexString(arr[0]); } - @HttpResource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") + @Http({ + @Http.Resource(path = "/bytes", content = "", resource = "0xfe", mimeType = "x-application/binary") + }) @BrwsrTest public void testReadByteViaInputStream() throws Exception { URL url = new URL("http:/bytes"); diff -r 8338ab1991e6 -r 5866e89ef568 vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java --- a/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Tue Feb 05 08:27:42 2013 +0100 +++ b/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java Tue Feb 05 08:48:23 2013 +0100 @@ -25,7 +25,7 @@ import org.apidesign.bck2brwsr.core.JavaScriptBody; import org.apidesign.bck2brwsr.vmtest.BrwsrTest; import org.apidesign.bck2brwsr.vmtest.Compare; -import org.apidesign.bck2brwsr.vmtest.HttpResource; +import org.apidesign.bck2brwsr.vmtest.Http; import org.apidesign.bck2brwsr.vmtest.VMTest; import org.testng.annotations.Factory; @@ -60,7 +60,9 @@ ) private static native Object loadVMResource(String res, String...path); - @HttpResource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip") + @Http({ + @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip") + }) @BrwsrTest public void canVmLoadResourceFromZip() throws IOException { Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar"); assert res instanceof InputStream : "Got array of bytes: " + res;