1.1 --- a/emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipArchive.java Mon Feb 25 19:00:08 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,154 +0,0 @@
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.compact.tck;
1.22 -
1.23 -import java.io.ByteArrayOutputStream;
1.24 -import java.io.IOException;
1.25 -import java.io.InputStream;
1.26 -import java.util.Arrays;
1.27 -import java.util.LinkedHashMap;
1.28 -import java.util.Map;
1.29 -import java.util.Objects;
1.30 -import java.util.zip.ZipEntry;
1.31 -import org.apidesign.bck2brwsr.emul.zip.ZipInputStream;
1.32 -
1.33 -/**
1.34 - *
1.35 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.36 - */
1.37 -final class ZipArchive {
1.38 - private final Map<String, byte[]> entries = new LinkedHashMap<>();
1.39 -
1.40 - public static ZipArchive createZip(InputStream is) throws IOException {
1.41 - ZipArchive a = new ZipArchive();
1.42 - readZip(is, a);
1.43 - return a;
1.44 - }
1.45 -
1.46 - public static ZipArchive createReal(InputStream is) throws IOException {
1.47 - ZipArchive a = new ZipArchive();
1.48 - realZip(is, a);
1.49 - return a;
1.50 - }
1.51 -
1.52 - /**
1.53 - * Registers entry name and data
1.54 - */
1.55 - final void register(String entry, InputStream is) throws IOException {
1.56 - ByteArrayOutputStream os = new ByteArrayOutputStream();
1.57 - for (;;) {
1.58 - int ch = is.read();
1.59 - if (ch == -1) {
1.60 - break;
1.61 - }
1.62 - os.write(ch);
1.63 - }
1.64 - os.close();
1.65 - entries.put(entry, os.toByteArray());
1.66 - }
1.67 -
1.68 - @Override
1.69 - public int hashCode() {
1.70 - return entries.hashCode();
1.71 - }
1.72 -
1.73 - @Override
1.74 - public boolean equals(Object obj) {
1.75 - if (obj == null) {
1.76 - return false;
1.77 - }
1.78 - if (getClass() != obj.getClass()) {
1.79 - return false;
1.80 - }
1.81 - final ZipArchive other = (ZipArchive) obj;
1.82 - if (!Objects.deepEquals(this.entries, other.entries)) {
1.83 - return false;
1.84 - }
1.85 - return true;
1.86 - }
1.87 -
1.88 - @Override
1.89 - public String toString() {
1.90 - StringBuilder sb = new StringBuilder();
1.91 - for (Map.Entry<String, byte[]> en : entries.entrySet()) {
1.92 - String string = en.getKey();
1.93 - byte[] bs = en.getValue();
1.94 - sb.append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
1.95 - }
1.96 - return sb.toString();
1.97 - }
1.98 -
1.99 - public void assertEquals(ZipArchive zip, String msg) {
1.100 - boolean ok = true;
1.101 - StringBuilder sb = new StringBuilder();
1.102 - sb.append(msg);
1.103 - for (Map.Entry<String, byte[]> en : entries.entrySet()) {
1.104 - String string = en.getKey();
1.105 - byte[] bs = en.getValue();
1.106 - byte[] other = zip.entries.get(string);
1.107 - sb.append("\n");
1.108 - if (other == null) {
1.109 - sb.append("EXTRA ").append(string).append(" = ").append(Arrays.toString(bs));
1.110 - ok = false;
1.111 - continue;
1.112 - }
1.113 - if (Arrays.equals(bs, other)) {
1.114 - sb.append("OK ").append(string);
1.115 - continue;
1.116 - } else {
1.117 - sb.append("DIFF ").append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
1.118 - sb.append(" TO").append(string).append(" = ").append(Arrays.toString(other)).append("\n");
1.119 - ok = false;
1.120 - continue;
1.121 - }
1.122 - }
1.123 - for (Map.Entry<String, byte[]> entry : zip.entries.entrySet()) {
1.124 - String string = entry.getKey();
1.125 - if (entries.get(string) == null) {
1.126 - sb.append("MISS ").append(string).append(" = ").append(Arrays.toString(entry.getValue()));
1.127 - ok = false;
1.128 - }
1.129 - }
1.130 - if (!ok) {
1.131 - assert false : sb.toString();
1.132 - }
1.133 - }
1.134 -
1.135 - public static void readZip(InputStream is, ZipArchive data) throws IOException {
1.136 - ZipInputStream zip = new org.apidesign.bck2brwsr.emul.zip.ZipInputStream(is);
1.137 - for (;;) {
1.138 - ZipEntry en = zip.getNextEntry();
1.139 - if (en == null) {
1.140 - return;
1.141 - }
1.142 - data.register(en.getName(), zip);
1.143 - }
1.144 - }
1.145 -
1.146 - public static void realZip(InputStream is, ZipArchive data) throws IOException {
1.147 - java.util.zip.ZipInputStream zip = new java.util.zip.ZipInputStream(is);
1.148 - for (;;) {
1.149 - ZipEntry en = zip.getNextEntry();
1.150 - if (en == null) {
1.151 - return;
1.152 - }
1.153 - data.register(en.getName(), zip);
1.154 - }
1.155 - }
1.156 -
1.157 -}