emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipArchive.java
author Lubomir Nerad <lubomir.nerad@oracle.com>
Mon, 25 Feb 2013 19:00:08 +0100
brancharithmetic
changeset 755 5652acd48509
permissions -rw-r--r--
Added conversion to long for JavaScript implementation of System.currentTimeMillis
     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.bck2brwsr.compact.tck;
    19 
    20 import java.io.ByteArrayOutputStream;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    23 import java.util.Arrays;
    24 import java.util.LinkedHashMap;
    25 import java.util.Map;
    26 import java.util.Objects;
    27 import java.util.zip.ZipEntry;
    28 import org.apidesign.bck2brwsr.emul.zip.ZipInputStream;
    29 
    30 /**
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 final class ZipArchive {
    35     private final Map<String, byte[]> entries = new LinkedHashMap<>();
    36 
    37     public static ZipArchive createZip(InputStream is) throws IOException {
    38         ZipArchive a = new ZipArchive();
    39         readZip(is, a);
    40         return a;
    41     }
    42 
    43     public static ZipArchive createReal(InputStream is) throws IOException {
    44         ZipArchive a = new ZipArchive();
    45         realZip(is, a);
    46         return a;
    47     }
    48 
    49     /**
    50      * Registers entry name and data
    51      */
    52     final void register(String entry, InputStream is) throws IOException {
    53         ByteArrayOutputStream os = new ByteArrayOutputStream();
    54         for (;;) {
    55             int ch = is.read();
    56             if (ch == -1) {
    57                 break;
    58             }
    59             os.write(ch);
    60         }
    61         os.close();
    62         entries.put(entry, os.toByteArray());
    63     }
    64 
    65     @Override
    66     public int hashCode() {
    67         return entries.hashCode();
    68     }
    69 
    70     @Override
    71     public boolean equals(Object obj) {
    72         if (obj == null) {
    73             return false;
    74         }
    75         if (getClass() != obj.getClass()) {
    76             return false;
    77         }
    78         final ZipArchive other = (ZipArchive) obj;
    79         if (!Objects.deepEquals(this.entries, other.entries)) {
    80             return false;
    81         }
    82         return true;
    83     }
    84 
    85     @Override
    86     public String toString() {
    87         StringBuilder sb = new StringBuilder();
    88         for (Map.Entry<String, byte[]> en : entries.entrySet()) {
    89             String string = en.getKey();
    90             byte[] bs = en.getValue();
    91             sb.append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
    92         }
    93         return sb.toString();
    94     }
    95 
    96     public void assertEquals(ZipArchive zip, String msg) {
    97         boolean ok = true;
    98         StringBuilder sb = new StringBuilder();
    99         sb.append(msg);
   100         for (Map.Entry<String, byte[]> en : entries.entrySet()) {
   101             String string = en.getKey();
   102             byte[] bs = en.getValue();
   103             byte[] other = zip.entries.get(string);
   104             sb.append("\n");
   105             if (other == null) {
   106                 sb.append("EXTRA ").append(string).append(" = ").append(Arrays.toString(bs));
   107                 ok = false;
   108                 continue;
   109             }
   110             if (Arrays.equals(bs, other)) {
   111                 sb.append("OK    ").append(string);
   112                 continue;
   113             } else {
   114                 sb.append("DIFF  ").append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
   115                 sb.append("    TO").append(string).append(" = ").append(Arrays.toString(other)).append("\n");
   116                 ok = false;
   117                 continue;
   118             }
   119         }
   120         for (Map.Entry<String, byte[]> entry : zip.entries.entrySet()) {
   121             String string = entry.getKey();
   122             if (entries.get(string) == null) {
   123                 sb.append("MISS  ").append(string).append(" = ").append(Arrays.toString(entry.getValue()));
   124                 ok = false;
   125             }
   126         }
   127         if (!ok) {
   128             assert false : sb.toString();
   129         }
   130     }
   131 
   132     public static void readZip(InputStream is, ZipArchive data) throws IOException {
   133         ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
   134         for (;;) {
   135             ZipEntry en = zip.getNextEntry();
   136             if (en == null) {
   137                 return;
   138             }
   139             data.register(en.getName(), zip);
   140         }
   141     }
   142 
   143     public static void realZip(InputStream is, ZipArchive data) throws IOException {
   144         java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
   145         for (;;) {
   146             ZipEntry en = zip.getNextEntry();
   147             if (en == null) {
   148                 return;
   149             }
   150             data.register(en.getName(), zip);
   151         }
   152     }
   153     
   154 }