Skip to content

Commit

Permalink
fix 1884 listener trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
nhachicha committed Dec 11, 2015
1 parent 314f264 commit 133cd43
Show file tree
Hide file tree
Showing 11 changed files with 602 additions and 75 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Updated ProGuard configuration (#1904).
* Fixed a bug which could cause a crash when closing Realm instances in change listeners (#1900).
* Fix a crash occurring during update of multiple async queries (#1895).
* Fixed listeners not triggered for RealmObject & RealmResults created using copy or create methods (#1884).

0.86.0
* BREAKING CHANGE: The Migration API has been replaced with a new API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package io.realm;

import android.content.res.AssetManager;
import android.test.AndroidTestCase;
import android.util.Base64;

Expand All @@ -41,7 +40,6 @@
import io.realm.entities.NullTypes;
import io.realm.entities.OwnerPrimaryKey;
import io.realm.exceptions.RealmException;
import io.realm.exceptions.RealmPrimaryKeyConstraintException;

import static io.realm.internal.test.ExtraTests.assertArrayEquals;

Expand All @@ -61,11 +59,6 @@ protected void tearDown() throws Exception {
testRealm.close();
}

private InputStream loadJsonFromAssets(String file) throws IOException {
AssetManager assetManager = getContext().getAssets();
return assetManager.open(file);
}

private InputStream convertJsonObjectToStream(JSONObject obj) {
return new ByteArrayInputStream(obj.toString().getBytes());
}
Expand Down Expand Up @@ -350,7 +343,7 @@ public void testCreateAllFromJsonStream_null() throws IOException {
}

public void testCreateObjectFromJsonStream_allSimpleTypes() throws IOException {
InputStream in = loadJsonFromAssets("all_simple_types.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "all_simple_types.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -367,7 +360,7 @@ public void testCreateObjectFromJsonStream_allSimpleTypes() throws IOException {
}

public void testCreateObjectFromJsonStream_dateAsLong() throws IOException {
InputStream in = loadJsonFromAssets("date_as_long.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "date_as_long.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -379,7 +372,7 @@ public void testCreateObjectFromJsonStream_dateAsLong() throws IOException {
}

public void testCreateObjectFromJsonStream_dateAsString() throws IOException {
InputStream in = loadJsonFromAssets("date_as_string.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "date_as_string.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -391,7 +384,7 @@ public void testCreateObjectFromJsonStream_dateAsString() throws IOException {
}

public void testCreateObjectFromJsonStream_childObject() throws IOException {
InputStream in = loadJsonFromAssets("single_child_object.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "single_child_object.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -402,7 +395,7 @@ public void testCreateObjectFromJsonStream_childObject() throws IOException {
}

public void testCreateObjectFromJsonStream_emptyChildObjectList() throws IOException {
InputStream in = loadJsonFromAssets("realmlist_empty.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "realmlist_empty.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -413,7 +406,7 @@ public void testCreateObjectFromJsonStream_emptyChildObjectList() throws IOExcep
}

public void testCreateObjectFromJsonStream_childObjectList() throws IOException {
InputStream in = loadJsonFromAssets("realmlist.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "realmlist.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand All @@ -424,7 +417,7 @@ public void testCreateObjectFromJsonStream_childObjectList() throws IOException
}

public void testCreateObjectFromJsonStream_array() throws IOException {
InputStream in = loadJsonFromAssets("array.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "array.json");

testRealm.beginTransaction();
testRealm.createAllFromJson(Dog.class, in);
Expand All @@ -437,7 +430,7 @@ public void testCreateObjectFromJsonStream_array() throws IOException {

// Test if Json object doesn't have the field, then the field should have default value. Stream version.
public void testCreateObjectFromJsonStream_noValues() throws IOException {
InputStream in = loadJsonFromAssets("other_json_object.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "other_json_object.json");
testRealm.beginTransaction();
testRealm.createObjectFromJson(AllTypes.class, in);
testRealm.commitTransaction();
Expand Down Expand Up @@ -473,7 +466,7 @@ public void testUpdateObjectFromJsonStream_nullValues() throws IOException {
testRealm.copyToRealm(obj);
testRealm.commitTransaction();

InputStream in = loadJsonFromAssets("all_types_primary_key_field_only.json");
InputStream in = TestHelper.loadJsonFromAssets(getContext(), "all_types_primary_key_field_only.json");
testRealm.beginTransaction();
testRealm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, in);
testRealm.commitTransaction();
Expand Down Expand Up @@ -509,7 +502,7 @@ public void testUpdateObjectFromJsonObject_nullValues() throws IOException {
testRealm.copyToRealm(obj);
testRealm.commitTransaction();

String json = TestHelper.streamToString(loadJsonFromAssets("all_types_primary_key_field_only.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "all_types_primary_key_field_only.json"));
testRealm.beginTransaction();
testRealm.createOrUpdateObjectFromJson(AllTypesPrimaryKey.class, json);
testRealm.commitTransaction();
Expand Down Expand Up @@ -582,7 +575,7 @@ public void testCreateOrUpdateJsonObject() throws JSONException {
}

public void testCreateOrUpdateJsonObject_ignoreUnsetProperties() throws IOException {
String json = TestHelper.streamToString(loadJsonFromAssets("list_alltypes_primarykey.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "list_alltypes_primarykey.json"));
testRealm.beginTransaction();
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, json);
testRealm.commitTransaction();
Expand All @@ -597,7 +590,7 @@ public void testCreateOrUpdateJsonObject_ignoreUnsetProperties() throws IOExcept

public void testCreateOrUpdateJsonStream_ignoreUnsetProperties() throws IOException {
testRealm.beginTransaction();
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, loadJsonFromAssets("list_alltypes_primarykey.json"));
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, TestHelper.loadJsonFromAssets(getContext(), "list_alltypes_primarykey.json"));
testRealm.commitTransaction();

// No-op as no properties should be updated
Expand Down Expand Up @@ -675,7 +668,7 @@ public void testCreateOrUpdateAllString_noPrimaryKeyThrows() throws IOException
}

public void testCreateOrUpdateAllJsonArray() throws JSONException, IOException {
String json = TestHelper.streamToString(loadJsonFromAssets("list_alltypes_primarykey.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "list_alltypes_primarykey.json"));
JSONArray array = new JSONArray(json);
testRealm.beginTransaction();
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, array);
Expand All @@ -686,14 +679,14 @@ public void testCreateOrUpdateAllJsonArray() throws JSONException, IOException {

public void testCreateOrUpdateAllInputStream() throws IOException {
testRealm.beginTransaction();
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, loadJsonFromAssets("list_alltypes_primarykey.json"));
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, TestHelper.loadJsonFromAssets(getContext(), "list_alltypes_primarykey.json"));
testRealm.commitTransaction();

assertAllTypesPrimaryKeyUpdated();
}

public void testCreateOrUpdateAllString() throws IOException {
String json = TestHelper.streamToString(loadJsonFromAssets("list_alltypes_primarykey.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "list_alltypes_primarykey.json"));
testRealm.beginTransaction();
testRealm.createOrUpdateAllFromJson(AllTypesPrimaryKey.class, json);
testRealm.commitTransaction();
Expand Down Expand Up @@ -790,7 +783,7 @@ private void checkNullableValuesAreNotNull(NullTypes nullTypes2) {

// Testing create objects from Json, all nullable fields with null values or non-null values
public void testNullTypesJsonWithNulls() throws IOException, JSONException {
String json = TestHelper.streamToString(loadJsonFromAssets("nulltypes.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "nulltypes.json"));
JSONArray array = new JSONArray(json);
testRealm.beginTransaction();
testRealm.createAllFromJson(NullTypes.class, array);
Expand All @@ -809,7 +802,7 @@ public void testNullTypesJsonWithNulls() throws IOException, JSONException {
// Test creating objects form JSON stream, all nullable fields with null values or non-null values
public void testNullTypesStreamJSONWithNulls() throws IOException {
testRealm.beginTransaction();
testRealm.createAllFromJson(NullTypes.class, loadJsonFromAssets("nulltypes.json"));
testRealm.createAllFromJson(NullTypes.class, TestHelper.loadJsonFromAssets(getContext(), "nulltypes.json"));
testRealm.commitTransaction();

RealmResults<NullTypes> nullTypesRealmResults = testRealm.allObjects(NullTypes.class);
Expand All @@ -825,7 +818,7 @@ public void testNullTypesStreamJSONWithNulls() throws IOException {
// Test a nullable field already has a non-null value, update it through JSON with null value
// of the corresponding field.
public void testUpdateNullTypesJSONWithNulls() throws IOException, JSONException {
String json = TestHelper.streamToString(loadJsonFromAssets("nulltypes.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "nulltypes.json"));
JSONArray jsonArray = new JSONArray(json);
// Nullable fields with values
JSONObject jsonObject = jsonArray.getJSONObject(1);
Expand Down Expand Up @@ -855,7 +848,7 @@ public void testUpdateNullTypesJSONWithNulls() throws IOException, JSONException
// If JSON has a field with value null, and corresponding object's field is not nullable,
// an exception should be throw.
public void testNullTypesJSONToNotNullFields() throws IOException, JSONException {
String json = TestHelper.streamToString(loadJsonFromAssets("nulltypes_invalid.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "nulltypes_invalid.json"));
JSONArray array = new JSONArray(json);
testRealm.beginTransaction();

Expand Down Expand Up @@ -935,7 +928,7 @@ public void testNullTypesJSONToNotNullFields() throws IOException, JSONException
// If JSON has a field with value null, and corresponding object's field is not nullable,
// an exception should be throw. Stream version.
public void testNullTypesJSONStreamToNotNullFields() throws IOException, JSONException {
String json = TestHelper.streamToString(loadJsonFromAssets("nulltypes_invalid.json"));
String json = TestHelper.streamToString(TestHelper.loadJsonFromAssets(getContext(), "nulltypes_invalid.json"));
JSONArray array = new JSONArray(json);

// 1 String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,4 +677,9 @@ public static void exitOrThrow(final ExecutorService executorService,
throw throwable[0];
}
}

public static InputStream loadJsonFromAssets(Context context, String file) throws IOException {
AssetManager assetManager = context.getAssets();
return assetManager.open(file);
}
}
Loading

0 comments on commit 133cd43

Please sign in to comment.