Loading core/java/android/content/ContentProviderOperation.java +29 −18 Original line number Diff line number Diff line Loading @@ -221,17 +221,20 @@ public class ContentProviderOperation implements Parcelable { } else if (mType == TYPE_UPDATE) { numRows = provider.update(mUri, values, mSelection, selectionArgs); } else if (mType == TYPE_ASSERT) { // Assert that all rows match expected values String[] projection = null; if (values != null) { // Build projection map from expected values final ArrayList<String> projectionList = new ArrayList<String>(); for (Map.Entry<String, Object> entry : values.valueSet()) { projectionList.add(entry.getKey()); } // Assert that all rows match expected values final String[] projection = projectionList.toArray(new String[projectionList.size()]); projection = projectionList.toArray(new String[projectionList.size()]); } final Cursor cursor = provider.query(mUri, projection, mSelection, selectionArgs, null); numRows = cursor.getCount(); try { numRows = cursor.getCount(); if (projection != null) { while (cursor.moveToNext()) { for (int i = 0; i < projection.length; i++) { final String cursorValue = cursor.getString(i); Loading @@ -244,6 +247,7 @@ public class ContentProviderOperation implements Parcelable { } } } } } finally { cursor.close(); } Loading Loading @@ -395,12 +399,19 @@ public class ContentProviderOperation implements Parcelable { /** Create a ContentProviderOperation from this {@link Builder}. */ public ContentProviderOperation build() { if (mType == TYPE_UPDATE || mType == TYPE_ASSERT) { if (mType == TYPE_UPDATE) { if ((mValues == null || mValues.size() == 0) && (mValuesBackReferences == null || mValuesBackReferences.size() == 0)) { throw new IllegalArgumentException("Empty values"); } } if (mType == TYPE_ASSERT) { if ((mValues == null || mValues.size() == 0) && (mValuesBackReferences == null || mValuesBackReferences.size() == 0) && (mExpectedCount == null)) { throw new IllegalArgumentException("Empty values"); } } return new ContentProviderOperation(this); } Loading tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java +56 −4 Original line number Diff line number Diff line Loading @@ -29,7 +29,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Hashtable; import java.util.Set; import java.util.Map; import java.util.Map.Entry; Loading Loading @@ -145,7 +144,7 @@ public class ContentProviderOperationTest extends TestCase { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1); return getCursor(sTestValues1, 1); } }, null, 0); } catch (OperationApplicationException e) { Loading @@ -153,11 +152,62 @@ public class ContentProviderOperationTest extends TestCase { } } public void testAssertNoValues() { // Build an operation to assert values match provider ContentProviderOperation op1 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(1).build(); try { // Assert that values match from cursor ContentProviderResult result = op1.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 1); } }, null, 0); } catch (OperationApplicationException e) { fail("newAssert() failed"); } ContentProviderOperation op2 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(0).build(); try { // Assert that values match from cursor ContentProviderResult result = op2.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 0); } }, null, 0); } catch (OperationApplicationException e) { fail("newAssert() failed"); } ContentProviderOperation op3 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(2).build(); try { // Assert that values match from cursor ContentProviderResult result = op3.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 5); } }, null, 0); fail("we expect the exception to be thrown"); } catch (OperationApplicationException e) { } } /** * Build a {@link Cursor} with a single row that contains all values * provided through the given {@link ContentValues}. */ private Cursor getCursor(ContentValues contentValues) { private Cursor getCursor(ContentValues contentValues, int numRows) { final Set<Entry<String, Object>> valueSet = contentValues.valueSet(); final String[] keys = new String[valueSet.size()]; final Object[] values = new Object[valueSet.size()]; Loading @@ -170,7 +220,9 @@ public class ContentProviderOperationTest extends TestCase { } final MatrixCursor cursor = new MatrixCursor(keys); for (i = 0; i < numRows; i++) { cursor.addRow(values); } return cursor; } Loading Loading
core/java/android/content/ContentProviderOperation.java +29 −18 Original line number Diff line number Diff line Loading @@ -221,17 +221,20 @@ public class ContentProviderOperation implements Parcelable { } else if (mType == TYPE_UPDATE) { numRows = provider.update(mUri, values, mSelection, selectionArgs); } else if (mType == TYPE_ASSERT) { // Assert that all rows match expected values String[] projection = null; if (values != null) { // Build projection map from expected values final ArrayList<String> projectionList = new ArrayList<String>(); for (Map.Entry<String, Object> entry : values.valueSet()) { projectionList.add(entry.getKey()); } // Assert that all rows match expected values final String[] projection = projectionList.toArray(new String[projectionList.size()]); projection = projectionList.toArray(new String[projectionList.size()]); } final Cursor cursor = provider.query(mUri, projection, mSelection, selectionArgs, null); numRows = cursor.getCount(); try { numRows = cursor.getCount(); if (projection != null) { while (cursor.moveToNext()) { for (int i = 0; i < projection.length; i++) { final String cursorValue = cursor.getString(i); Loading @@ -244,6 +247,7 @@ public class ContentProviderOperation implements Parcelable { } } } } } finally { cursor.close(); } Loading Loading @@ -395,12 +399,19 @@ public class ContentProviderOperation implements Parcelable { /** Create a ContentProviderOperation from this {@link Builder}. */ public ContentProviderOperation build() { if (mType == TYPE_UPDATE || mType == TYPE_ASSERT) { if (mType == TYPE_UPDATE) { if ((mValues == null || mValues.size() == 0) && (mValuesBackReferences == null || mValuesBackReferences.size() == 0)) { throw new IllegalArgumentException("Empty values"); } } if (mType == TYPE_ASSERT) { if ((mValues == null || mValues.size() == 0) && (mValuesBackReferences == null || mValuesBackReferences.size() == 0) && (mExpectedCount == null)) { throw new IllegalArgumentException("Empty values"); } } return new ContentProviderOperation(this); } Loading
tests/FrameworkTest/tests/src/android/content/ContentProviderOperationTest.java +56 −4 Original line number Diff line number Diff line Loading @@ -29,7 +29,6 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Hashtable; import java.util.Set; import java.util.Map; import java.util.Map.Entry; Loading Loading @@ -145,7 +144,7 @@ public class ContentProviderOperationTest extends TestCase { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1); return getCursor(sTestValues1, 1); } }, null, 0); } catch (OperationApplicationException e) { Loading @@ -153,11 +152,62 @@ public class ContentProviderOperationTest extends TestCase { } } public void testAssertNoValues() { // Build an operation to assert values match provider ContentProviderOperation op1 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(1).build(); try { // Assert that values match from cursor ContentProviderResult result = op1.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 1); } }, null, 0); } catch (OperationApplicationException e) { fail("newAssert() failed"); } ContentProviderOperation op2 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(0).build(); try { // Assert that values match from cursor ContentProviderResult result = op2.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 0); } }, null, 0); } catch (OperationApplicationException e) { fail("newAssert() failed"); } ContentProviderOperation op3 = ContentProviderOperation.newAssertQuery(sTestUri1) .withExpectedCount(2).build(); try { // Assert that values match from cursor ContentProviderResult result = op3.apply(new TestContentProvider() { public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // Return cursor over specific set of values return getCursor(sTestValues1, 5); } }, null, 0); fail("we expect the exception to be thrown"); } catch (OperationApplicationException e) { } } /** * Build a {@link Cursor} with a single row that contains all values * provided through the given {@link ContentValues}. */ private Cursor getCursor(ContentValues contentValues) { private Cursor getCursor(ContentValues contentValues, int numRows) { final Set<Entry<String, Object>> valueSet = contentValues.valueSet(); final String[] keys = new String[valueSet.size()]; final Object[] values = new Object[valueSet.size()]; Loading @@ -170,7 +220,9 @@ public class ContentProviderOperationTest extends TestCase { } final MatrixCursor cursor = new MatrixCursor(keys); for (i = 0; i < numRows; i++) { cursor.addRow(values); } return cursor; } Loading