Search in sources :

Example 16 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class LinkingObjectsManagedTests method json_jsonUpdateCausesNotification.

// A JSON update should generate a notifcation
@Test
@RunTestInLooperThread
public void json_jsonUpdateCausesNotification() {
    final Realm looperThreadRealm = looperThread.realm;
    looperThreadRealm.beginTransaction();
    AllJavaTypes child = looperThreadRealm.createObject(AllJavaTypes.class, 1);
    AllJavaTypes parent = looperThreadRealm.createObject(AllJavaTypes.class, 2);
    parent.setFieldObject(child);
    looperThreadRealm.commitTransaction();
    RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
    assertNotNull(results);
    assertEquals(results.size(), 1);
    child = results.first();
    RealmResults<AllJavaTypes> parents = child.getObjectParents();
    assertNotNull(parents);
    assertEquals(1, parents.size());
    final AtomicInteger counter = new AtomicInteger(0);
    RealmChangeListener<AllJavaTypes> listener = new RealmChangeListener<AllJavaTypes>() {

        @Override
        public void onChange(AllJavaTypes object) {
            counter.incrementAndGet();
        }
    };
    child.addChangeListener(listener);
    looperThreadRealm.beginTransaction();
    try {
        looperThreadRealm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 2, \"fieldObject\" : null }]");
    } catch (RealmException e) {
        fail("Failed loading JSON" + e);
    }
    looperThreadRealm.commitTransaction();
    verifyPostConditions(looperThreadRealm, new PostConditions() {

        public void run(Realm realm) {
            RealmResults<AllJavaTypes> results = looperThreadRealm.where(AllJavaTypes.class).equalTo("fieldId", 1).findAll();
            assertNotNull(results);
            assertEquals(results.size(), 1);
            AllJavaTypes child = results.first();
            RealmResults<AllJavaTypes> parents = child.getObjectParents();
            assertNotNull(parents);
            assertEquals(0, parents.size());
            assertEquals(1, counter.get());
        }
    }, child, parent);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AllJavaTypes(io.realm.entities.AllJavaTypes) RealmException(io.realm.exceptions.RealmException) RunTestInLooperThread(io.realm.rule.RunTestInLooperThread) Test(org.junit.Test)

Example 17 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class LinkingObjectsManagedTests method json_updateList.

// Fields annotated with @LinkingObjects should not be affected by JSON updates
@Test
public void json_updateList() {
    realm.beginTransaction();
    AllJavaTypes child = realm.createObject(AllJavaTypes.class, 1);
    AllJavaTypes parent = realm.createObject(AllJavaTypes.class, 2);
    parent.getFieldList().add(child);
    realm.commitTransaction();
    RealmResults<AllJavaTypes> parents = child.getListParents();
    assertNotNull(parents);
    assertEquals(1, parents.size());
    assertTrue(parents.contains(parent));
    realm.beginTransaction();
    try {
        realm.createOrUpdateAllFromJson(AllJavaTypes.class, "[{ \"fieldId\" : 1, \"listParents\" : null }]");
    } catch (RealmException e) {
        fail("Failed loading JSON" + e);
    }
    realm.commitTransaction();
    parents = child.getListParents();
    assertNotNull(parents);
    assertEquals(1, parents.size());
    assertTrue(parents.contains(parent));
}
Also used : AllJavaTypes(io.realm.entities.AllJavaTypes) RealmException(io.realm.exceptions.RealmException) Test(org.junit.Test)

Example 18 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class JsonUtilsTest method testParseInvalidDateShouldThrowRealmException.

public void testParseInvalidDateShouldThrowRealmException() {
    String invalidLongDate = "123abc";
    try {
        Date d = JsonUtils.stringToDate(invalidLongDate);
        fail("Should fail with a RealmException.");
    } catch (RealmException e) {
        assertNotNull(e);
        assertTrue(e.getCause() instanceof ParseException);
    }
}
Also used : ParseException(java.text.ParseException) Date(java.util.Date) RealmException(io.realm.exceptions.RealmException)

Example 19 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class JsonUtilsTest method testParseInvalidNumericDateShouldThrowRealmException.

public void testParseInvalidNumericDateShouldThrowRealmException() {
    // not a date.
    String invalidLongDate = "2342347289374398342759873495743";
    try {
        Date d = JsonUtils.stringToDate(invalidLongDate);
        fail("Should fail with a RealmException.");
    } catch (RealmException e) {
        assertNotNull(e);
        assertTrue(e.getCause() instanceof NumberFormatException);
    }
}
Also used : Date(java.util.Date) RealmException(io.realm.exceptions.RealmException)

Example 20 with RealmException

use of io.realm.exceptions.RealmException in project realm-java by realm.

the class ModulesExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modules_example);
    rootLayout = ((LinearLayout) findViewById(R.id.container));
    rootLayout.removeAllViews();
    // The default Realm instance implicitly knows about all classes in the realmModuleAppExample Android Studio
    // module. This does not include the classes from the realmModuleLibraryExample AS module so a Realm using this
    // configuration would know about the following classes: { Cow, Pig, Snake, Spider }
    RealmConfiguration defaultConfig = new RealmConfiguration.Builder().build();
    // It is possible to extend the default schema by adding additional Realm modules using modules(). This can
    // also be Realm modules from libraries. The below Realm contains the following classes: { Cow, Pig, Snake,
    // Spider, Cat, Dog }
    RealmConfiguration farmAnimalsConfig = new RealmConfiguration.Builder().name("farm.realm").modules(Realm.getDefaultModule(), new DomesticAnimalsModule()).build();
    // Or you can completely replace the default schema.
    // This Realm contains the following classes: { Elephant, Lion, Zebra, Snake, Spider }
    RealmConfiguration exoticAnimalsConfig = new RealmConfiguration.Builder().name("exotic.realm").modules(new ZooAnimalsModule(), new CreepyAnimalsModule()).build();
    // Multiple Realms can be open at the same time
    showStatus("Opening multiple Realms");
    Realm defaultRealm = Realm.getInstance(defaultConfig);
    final Realm farmRealm = Realm.getInstance(farmAnimalsConfig);
    Realm exoticRealm = Realm.getInstance(exoticAnimalsConfig);
    // Objects can be added to each Realm independantly
    showStatus("Create objects in the default Realm");
    defaultRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Cow.class);
            realm.createObject(Pig.class);
            realm.createObject(Snake.class);
            realm.createObject(Spider.class);
        }
    });
    showStatus("Create objects in the farm Realm");
    farmRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Cow.class);
            realm.createObject(Pig.class);
            realm.createObject(Cat.class);
            realm.createObject(Dog.class);
        }
    });
    showStatus("Create objects in the exotic Realm");
    exoticRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.createObject(Elephant.class);
            realm.createObject(Lion.class);
            realm.createObject(Zebra.class);
            realm.createObject(Snake.class);
            realm.createObject(Spider.class);
        }
    });
    // You can copy objects between Realms
    showStatus("Copy objects between Realms");
    showStatus("Number of pigs on the farm : " + farmRealm.where(Pig.class).count());
    showStatus("Copy pig from defaultRealm to farmRealm");
    final Pig defaultPig = defaultRealm.where(Pig.class).findFirst();
    farmRealm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.copyToRealm(defaultPig);
        }
    });
    showStatus("Number of pigs on the farm : " + farmRealm.where(Pig.class).count());
    // Each Realm is restricted to only accept the classes in their schema.
    showStatus("Trying to add an unsupported class");
    defaultRealm.beginTransaction();
    try {
        defaultRealm.createObject(Elephant.class);
    } catch (RealmException expected) {
        showStatus("This throws a :" + expected.toString());
    } finally {
        defaultRealm.cancelTransaction();
    }
    // And Realms in library projects are independent from Realms in the app code
    showStatus("Interacting with library code that uses Realm internally");
    int animals = 5;
    Zoo libraryZoo = new Zoo();
    libraryZoo.open();
    showStatus("Adding animals: " + animals);
    libraryZoo.addAnimals(5);
    showStatus("Number of animals in the library Realm:" + libraryZoo.getNoOfAnimals());
    libraryZoo.close();
    // Remember to close all open Realms
    defaultRealm.close();
    farmRealm.close();
    exoticRealm.close();
}
Also used : ZooAnimalsModule(io.realm.examples.librarymodules.modules.ZooAnimalsModule) CreepyAnimalsModule(io.realm.examples.appmodules.modules.CreepyAnimalsModule) Snake(io.realm.examples.appmodules.model.Snake) Zebra(io.realm.examples.librarymodules.model.Zebra) Cow(io.realm.examples.appmodules.model.Cow) DomesticAnimalsModule(io.realm.examples.librarymodules.modules.DomesticAnimalsModule) RealmException(io.realm.exceptions.RealmException) Pig(io.realm.examples.appmodules.model.Pig) RealmConfiguration(io.realm.RealmConfiguration) Cat(io.realm.examples.librarymodules.model.Cat) Elephant(io.realm.examples.librarymodules.model.Elephant) Spider(io.realm.examples.appmodules.model.Spider) Lion(io.realm.examples.librarymodules.model.Lion) Zoo(io.realm.examples.librarymodules.Zoo) Realm(io.realm.Realm) Dog(io.realm.examples.librarymodules.model.Dog) LinearLayout(android.widget.LinearLayout)

Aggregations

RealmException (io.realm.exceptions.RealmException)31 Test (org.junit.Test)10 JSONException (org.json.JSONException)9 AllJavaTypes (io.realm.entities.AllJavaTypes)5 JSONObject (org.json.JSONObject)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 TargetApi (android.annotation.TargetApi)3 RealmConfiguration (io.realm.RealmConfiguration)3 AllTypesPrimaryKey (io.realm.entities.AllTypesPrimaryKey)3 Table (io.realm.internal.Table)3 Date (java.util.Date)3 Scanner (java.util.Scanner)3 JSONArray (org.json.JSONArray)3 Realm (io.realm.Realm)2 AllTypes (io.realm.entities.AllTypes)2 RealmPrimaryKeyConstraintException (io.realm.exceptions.RealmPrimaryKeyConstraintException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 Method (java.lang.reflect.Method)2 ParseException (java.text.ParseException)2