Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/ZFSTests.java
38833 views
/*1* Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 7156873 8028480 8034773 806177725@summary ZipFileSystem regression tests26*/2728import java.io.OutputStream;29import java.net.URI;30import java.nio.ByteBuffer;31import java.nio.channels.*;32import java.nio.file.*;33import java.nio.file.spi.*;34import java.util.*;3536public class ZFSTests {3738public static void main(String[] args) throws Throwable {39test7156873();40test8061777();41testOpenOptions();42}4344static void test7156873() throws Throwable {45String DIRWITHSPACE = "testdir with spaces";46Path dir = Paths.get(DIRWITHSPACE);47Path path = Paths.get(DIRWITHSPACE, "file.zip");48try {49Files.createDirectory(dir);50URI uri = URI.create("jar:" + path.toUri());51Map<String, Object> env = new HashMap<String, Object>();52env.put("create", "true");53try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {}54} finally {55Files.deleteIfExists(path);56Files.deleteIfExists(dir);57}58}5960static void test8061777() throws Throwable {61Path path = Paths.get("file.zip");62try {63URI uri = URI.create("jar:" + path.toUri());64Map<String, Object> env = new HashMap<String, Object>();65env.put("create", "true");66env.put("encoding", "Shift_JIS");67try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {68FileSystemProvider fsp = fs.provider();69Path p = fs.getPath("/\u8868\u7533.txt"); // 0x95 0x5c 0x90 0x5c70try (OutputStream os = fsp.newOutputStream(p)) {71os.write("Hello!".getBytes("ASCII"));72}73Path dir = fs.getPath("/");74Files.list(dir)75.forEach( child -> {76System.out.println("child:" + child);77if (!child.toString().equals(p.toString()))78throw new RuntimeException("wrong path name created");79});80if (!"Hello!".equals(new String(Files.readAllBytes(p), "ASCII")))81throw new RuntimeException("wrong content in newly created file");82}83} finally {84Files.deleteIfExists(path);85}86}8788static void testOpenOptions() throws Throwable {89Path path = Paths.get("file.zip");90try {91URI uri = URI.create("jar:" + path.toUri());92Map<String, Object> env = new HashMap<String, Object>();93env.put("create", "true");94try (FileSystem fs = FileSystems.newFileSystem(uri, env)) {95FileSystemProvider fsp = fs.provider();96Set<? extends OpenOption> options;97Path p = fs.getPath("test.txt");98// 802848099options = EnumSet.of(StandardOpenOption.CREATE,100StandardOpenOption.WRITE,101StandardOpenOption.APPEND);102try (FileChannel ch = fsp.newFileChannel(p, options)) {103ch.write(ByteBuffer.wrap("Hello!".getBytes("ASCII")));104}105// 8034773106try (OutputStream os = fsp.newOutputStream(p, new OpenOption[0])) {107os.write("Hello2!".getBytes("ASCII"));108}109if (!"Hello2!".equals(new String(110Files.readAllBytes(fs.getPath("test.txt"))))) {111throw new RuntimeException("failed to open as truncate_existing");112}113114options = EnumSet.of(StandardOpenOption.CREATE,115StandardOpenOption.APPEND,116StandardOpenOption.TRUNCATE_EXISTING);117try (FileChannel ch = fsp.newFileChannel(p, options)) {118throw new RuntimeException("expected IAE not thrown!");119} catch (IllegalArgumentException x) {120// expected x.printStackTrace();121}122}123} finally {124Files.deleteIfExists(path);125}126}127}128129130