Back to project page Sunshine.
The source code is released under:
MIT License
If you think the Android project Sunshine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package cloudchen.com.sunshine; // www. j a va 2s. c om import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.test.AndroidTestCase; import android.util.Log; import java.util.Map; import java.util.Set; import cloudchen.com.sunshine.data.WeatherContract.LocationEntry; import cloudchen.com.sunshine.data.WeatherContract.WeatherEntry; import cloudchen.com.sunshine.data.WeatherDbHelper; import cloudchen.com.sunshine.data.WeatherProvider; public class TestProvider extends AndroidTestCase { private static final String LOG_TAG = TestProvider.class.getName(); private static final String TEST_CITY_NAME = "North Pole"; private static final String TEST_LOCATION = "99705"; private static final String TEST_DATE = "20141205"; public void testInsertReadDb() { // If there's an error in those massive SQL table creation Strings, // errors will be thrown here when you try to get a writable database. WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues locationValues = createNorthPoleLocationValues(); Uri locationInsertUri = mContext.getContentResolver().insert(LocationEntry.CONTENT_URI, locationValues); long locationRowId = ContentUris.parseId(locationInsertUri); // Verify we got a row back. assertTrue(locationRowId != -1); Log.d(LOG_TAG, "New row id: " + locationRowId); // Data's inserted. IN THEORY. Now pull some out to stare at it and verify it made // the round trip. // A cursor is your primary interface to the query results. Cursor cursor = db.query( LocationEntry.TABLE_NAME, // Table to Query null, // all columns null, // Columns for the "where" clause null, // Values for the "where" clause null, // columns to group by null, // columns to filter by row groups null // sort order ); validateCursor(cursor, locationValues); // Fantastic. Now that we have a location, add some weather! ContentValues weatherValues = createWeatherValues(locationRowId); Uri weatherInsertUri = mContext.getContentResolver().insert(WeatherEntry.CONTENT_URI, weatherValues); assertTrue(weatherInsertUri != null); // A cursor is your primary interface to the query results. Cursor weatherCursor = mContext.getContentResolver().query( WeatherEntry.CONTENT_URI, null, // leaving "columns" null just returns all the columns. null, // cols for "where" clause null, // values for "where" clause null // sort order ); validateCursor(weatherCursor, weatherValues); weatherCursor.close(); dbHelper.close(); } private static ContentValues createNorthPoleLocationValues() { ContentValues values = new ContentValues(); values.put(LocationEntry.COLUMN_LOCATION_SETTING, TEST_LOCATION); values.put(LocationEntry.COLUMN_CITY_NAME, TEST_CITY_NAME); values.put(LocationEntry.COLUMN_COORD_LAT, 64.7488); values.put(LocationEntry.COLUMN_COORD_LONG, -147.353); return values; } private static ContentValues createWeatherValues(long locationRowId) { ContentValues weatherValues = new ContentValues(); weatherValues.put(WeatherEntry.COLUMN_LOC_KEY, locationRowId); weatherValues.put(WeatherEntry.COLUMN_DATETEXT, TEST_DATE); weatherValues.put(WeatherEntry.COLUMN_DEGREES, 1.1); weatherValues.put(WeatherEntry.COLUMN_HUMIDITY, 1.2); weatherValues.put(WeatherEntry.COLUMN_PRESSURE, 1.3); weatherValues.put(WeatherEntry.COLUMN_MAX_TEMP, 75); weatherValues.put(WeatherEntry.COLUMN_MIN_TEMP, 65); weatherValues.put(WeatherEntry.COLUMN_SHORT_DESC, "Asteroids"); weatherValues.put(WeatherEntry.COLUMN_WIND_SPEED, 5.5); weatherValues.put(WeatherEntry.COLUMN_WEATHER_ID, 321); return weatherValues; } private static void validateCursor(Cursor valueCursor, ContentValues expectedValues) { assertTrue(valueCursor.moveToFirst()); Set<Map.Entry<String, Object>> valueSet = expectedValues.valueSet(); for (Map.Entry<String, Object> entry : valueSet) { String columnName = entry.getKey(); int idx = valueCursor.getColumnIndex(columnName); assertFalse(idx == -1); String expectedValue = entry.getValue().toString(); assertEquals(expectedValue, valueCursor.getString(idx)); } valueCursor.close(); } public void testGetType() { // content://com.example.android.sunshine.app/weather/ String type = mContext.getContentResolver().getType(WeatherEntry.CONTENT_URI); // vnd.android.cursor.dir/com.example.android.sunshine.app/weather assertEquals(WeatherEntry.CONTENT_TYPE, type); String testLocation = "94074"; // content://com.example.android.sunshine.app/weather/94074 type = mContext.getContentResolver().getType( WeatherEntry.buildWeatherLocation(testLocation)); // vnd.android.cursor.dir/com.example.android.sunshine.app/weather assertEquals(WeatherEntry.CONTENT_TYPE, type); String testDate = "20140612"; // content://com.example.android.sunshine.app/weather/94074/20140612 type = mContext.getContentResolver().getType( WeatherEntry.buildWeatherLocationWithDate(testLocation, testDate)); // vnd.android.cursor.item/com.example.android.sunshine.app/weather assertEquals(WeatherEntry.CONTENT_ITEM_TYPE, type); // content://com.example.android.sunshine.app/location/ type = mContext.getContentResolver().getType(LocationEntry.CONTENT_URI); // vnd.android.cursor.dir/com.example.android.sunshine.app/location assertEquals(LocationEntry.CONTENT_TYPE, type); // content://com.example.android.sunshine.app/location/1 type = mContext.getContentResolver().getType(LocationEntry.buildLocationUri(1L)); // vnd.android.cursor.item/com.example.android.sunshine.app/location assertEquals(LocationEntry.CONTENT_ITEM_TYPE, type); } // brings our database to an empty state public void deleteAllRecords() { mContext.getContentResolver().delete( WeatherEntry.CONTENT_URI, null, null ); mContext.getContentResolver().delete( LocationEntry.CONTENT_URI, null, null ); Cursor cursor = mContext.getContentResolver().query( WeatherEntry.CONTENT_URI, null, null, null, null ); assertEquals(0, cursor.getCount()); cursor.close(); cursor = mContext.getContentResolver().query( LocationEntry.CONTENT_URI, null, null, null, null ); assertEquals(0, cursor.getCount()); cursor.close(); } // Since we want each test to start with a clean slate, run deleteAllRecords // in setUp (called by the test runner before each test). public void setUp() { deleteAllRecords(); } public void testUpdateLocation() { // Create a new map of values, where column names are the keys ContentValues values = TestDb.createNorthPoleLocationValues(); Uri locationUri = mContext.getContentResolver(). insert(LocationEntry.CONTENT_URI, values); long locationRowId = ContentUris.parseId(locationUri); // Verify we got a row back. assertTrue(locationRowId != -1); Log.d(LOG_TAG, "New row id: " + locationRowId); ContentValues updatedValues = new ContentValues(values); updatedValues.put(LocationEntry._ID, locationRowId); updatedValues.put(LocationEntry.COLUMN_CITY_NAME, "Santa's Village"); int count = mContext.getContentResolver().update( LocationEntry.CONTENT_URI, updatedValues, LocationEntry._ID + " = ?", new String[] { Long.toString(locationRowId)}); assertEquals(count, 1); // A cursor is your primary interface to the query results. Cursor cursor = mContext.getContentResolver().query( LocationEntry.buildLocationUri(locationRowId), null, null, // Columns for the "where" clause null, // Values for the "where" clause null // sort order ); TestDb.validateCursor(cursor, updatedValues); } // Make sure we can still delete after adding/updating stuff public void testDeleteRecordsAtEnd() { deleteAllRecords(); } }