rt/vm/src/test/java/org/apidesign/vm4brwsr/Resources.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 26 Sep 2014 09:22:31 +0200
changeset 1707 61dd2c555a1f
parent 1587 bf08bd96d408
child 1709 ce898bccdbc8
permissions -rw-r--r--
Test at least a bit of obfuscation in same unit test
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.IOException;
    21 import java.io.InputStream;
    22 import java.net.URL;
    23 import java.util.Enumeration;
    24 import net.java.html.js.JavaScriptBody;
    25 import net.java.html.js.JavaScriptResource;
    26 
    27 /**
    28  *
    29  * @author Jaroslav Tulach <jtulach@netbeans.org>
    30  */
    31 @JavaScriptResource("obj.js")
    32 public class Resources {
    33     @JavaScriptBody(args = {}, body = "return obj;")
    34     static Object retObj() {
    35         return null;
    36     }
    37     
    38     public static boolean isObj() {
    39         return retObj() != null;
    40     }
    41     public static boolean isResource() {
    42         return Resources.class.getResource("obj.js") != null;
    43     }
    44     
    45     public static String loadKO() throws IOException {
    46         InputStream is = Resources.class.getResourceAsStream("ko.js");
    47         return readIS(is, false);
    48     }
    49     
    50     static String loadClazz() throws IOException {
    51         Object o = new Resources();
    52         InputStream is = o.getClass().getResourceAsStream("Bck2BrwsrToolkit.class");
    53         return readIS(is, false);
    54     }
    55 
    56     private static String readIS(InputStream is, boolean asString) throws IOException {
    57         if (is == null) {
    58             return "No resource found!";
    59         }
    60         byte[] arr = new byte[4092];
    61         int len = is.read(arr);
    62         if (len < 5) {
    63             return "No data read! Len: " + len;
    64         }
    65         
    66         if (asString) {
    67             return new String(arr, 0, len, "UTF-8").toString().toString();
    68         }
    69         
    70         StringBuilder sb = new StringBuilder();
    71         sb.append("[");
    72         for (int i = 0; i < len; i++) {
    73             sb.append(arr[i]).append(", ");
    74         }
    75         
    76         return sb.toString().toString();
    77     }
    78     static long bytesToLong(byte b1, byte b2, int shift) {
    79         return (((long)b1 << 56) +
    80                 ((long)b2 & 255) << 48) >> shift;
    81     }
    82 
    83     public static String loadHello() throws IOException {
    84         Enumeration<URL> en;
    85         try {
    86             en = Resources.class.getClassLoader().getResources("META-INF/ahoj");
    87         } catch (SecurityException ex) {
    88             return "SecurityException";
    89         }
    90         StringBuilder sb = new StringBuilder();
    91         while (en.hasMoreElements()) {
    92             URL url = en.nextElement();
    93             sb.append(readIS(url.openStream(), true));
    94         }
    95         return sb.toString().toString();
    96     }
    97     public static String loadJustHello() throws IOException {
    98         URL url = Resources.class.getResource("/META-INF/ahoj");
    99         StringBuilder sb = new StringBuilder();
   100         sb.append(readIS(url.openStream(), true));
   101         return sb.toString().toString();
   102     }
   103 }