2010-10-09 04:33:28 +00:00
|
|
|
/*
|
2011-04-07 05:06:11 +00:00
|
|
|
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
|
2010-10-09 04:33:28 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.nio.file.*;
|
|
|
|
import java.nio.file.attribute.*;
|
|
|
|
import java.nio.file.spi.FileSystemProvider;
|
|
|
|
import java.util.*;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Basic test for zip provider
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class Basic {
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
|
|
Path zipfile = Paths.get(args[0]);
|
|
|
|
|
|
|
|
// Test: zip should should be returned in provider list
|
|
|
|
boolean found = false;
|
|
|
|
|
|
|
|
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
|
2010-12-06 21:18:16 +00:00
|
|
|
if (provider.getScheme().equalsIgnoreCase("jar")) {
|
2010-10-09 04:33:28 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
2010-12-06 21:18:16 +00:00
|
|
|
throw new RuntimeException("'jar' provider not installed");
|
2010-10-09 04:33:28 +00:00
|
|
|
|
2011-02-01 22:20:01 +00:00
|
|
|
// Test: FileSystems#newFileSystem(Path)
|
2010-10-09 04:33:28 +00:00
|
|
|
Map<String,?> env = new HashMap<String,Object>();
|
2011-02-01 22:20:01 +00:00
|
|
|
FileSystems.newFileSystem(zipfile, null).close();
|
2010-10-09 04:33:28 +00:00
|
|
|
|
|
|
|
// Test: FileSystems#newFileSystem(URI)
|
2010-12-06 21:18:16 +00:00
|
|
|
URI uri = new URI("jar", zipfile.toUri().toString(), null);
|
2010-10-09 04:33:28 +00:00
|
|
|
FileSystem fs = FileSystems.newFileSystem(uri, env, null);
|
|
|
|
|
|
|
|
// Test: exercise toUri method
|
2010-12-06 21:18:16 +00:00
|
|
|
String expected = uri.toString() + "!/foo";
|
2010-10-09 04:33:28 +00:00
|
|
|
String actual = fs.getPath("/foo").toUri().toString();
|
|
|
|
if (!actual.equals(expected)) {
|
|
|
|
throw new RuntimeException("toUri returned '" + actual +
|
|
|
|
"', expected '" + expected + "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test: exercise directory iterator and retrieval of basic attributes
|
|
|
|
Files.walkFileTree(fs.getPath("/"), new FileTreePrinter());
|
|
|
|
|
|
|
|
// Test: DirectoryStream
|
|
|
|
found = false;
|
2011-02-01 22:20:01 +00:00
|
|
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"))) {
|
2010-10-09 04:33:28 +00:00
|
|
|
for (Path entry: stream) {
|
|
|
|
found = entry.toString().equals("/META-INF/");
|
|
|
|
if (found) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
throw new RuntimeException("Expected file not found");
|
|
|
|
|
|
|
|
// Test: copy file from zip file to current (scratch) directory
|
|
|
|
Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");
|
2011-02-01 22:20:01 +00:00
|
|
|
if (Files.exists(source)) {
|
|
|
|
Path target = Paths.get(source.getFileName().toString());
|
|
|
|
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
|
2010-10-09 04:33:28 +00:00
|
|
|
try {
|
2011-02-01 22:20:01 +00:00
|
|
|
long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();
|
|
|
|
long s2 = Files.readAttributes(target, BasicFileAttributes.class).size();
|
2010-10-09 04:33:28 +00:00
|
|
|
if (s2 != s1)
|
|
|
|
throw new RuntimeException("target size != source size");
|
|
|
|
} finally {
|
2011-02-01 22:20:01 +00:00
|
|
|
Files.delete(target);
|
2010-10-09 04:33:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test: FileStore
|
2011-02-01 22:20:01 +00:00
|
|
|
FileStore store = Files.getFileStore(fs.getPath("/"));
|
2010-10-09 04:33:28 +00:00
|
|
|
if (!store.supportsFileAttributeView("basic"))
|
|
|
|
throw new RuntimeException("BasicFileAttributeView should be supported");
|
|
|
|
|
|
|
|
// Test: ClosedFileSystemException
|
|
|
|
fs.close();
|
|
|
|
if (fs.isOpen())
|
|
|
|
throw new RuntimeException("FileSystem should be closed");
|
|
|
|
try {
|
2011-02-01 22:20:01 +00:00
|
|
|
fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ);
|
2010-10-09 04:33:28 +00:00
|
|
|
} catch (ClosedFileSystemException x) { }
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileVisitor that pretty prints a file tree
|
|
|
|
static class FileTreePrinter extends SimpleFileVisitor<Path> {
|
|
|
|
private int indent = 0;
|
|
|
|
|
|
|
|
private void indent() {
|
|
|
|
StringBuilder sb = new StringBuilder(indent);
|
|
|
|
for (int i=0; i<indent; i++) sb.append(" ");
|
|
|
|
System.out.print(sb);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileVisitResult preVisitDirectory(Path dir,
|
|
|
|
BasicFileAttributes attrs)
|
|
|
|
{
|
2011-02-01 22:20:01 +00:00
|
|
|
if (dir.getFileName() != null) {
|
2010-10-09 04:33:28 +00:00
|
|
|
indent();
|
2011-02-01 22:20:01 +00:00
|
|
|
System.out.println(dir.getFileName() + "/");
|
2010-10-09 04:33:28 +00:00
|
|
|
indent++;
|
|
|
|
}
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileVisitResult visitFile(Path file,
|
|
|
|
BasicFileAttributes attrs)
|
|
|
|
{
|
|
|
|
indent();
|
2011-02-01 22:20:01 +00:00
|
|
|
System.out.print(file.getFileName());
|
2010-10-09 04:33:28 +00:00
|
|
|
if (attrs.isRegularFile())
|
|
|
|
System.out.format(" (%d)", attrs.size());
|
|
|
|
System.out.println();
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
|
|
|
|
throws IOException
|
|
|
|
{
|
|
|
|
if (exc != null)
|
|
|
|
super.postVisitDirectory(dir, exc);
|
2011-02-01 22:20:01 +00:00
|
|
|
if (dir.getFileName() != null)
|
2010-10-09 04:33:28 +00:00
|
|
|
indent--;
|
|
|
|
return FileVisitResult.CONTINUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|