Skip to content

Commit

Permalink
Приведение к одинаковому поведению для uri файловых схемы без authority
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 committed Feb 6, 2020
1 parent e87d4be commit 947feab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/main/java/com/github/_1c_syntax/utils/Absolute.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,17 @@ public static URI uri(String uri) {
}

public static URI uri(URI uri) {
return URI.create(uri.getScheme() + ":" + uri.getSchemeSpecificPart());
var decodedUri = URI.create(uri.getScheme() + ":" + uri.getSchemeSpecificPart());

if ("file".equals(decodedUri.getScheme()) && decodedUri.getAuthority() == null) {
return path(new File(decodedUri)).toUri();
}

return decodedUri;
}

public static URI uri(File file) {
return uri(path(file).toUri());
}

public static Path path(String path) {
Expand Down
24 changes: 21 additions & 3 deletions src/test/java/com/github/_1c_syntax/utils/AbsoluteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.net.URI;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -55,11 +56,28 @@ void testUriFromFile() {
var file = new File("/fake.bsl");

// when
var uri = Absolute.uri(file.toURI());
var uriFromUri = Absolute.uri(file.toURI());
var uriFromFile = Absolute.uri(file);

// then
assertThat(uri).hasScheme("file");
assertThat(uri.getPath()).endsWith("fake.bsl");
assertThat(uriFromUri).isEqualTo(uriFromFile);
assertThat(uriFromFile).hasScheme("file");
assertThat(uriFromFile.getPath()).endsWith("fake.bsl");
}

@Test
void testUriFromString() {
// given
var uriString = "file:///fake.bsl";

// when
var uriFromString = Absolute.uri(uriString);
var uriFromFile = Absolute.uri(new File(URI.create(uriString)));

// then
assertThat(uriFromString).isEqualTo(uriFromFile);
assertThat(uriFromFile).hasScheme("file");
assertThat(uriFromFile.getPath()).endsWith("fake.bsl");
}

@Test
Expand Down

0 comments on commit 947feab

Please sign in to comment.