From c31d5805dd888c1fa63c61d884370da0c615fff8 Mon Sep 17 00:00:00 2001 From: Lyla Date: Mon, 9 Feb 2015 16:07:31 -0800 Subject: [PATCH] 4.09 Write the urimatcher --- .../sunshine/app/data/TestUriMatcher.java | 25 +++++++++--------- .../sunshine/app/data/WeatherProvider.java | 26 ++++++++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java index 68b32c3de..ab692eb58 100644 --- a/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java +++ b/app/src/androidTest/java/com/example/android/sunshine/app/data/TestUriMatcher.java @@ -15,6 +15,7 @@ */ package com.example.android.sunshine.app.data; +import android.content.UriMatcher; import android.net.Uri; import android.test.AndroidTestCase; @@ -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); + } } diff --git a/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java b/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java index 7ff87632d..b7f17af36 100644 --- a/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java +++ b/app/src/main/java/com/example/android/sunshine/app/data/WeatherProvider.java @@ -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; } /*