Search in sources :

Example 11 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_dateAsString.

@Test
public void createObjectFromJson_dateAsString() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("columnDate", "/Date(1000)/");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, json);
    realm.commitTransaction();
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Date(1000), obj.getColumnDate());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Example 12 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_streamDateAsISO8601String.

@Test
public void createObjectFromJson_streamDateAsISO8601String() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "date_as_iso8601_string.json");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, in);
    realm.commitTransaction();
    in.close();
    Calendar cal = new GregorianCalendar(2007, 8 - 1, 13, 19, 51, 23);
    cal.setTimeZone(TimeZone.getTimeZone("GMT"));
    cal.set(Calendar.MILLISECOND, 789);
    Date date = cal.getTime();
    // Checks that all primitive types are imported correctly.
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(date, obj.getColumnDate());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Example 13 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_noValues.

// Tests if Json object doesn't have the field, then the field should have default value.
@Test
public void createObjectFromJson_noValues() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("noThingHere", JSONObject.NULL);
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, json);
    realm.commitTransaction();
    // Checks that all primitive types are imported correctly.
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals("", obj.getColumnString());
    assertEquals(0L, obj.getColumnLong());
    assertEquals(0F, obj.getColumnFloat(), 0F);
    assertEquals(0D, obj.getColumnDouble(), 0D);
    assertEquals(false, obj.isColumnBoolean());
    assertEquals(new Date(0), obj.getColumnDate());
    assertArrayEquals(new byte[0], obj.getColumnBinary());
    assertNull(obj.getColumnRealmObject());
    assertEquals(0, obj.getColumnRealmList().size());
}
Also used : JSONObject(org.json.JSONObject) AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Example 14 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_streamDateAsString.

@Test
public void createObjectFromJson_streamDateAsString() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "date_as_string.json");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, in);
    realm.commitTransaction();
    in.close();
    // Checks that all primitive types are imported correctly.
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Date(1000), obj.getColumnDate());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Example 15 with AllTypes

use of io.realm.entities.AllTypes in project realm-java by realm.

the class RealmJsonTests method createObjectFromJson_streamDateAsLong.

@Test
public void createObjectFromJson_streamDateAsLong() throws IOException {
    assumeThat(Build.VERSION.SDK_INT, greaterThanOrEqualTo(Build.VERSION_CODES.HONEYCOMB));
    InputStream in = TestHelper.loadJsonFromAssets(context, "date_as_long.json");
    realm.beginTransaction();
    realm.createObjectFromJson(AllTypes.class, in);
    realm.commitTransaction();
    in.close();
    // Checks that all primitive types are imported correctly.
    AllTypes obj = realm.where(AllTypes.class).findFirst();
    assertEquals(new Date(1000), obj.getColumnDate());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) AllTypes(io.realm.entities.AllTypes) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AllTypes (io.realm.entities.AllTypes)134 Test (org.junit.Test)122 UiThreadTest (android.support.test.annotation.UiThreadTest)40 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)33 Date (java.util.Date)27 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Action1 (rx.functions.Action1)12 JSONObject (org.json.JSONObject)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 Dog (io.realm.entities.Dog)8 InputStream (java.io.InputStream)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 NonLatinFieldNames (io.realm.entities.NonLatinFieldNames)5 SharedRealm (io.realm.internal.SharedRealm)5 RealmException (io.realm.exceptions.RealmException)4 RunInLooperThread (io.realm.rule.RunInLooperThread)3 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutionException (java.util.concurrent.ExecutionException)3 JSONException (org.json.JSONException)3