Path: blob/master/test/jdk/java/nio/file/spi/TestProvider.java
66645 views
/*1* Copyright (c) 2008, 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.io.File;24import java.nio.file.*;25import java.nio.file.attribute.BasicFileAttributes;26import java.nio.file.attribute.FileAttribute;27import java.nio.file.attribute.FileAttributeView;28import java.nio.file.attribute.UserPrincipalLookupService;29import java.nio.file.spi.FileSystemProvider;30import java.nio.channels.FileChannel;31import java.nio.channels.SeekableByteChannel;32import java.net.URI;33import java.io.IOException;34import java.util.Collections;35import java.util.Iterator;36import java.util.Map;37import java.util.Set;3839public class TestProvider extends FileSystemProvider {4041private final FileSystemProvider defaultProvider;42private final TestFileSystem theFileSystem;4344public TestProvider(FileSystemProvider defaultProvider) {45this.defaultProvider = defaultProvider;46FileSystem fs = defaultProvider.getFileSystem(URI.create("file:/"));47this.theFileSystem = new TestFileSystem(fs, this);48}4950FileSystemProvider defaultProvider() {51return defaultProvider;52}5354@Override55public String getScheme() {56return "file";57}5859@Override60public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException {61return defaultProvider.newFileSystem(uri, env);62}6364@Override65public FileSystem getFileSystem(URI uri) {66return theFileSystem;67}6869@Override70public Path getPath(URI uri) {71Path path = defaultProvider.getPath(uri);72return theFileSystem.wrap(path);73}7475@Override76public void setAttribute(Path file, String attribute, Object value,77LinkOption... options)78throws IOException79{80throw new RuntimeException("not implemented");81}8283@Override84public Map<String,Object> readAttributes(Path file, String attributes,85LinkOption... options)86throws IOException87{88Path delegate = theFileSystem.unwrap(file);89return defaultProvider.readAttributes(delegate, attributes, options);90}9192@Override93public <A extends BasicFileAttributes> A readAttributes(Path file,94Class<A> type,95LinkOption... options)96throws IOException97{98Path delegate = theFileSystem.unwrap(file);99return defaultProvider.readAttributes(delegate, type, options);100}101102@Override103public <V extends FileAttributeView> V getFileAttributeView(Path file,104Class<V> type,105LinkOption... options)106{107Path delegate = theFileSystem.unwrap(file);108return defaultProvider.getFileAttributeView(delegate, type, options);109}110111@Override112public void delete(Path file) throws IOException {113Path delegate = theFileSystem.unwrap(file);114defaultProvider.delete(delegate);115}116117@Override118public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)119throws IOException120{121throw new RuntimeException("not implemented");122}123124@Override125public void createLink(Path link, Path existing) throws IOException {126throw new RuntimeException("not implemented");127}128129@Override130public Path readSymbolicLink(Path link) throws IOException {131Path delegate = theFileSystem.unwrap(link);132Path target = defaultProvider.readSymbolicLink(delegate);133return theFileSystem.wrap(target);134}135136@Override137public void copy(Path source, Path target, CopyOption... options)138throws IOException139{140throw new RuntimeException("not implemented");141}142143@Override144public void move(Path source, Path target, CopyOption... options)145throws IOException146{147throw new RuntimeException("not implemented");148}149150@Override151public DirectoryStream<Path> newDirectoryStream(Path dir,152DirectoryStream.Filter<? super Path> filter)153throws IOException154{155throw new RuntimeException("not implemented");156}157158@Override159public void createDirectory(Path dir, FileAttribute<?>... attrs)160throws IOException161{162Path delegate = theFileSystem.unwrap(dir);163defaultProvider.createDirectory(delegate, attrs);164}165166@Override167public SeekableByteChannel newByteChannel(Path file,168Set<? extends OpenOption> options,169FileAttribute<?>... attrs)170throws IOException171{172Path delegate = theFileSystem.unwrap(file);173return defaultProvider.newByteChannel(delegate, options, attrs);174}175176@Override177public FileChannel newFileChannel(Path file,178Set<? extends OpenOption> options,179FileAttribute<?>... attrs)180throws IOException181{182Path delegate = theFileSystem.unwrap(file);183return defaultProvider.newFileChannel(delegate, options, attrs);184}185186@Override187public boolean isHidden(Path file) throws IOException {188throw new ReadOnlyFileSystemException();189}190191@Override192public FileStore getFileStore(Path file) throws IOException {193throw new RuntimeException("not implemented");194}195196@Override197public boolean isSameFile(Path file, Path other) throws IOException {198throw new RuntimeException("not implemented");199}200201@Override202public void checkAccess(Path file, AccessMode... modes)203throws IOException204{205throw new RuntimeException("not implemented");206}207208static class TestFileSystem extends FileSystem {209private final FileSystem delegate;210private final TestProvider provider;211212TestFileSystem(FileSystem delegate, TestProvider provider) {213this.delegate = delegate;214this.provider = provider;215}216217Path wrap(Path path) {218return (path != null) ? new TestPath(this, path) : null;219}220221Path unwrap(Path wrapper) {222if (wrapper == null)223throw new NullPointerException();224if (!(wrapper instanceof TestPath))225throw new ProviderMismatchException();226return ((TestPath)wrapper).unwrap();227}228229@Override230public FileSystemProvider provider() {231return provider;232}233234@Override235public void close() throws IOException {236throw new RuntimeException("not implemented");237}238239@Override240public boolean isOpen() {241return true;242}243244@Override245public boolean isReadOnly() {246return false;247}248249@Override250public String getSeparator() {251return delegate.getSeparator();252}253254@Override255public Iterable<Path> getRootDirectories() {256throw new RuntimeException("not implemented");257}258259@Override260public Iterable<FileStore> getFileStores() {261throw new RuntimeException("not implemented");262}263264@Override265public Set<String> supportedFileAttributeViews() {266return delegate.supportedFileAttributeViews();267}268269@Override270public Path getPath(String first, String... more) {271Path path = delegate.getPath(first, more);272return wrap(path);273}274275@Override276public PathMatcher getPathMatcher(String syntaxAndPattern) {277return delegate.getPathMatcher(syntaxAndPattern);278}279280@Override281public UserPrincipalLookupService getUserPrincipalLookupService() {282return delegate.getUserPrincipalLookupService();283}284285@Override286public WatchService newWatchService() throws IOException {287throw new UnsupportedOperationException();288}289}290291static class TestPath implements Path {292private final TestFileSystem fs;293private final Path delegate;294295TestPath(TestFileSystem fs, Path delegate) {296this.fs = fs;297this.delegate = delegate;298}299300Path unwrap() {301return delegate;302}303304@Override305public FileSystem getFileSystem() {306return fs;307}308309@Override310public boolean isAbsolute() {311return delegate.isAbsolute();312}313314@Override315public Path getRoot() {316return fs.wrap(delegate.getRoot());317}318319@Override320public Path getParent() {321return fs.wrap(delegate.getParent());322}323324@Override325public int getNameCount() {326return delegate.getNameCount();327}328329@Override330public Path getFileName() {331return fs.wrap(delegate.getFileName());332}333334@Override335public Path getName(int index) {336return fs.wrap(delegate.getName(index));337}338339@Override340public Path subpath(int beginIndex, int endIndex) {341return fs.wrap(delegate.subpath(beginIndex, endIndex));342}343344@Override345public boolean startsWith(Path other) {346return delegate.startsWith(fs.unwrap(other));347}348349@Override350public boolean startsWith(String other) {351return delegate.startsWith(other);352}353354@Override355public boolean endsWith(Path other) {356return delegate.endsWith(fs.unwrap(other));357}358359@Override360public boolean endsWith(String other) {361return delegate.endsWith(other);362}363364@Override365public Path normalize() {366return fs.wrap(delegate.normalize());367}368369@Override370public Path resolve(Path other) {371return fs.wrap(delegate.resolve(fs.unwrap(other)));372}373374@Override375public Path resolve(String other) {376return fs.wrap(delegate.resolve(other));377}378379@Override380public Path resolveSibling(Path other) {381return fs.wrap(delegate.resolveSibling(fs.unwrap(other)));382}383384@Override385public Path resolveSibling(String other) {386return fs.wrap(delegate.resolveSibling(other));387}388389@Override390public Path relativize(Path other) {391return fs.wrap(delegate.relativize(fs.unwrap(other)));392}393394@Override395public boolean equals(Object other) {396if (!(other instanceof TestPath))397return false;398return delegate.equals(fs.unwrap((TestPath) other));399}400401@Override402public int hashCode() {403return delegate.hashCode();404}405406@Override407public String toString() {408return delegate.toString();409}410411@Override412public URI toUri() {413String ssp = delegate.toUri().getSchemeSpecificPart();414return URI.create(fs.provider().getScheme() + ":" + ssp);415}416417@Override418public Path toAbsolutePath() {419return fs.wrap(delegate.toAbsolutePath());420}421422@Override423public Path toRealPath(LinkOption... options) throws IOException {424return fs.wrap(delegate.toRealPath(options));425}426427@Override428public File toFile() {429return new File(toString());430}431432@Override433public Iterator<Path> iterator() {434final Iterator<Path> itr = delegate.iterator();435return new Iterator<Path>() {436@Override437public boolean hasNext() {438return itr.hasNext();439}440@Override441public Path next() {442return fs.wrap(itr.next());443}444@Override445public void remove() {446itr.remove();447}448};449}450451@Override452public int compareTo(Path other) {453return delegate.compareTo(fs.unwrap(other));454}455456@Override457public WatchKey register(WatchService watcher,458WatchEvent.Kind<?>[] events,459WatchEvent.Modifier... modifiers)460{461throw new UnsupportedOperationException();462}463464@Override465public WatchKey register(WatchService watcher,466WatchEvent.Kind<?>... events)467{468throw new UnsupportedOperationException();469}470}471}472473474