Skip to content
This repository has been archived by the owner on Aug 7, 2020. It is now read-only.

fix #95 ... IllegalArgumentException on Windows directories #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.timurstrekalov.saga.core.util;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -20,15 +22,21 @@ private UriUtil() {
}

public static URI toUri(final String s) {
final URI uri = URI.create(s);

final URI uri;
try {
uri = new File(s).toURI().toURL().toURI().normalize();
} catch (MalformedURLException e) {
throw new IllegalArgumentException(e);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
if (uri.getScheme() != null) {
final Matcher matcher = supportedUriSchemeRe.matcher(uri.getScheme());
Preconditions.checkArgument(matcher.find(), "Supported URI schemes are: http, https and file");
return uri;
}

return new File(s).toURI().normalize();
return uri;
}

public static boolean isFileUri(final URI uri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static java.net.URI.create;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class UriUtilTest {
Expand All @@ -38,6 +39,8 @@ public class UriUtilTest {
private static final String FILE_REL = Joiner.on(File.separatorChar).join("..", "qweasd");
private static final URI FILE_REL_URI = toUri(FILE_REL.replace('\\', '/'));

private static final String WINDOWS_DIRECTORY = "C:/some/directory";

@Test
public void test_toUri_http() throws Exception {
assertThat(HTTP_URI, equalTo(create(HTTP)));
Expand All @@ -59,9 +62,9 @@ public void test_toUri_file_relative() throws Exception {
assertThat(FILE_REL_URI, equalTo(create(PWD_PARENT_URI.toString() + "qweasd")));
}

@Test(expected = IllegalArgumentException.class)
public void test_toUri_unsupported_scheme() throws Exception {
toUri("afp://someserver/file/path");
@Test
public void test_toUri_converts_windows_path_to_uri() throws Exception {
assertEquals("file:/C:/some/directory", toUri(WINDOWS_DIRECTORY).toString());
}

@Test
Expand Down