Merge change 6820 into donut

* changes:
  Fixed NPE bugs in Uri. Fixes internal issue #1724719. Modified getQueryParameter() to use the encoded query string. Fixes internal issue #1749094.
This commit is contained in:
Android (Google) Code Review
2009-07-10 15:12:57 -07:00
2 changed files with 46 additions and 16 deletions

View File

@@ -1083,7 +1083,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
/** Used in parcelling. */ /** Used in parcelling. */
static final int TYPE_ID = 3; static final int TYPE_ID = 3;
private final String scheme; private final String scheme; // can be null
private final Part authority; private final Part authority;
private final PathPart path; private final PathPart path;
private final Part query; private final Part query;
@@ -1092,10 +1092,10 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
private HierarchicalUri(String scheme, Part authority, PathPart path, private HierarchicalUri(String scheme, Part authority, PathPart path,
Part query, Part fragment) { Part query, Part fragment) {
this.scheme = scheme; this.scheme = scheme;
this.authority = authority; this.authority = Part.nonNull(authority);
this.path = path; this.path = path == null ? PathPart.NULL : path;
this.query = query; this.query = Part.nonNull(query);
this.fragment = fragment; this.fragment = Part.nonNull(fragment);
} }
static Uri readFrom(Parcel parcel) { static Uri readFrom(Parcel parcel) {
@@ -1158,21 +1158,18 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
} }
private void appendSspTo(StringBuilder builder) { private void appendSspTo(StringBuilder builder) {
if (authority != null) { String encodedAuthority = authority.getEncoded();
String encodedAuthority = authority.getEncoded(); if (encodedAuthority != null) {
if (encodedAuthority != null) { // Even if the authority is "", we still want to append "//".
// Even if the authority is "", we still want to append "//". builder.append("//").append(encodedAuthority);
builder.append("//").append(encodedAuthority);
}
} }
// path is never null.
String encodedPath = path.getEncoded(); String encodedPath = path.getEncoded();
if (encodedPath != null) { if (encodedPath != null) {
builder.append(encodedPath); builder.append(encodedPath);
} }
if (query != null && !query.isEmpty()) { if (!query.isEmpty()) {
builder.append('?').append(query.getEncoded()); builder.append('?').append(query.getEncoded());
} }
} }
@@ -1232,7 +1229,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
appendSspTo(builder); appendSspTo(builder);
if (fragment != null && !fragment.isEmpty()) { if (!fragment.isEmpty()) {
builder.append('#').append(fragment.getEncoded()); builder.append('#').append(fragment.getEncoded());
} }
@@ -1506,7 +1503,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
throw new UnsupportedOperationException(NOT_HIERARCHICAL); throw new UnsupportedOperationException(NOT_HIERARCHICAL);
} }
String query = getQuery(); String query = getEncodedQuery();
if (query == null) { if (query == null) {
return Collections.emptyList(); return Collections.emptyList();
} }
@@ -1569,7 +1566,7 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
throw new UnsupportedOperationException(NOT_HIERARCHICAL); throw new UnsupportedOperationException(NOT_HIERARCHICAL);
} }
String query = getQuery(); String query = getEncodedQuery();
if (query == null) { if (query == null) {
return null; return null;

View File

@@ -503,4 +503,37 @@ public class UriTest extends TestCase {
public void testEmptyToStringNotNull() { public void testEmptyToStringNotNull() {
assertNotNull(Uri.EMPTY.toString()); assertNotNull(Uri.EMPTY.toString());
} }
@SmallTest
public void testParcellingWithoutFragment() {
parcelAndUnparcel(Uri.parse("foo:bob%20lee"));
parcelAndUnparcel(Uri.fromParts("foo", "bob lee", "fragment"));
parcelAndUnparcel(new Uri.Builder()
.scheme("http")
.authority("crazybob.org")
.path("/rss/")
.encodedQuery("a=b")
.build());
}
public void testGetQueryParameter() {
String nestedUrl = "http://crazybob.org/?a=1&b=2";
Uri uri = Uri.parse("http://test/").buildUpon()
.appendQueryParameter("foo", "bar")
.appendQueryParameter("nested", nestedUrl).build();
assertEquals(nestedUrl, uri.getQueryParameter("nested"));
assertEquals(nestedUrl, uri.getQueryParameters("nested").get(0));
}
public void testGetQueryParameterWorkaround() {
// This was a workaround for a bug where getQueryParameter called
// getQuery() instead of getEncodedQuery().
String nestedUrl = "http://crazybob.org/?a=1&b=2";
Uri uri = Uri.parse("http://test/").buildUpon()
.appendQueryParameter("foo", "bar")
.appendQueryParameter("nested", Uri.encode(nestedUrl)).build();
assertEquals(nestedUrl, Uri.decode(uri.getQueryParameter("nested")));
assertEquals(nestedUrl,
Uri.decode(uri.getQueryParameters("nested").get(0)));
}
} }