Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/Basic.java
38833 views
/*1* Copyright (c) 2009, 2011, 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*/2223import java.nio.file.*;24import java.nio.file.attribute.*;25import java.nio.file.spi.FileSystemProvider;26import java.util.*;27import java.net.URI;28import java.io.IOException;2930/**31* Basic test for zip provider32*/3334public class Basic {35public static void main(String[] args) throws Exception {36Path zipfile = Paths.get(args[0]);3738// Test: zip should should be returned in provider list39boolean found = false;4041for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {42if (provider.getScheme().equalsIgnoreCase("jar")) {43found = true;44break;45}46}47if (!found)48throw new RuntimeException("'jar' provider not installed");4950// Test: FileSystems#newFileSystem(Path)51Map<String,?> env = new HashMap<String,Object>();52FileSystems.newFileSystem(zipfile, null).close();5354// Test: FileSystems#newFileSystem(URI)55URI uri = new URI("jar", zipfile.toUri().toString(), null);56FileSystem fs = FileSystems.newFileSystem(uri, env, null);5758// Test: exercise toUri method59String expected = uri.toString() + "!/foo";60String actual = fs.getPath("/foo").toUri().toString();61if (!actual.equals(expected)) {62throw new RuntimeException("toUri returned '" + actual +63"', expected '" + expected + "'");64}6566// Test: exercise directory iterator and retrieval of basic attributes67Files.walkFileTree(fs.getPath("/"), new FileTreePrinter());6869// Test: DirectoryStream70found = false;71try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"))) {72for (Path entry: stream) {73found = entry.toString().equals("/META-INF/");74if (found) break;75}76}7778if (!found)79throw new RuntimeException("Expected file not found");8081// Test: copy file from zip file to current (scratch) directory82Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");83if (Files.exists(source)) {84Path target = Paths.get(source.getFileName().toString());85Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);86try {87long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();88long s2 = Files.readAttributes(target, BasicFileAttributes.class).size();89if (s2 != s1)90throw new RuntimeException("target size != source size");91} finally {92Files.delete(target);93}94}9596// Test: FileStore97FileStore store = Files.getFileStore(fs.getPath("/"));98if (!store.supportsFileAttributeView("basic"))99throw new RuntimeException("BasicFileAttributeView should be supported");100101// Test: ClosedFileSystemException102fs.close();103if (fs.isOpen())104throw new RuntimeException("FileSystem should be closed");105try {106fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ);107} catch (ClosedFileSystemException x) { }108}109110// FileVisitor that pretty prints a file tree111static class FileTreePrinter extends SimpleFileVisitor<Path> {112private int indent = 0;113114private void indent() {115StringBuilder sb = new StringBuilder(indent);116for (int i=0; i<indent; i++) sb.append(" ");117System.out.print(sb);118}119120@Override121public FileVisitResult preVisitDirectory(Path dir,122BasicFileAttributes attrs)123{124if (dir.getFileName() != null) {125indent();126System.out.println(dir.getFileName() + "/");127indent++;128}129return FileVisitResult.CONTINUE;130}131132@Override133public FileVisitResult visitFile(Path file,134BasicFileAttributes attrs)135{136indent();137System.out.print(file.getFileName());138if (attrs.isRegularFile())139System.out.format(" (%d)", attrs.size());140System.out.println();141return FileVisitResult.CONTINUE;142}143144@Override145public FileVisitResult postVisitDirectory(Path dir, IOException exc)146throws IOException147{148if (exc != null)149super.postVisitDirectory(dir, exc);150if (dir.getFileName() != null)151indent--;152return FileVisitResult.CONTINUE;153}154}155}156157158