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

Commit

Permalink
Merge pull request #126 from pgottinger/master
Browse files Browse the repository at this point in the history
Enable baseDir configuration property to contain a Windows path
  • Loading branch information
timurstrekalov committed Feb 15, 2015
2 parents 3860bca + 676473a commit 8bc2270
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import org.apache.commons.lang.StringUtils;

public final class UriUtil {

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

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

if (uri.getScheme() != null) {
final Matcher matcher = supportedUriSchemeRe.matcher(uri.getScheme());
Expand All @@ -31,6 +32,17 @@ public static URI toUri(final String s) {
return new File(s).toURI().normalize();
}

private static URI createURI(final String s) {
URI uri;
if (s.contains("\\")) {
// Windows path
uri = URI.create("file:/" + s.replace("\\", "/"));
} else {
uri = URI.create(s);
}
return uri;
}

public static boolean isFileUri(final URI uri) {
return "file".equals(uri.getScheme());
}
Expand All @@ -46,10 +58,7 @@ private static Optional<String> getSegment(final URI uri, final int index) {
return Optional.absent();
}

final Iterable<String> parts = Splitter.on('/').
omitEmptyStrings().
trimResults().
split(path);
final Iterable<String> parts = Splitter.on('/').omitEmptyStrings().trimResults().split(path);

final int size = Iterables.size(parts);
final int actualIndex = index < 0 ? size + index : index;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.github.timurstrekalov.saga.core.util;

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

import com.google.common.base.Joiner;
import com.google.common.base.Optional;
import org.junit.Test;

import static com.github.timurstrekalov.saga.core.util.UriUtil.getLastSegment;
import static com.github.timurstrekalov.saga.core.util.UriUtil.getParent;
import static com.github.timurstrekalov.saga.core.util.UriUtil.getPath;
Expand All @@ -17,6 +10,14 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

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

import org.junit.Test;

import com.google.common.base.Joiner;
import com.google.common.base.Optional;

public class UriUtilTest {

private static final String PWD = System.getProperty("user.dir");
Expand Down Expand Up @@ -50,8 +51,8 @@ public void test_toUri_https() throws Exception {

@Test
public void test_toUri_file_absolute() throws Exception {
assertThat(new File(FILE_ABS_URI).getAbsolutePath(),
equalTo(new File(create("file:" + FILE_ABS.replace('\\', '/'))).getAbsolutePath()));
assertThat(new File(FILE_ABS_URI).getAbsolutePath(),
equalTo(new File(create("file:" + FILE_ABS.replace('\\', '/'))).getAbsolutePath()));
}

@Test
Expand All @@ -75,9 +76,9 @@ public void test_isFileUri() throws Exception {

@Test
public void test_getLastSegment() throws Exception {
assertThat(getLastSegment(HTTP_URI), equalTo(Optional.<String>absent()));
assertThat(getLastSegment(HTTP_URI), equalTo(Optional.<String> absent()));
assertThat(getLastSegment(HTTP_WITH_PATH_URI), equalTo(Optional.of("here")));
assertThat(getLastSegment(HTTPS_URI), equalTo(Optional.<String>absent()));
assertThat(getLastSegment(HTTPS_URI), equalTo(Optional.<String> absent()));

assertThat(getLastSegment(FILE_ABS_URI), equalTo(Optional.of("asd")));
assertThat(getLastSegment(FILE_REL_URI), equalTo(Optional.of("qweasd")));
Expand All @@ -96,8 +97,8 @@ public void test_getParent() throws Exception {
assertThat(getParent(create("http://localhost:8080/")), equalTo("/"));

assertThat(getParent(HTTP_WITH_PATH_URI), equalTo("/some/file"));
assertThat(new File(getParent(FILE_ABS_URI)).getAbsolutePath(),
equalTo(new File(File.separator + "home" + File.separator + "user").getAbsolutePath()));
assertThat(new File(getParent(FILE_ABS_URI)).getAbsolutePath(),
equalTo(new File(File.separator + "home" + File.separator + "user").getAbsolutePath()));
assertThat(new File(getParent(FILE_REL_URI)).getAbsolutePath(), equalTo(new File(PWD_PARENT).getAbsolutePath()));
}

Expand All @@ -112,4 +113,10 @@ public void test_getPath_http_uri_with_query() throws Exception {
assertThat(getPath(URI.create("http://localhost/Class.js?123")), equalTo("http://localhost/Class.js?123"));
}

@Test
public void test_toUri_with_windows_filepath() throws Exception {
URI uri = UriUtil.toUri("c:\\Users\\timur\\workspace\\saga");
assertThat(uri.toString(), equalTo("file:/c:/Users/timur/workspace/saga"));
}

}

0 comments on commit 8bc2270

Please sign in to comment.