Path: blob/master/test/jdk/java/nio/file/spi/TestProvider.java
41153 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.SeekableByteChannel;31import java.net.URI;32import java.io.IOException;33import java.util.Collections;34import java.util.Iterator;35import java.util.Map;36import java.util.Set;3738public class TestProvider extends FileSystemProvider {3940private final FileSystemProvider defaultProvider;41private final TestFileSystem theFileSystem;4243public TestProvider(FileSystemProvider defaultProvider) {44this.defaultProvider = defaultProvider;45FileSystem fs = defaultProvider.getFileSystem(URI.create("file:/"));46this.theFileSystem = new TestFileSystem(fs, this);47}4849FileSystemProvider defaultProvider() {50return defaultProvider;51}5253@Override54public String getScheme() {55return "file";56}5758@Override59public FileSystem newFileSystem(URI uri, Map<String,?> env) throws IOException {60return defaultProvider.newFileSystem(uri, env);61}6263@Override64public FileSystem getFileSystem(URI uri) {65return theFileSystem;66}6768@Override69public Path getPath(URI uri) {70Path path = defaultProvider.getPath(uri);71return theFileSystem.wrap(path);72}7374@Override75public void setAttribute(Path file, String attribute, Object value,76LinkOption... options)77throws IOException78{79throw new RuntimeException("not implemented");80}8182@Override83public Map<String,Object> readAttributes(Path file, String attributes,84LinkOption... options)85throws IOException86{87Path delegate = theFileSystem.unwrap(file);88return defaultProvider.readAttributes(delegate, attributes, options);89}9091@Override92public <A extends BasicFileAttributes> A readAttributes(Path file,93Class<A> type,94LinkOption... options)95throws IOException96{97Path delegate = theFileSystem.unwrap(file);98return defaultProvider.readAttributes(delegate, type, options);99}100101@Override102public <V extends FileAttributeView> V getFileAttributeView(Path file,103Class<V> type,104LinkOption... options)105{106Path delegate = theFileSystem.unwrap(file);107return defaultProvider.getFileAttributeView(delegate, type, options);108}109110@Override111public void delete(Path file) throws IOException {112Path delegate = theFileSystem.unwrap(file);113defaultProvider.delete(delegate);114}115116@Override117public void createSymbolicLink(Path link, Path target, FileAttribute<?>... attrs)118throws IOException119{120throw new RuntimeException("not implemented");121}122123@Override124public void createLink(Path link, Path existing) throws IOException {125throw new RuntimeException("not implemented");126}127128@Override129public Path readSymbolicLink(Path link) throws IOException {130Path delegate = theFileSystem.unwrap(link);131Path target = defaultProvider.readSymbolicLink(delegate);132return theFileSystem.wrap(target);133}134135@Override136public void copy(Path source, Path target, CopyOption... options)137throws IOException138{139throw new RuntimeException("not implemented");140}141142@Override143public void move(Path source, Path target, CopyOption... options)144throws IOException145{146throw new RuntimeException("not implemented");147}148149@Override150public DirectoryStream<Path> newDirectoryStream(Path dir,151DirectoryStream.Filter<? super Path> filter)152throws IOException153{154throw new RuntimeException("not implemented");155}156157@Override158public void createDirectory(Path dir, FileAttribute<?>... attrs)159throws IOException160{161Path delegate = theFileSystem.unwrap(dir);162defaultProvider.createDirectory(delegate, attrs);163}164165@Override166public SeekableByteChannel newByteChannel(Path file,167Set<? extends OpenOption> options,168FileAttribute<?>... attrs)169throws IOException170{171Path delegate = theFileSystem.unwrap(file);172return defaultProvider.newByteChannel(delegate, options, attrs);173}174175@Override176public boolean isHidden(Path file) throws IOException {177throw new ReadOnlyFileSystemException();178}179180@Override181public FileStore getFileStore(Path file) throws IOException {182throw new RuntimeException("not implemented");183}184185@Override186public boolean isSameFile(Path file, Path other) throws IOException {187throw new RuntimeException("not implemented");188}189190@Override191public void checkAccess(Path file, AccessMode... modes)192throws IOException193{194throw new RuntimeException("not implemented");195}196197static class TestFileSystem extends FileSystem {198private final FileSystem delegate;199private final TestProvider provider;200201TestFileSystem(FileSystem delegate, TestProvider provider) {202this.delegate = delegate;203this.provider = provider;204}205206Path wrap(Path path) {207return (path != null) ? new TestPath(this, path) : null;208}209210Path unwrap(Path wrapper) {211if (wrapper == null)212throw new NullPointerException();213if (!(wrapper instanceof TestPath))214throw new ProviderMismatchException();215return ((TestPath)wrapper).unwrap();216}217218@Override219public FileSystemProvider provider() {220return provider;221}222223@Override224public void close() throws IOException {225throw new RuntimeException("not implemented");226}227228@Override229public boolean isOpen() {230return true;231}232233@Override234public boolean isReadOnly() {235return false;236}237238@Override239public String getSeparator() {240return delegate.getSeparator();241}242243@Override244public Iterable<Path> getRootDirectories() {245throw new RuntimeException("not implemented");246}247248@Override249public Iterable<FileStore> getFileStores() {250throw new RuntimeException("not implemented");251}252253@Override254public Set<String> supportedFileAttributeViews() {255return delegate.supportedFileAttributeViews();256}257258@Override259public Path getPath(String first, String... more) {260Path path = delegate.getPath(first, more);261return wrap(path);262}263264@Override265public PathMatcher getPathMatcher(String syntaxAndPattern) {266return delegate.getPathMatcher(syntaxAndPattern);267}268269@Override270public UserPrincipalLookupService getUserPrincipalLookupService() {271return delegate.getUserPrincipalLookupService();272}273274@Override275public WatchService newWatchService() throws IOException {276throw new UnsupportedOperationException();277}278}279280static class TestPath implements Path {281private final TestFileSystem fs;282private final Path delegate;283284TestPath(TestFileSystem fs, Path delegate) {285this.fs = fs;286this.delegate = delegate;287}288289Path unwrap() {290return delegate;291}292293@Override294public FileSystem getFileSystem() {295return fs;296}297298@Override299public boolean isAbsolute() {300return delegate.isAbsolute();301}302303@Override304public Path getRoot() {305return fs.wrap(delegate.getRoot());306}307308@Override309public Path getParent() {310return fs.wrap(delegate.getParent());311}312313@Override314public int getNameCount() {315return delegate.getNameCount();316}317318@Override319public Path getFileName() {320return fs.wrap(delegate.getFileName());321}322323@Override324public Path getName(int index) {325return fs.wrap(delegate.getName(index));326}327328@Override329public Path subpath(int beginIndex, int endIndex) {330return fs.wrap(delegate.subpath(beginIndex, endIndex));331}332333@Override334public boolean startsWith(Path other) {335return delegate.startsWith(fs.unwrap(other));336}337338@Override339public boolean startsWith(String other) {340return delegate.startsWith(other);341}342343@Override344public boolean endsWith(Path other) {345return delegate.endsWith(fs.unwrap(other));346}347348@Override349public boolean endsWith(String other) {350return delegate.endsWith(other);351}352353@Override354public Path normalize() {355return fs.wrap(delegate.normalize());356}357358@Override359public Path resolve(Path other) {360return fs.wrap(delegate.resolve(fs.unwrap(other)));361}362363@Override364public Path resolve(String other) {365return fs.wrap(delegate.resolve(other));366}367368@Override369public Path resolveSibling(Path other) {370return fs.wrap(delegate.resolveSibling(fs.unwrap(other)));371}372373@Override374public Path resolveSibling(String other) {375return fs.wrap(delegate.resolveSibling(other));376}377378@Override379public Path relativize(Path other) {380return fs.wrap(delegate.relativize(fs.unwrap(other)));381}382383@Override384public boolean equals(Object other) {385if (!(other instanceof TestPath))386return false;387return delegate.equals(fs.unwrap((TestPath) other));388}389390@Override391public int hashCode() {392return delegate.hashCode();393}394395@Override396public String toString() {397return delegate.toString();398}399400@Override401public URI toUri() {402String ssp = delegate.toUri().getSchemeSpecificPart();403return URI.create(fs.provider().getScheme() + ":" + ssp);404}405406@Override407public Path toAbsolutePath() {408return fs.wrap(delegate.toAbsolutePath());409}410411@Override412public Path toRealPath(LinkOption... options) throws IOException {413return fs.wrap(delegate.toRealPath(options));414}415416@Override417public File toFile() {418return new File(toString());419}420421@Override422public Iterator<Path> iterator() {423final Iterator<Path> itr = delegate.iterator();424return new Iterator<Path>() {425@Override426public boolean hasNext() {427return itr.hasNext();428}429@Override430public Path next() {431return fs.wrap(itr.next());432}433@Override434public void remove() {435itr.remove();436}437};438}439440@Override441public int compareTo(Path other) {442return delegate.compareTo(fs.unwrap(other));443}444445@Override446public WatchKey register(WatchService watcher,447WatchEvent.Kind<?>[] events,448WatchEvent.Modifier... modifiers)449{450throw new UnsupportedOperationException();451}452453@Override454public WatchKey register(WatchService watcher,455WatchEvent.Kind<?>... events)456{457throw new UnsupportedOperationException();458}459}460}461462463