emul/compact/src/test/java/org/apidesign/bck2brwsr/compact/tck/ZipCompatibilityTest.java
2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.bck2brwsr.compact.tck;
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;
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;
35 * @author Jaroslav Tulach <jtulach@netbeans.org>
37 public class ZipCompatibilityTest {
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();
46 public static Object[] create() {
47 return VMTest.create(ZipCompatibilityTest.class);
50 private static final class Archive {
52 private final Map<String, byte[]> entries = new LinkedHashMap<>();
54 public static Archive createZip(InputStream is) throws IOException {
55 Archive a = new Archive();
61 * Registers entry name and data
63 final void register(String entry, InputStream is) throws IOException {
64 ByteArrayOutputStream os = new ByteArrayOutputStream();
74 entries.put(entry, os.toByteArray());
78 public int hashCode() {
79 return entries.hashCode();
83 public boolean equals(Object obj) {
87 if (getClass() != obj.getClass()) {
90 final Archive other = (Archive) obj;
91 if (!Objects.deepEquals(this.entries, other.entries)) {
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();
104 sb.append(string).append(" = ").append(Arrays.toString(bs)).append("\n");
106 return sb.toString();
109 public void assertEquals(Archive zip, String msg) {
112 StringBuilder sb = new StringBuilder();
114 for (Map.Entry<String, byte[]> en : entries.entrySet()) {
115 String string = en.getKey();
116 byte[] bs = en.getValue();
118 byte[] other = zip.entries.get(string);
123 sb.append("EXTRA ").append(string).append(" = ").append(Arrays.toString(bs));
127 if (Arrays.equals(bs, other)) {
128 sb.append("OK ").append(string);
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");
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()));
145 assert false : sb.toString();
148 public static void readZip(InputStream is, Archive data) throws IOException {
149 ZipInputStream zip = new ZipInputStream(is);
151 ZipEntry en = zip.getNextEntry();
155 data.register(en.getName(), zip);