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