Skip to content

Commit

Permalink
S09.01-Exercise-ContentProviderFoundation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Silver committed Jan 19, 2017
1 parent b2e7a10 commit a12edc1
Show file tree
Hide file tree
Showing 9 changed files with 596 additions and 27 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ android {
minifyEnabled false
}
}
productFlavors {
}
}

dependencies {
Expand All @@ -25,7 +27,6 @@ dependencies {

compile 'com.android.support:recyclerview-v7:25.0.1'
compile 'com.android.support:preference-v7:25.0.1'

// Instrumentation dependencies use androidTestCompile
// (as opposed to testCompile for local unit tests run in the JVM)
androidTestCompile 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.sunshine.data;

import android.content.UriMatcher;
import android.net.Uri;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import static com.example.android.sunshine.data.TestUtilities.getStaticIntegerField;
import static com.example.android.sunshine.data.TestUtilities.studentReadableNoSuchField;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;

@RunWith(AndroidJUnit4.class)
public class TestUriMatcher {

private static final Uri TEST_WEATHER_DIR = WeatherContract.WeatherEntry.CONTENT_URI;
private static final Uri TEST_WEATHER_WITH_DATE_DIR = WeatherContract.WeatherEntry
.buildWeatherUriWithDate(TestUtilities.DATE_NORMALIZED);

private static final String weatherCodeVariableName = "CODE_WEATHER";
private static int REFLECTED_WEATHER_CODE;

private static final String weatherCodeWithDateVariableName = "CODE_WEATHER_WITH_DATE";
private static int REFLECTED_WEATHER_WITH_DATE_CODE;

private UriMatcher testMatcher;

@Before
public void before() {
try {

Method buildUriMatcher = WeatherProvider.class.getDeclaredMethod("buildUriMatcher");
testMatcher = (UriMatcher) buildUriMatcher.invoke(WeatherProvider.class);

REFLECTED_WEATHER_CODE = getStaticIntegerField(
WeatherProvider.class,
weatherCodeVariableName);

REFLECTED_WEATHER_WITH_DATE_CODE = getStaticIntegerField(
WeatherProvider.class,
weatherCodeWithDateVariableName);

} catch (NoSuchFieldException e) {
fail(studentReadableNoSuchField(e));
} catch (IllegalAccessException e) {
fail(e.getMessage());
} catch (NoSuchMethodException e) {
String noBuildUriMatcherMethodFound =
"It doesn't appear that you have created a method called buildUriMatcher in " +
"the WeatherProvider class.";
fail(noBuildUriMatcherMethodFound);
} catch (InvocationTargetException e) {
fail(e.getMessage());
}
}

/**
* Students: This function tests that your UriMatcher returns the correct integer value for
* each of the Uri types that our ContentProvider can handle. Uncomment this when you are
* ready to test your UriMatcher.
*/
@Test
public void testUriMatcher() {

/* Test that the code returned from our matcher matches the expected weather code */
String weatherUriDoesNotMatch = "Error: The CODE_WEATHER URI was matched incorrectly.";
int actualWeatherCode = testMatcher.match(TEST_WEATHER_DIR);
int expectedWeatherCode = REFLECTED_WEATHER_CODE;
assertEquals(weatherUriDoesNotMatch,
expectedWeatherCode,
actualWeatherCode);

/*
* Test that the code returned from our matcher matches the expected weather with date code
*/
String weatherWithDateUriCodeDoesNotMatch =
"Error: The CODE_WEATHER WITH DATE URI was matched incorrectly.";
int actualWeatherWithDateCode = testMatcher.match(TEST_WEATHER_WITH_DATE_DIR);
int expectedWeatherWithDateCode = REFLECTED_WEATHER_WITH_DATE_CODE;
assertEquals(weatherWithDateUriCodeDoesNotMatch,
expectedWeatherWithDateCode,
actualWeatherWithDateCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,17 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_DATE;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_HUMIDITY;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_MAX;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_MIN;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_PRESSURE;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_WEATHER_ID;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_WIND_DIR;
import static com.example.android.sunshine.data.TestSunshineDatabase.REFLECTED_COLUMN_WIND_SPEED;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_DATE;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_DEGREES;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_HUMIDITY;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_MAX_TEMP;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_MIN_TEMP;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_PRESSURE;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_WEATHER_ID;
import static com.example.android.sunshine.data.WeatherContract.WeatherEntry.COLUMN_WIND_SPEED;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

/**
Expand All @@ -67,6 +68,10 @@ class TestUtilities {
* @param expectedValues The values we expect to receive in valueCursor
*/
static void validateThenCloseCursor(String error, Cursor valueCursor, ContentValues expectedValues) {
assertNotNull(
"This cursor is null. Did you make sure to register your ContentProvider in the manifest?",
valueCursor);

assertTrue("Empty cursor returned. " + error, valueCursor.moveToFirst());
validateCurrentRecord(error, valueCursor, expectedValues);
valueCursor.close();
Expand Down Expand Up @@ -115,14 +120,14 @@ static ContentValues createTestWeatherContentValues() {

ContentValues testWeatherValues = new ContentValues();

testWeatherValues.put(REFLECTED_COLUMN_DATE, DATE_NORMALIZED);
testWeatherValues.put(REFLECTED_COLUMN_WIND_DIR, 1.1);
testWeatherValues.put(REFLECTED_COLUMN_HUMIDITY, 1.2);
testWeatherValues.put(REFLECTED_COLUMN_PRESSURE, 1.3);
testWeatherValues.put(REFLECTED_COLUMN_MAX, 75);
testWeatherValues.put(REFLECTED_COLUMN_MIN, 65);
testWeatherValues.put(REFLECTED_COLUMN_WIND_SPEED, 5.5);
testWeatherValues.put(REFLECTED_COLUMN_WEATHER_ID, 321);
testWeatherValues.put(COLUMN_DATE, DATE_NORMALIZED);
testWeatherValues.put(COLUMN_DEGREES, 1.1);
testWeatherValues.put(COLUMN_HUMIDITY, 1.2);
testWeatherValues.put(COLUMN_PRESSURE, 1.3);
testWeatherValues.put(COLUMN_MAX_TEMP, 75);
testWeatherValues.put(COLUMN_MIN_TEMP, 65);
testWeatherValues.put(COLUMN_WIND_SPEED, 5.5);
testWeatherValues.put(COLUMN_WEATHER_ID, 321);

return testWeatherValues;
}
Expand Down Expand Up @@ -152,14 +157,14 @@ static ContentValues[] createBulkInsertTestWeatherValues() {

ContentValues weatherValues = new ContentValues();

weatherValues.put(REFLECTED_COLUMN_DATE, normalizedTestDate);
weatherValues.put(REFLECTED_COLUMN_WIND_DIR, 1.1);
weatherValues.put(REFLECTED_COLUMN_HUMIDITY, 1.2 + 0.01 * (float) i);
weatherValues.put(REFLECTED_COLUMN_PRESSURE, 1.3 - 0.01 * (float) i);
weatherValues.put(REFLECTED_COLUMN_MAX, 75 + i);
weatherValues.put(REFLECTED_COLUMN_MIN, 65 - i);
weatherValues.put(REFLECTED_COLUMN_WIND_SPEED, 5.5 + 0.2 * (float) i);
weatherValues.put(REFLECTED_COLUMN_WEATHER_ID, 321);
weatherValues.put(COLUMN_DATE, normalizedTestDate);
weatherValues.put(COLUMN_DEGREES, 1.1);
weatherValues.put(COLUMN_HUMIDITY, 1.2 + 0.01 * (float) i);
weatherValues.put(COLUMN_PRESSURE, 1.3 - 0.01 * (float) i);
weatherValues.put(COLUMN_MAX_TEMP, 75 + i);
weatherValues.put(COLUMN_MIN_TEMP, 65 - i);
weatherValues.put(COLUMN_WIND_SPEED, 5.5 + 0.2 * (float) i);
weatherValues.put(COLUMN_WEATHER_ID, 321);

bulkTestWeatherValues[i] = weatherValues;
}
Expand Down
Loading

0 comments on commit a12edc1

Please sign in to comment.