rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java
changeset 772 d382dacfd73f
parent 729 1ee59fe94653
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vmtest/src/test/java/org/apidesign/bck2brwsr/vmtest/impl/ZipFileTest.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.vmtest.impl;
    1.22 +
    1.23 +import java.io.IOException;
    1.24 +import java.io.InputStream;
    1.25 +import java.util.Objects;
    1.26 +import java.util.zip.ZipEntry;
    1.27 +import java.util.zip.ZipInputStream;
    1.28 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
    1.29 +import org.apidesign.bck2brwsr.vmtest.BrwsrTest;
    1.30 +import org.apidesign.bck2brwsr.vmtest.Compare;
    1.31 +import org.apidesign.bck2brwsr.vmtest.Http;
    1.32 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    1.33 +import org.testng.annotations.Factory;
    1.34 +
    1.35 +/**
    1.36 + *
    1.37 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.38 + */
    1.39 +@GenerateZip(name = "readAnEntry.zip", contents = { 
    1.40 +    "my/main/file.txt", "Hello World!"
    1.41 +})
    1.42 +public class ZipFileTest {
    1.43 +    
    1.44 +    @Compare public String readAnEntry() throws IOException {
    1.45 +        InputStream is = ZipFileTest.class.getResourceAsStream("readAnEntry.zip");
    1.46 +        ZipInputStream zip = new ZipInputStream(is);
    1.47 +        ZipEntry entry = zip.getNextEntry();
    1.48 +        assertEquals(entry.getName(), "my/main/file.txt", "Correct entry");
    1.49 +
    1.50 +        byte[] arr = new byte[4096];
    1.51 +        int len = zip.read(arr);
    1.52 +        
    1.53 +        assertEquals(zip.getNextEntry(), null, "No next entry");
    1.54 +        
    1.55 +        final String ret = new String(arr, 0, len, "UTF-8");
    1.56 +        return ret;
    1.57 +    }
    1.58 +    
    1.59 +    @JavaScriptBody(args = { "res", "path" }, body = 
    1.60 +          "var myvm = bck2brwsr.apply(null, path);\n"
    1.61 +        + "var cls = myvm.loadClass('java.lang.String');\n"
    1.62 +        + "return cls.getClass__Ljava_lang_Class_2().getResourceAsStream__Ljava_io_InputStream_2Ljava_lang_String_2(res);\n"
    1.63 +    )
    1.64 +    private static native Object loadVMResource(String res, String...path);
    1.65 +
    1.66 +    @Http({
    1.67 +        @Http.Resource(path = "/readAnEntry.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip")
    1.68 +    })
    1.69 +    @BrwsrTest  public void canVmLoadResourceFromZip() throws IOException {
    1.70 +        Object res = loadVMResource("/my/main/file.txt", "/readAnEntry.jar");
    1.71 +        assert res instanceof InputStream : "Got array of bytes: " + res;
    1.72 +        InputStream is = (InputStream)res;
    1.73 +        
    1.74 +        byte[] arr = new byte[4096];
    1.75 +        int len = is.read(arr);
    1.76 +        
    1.77 +        final String ret = new String(arr, 0, len, "UTF-8");
    1.78 +
    1.79 +        assertEquals(ret, "Hello World!", "Can read the bytes");
    1.80 +    }
    1.81 +    
    1.82 +    @GenerateZip(name = "cpattr.zip", contents = { 
    1.83 +        "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n"
    1.84 +        + "Created-By: hand\n"
    1.85 +        + "Class-Path: realJar.jar\n\n\n"
    1.86 +    })
    1.87 +    @Http({
    1.88 +        @Http.Resource(path = "/readComplexEntry.jar", mimeType = "x-application/zip", content = "", resource="cpattr.zip"),
    1.89 +        @Http.Resource(path = "/realJar.jar", mimeType = "x-application/zip", content = "", resource="readAnEntry.zip"),
    1.90 +    })
    1.91 +    @BrwsrTest  public void understandsClassPathAttr() throws IOException {
    1.92 +        Object res = loadVMResource("/my/main/file.txt", "/readComplexEntry.jar");
    1.93 +        assert res instanceof InputStream : "Got array of bytes: " + res;
    1.94 +        InputStream is = (InputStream)res;
    1.95 +        
    1.96 +        byte[] arr = new byte[4096];
    1.97 +        int len = is.read(arr);
    1.98 +        
    1.99 +        final String ret = new String(arr, 0, len, "UTF-8");
   1.100 +
   1.101 +        assertEquals(ret, "Hello World!", "Can read the bytes from secondary JAR");
   1.102 +    }
   1.103 +    
   1.104 +    private static void assertEquals(Object real, Object exp, String msg) {
   1.105 +        assert Objects.equals(exp, real) : msg + " exp: " + exp + " real: " + real;
   1.106 +    }
   1.107 +    
   1.108 +    @Factory public static Object[] create() {
   1.109 +        return VMTest.create(ZipFileTest.class);
   1.110 +    }
   1.111 +}