You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@shindig.apache.org by et...@apache.org on 2008/10/01 21:11:41 UTC
svn commit: r700874 - in /incubator/shindig/trunk/java/common/src:
main/java/org/apache/shindig/common/uri/Uri.java
test/java/org/apache/shindig/common/uri/UriTest.java
Author: etnu
Date: Wed Oct 1 12:11:40 2008
New Revision: 700874
URL: http://svn.apache.org/viewvc?rev=700874&view=rev
Log:
Added utility methods resolve and isAbsolute to Uri to support more features from java.net.URI.
Modified:
incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriTest.java
Modified: incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java?rev=700874&r1=700873&r2=700874&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java (original)
+++ incubator/shindig/trunk/java/common/src/main/java/org/apache/shindig/common/uri/Uri.java Wed Oct 1 12:11:40 2008
@@ -104,7 +104,31 @@
* @return a java.net.URI equal to this Uri.
*/
public URI toJavaUri() {
- return URI.create(toString());
+ try {
+ return new URI(scheme, authority, path, query, fragment);
+ } catch (URISyntaxException e) {
+ // Shouldn't ever happen.
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ /**
+ * Resolves a given url relative to this url. Resolution rules are the same as for
+ * {@code java.net.URI.resolve(URI)}
+ *
+ * @param other The url to resolve against.
+ * @return The new url.
+ */
+ public Uri resolve(Uri other) {
+ // TODO: We can probably make this more efficient by implementing resolve ourselves.
+ return fromJavaUri(toJavaUri().resolve(other.toJavaUri()));
+ }
+
+ /**
+ * @return True if the Uri is absolute.
+ */
+ public boolean isAbsolute() {
+ return scheme != null;
}
/**
@@ -146,7 +170,7 @@
* @return All query parameters with the given name.
*/
public Collection<String> getQueryParameters(String name) {
- return queryParameters.get(name);
+ return queryParameters.get(name);
}
/**
Modified: incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriTest.java
URL: http://svn.apache.org/viewvc/incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriTest.java?rev=700874&r1=700873&r2=700874&view=diff
==============================================================================
--- incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriTest.java (original)
+++ incubator/shindig/trunk/java/common/src/test/java/org/apache/shindig/common/uri/UriTest.java Wed Oct 1 12:11:40 2008
@@ -18,8 +18,10 @@
package org.apache.shindig.common.uri;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+
import org.junit.Test;
import java.net.URI;
@@ -149,6 +151,66 @@
}
@Test
+ public void resolveFragment() throws Exception {
+ Uri base = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("#bar");
+
+ assertEquals("http://example.org/foo/bar/baz?blah=blah#bar", base.resolve(other).toString());
+ }
+
+ @Test
+ public void resolveQuery() throws Exception {
+ Uri base = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("?hello=world");
+
+ assertEquals("http://example.org/foo/bar/?hello=world", base.resolve(other).toString());
+ }
+
+ @Test
+ public void resolvePathRelative() throws Exception {
+ Uri base = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("wee");
+
+ assertEquals("http://example.org/foo/bar/wee", base.resolve(other).toString());
+ }
+
+ @Test
+ public void resolvePathAbsolute() throws Exception {
+ Uri base = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("/blah");
+
+ assertEquals("http://example.org/blah", base.resolve(other).toString());
+ }
+
+ @Test
+ public void resolveAuthority() throws Exception {
+ Uri base = Uri.parse("https://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("//example.com/blah");
+
+ assertEquals("https://example.com/blah", base.resolve(other).toString());
+ }
+
+ @Test
+ public void resolveAbsolute() throws Exception {
+ Uri base = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
+ Uri other = Uri.parse("http://www.ietf.org/rfc/rfc2396.txt");
+
+ assertEquals("http://www.ietf.org/rfc/rfc2396.txt", base.resolve(other).toString());
+ }
+
+ @Test
+ public void absoluteUrlIsAbsolute() {
+ assertTrue("Url with scheme not reported absolute.",
+ Uri.parse("http://example.org/foo").isAbsolute());
+ }
+
+ @Test
+ public void relativeUrlIsNotAbsolute() {
+ assertFalse("Url without scheme reported absolute.",
+ Uri.parse("//example.org/foo").isAbsolute());
+ }
+
+ @Test
public void equalsAndHashCodeOk() {
Uri uri = Uri.parse("http://example.org/foo/bar/baz?blah=blah#boo");
Uri uri2 = new UriBuilder()
@@ -160,7 +222,7 @@
.toUri();
assertEquals(uri, uri2);
- assertEquals(uri2, uri);
+ assertEquals(uri2, uri);
assertTrue(uri.hashCode() == uri2.hashCode());
}