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

Commit

Permalink
4.09 Write the urimatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyla committed Mar 4, 2015
1 parent 37e439c commit c31d580
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.example.android.sunshine.app.data;

import android.content.UriMatcher;
import android.net.Uri;
import android.test.AndroidTestCase;

Expand All @@ -41,16 +42,16 @@ public class TestUriMatcher extends AndroidTestCase {
for each of the Uri types that our ContentProvider can handle. Uncomment this when you are
ready to test your UriMatcher.
*/
// public void testUriMatcher() {
// UriMatcher testMatcher = WeatherProvider.buildUriMatcher();
//
// assertEquals("Error: The WEATHER URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
// assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
// assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
// testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
// assertEquals("Error: The LOCATION URI was matched incorrectly.",
// testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
// }
public void testUriMatcher() {
UriMatcher testMatcher = WeatherProvider.buildUriMatcher();

assertEquals("Error: The WEATHER URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_DIR), WeatherProvider.WEATHER);
assertEquals("Error: The WEATHER WITH LOCATION URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_WITH_LOCATION_DIR), WeatherProvider.WEATHER_WITH_LOCATION);
assertEquals("Error: The WEATHER WITH LOCATION AND DATE URI was matched incorrectly.",
testMatcher.match(TEST_WEATHER_WITH_LOCATION_AND_DATE_DIR), WeatherProvider.WEATHER_WITH_LOCATION_AND_DATE);
assertEquals("Error: The LOCATION URI was matched incorrectly.",
testMatcher.match(TEST_LOCATION_DIR), WeatherProvider.LOCATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,22 @@ private Cursor getWeatherByLocationSettingAndDate(
testUriMatcher test within TestUriMatcher.
*/
static UriMatcher buildUriMatcher() {
// 1) The code passed into the constructor represents the code to return for the root
// URI. It's common to use NO_MATCH as the code for this case. Add the constructor below.


// 2) Use the addURI function to match each of the types. Use the constants from
// WeatherContract to help define the types to the UriMatcher.


// 3) Return the new matcher!
return null;
// I know what you're thinking. Why create a UriMatcher when you can use regular
// expressions instead? Because you're not crazy, that's why.

// All paths added to the UriMatcher have a corresponding code to return when a match is
// found. The code passed into the constructor represents the code to return for the root
// URI. It's common to use NO_MATCH as the code for this case.
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
final String authority = WeatherContract.CONTENT_AUTHORITY;

// For each type of URI you want to add, create a corresponding code.
matcher.addURI(authority, WeatherContract.PATH_WEATHER, WEATHER);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*", WEATHER_WITH_LOCATION);
matcher.addURI(authority, WeatherContract.PATH_WEATHER + "/*/#", WEATHER_WITH_LOCATION_AND_DATE);

matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION);
return matcher;
}

/*
Expand Down

1 comment on commit c31d580

@mahye82
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a minor error in the testUriMatcher() method of TestUriMatcher.java. It makes use of assertEquals.

Correct order of parameters for assertEquals():
assertEquals(String message, int expected, int actual)

However, the code above has been written as if assertEquals() was instead:
assertEquals(String message, int actual, int expected)

Please sign in to comment.