Code example for Entity

Methods: addSubValue

0
        phone.put(Data._ID, phoneId);
        phone.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        phone.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
        phone.put(Phone.TYPE, Phone.TYPE_HOME);
 
        final Entity before = new Entity(contact);
        before.addSubValue(Data.CONTENT_URI, phone);
        return before;
    } 
 
    /** 
     * Test that {@link EntityDelta#mergeAfter(EntityDelta)} correctly passes 
     * any changes through the {@link Parcel} object. This enforces that 
     * {@link EntityDelta} should be identical when serialized against the same 
     * "before" {@link Entity}. 
     */ 
    public void testParcelChangesNone() { 
        final Entity before = getEntity(TEST_CONTACT_ID, TEST_PHONE_ID);
        final EntityDelta source = EntityDelta.fromBefore(before);
        final EntityDelta dest = EntityDelta.fromBefore(before);
 
        // Merge modified values and assert they match 
        final EntityDelta merged = EntityDelta.mergeAfter(dest, source);
        assertEquals("Unexpected change when merging", source, merged);
    } 
 
    public void testParcelChangesInsert() { 
        final Entity before = getEntity(TEST_CONTACT_ID, TEST_PHONE_ID);
        final EntityDelta source = EntityDelta.fromBefore(before);
        final EntityDelta dest = EntityDelta.fromBefore(before);
 
        // Add a new row and pass across parcel, should be same 
        final ContentValues phone = new ContentValues();
        phone.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        phone.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
        phone.put(Phone.TYPE, Phone.TYPE_WORK);
        source.addEntry(ValuesDelta.fromAfter(phone));
 
        // Merge modified values and assert they match 
        final EntityDelta merged = EntityDelta.mergeAfter(dest, source);
        assertEquals("Unexpected change when merging", source, merged);
    } 
 
    public void testParcelChangesUpdate() { 
        // Update existing row and pass across parcel, should be same 
        final Entity before = getEntity(TEST_CONTACT_ID, TEST_PHONE_ID);
        final EntityDelta source = EntityDelta.fromBefore(before);
        final EntityDelta dest = EntityDelta.fromBefore(before);
 
        final ValuesDelta child = source.getEntry(TEST_PHONE_ID);
        child.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
 
        // Merge modified values and assert they match 
        final EntityDelta merged = EntityDelta.mergeAfter(dest, source);
        assertEquals("Unexpected change when merging", source, merged);
    } 
 
    public void testParcelChangesDelete() { 
        // Delete a row and pass across parcel, should be same 
        final Entity before = getEntity(TEST_CONTACT_ID, TEST_PHONE_ID);
        final EntityDelta source = EntityDelta.fromBefore(before);
        final EntityDelta dest = EntityDelta.fromBefore(before);
 
        final ValuesDelta child = source.getEntry(TEST_PHONE_ID);
        child.markDeleted();
 
        // Merge modified values and assert they match 
        final EntityDelta merged = EntityDelta.mergeAfter(dest, source);
        assertEquals("Unexpected change when merging", source, merged);
    } 
 
    /** 
     * Test that {@link ValuesDelta#buildDiff(android.net.Uri)} is correctly 
     * built for insert, update, and delete cases. Note this only tests behavior 
     * for individual {@link Data} rows. 
     */ 
    public void testValuesDiffNone() { 
        final ContentValues before = new ContentValues();
        before.put(Data._ID, TEST_PHONE_ID);
        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
 
        final ValuesDelta values = ValuesDelta.fromBefore(before);
 
        // None action shouldn't produce a builder 
        final Builder builder = values.buildDiff(Data.CONTENT_URI);
        assertNull("None action produced a builder", builder);
    } 
 
    public void testValuesDiffInsert() { 
        final ContentValues after = new ContentValues();
        after.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
 
        final ValuesDelta values = ValuesDelta.fromAfter(after);
 
        // Should produce an insert action 
        final Builder builder = values.buildDiff(Data.CONTENT_URI);
        final int type = builder.build().getType();
        assertEquals("Didn't produce insert action", TYPE_INSERT, type);
    } 
 
    public void testValuesDiffUpdate() { 
        final ContentValues before = new ContentValues();
        before.put(Data._ID, TEST_PHONE_ID);
        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
 
        final ValuesDelta values = ValuesDelta.fromBefore(before);
        values.put(Phone.NUMBER, TEST_PHONE_NUMBER_2);
 
        // Should produce an update action 
        final Builder builder = values.buildDiff(Data.CONTENT_URI);
        final int type = builder.build().getType();
        assertEquals("Didn't produce update action", TYPE_UPDATE, type);
    } 
 
    public void testValuesDiffDelete() { 
        final ContentValues before = new ContentValues();
        before.put(Data._ID, TEST_PHONE_ID);
        before.put(Phone.NUMBER, TEST_PHONE_NUMBER_1);
 
        final ValuesDelta values = ValuesDelta.fromBefore(before);
        values.markDeleted();
 
        // Should produce a delete action 
        final Builder builder = values.buildDiff(Data.CONTENT_URI);
        final int type = builder.build().getType();
        assertEquals("Didn't produce delete action", TYPE_DELETE, type);
    } 
 
    /** 
     * Test that {@link EntityDelta#buildDiff(ArrayList)} is correctly built for 
     * insert, update, and delete cases. This only tests a subset of possible 
     * {@link Data} row changes. 
     */ 
    public void testEntityDiffNone() { 
        final Entity before = getEntity(TEST_CONTACT_ID, TEST_PHONE_ID);
        final EntityDelta source = EntityDelta.fromBefore(before);
 
        // Assert that writing unchanged produces few operations 
        final ArrayList<ContentProviderOperation> diff = Lists.newArrayList();
        source.buildDiff(diff);