PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/frameworks/base/core/tests/coretests/src/android/content/ContentProviderOperationTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
Java | 550 lines | 454 code | 66 blank | 30 comment | 2 complexity | f6c27de98ea9ce4d8426cafe347883ac MD5 | raw file
  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.content;
  17. import android.content.ContentValues;
  18. import android.database.Cursor;
  19. import android.database.MatrixCursor;
  20. import android.net.Uri;
  21. import android.os.Parcel;
  22. import android.test.suitebuilder.annotation.SmallTest;
  23. import android.text.TextUtils;
  24. import junit.framework.TestCase;
  25. import java.lang.reflect.Constructor;
  26. import java.lang.reflect.Field;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.util.HashMap;
  29. import java.util.Set;
  30. import java.util.Map;
  31. import java.util.Map.Entry;
  32. @SmallTest
  33. public class ContentProviderOperationTest extends TestCase {
  34. private final static Uri sTestUri1 = Uri.parse("content://authority/blah");
  35. private final static ContentValues sTestValues1;
  36. private final static Class<ContentProviderOperation.Builder> CLASS_BUILDER =
  37. ContentProviderOperation.Builder.class;
  38. private final static Class<ContentProviderOperation> CLASS_OPERATION =
  39. ContentProviderOperation.class;
  40. static {
  41. sTestValues1 = new ContentValues();
  42. sTestValues1.put("a", 1);
  43. sTestValues1.put("b", "two");
  44. }
  45. public void testInsert() throws OperationApplicationException {
  46. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  47. .withValues(sTestValues1)
  48. .build();
  49. ContentProviderResult result = op1.apply(new TestContentProvider() {
  50. public Uri insert(Uri uri, ContentValues values) {
  51. assertEquals(sTestUri1.toString(), uri.toString());
  52. assertEquals(sTestValues1.toString(), values.toString());
  53. return uri.buildUpon().appendPath("19").build();
  54. }
  55. }, null, 0);
  56. assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString());
  57. }
  58. public void testInsertNoValues() throws OperationApplicationException {
  59. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  60. .build();
  61. ContentProviderResult result = op1.apply(new TestContentProvider() {
  62. public Uri insert(Uri uri, ContentValues values) {
  63. assertEquals(sTestUri1.toString(), uri.toString());
  64. assertNull(values);
  65. return uri.buildUpon().appendPath("19").build();
  66. }
  67. }, null, 0);
  68. assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString());
  69. }
  70. public void testInsertFailed() {
  71. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  72. .withValues(sTestValues1)
  73. .build();
  74. try {
  75. op1.apply(new TestContentProvider() {
  76. public Uri insert(Uri uri, ContentValues values) {
  77. assertEquals(sTestUri1.toString(), uri.toString());
  78. assertEquals(sTestValues1.toString(), values.toString());
  79. return null;
  80. }
  81. }, null, 0);
  82. fail("the apply should have thrown an OperationApplicationException");
  83. } catch (OperationApplicationException e) {
  84. // this is the expected case
  85. }
  86. }
  87. public void testInsertWithBackRefs() throws OperationApplicationException {
  88. ContentProviderResult[] previousResults = new ContentProviderResult[4];
  89. previousResults[0] = new ContentProviderResult(100);
  90. previousResults[1] = new ContentProviderResult(101);
  91. previousResults[2] = new ContentProviderResult(102);
  92. previousResults[3] = new ContentProviderResult(103);
  93. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  94. .withValues(sTestValues1)
  95. .withValueBackReference("a1", 3)
  96. .withValueBackReference("a2", 1)
  97. .build();
  98. ContentProviderResult result = op1.apply(new TestContentProvider() {
  99. public Uri insert(Uri uri, ContentValues values) {
  100. assertEquals(sTestUri1.toString(), uri.toString());
  101. ContentValues expected = new ContentValues(sTestValues1);
  102. expected.put("a1", 103);
  103. expected.put("a2", 101);
  104. assertEquals(expected.toString(), values.toString());
  105. return uri.buildUpon().appendPath("19").build();
  106. }
  107. }, previousResults, previousResults.length);
  108. assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString());
  109. }
  110. public void testUpdate() throws OperationApplicationException {
  111. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  112. .withValues(sTestValues1)
  113. .build();
  114. ContentProviderResult[] backRefs = new ContentProviderResult[2];
  115. ContentProviderResult result = op1.apply(new TestContentProvider() {
  116. public Uri insert(Uri uri, ContentValues values) {
  117. assertEquals(sTestUri1.toString(), uri.toString());
  118. assertEquals(sTestValues1.toString(), values.toString());
  119. return uri.buildUpon().appendPath("19").build();
  120. }
  121. }, backRefs, 1);
  122. assertEquals(sTestUri1.buildUpon().appendPath("19").toString(), result.uri.toString());
  123. }
  124. public void testAssert() {
  125. // Build an operation to assert values match provider
  126. ContentProviderOperation op1 = ContentProviderOperation.newAssertQuery(sTestUri1)
  127. .withValues(sTestValues1).build();
  128. try {
  129. // Assert that values match from cursor
  130. ContentProviderResult result = op1.apply(new TestContentProvider() {
  131. public Cursor query(Uri uri, String[] projection, String selection,
  132. String[] selectionArgs, String sortOrder) {
  133. // Return cursor over specific set of values
  134. return getCursor(sTestValues1, 1);
  135. }
  136. }, null, 0);
  137. } catch (OperationApplicationException e) {
  138. fail("newAssert() failed");
  139. }
  140. }
  141. public void testAssertNoValues() {
  142. // Build an operation to assert values match provider
  143. ContentProviderOperation op1 = ContentProviderOperation.newAssertQuery(sTestUri1)
  144. .withExpectedCount(1).build();
  145. try {
  146. // Assert that values match from cursor
  147. ContentProviderResult result = op1.apply(new TestContentProvider() {
  148. public Cursor query(Uri uri, String[] projection, String selection,
  149. String[] selectionArgs, String sortOrder) {
  150. // Return cursor over specific set of values
  151. return getCursor(sTestValues1, 1);
  152. }
  153. }, null, 0);
  154. } catch (OperationApplicationException e) {
  155. fail("newAssert() failed");
  156. }
  157. ContentProviderOperation op2 = ContentProviderOperation.newAssertQuery(sTestUri1)
  158. .withExpectedCount(0).build();
  159. try {
  160. // Assert that values match from cursor
  161. ContentProviderResult result = op2.apply(new TestContentProvider() {
  162. public Cursor query(Uri uri, String[] projection, String selection,
  163. String[] selectionArgs, String sortOrder) {
  164. // Return cursor over specific set of values
  165. return getCursor(sTestValues1, 0);
  166. }
  167. }, null, 0);
  168. } catch (OperationApplicationException e) {
  169. fail("newAssert() failed");
  170. }
  171. ContentProviderOperation op3 = ContentProviderOperation.newAssertQuery(sTestUri1)
  172. .withExpectedCount(2).build();
  173. try {
  174. // Assert that values match from cursor
  175. ContentProviderResult result = op3.apply(new TestContentProvider() {
  176. public Cursor query(Uri uri, String[] projection, String selection,
  177. String[] selectionArgs, String sortOrder) {
  178. // Return cursor over specific set of values
  179. return getCursor(sTestValues1, 5);
  180. }
  181. }, null, 0);
  182. fail("we expect the exception to be thrown");
  183. } catch (OperationApplicationException e) {
  184. }
  185. }
  186. /**
  187. * Build a {@link Cursor} with a single row that contains all values
  188. * provided through the given {@link ContentValues}.
  189. */
  190. private Cursor getCursor(ContentValues contentValues, int numRows) {
  191. final Set<Entry<String, Object>> valueSet = contentValues.valueSet();
  192. final String[] keys = new String[valueSet.size()];
  193. final Object[] values = new Object[valueSet.size()];
  194. int i = 0;
  195. for (Entry<String, Object> entry : valueSet) {
  196. keys[i] = entry.getKey();
  197. values[i] = entry.getValue();
  198. i++;
  199. }
  200. final MatrixCursor cursor = new MatrixCursor(keys);
  201. for (i = 0; i < numRows; i++) {
  202. cursor.addRow(values);
  203. }
  204. return cursor;
  205. }
  206. public void testValueBackRefs() {
  207. ContentValues values = new ContentValues();
  208. values.put("a", "in1");
  209. values.put("a2", "in2");
  210. values.put("b", "in3");
  211. values.put("c", "in4");
  212. ContentProviderResult[] previousResults = new ContentProviderResult[4];
  213. previousResults[0] = new ContentProviderResult(100);
  214. previousResults[1] = new ContentProviderResult(101);
  215. previousResults[2] = new ContentProviderResult(102);
  216. previousResults[3] = new ContentProviderResult(103);
  217. ContentValues expectedValues = new ContentValues(values);
  218. expectedValues.put("a1", (long) 103);
  219. expectedValues.put("a2", (long) 101);
  220. expectedValues.put("a3", (long) 102);
  221. ContentProviderOperation op1 = ContentProviderOperation.newInsert(sTestUri1)
  222. .withValues(values)
  223. .withValueBackReference("a1", 3)
  224. .withValueBackReference("a2", 1)
  225. .withValueBackReference("a3", 2)
  226. .build();
  227. ContentValues v2 = op1.resolveValueBackReferences(previousResults, previousResults.length);
  228. assertEquals(expectedValues, v2);
  229. }
  230. public void testSelectionBackRefs() {
  231. ContentProviderResult[] previousResults = new ContentProviderResult[4];
  232. previousResults[0] = new ContentProviderResult(100);
  233. previousResults[1] = new ContentProviderResult(101);
  234. previousResults[2] = new ContentProviderResult(102);
  235. previousResults[3] = new ContentProviderResult(103);
  236. String[] selectionArgs = new String[]{"a", null, null, "b", null};
  237. final ContentValues values = new ContentValues();
  238. values.put("unused", "unused");
  239. ContentProviderOperation op1 = ContentProviderOperation.newUpdate(sTestUri1)
  240. .withSelectionBackReference(1, 3)
  241. .withSelectionBackReference(2, 1)
  242. .withSelectionBackReference(4, 2)
  243. .withSelection("unused", selectionArgs)
  244. .withValues(values)
  245. .build();
  246. String[] s2 = op1.resolveSelectionArgsBackReferences(
  247. previousResults, previousResults.length);
  248. assertEquals("a,103,101,b,102", TextUtils.join(",", s2));
  249. }
  250. public void testParcelingOperation() throws NoSuchFieldException, IllegalAccessException,
  251. NoSuchMethodException, InvocationTargetException, InstantiationException {
  252. Parcel parcel = Parcel.obtain();
  253. ContentProviderOperation op1;
  254. ContentProviderOperation op2;
  255. HashMap<Integer, Integer> selArgsBackRef = new HashMap<Integer, Integer>();
  256. selArgsBackRef.put(1, 2);
  257. selArgsBackRef.put(3, 4);
  258. ContentValues values = new ContentValues();
  259. values.put("v1", "val1");
  260. values.put("v2", "43");
  261. ContentValues valuesBackRef = new ContentValues();
  262. values.put("v3", "val3");
  263. values.put("v4", "44");
  264. try {
  265. ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(
  266. Uri.parse("content://goo/bar"));
  267. builderSetExpectedCount(builder, 42);
  268. builderSetSelection(builder, "selection");
  269. builderSetSelectionArgs(builder, new String[]{"a", "b"});
  270. builderSetSelectionArgsBackReferences(builder, selArgsBackRef);
  271. builderSetValues(builder, values);
  272. builderSetValuesBackReferences(builder, valuesBackRef);
  273. op1 = newOperationFromBuilder(builder);
  274. op1.writeToParcel(parcel, 0);
  275. parcel.setDataPosition(0);
  276. op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel);
  277. assertEquals(ContentProviderOperation.TYPE_INSERT, operationGetType(op2));
  278. assertEquals("content://goo/bar", operationGetUri(op2).toString());
  279. assertEquals(Integer.valueOf(42), operationGetExpectedCount(op2));
  280. assertEquals("selection", operationGetSelection(op2));
  281. assertEquals(2, operationGetSelectionArgs(op2).length);
  282. assertEquals("a", operationGetSelectionArgs(op2)[0]);
  283. assertEquals("b", operationGetSelectionArgs(op2)[1]);
  284. assertEquals(values, operationGetValues(op2));
  285. assertEquals(valuesBackRef, operationGetValuesBackReferences(op2));
  286. assertEquals(2, operationGetSelectionArgsBackReferences(op2).size());
  287. assertEquals(Integer.valueOf(2), operationGetSelectionArgsBackReferences(op2).get(1));
  288. assertEquals(Integer.valueOf(4), operationGetSelectionArgsBackReferences(op2).get(3));
  289. } finally {
  290. parcel.recycle();
  291. }
  292. try {
  293. ContentProviderOperation.Builder builder = ContentProviderOperation.newUpdate(
  294. Uri.parse("content://goo/bar"));
  295. builderSetSelectionArgsBackReferences(builder, selArgsBackRef);
  296. op1 = newOperationFromBuilder(builder);
  297. op1.writeToParcel(parcel, 0);
  298. parcel.setDataPosition(0);
  299. op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel);
  300. assertEquals(ContentProviderOperation.TYPE_UPDATE, operationGetType(op2));
  301. assertEquals("content://goo/bar", operationGetUri(op2).toString());
  302. assertNull(operationGetExpectedCount(op2));
  303. assertNull(operationGetSelection(op2));
  304. assertNull(operationGetSelectionArgs(op2));
  305. assertNull(operationGetValues(op2));
  306. assertNull(operationGetValuesBackReferences(op2));
  307. assertEquals(2, operationGetSelectionArgsBackReferences(op2).size());
  308. assertEquals(Integer.valueOf(2), operationGetSelectionArgsBackReferences(op2).get(1));
  309. assertEquals(Integer.valueOf(4), operationGetSelectionArgsBackReferences(op2).get(3));
  310. } finally {
  311. parcel.recycle();
  312. }
  313. try {
  314. ContentProviderOperation.Builder builder = ContentProviderOperation.newDelete(
  315. Uri.parse("content://goo/bar"));
  316. op1 = newOperationFromBuilder(builder);
  317. op1.writeToParcel(parcel, 0);
  318. parcel.setDataPosition(0);
  319. op2 = ContentProviderOperation.CREATOR.createFromParcel(parcel);
  320. assertEquals(ContentProviderOperation.TYPE_DELETE, operationGetType(op2));
  321. assertEquals("content://goo/bar", operationGetUri(op2).toString());
  322. assertNull(operationGetExpectedCount(op2));
  323. assertNull(operationGetSelection(op2));
  324. assertNull(operationGetSelectionArgs(op2));
  325. assertNull(operationGetValues(op2));
  326. assertNull(operationGetValuesBackReferences(op2));
  327. assertNull(operationGetSelectionArgsBackReferences(op2));
  328. } finally {
  329. parcel.recycle();
  330. }
  331. }
  332. private static ContentProviderOperation newOperationFromBuilder(
  333. ContentProviderOperation.Builder builder)
  334. throws NoSuchMethodException, InstantiationException, IllegalAccessException,
  335. InvocationTargetException {
  336. final Constructor constructor = CLASS_OPERATION.getDeclaredConstructor(CLASS_BUILDER);
  337. constructor.setAccessible(true);
  338. return (ContentProviderOperation) constructor.newInstance(builder);
  339. }
  340. private void builderSetSelectionArgsBackReferences(
  341. ContentProviderOperation.Builder builder, HashMap<Integer, Integer> selArgsBackRef)
  342. throws NoSuchFieldException, IllegalAccessException {
  343. Field field;
  344. field = CLASS_BUILDER.getDeclaredField("mSelectionArgsBackReferences");
  345. field.setAccessible(true);
  346. field.set(builder, selArgsBackRef);
  347. }
  348. private void builderSetValuesBackReferences(
  349. ContentProviderOperation.Builder builder, ContentValues valuesBackReferences)
  350. throws NoSuchFieldException, IllegalAccessException {
  351. Field field;
  352. field = CLASS_BUILDER.getDeclaredField("mValuesBackReferences");
  353. field.setAccessible(true);
  354. field.set(builder, valuesBackReferences);
  355. }
  356. private void builderSetSelection(
  357. ContentProviderOperation.Builder builder, String selection)
  358. throws NoSuchFieldException, IllegalAccessException {
  359. Field field;
  360. field = CLASS_BUILDER.getDeclaredField("mSelection");
  361. field.setAccessible(true);
  362. field.set(builder, selection);
  363. }
  364. private void builderSetSelectionArgs(
  365. ContentProviderOperation.Builder builder, String[] selArgs)
  366. throws NoSuchFieldException, IllegalAccessException {
  367. Field field;
  368. field = CLASS_BUILDER.getDeclaredField("mSelectionArgs");
  369. field.setAccessible(true);
  370. field.set(builder, selArgs);
  371. }
  372. private void builderSetValues(
  373. ContentProviderOperation.Builder builder, ContentValues values)
  374. throws NoSuchFieldException, IllegalAccessException {
  375. Field field;
  376. field = CLASS_BUILDER.getDeclaredField("mValues");
  377. field.setAccessible(true);
  378. field.set(builder, values);
  379. }
  380. private void builderSetExpectedCount(
  381. ContentProviderOperation.Builder builder, Integer expectedCount)
  382. throws NoSuchFieldException, IllegalAccessException {
  383. Field field;
  384. field = CLASS_BUILDER.getDeclaredField("mExpectedCount");
  385. field.setAccessible(true);
  386. field.set(builder, expectedCount);
  387. }
  388. private int operationGetType(ContentProviderOperation operation)
  389. throws NoSuchFieldException, IllegalAccessException {
  390. final Field field = CLASS_OPERATION.getDeclaredField("mType");
  391. field.setAccessible(true);
  392. return field.getInt(operation);
  393. }
  394. private Uri operationGetUri(ContentProviderOperation operation)
  395. throws NoSuchFieldException, IllegalAccessException {
  396. final Field field = CLASS_OPERATION.getDeclaredField("mUri");
  397. field.setAccessible(true);
  398. return (Uri) field.get(operation);
  399. }
  400. private String operationGetSelection(ContentProviderOperation operation)
  401. throws NoSuchFieldException, IllegalAccessException {
  402. final Field field = CLASS_OPERATION.getDeclaredField("mSelection");
  403. field.setAccessible(true);
  404. return (String) field.get(operation);
  405. }
  406. private String[] operationGetSelectionArgs(ContentProviderOperation operation)
  407. throws NoSuchFieldException, IllegalAccessException {
  408. final Field field = CLASS_OPERATION.getDeclaredField("mSelectionArgs");
  409. field.setAccessible(true);
  410. return (String[]) field.get(operation);
  411. }
  412. private ContentValues operationGetValues(ContentProviderOperation operation)
  413. throws NoSuchFieldException, IllegalAccessException {
  414. final Field field = CLASS_OPERATION.getDeclaredField("mValues");
  415. field.setAccessible(true);
  416. return (ContentValues) field.get(operation);
  417. }
  418. private Integer operationGetExpectedCount(ContentProviderOperation operation)
  419. throws NoSuchFieldException, IllegalAccessException {
  420. final Field field = CLASS_OPERATION.getDeclaredField("mExpectedCount");
  421. field.setAccessible(true);
  422. return (Integer) field.get(operation);
  423. }
  424. private ContentValues operationGetValuesBackReferences(ContentProviderOperation operation)
  425. throws NoSuchFieldException, IllegalAccessException {
  426. final Field field = CLASS_OPERATION.getDeclaredField("mValuesBackReferences");
  427. field.setAccessible(true);
  428. return (ContentValues) field.get(operation);
  429. }
  430. private Map<Integer, Integer> operationGetSelectionArgsBackReferences(
  431. ContentProviderOperation operation)
  432. throws NoSuchFieldException, IllegalAccessException {
  433. final Field field = CLASS_OPERATION.getDeclaredField("mSelectionArgsBackReferences");
  434. field.setAccessible(true);
  435. return (Map<Integer, Integer>) field.get(operation);
  436. }
  437. public void testParcelingResult() {
  438. Parcel parcel = Parcel.obtain();
  439. ContentProviderResult result1;
  440. ContentProviderResult result2;
  441. try {
  442. result1 = new ContentProviderResult(Uri.parse("content://goo/bar"));
  443. result1.writeToParcel(parcel, 0);
  444. parcel.setDataPosition(0);
  445. result2 = ContentProviderResult.CREATOR.createFromParcel(parcel);
  446. assertEquals("content://goo/bar", result2.uri.toString());
  447. assertNull(result2.count);
  448. } finally {
  449. parcel.recycle();
  450. }
  451. parcel = Parcel.obtain();
  452. try {
  453. result1 = new ContentProviderResult(42);
  454. result1.writeToParcel(parcel, 0);
  455. parcel.setDataPosition(0);
  456. result2 = ContentProviderResult.CREATOR.createFromParcel(parcel);
  457. assertEquals(Integer.valueOf(42), result2.count);
  458. assertNull(result2.uri);
  459. } finally {
  460. parcel.recycle();
  461. }
  462. }
  463. static class TestContentProvider extends ContentProvider {
  464. public boolean onCreate() {
  465. throw new UnsupportedOperationException();
  466. }
  467. public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
  468. String sortOrder) {
  469. throw new UnsupportedOperationException();
  470. }
  471. public String getType(Uri uri) {
  472. throw new UnsupportedOperationException();
  473. }
  474. public Uri insert(Uri uri, ContentValues values) {
  475. throw new UnsupportedOperationException();
  476. }
  477. public int delete(Uri uri, String selection, String[] selectionArgs) {
  478. throw new UnsupportedOperationException();
  479. }
  480. public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
  481. throw new UnsupportedOperationException();
  482. }
  483. }
  484. }