Skip to content
This repository was archived by the owner on Jun 23, 2022. It is now read-only.

Commit c31d580

Browse files
author
Lyla
committed
4.09 Write the urimatcher
1 parent 37e439c commit c31d580

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.example.android.sunshine.app.data;
1717

18+
import android.content.UriMatcher;
1819
import android.net.Uri;
1920
import android.test.AndroidTestCase;
2021

@@ -41,16 +42,16 @@ public class TestUriMatcher extends AndroidTestCase {
4142
for each of the Uri types that our ContentProvider can handle. Uncomment this when you are
4243
ready to test your UriMatcher.
4344
*/
44-
// public void testUriMatcher() {
45-
// UriMatcher testMatcher = WeatherProvider.buildUriMatcher();
46-
//
47-
// assertEquals("Error: The WEATHER URI was matched incorrectly.",
48-
// testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
49-
// assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
50-
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
51-
// assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
52-
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
53-
// assertEquals("Error: The LOCATION URI was matched incorrectly.",
54-
// testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
55-
// }
45+
public void testUriMatcher() {
46+
UriMatcher testMatcher = WeatherProvider.buildUriMatcher();
47+
48+
assertEquals("Error: The WEATHER URI was matched incorrectly.",
49+
testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
50+
assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
51+
testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
52+
assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
53+
testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
54+
assertEquals("Error: The LOCATION URI was matched incorrectly.",
55+
testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
56+
}
5657
}

app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,22 @@ private Cursor getWeatherByLocationSettingAndDate(
115115
testUriMatcher test within TestUriMatcher.
116116
*/
117117
static UriMatcher buildUriMatcher() {
118-
// 1) The code passed into the constructor represents the code to return for the root
119-
// URI. It's common to use NO_MATCH as the code for this case. Add the constructor below.
120-
121-
122-
// 2) Use the addURI function to match each of the types. Use the constants from
123-
// WeatherContract to help define the types to the UriMatcher.
124-
125-
126-
// 3) Return the new matcher!
127-
return null;
118+
// I know what you're thinking. Why create a UriMatcher when you can use regular
119+
// expressions instead? Because you're not crazy, that's why.
120+
121+
// All paths added to the UriMatcher have a corresponding code to return when a match is
122+
// found. The code passed into the constructor represents the code to return for the root
123+
// URI. It's common to use NO_MATCH as the code for this case.
124+
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
125+
final String authority = WeatherContract.CONTENT_AUTHORITY;
126+
127+
// For each type of URI you want to add, create a corresponding code.
128+
matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER);
129+
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION);
130+
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE);
131+
132+
matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION);
133+
return matcher;
128134
}
129135

130136
/*

0 commit comments

Comments
 (0)