diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/com/vaadin/shared/util/SharedUtil.java | 9 | ||||
-rw-r--r-- | shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java | 43 |
2 files changed, 52 insertions, 0 deletions
diff --git a/shared/src/com/vaadin/shared/util/SharedUtil.java b/shared/src/com/vaadin/shared/util/SharedUtil.java index 80efe68d83..449685705f 100644 --- a/shared/src/com/vaadin/shared/util/SharedUtil.java +++ b/shared/src/com/vaadin/shared/util/SharedUtil.java @@ -44,4 +44,13 @@ public class SharedUtil implements Serializable { return o1.equals(o2); } + /** + * Trims trailing slashes (if any) from a string. + * @param value The string value to be trimmed. Cannot be null. + * @return String value without trailing slashes. + */ + public static String trimTrailingSlashes(String value) { + return value.replaceAll("/*$", ""); + } + } diff --git a/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java new file mode 100644 index 0000000000..b593032bd6 --- /dev/null +++ b/shared/tests/src/com/vaadin/shared/util/SharedUtilTests.java @@ -0,0 +1,43 @@ +package com.vaadin.shared.util; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public class SharedUtilTests { + + private SharedUtil sut; + + @Before + public void setup() { + sut = new SharedUtil(); + } + + @Test + public void trailingSlashIsTrimmed() { + assertThat(sut.trimTrailingSlashes("/path/"), is("/path")); + } + + @Test + public void noTrailingSlashForTrimming() { + assertThat(sut.trimTrailingSlashes("/path"), is("/path")); + } + + @Test + public void trailingSlashesAreTrimmed() { + assertThat(sut.trimTrailingSlashes("/path///"), is("/path")); + } + + @Test + public void emptyStringIsHandled() { + assertThat(sut.trimTrailingSlashes(""), is("")); + } + + @Test + public void rootSlashIsTrimmed() { + assertThat(sut.trimTrailingSlashes("/"), is("")); + } + +} |