google/jimfs

URISyntaxException with jimfs URI

uhrm opened this issue · 1 comments

uhrm commented

The following program

package test;

import java.net.URI;
import java.nio.file.Paths;

public class App
{
    public static void main(String[] args) throws Exception
    {
        Paths.get(new URI("jimfs:/"));
    }
}

throws an AssertionError

Exception in thread "main" java.lang.AssertionError: java.net.URISyntaxException: Expected scheme-specific part at index 6: jimfs:
	at com.google.common.jimfs.SystemJimfsFileSystemProvider.toFileSystemUri(SystemJimfsFileSystemProvider.java:166)
	at com.google.common.jimfs.SystemJimfsFileSystemProvider.getPath(SystemJimfsFileSystemProvider.java:144)
	at java.base/java.nio.file.Path.of(Path.java:208)
	at java.base/java.nio.file.Paths.get(Paths.java:98)
	at ch.rohag.test.App.main(App.java:10)
Caused by: java.net.URISyntaxException: Expected scheme-specific part at index 6: jimfs:
	at java.base/java.net.URI$Parser.fail(URI.java:2936)
	at java.base/java.net.URI$Parser.failExpecting(URI.java:2942)
	at java.base/java.net.URI$Parser.parse(URI.java:3142)
	at java.base/java.net.URI.<init>(URI.java:708)
	at com.google.common.jimfs.SystemJimfsFileSystemProvider.toFileSystemUri(SystemJimfsFileSystemProvider.java:163)
	... 4 more

I would have expected it to return the root path of a jimfs file system.

Jimfs allows for multiple independent file systems to be created; each one has an identifier which must be present in the URI. And before you can get a Jimfs Path by URI, you need to have created a file system in the first place. If you want to create a new FileSystem and get the root path, you'll want do something like this:

Path root = Jimfs.newFileSystem(Configuration.unix()).getPath("/");

// If you want to get the URI of that file:
URI rootUri = root.toUri();
assertThat(Paths.get(rootUri)).isEqualTo(root);

In general I wouldn't recommend trying to do direct URI manipulation with Jimfs. It's possible, but you need to be aware of what the actual URI structure is.