emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Feb 2013 15:22:57 +0100
branchemul
changeset 686 63982243369c
child 694 0d277415ed02
permissions -rw-r--r--
Adding failing 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.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 java.util.zip.ZipInputStream;
    29 import org.apidesign.bck2brwsr.vmtest.Compare;
    30 import org.apidesign.bck2brwsr.vmtest.VMTest;
    31 import org.testng.annotations.Factory;
    32 
    33 /**
    34  *
    35  * @author Jaroslav Tulach <jtulach@netbeans.org>
    36  */
    37 public class ZipCompatibilityTest {
    38     @Compare
    39     public String testDemoStaticCalculator() throws IOException {
    40         InputStream is = getClass().getResourceAsStream("demo.static.calculator-0.3-SNAPSHOT.jar");
    41         Archive zip = Archive.createZip(is);
    42         return zip.toString();
    43     }
    44     
    45     @Factory
    46     public static Object[] create() {
    47         return VMTest.create(ZipCompatibilityTest.class);
    48     }
    49     
    50     private static final class Archive {
    51 
    52         private final Map<String, byte[]> entries = new LinkedHashMap<>();
    53 
    54         public static Archive createZip(InputStream is) throws IOException {
    55             Archive a = new Archive();
    56             readZip(is, a);
    57             return a;
    58         }
    59 
    60         /**
    61          * Registers entry name and data
    62          */
    63         final void register(String entry, InputStream is) throws IOException {
    64             ByteArrayOutputStream os = new ByteArrayOutputStream();
    65             for (;;) {
    66                 int ch = is.read();
    67                 if (ch == -1) {
    68                     break;
    69                 }
    70                 os.write(ch);
    71             }
    72             os.close();
    73 
    74             entries.put(entry, os.toByteArray());
    75         }
    76 
    77         @Override
    78         public int hashCode() {
    79             return entries.hashCode();
    80         }
    81 
    82         @Override
    83         public boolean equals(Object obj) {
    84             if (obj == null) {
    85                 return false;
    86             }
    87             if (getClass() != obj.getClass()) {
    88                 return false;
    89             }
    90             final Archive other = (Archive) obj;
    91             if (!Objects.deepEquals(this.entries, other.entries)) {
    92                 return false;
    93             }
    94             return true;
    95         }
    96 
    97         @Override
    98         public String toString() {
    99             StringBuilder sb = new StringBuilder();
   100             for (Map.Entry<String, byte[]> en : entries.entrySet()) {
   101                 String string = en.getKey();
   102                 byte[] bs = en.getValue();
   103 
   104                 sb.append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
   105             }
   106             return sb.toString();
   107         }
   108 
   109         public void assertEquals(Archive zip, String msg) {
   110             boolean ok = true;
   111 
   112             StringBuilder sb = new StringBuilder();
   113             sb.append(msg);
   114             for (Map.Entry<String, byte[]> en : entries.entrySet()) {
   115                 String string = en.getKey();
   116                 byte[] bs = en.getValue();
   117 
   118                 byte[] other = zip.entries.get(string);
   119 
   120                 sb.append("\n");
   121 
   122                 if (other == null) {
   123                     sb.append("EXTRA ").append(string).append(" = ").append(Arrays.toString(bs));
   124                     ok = false;
   125                     continue;
   126                 }
   127                 if (Arrays.equals(bs, other)) {
   128                     sb.append("OK    ").append(string);
   129                     continue;
   130                 } else {
   131                     sb.append("DIFF  ").append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
   132                     sb.append("    TO").append(string).append(" = ").append(Arrays.toString(other)).append("\n");
   133                     ok = false;
   134                     continue;
   135                 }
   136             }
   137             for (Map.Entry<String, byte[]> entry : zip.entries.entrySet()) {
   138                 String string = entry.getKey();
   139                 if (entries.get(string) == null) {
   140                     sb.append("MISS  ").append(string).append(" = ").append(Arrays.toString(entry.getValue()));
   141                     ok = false;
   142                 }
   143             }
   144             if (!ok) {
   145                 assert false : sb.toString();
   146             }
   147         }
   148         public static void readZip(InputStream is, Archive data) throws IOException {
   149             ZipInputStream zip = new ZipInputStream(is);
   150             for (;;) {
   151                 ZipEntry en = zip.getNextEntry();
   152                 if (en == null) {
   153                     return;
   154                 }
   155                 data.register(en.getName(), zip);
   156             }
   157         }
   158     }
   159     
   160 }