|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 import org.testng.annotations.Test; |
|
25 |
|
26 import java.io.FileInputStream; |
|
27 import java.io.IOException; |
|
28 import java.io.InputStream; |
|
29 import java.io.OutputStream; |
|
30 import java.nio.charset.StandardCharsets; |
|
31 import java.nio.file.FileSystem; |
|
32 import java.nio.file.FileSystems; |
|
33 import java.nio.file.Files; |
|
34 import java.nio.file.Path; |
|
35 import java.util.Arrays; |
|
36 import java.util.Map; |
|
37 import java.util.zip.ZipEntry; |
|
38 import java.util.zip.ZipFile; |
|
39 import java.util.zip.ZipInputStream; |
|
40 |
|
41 import static org.testng.Assert.*; |
|
42 |
|
43 /** |
|
44 * @test |
|
45 * @bug 8232879 |
|
46 * @summary Test OutputStream::write with Zip FS |
|
47 * @modules jdk.zipfs |
|
48 * @run testng/othervm CRCWriteTest |
|
49 */ |
|
50 public class CRCWriteTest { |
|
51 |
|
52 // Jar File path used by the test |
|
53 private final Path JAR_FILE = Path.of("CRCWrite.jar"); |
|
54 |
|
55 /** |
|
56 * Validate that an OutputStream obtained for the Zip FileSystem |
|
57 * can be used successfully with the OutputStream write methods |
|
58 */ |
|
59 @Test |
|
60 private void zipFsOsDeflatedWriteTest() throws Exception { |
|
61 Files.deleteIfExists(JAR_FILE); |
|
62 String[] msg = {"Hello ", "Tennis Anyone", "!!!!"}; |
|
63 Entry e0 = Entry.of("Entry-0", ZipEntry.DEFLATED, Arrays.toString(msg)); |
|
64 |
|
65 try (FileSystem zipfs = FileSystems.newFileSystem(JAR_FILE, |
|
66 Map.of("create", "true"))) { |
|
67 |
|
68 // Write to the Jar file using the various OutputStream write methods |
|
69 try (OutputStream os = Files.newOutputStream(zipfs.getPath(e0.name))) { |
|
70 for (byte b : msg[0].getBytes(StandardCharsets.UTF_8)) { |
|
71 os.write(b); |
|
72 } |
|
73 os.write(msg[1].getBytes(StandardCharsets.UTF_8)); |
|
74 os.write(msg[2].getBytes(StandardCharsets.UTF_8), 0, msg[2].length()); |
|
75 } |
|
76 } |
|
77 verify(JAR_FILE, e0); |
|
78 } |
|
79 |
|
80 /** |
|
81 * Represents an entry in a Zip file. An entry encapsulates a name, a |
|
82 * compression method, and its contents/data. |
|
83 */ |
|
84 static class Entry { |
|
85 private final String name; |
|
86 private final int method; |
|
87 private final byte[] bytes; |
|
88 |
|
89 Entry(String name, int method, String contents) { |
|
90 this.name = name; |
|
91 this.method = method; |
|
92 this.bytes = contents.getBytes(StandardCharsets.UTF_8); |
|
93 } |
|
94 |
|
95 static Entry of(String name, int method, String contents) { |
|
96 return new Entry(name, method, contents); |
|
97 } |
|
98 |
|
99 /** |
|
100 * Returns a new Entry with the same name and compression method as this |
|
101 * Entry but with the given content. |
|
102 */ |
|
103 Entry content(String contents) { |
|
104 return new Entry(name, method, contents); |
|
105 } |
|
106 } |
|
107 |
|
108 /** |
|
109 * Verify that the given path is a Zip file containing exactly the |
|
110 * given entries. |
|
111 */ |
|
112 private static void verify(Path zipfile, Entry... entries) throws IOException { |
|
113 |
|
114 // check entries with ZipFile API |
|
115 try (ZipFile zf = new ZipFile(zipfile.toFile())) { |
|
116 // check entry count |
|
117 assertEquals(entries.length, zf.size()); |
|
118 |
|
119 // Check compression method and content of each entry |
|
120 for (Entry e : entries) { |
|
121 ZipEntry ze = zf.getEntry(e.name); |
|
122 assertNotNull(ze); |
|
123 assertEquals(e.method, ze.getMethod()); |
|
124 try (InputStream in = zf.getInputStream(ze)) { |
|
125 byte[] bytes = in.readAllBytes(); |
|
126 assertTrue(Arrays.equals(bytes, e.bytes)); |
|
127 } |
|
128 } |
|
129 } |
|
130 |
|
131 // Check entries using ZipInputStream |
|
132 for (Entry e : entries) { |
|
133 try (ZipInputStream zis = |
|
134 new ZipInputStream(new FileInputStream(zipfile.toFile()))) { |
|
135 ZipEntry ze; |
|
136 while ((ze = zis.getNextEntry()) != null) { |
|
137 assertEquals(e.method, ze.getMethod()); |
|
138 byte[] bytes = zis.readAllBytes(); |
|
139 assertTrue(Arrays.equals(bytes, e.bytes)); |
|
140 } |
|
141 } |
|
142 } |
|
143 |
|
144 // Check entries with FileSystem API |
|
145 try (FileSystem fs = FileSystems.newFileSystem(zipfile)) { |
|
146 // check entry count |
|
147 Path top = fs.getPath("/"); |
|
148 long count = Files.find(top, Integer.MAX_VALUE, |
|
149 (path, attrs) -> attrs.isRegularFile()).count(); |
|
150 assertEquals(entries.length, count); |
|
151 |
|
152 // check content of each entry |
|
153 for (Entry e : entries) { |
|
154 Path file = fs.getPath(e.name); |
|
155 byte[] bytes = Files.readAllBytes(file); |
|
156 assertTrue(Arrays.equals(bytes, e.bytes)); |
|
157 } |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 |