Search in sources :

Example 1 with Person

use of io.realm.examples.unittesting.model.Person in project realm-java by realm.

the class ExampleActivity method basicCRUD.

private void basicCRUD(Realm realm) {
    showStatus("Perform basic Create/Read/Update/Delete (CRUD) operations...");
    // All writes must be wrapped in a transaction to facilitate safe multi threading
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            // Add a person
            Person person = realm.createObject(Person.class);
            person.setId(1);
            person.setName("John Young");
            person.setAge(14);
        }
    });
    // Find the first person (no query conditions) and read a field
    final Person person = realm.where(Person.class).findFirst();
    showStatus(person.getName() + ":" + person.getAge());
    // Update person in a transaction
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            person.setName("John Senior");
            person.setAge(89);
        }
    });
    showStatus(person.getName() + " got older: " + person.getAge());
    // Add two more people
    realm.executeTransaction(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            Person jane = realm.createObject(Person.class);
            jane.setName("Jane");
            jane.setAge(27);
            Person doug = realm.createObject(Person.class);
            doug.setName("Robert");
            doug.setAge(42);
        }
    });
    RealmResults<Person> people = realm.where(Person.class).findAll();
    showStatus(String.format("Found %s people", people.size()));
    for (Person p : people) {
        showStatus("Found " + p.getName());
    }
}
Also used : Realm(io.realm.Realm) Person(io.realm.examples.unittesting.model.Person)

Example 2 with Person

use of io.realm.examples.unittesting.model.Person in project realm-java by realm.

the class ExampleActivity method complexQuery.

private String complexQuery() {
    String status = "\n\nPerforming complex Query operation...";
    Realm realm = Realm.getDefaultInstance();
    status += "\nNumber of people in the DB: " + realm.where(Person.class).count();
    // Find all persons where age between 1 and 99 and name begins with "J".
    RealmResults<Person> results = realm.where(Person.class).between("age", 1, // Notice implicit "and" operation
    99).beginsWith("name", "J").findAll();
    status += "\nNumber of people aged between 1 and 99 who's name start with 'J': " + results.size();
    realm.close();
    return status;
}
Also used : Realm(io.realm.Realm) Person(io.realm.examples.unittesting.model.Person)

Example 3 with Person

use of io.realm.examples.unittesting.model.Person in project realm-java by realm.

the class ExampleActivityTest method setup.

@Before
public void setup() throws Exception {
    // Setup Realm to be mocked. The order of these matters
    mockStatic(RealmCore.class);
    mockStatic(RealmLog.class);
    mockStatic(Realm.class);
    mockStatic(RealmConfiguration.class);
    Realm.init(RuntimeEnvironment.application);
    // Create the mock
    final Realm mockRealm = mock(Realm.class);
    final RealmConfiguration mockRealmConfig = mock(RealmConfiguration.class);
    // TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some
    // problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which
    // will be called by RealmConfiguration.Builder's constructor.
    doNothing().when(RealmCore.class);
    RealmCore.loadLibrary(any(Context.class));
    // TODO: Mock the RealmConfiguration's constructor. If the RealmConfiguration.Builder.build can be mocked, this
    // is not necessary anymore.
    whenNew(RealmConfiguration.class).withAnyArguments().thenReturn(mockRealmConfig);
    // Anytime getInstance is called with any configuration, then return the mockRealm
    when(Realm.getDefaultInstance()).thenReturn(mockRealm);
    // Anytime we ask Realm to create a Person, return a new instance.
    when(mockRealm.createObject(Person.class)).thenReturn(new Person());
    // Set up some naive stubs
    Person p1 = new Person();
    p1.setAge(14);
    p1.setName("John Young");
    Person p2 = new Person();
    p2.setAge(89);
    p2.setName("John Senior");
    Person p3 = new Person();
    p3.setAge(27);
    p3.setName("Jane");
    Person p4 = new Person();
    p4.setAge(42);
    p4.setName("Robert");
    List<Person> personList = Arrays.asList(p1, p2, p3, p4);
    // Create a mock RealmQuery
    RealmQuery<Person> personQuery = mockRealmQuery();
    // When the RealmQuery performs findFirst, return the first record in the list.
    when(personQuery.findFirst()).thenReturn(personList.get(0));
    // When the where clause is called on the Realm, return the mock query.
    when(mockRealm.where(Person.class)).thenReturn(personQuery);
    // When the RealmQuery is filtered on any string and any integer, return the person query
    when(personQuery.equalTo(anyString(), anyInt())).thenReturn(personQuery);
    // RealmResults is final, must mock static and also place this in the PrepareForTest annotation array.
    mockStatic(RealmResults.class);
    // Create a mock RealmResults
    RealmResults<Person> people = mockRealmResults();
    // When we ask Realm for all of the Person instances, return the mock RealmResults
    when(mockRealm.where(Person.class).findAll()).thenReturn(people);
    // When a between query is performed with any string as the field and any int as the
    // value, then return the personQuery itself
    when(personQuery.between(anyString(), anyInt(), anyInt())).thenReturn(personQuery);
    // When a beginsWith clause is performed with any string field and any string value
    // return the same person query
    when(personQuery.beginsWith(anyString(), anyString())).thenReturn(personQuery);
    // When we ask the RealmQuery for all of the Person objects, return the mock RealmResults
    when(personQuery.findAll()).thenReturn(people);
    // The for(...) loop in Java needs an iterator, so we're giving it one that has items,
    // since the mock RealmResults does not provide an implementation. Therefore, anytime
    // anyone asks for the RealmResults Iterator, give them a functioning iterator from the
    // ArrayList of Persons we created above. This will allow the loop to execute.
    when(people.iterator()).thenReturn(personList.iterator());
    // Return the size of the mock list.
    when(people.size()).thenReturn(personList.size());
    this.mockRealm = mockRealm;
    this.people = people;
}
Also used : Context(android.content.Context) RealmConfiguration(io.realm.RealmConfiguration) Realm(io.realm.Realm) Person(io.realm.examples.unittesting.model.Person) Before(org.junit.Before)

Aggregations

Realm (io.realm.Realm)3 Person (io.realm.examples.unittesting.model.Person)3 Context (android.content.Context)1 RealmConfiguration (io.realm.RealmConfiguration)1 Before (org.junit.Before)1