{"action":"created","comment":{"url":"https://api.github.com/repos/realm/realm-java/pulls/comments/33013895","id":33013895,"diff_hunk":"@@ -0,0 +1,225 @@\n+/*\n+ * Copyright 2015 Realm Inc.\n+ *\n+ * Licensed under the Apache License, Version 2.0 (the \"License\");\n+ * you may not use this file except in compliance with the License.\n+ * You may obtain a copy of the License at\n+ *\n+ * http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+package io.realm;\n+\n+import android.test.AndroidTestCase;\n+\n+import java.io.File;\n+import java.io.IOException;\n+import java.util.concurrent.ExecutionException;\n+import java.util.concurrent.atomic.AtomicBoolean;\n+\n+import io.realm.entities.Dog;\n+\n+public class RealmInMemoryTest extends AndroidTestCase {\n+\n+    private final static String IDENTIFIER = \"InMemRealmTest\";\n+\n+    private Realm testRealm;\n+    private RealmConfiguration inMemConf;\n+\n+    @Override\n+    protected void setUp() throws Exception {\n+\n+        RealmConfiguration onDiskConf = new RealmConfiguration.Builder(getContext())\n+                .name(IDENTIFIER)\n+                .build();\n+        inMemConf = new RealmConfiguration.Builder(getContext())\n+                .name(IDENTIFIER)\n+                .inMemory()\n+                .build();\n+\n+        // Delete the same name Realm file just in case\n+        Realm.deleteRealm(onDiskConf);\n+\n+        testRealm = Realm.getInstance(inMemConf);\n+    }\n+\n+    @Override\n+    protected void tearDown() throws Exception {\n+        if (testRealm != null) {\n+            testRealm.close();\n+        }\n+    }\n+\n+    // Testing the in-memory Realm by Creating one instance, adding a record, then close the instance.\n+    // By the next time in-memory Realm instance with the same name created, it should be empty.\n+    // Two in-memory Realms with different names should not affect each other.\n+    public void testInMemoryRealm() {\n+        testRealm.beginTransaction();\n+        Dog dog = testRealm.createObject(Dog.class);\n+        dog.setName(\"DinoDog\");\n+        testRealm.commitTransaction();\n+\n+        // Create the 2nd in-memory Realm with different name. To make sure they are not affecting each other.\n+        RealmConfiguration inMemConf2 = new RealmConfiguration.Builder(getContext())\n+                        .name(IDENTIFIER + \"2\")\n+                        .inMemory()\n+                        .build();\n+        Realm testRealm2 = Realm.getInstance(inMemConf2);\n+        testRealm2.beginTransaction();\n+        Dog dog2 = testRealm2.createObject(Dog.class);\n+        dog2.setName(\"UFODog\");\n+        testRealm2.commitTransaction();\n+\n+        assertEquals(testRealm.allObjects(Dog.class).size(), 1);\n+        assertEquals(testRealm.allObjects(Dog.class).first().getName(), \"DinoDog\");\n+\n+        assertEquals(testRealm2.allObjects(Dog.class).size(), 1);\n+        assertEquals(testRealm2.allObjects(Dog.class).first().getName(), \"UFODog\");\n+\n+        testRealm.close();\n+        testRealm2.close();\n+\n+        // After all references to the in-mem-realm destroyed,\n+        // in-mem-realm with same identifier should create a fresh new instance.\n+        testRealm = Realm.getInstance(inMemConf);\n+        assertEquals(testRealm.allObjects(Dog.class).size(), 0);\n+    }\n+\n+    // Test deleteRealm called on a in-memory Realm instance\n+    public void testDelete() {\n+        RealmConfiguration configuration = testRealm.getConfiguration();\n+        try {\n+            Realm.deleteRealm(configuration);\n+            fail(\"Realm.deleteRealm should fail with illegal state\");\n+        } catch (IllegalStateException iae) { //NOPMD\n+        }\n+\n+        // Nothing should happen when delete a closed in-mem-realm.\n+        testRealm.close();\n+        testRealm = null;\n+        assertTrue(Realm.deleteRealm(configuration));\n+    }\n+\n+    // Test if an in-memory Realm can be written to disk with/without encryption\n+    public void testWriteCopyTo() throws IOException {\n+        byte[] key = TestHelper.getRandomKey();\n+        String fileName = IDENTIFIER + \".realm\";\n+        String encFileName = IDENTIFIER + \".enc.realm\";\n+        RealmConfiguration conf = new RealmConfiguration.Builder(getContext())\n+                .name(fileName)\n+                .build();\n+        RealmConfiguration encConf = new RealmConfiguration.Builder(getContext())\n+                .name(encFileName)\n+                .encryptionKey(key)\n+                .build();\n+\n+        Realm.deleteRealm(conf);\n+        Realm.deleteRealm(encConf);\n+\n+        testRealm.beginTransaction();\n+        Dog dog = testRealm.createObject(Dog.class);\n+        dog.setName(\"DinoDog\");\n+        testRealm.commitTransaction();\n+\n+        // Test a normal Realm file\n+        testRealm.writeCopyTo(new File(getContext().getFilesDir(), fileName));\n+        Realm onDiskRealm = Realm.getInstance(conf);\n+        assertEquals(onDiskRealm.allObjects(Dog.class).size(), 1);\n+        onDiskRealm.close();\n+\n+        // Test a encrypted Realm file\n+        testRealm.writeEncryptedCopyTo(new File(getContext().getFilesDir(), encFileName), key);\n+        onDiskRealm = Realm.getInstance(encConf);\n+        assertEquals(onDiskRealm.allObjects(Dog.class).size(), 1);\n+        onDiskRealm.close();\n+        // Test with a wrong key to see if it fails as expected.\n+        try {\n+            RealmConfiguration wrongKeyConf = new RealmConfiguration.Builder(getContext())\n+                    .name(encFileName)\n+                    .encryptionKey(TestHelper.getRandomKey(42))\n+                    .build();\n+            Realm.getInstance(wrongKeyConf);\n+            fail(\"Realm.getInstance should fail with illegal argument\");\n+        } catch (IllegalArgumentException iae) { //NOPMD","path":"realm/src/androidTest/java/io/realm/RealmInMemoryTest.java","position":148,"original_position":148,"commit_id":"b230647211eaab4574567c747b5b372b1efb9b40","original_commit_id":"b230647211eaab4574567c747b5b372b1efb9b40","user":{"login":"cmelchior","id":406066,"avatar_url":"https://avatars.githubusercontent.com/u/406066?v=3","gravatar_id":"","url":"https://api.github.com/users/cmelchior","html_url":"https://github.com/cmelchior","followers_url":"https://api.github.com/users/cmelchior/followers","following_url":"https://api.github.com/users/cmelchior/following{/other_user}","gists_url":"https://api.github.com/users/cmelchior/gists{/gist_id}","starred_url":"https://api.github.com/users/cmelchior/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cmelchior/subscriptions","organizations_url":"https://api.github.com/users/cmelchior/orgs","repos_url":"https://api.github.com/users/cmelchior/repos","events_url":"https://api.github.com/users/cmelchior/events{/privacy}","received_events_url":"https://api.github.com/users/cmelchior/received_events","type":"User","site_admin":false},"body":"We usually call this exception \"expected\" and remove the comment.","created_at":"2015-06-23T06:49:53Z","updated_at":"2015-06-23T06:49:53Z","html_url":"https://github.com/realm/realm-java/pull/1228#discussion_r33013895","pull_request_url":"https://api.github.com/repos/realm/realm-java/pulls/1228","_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments/33013895"},"html":{"href":"https://github.com/realm/realm-java/pull/1228#discussion_r33013895"},"pull_request":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1228"}}},"pull_request":{"url":"https://api.github.com/repos/realm/realm-java/pulls/1228","id":38181099,"html_url":"https://github.com/realm/realm-java/pull/1228","diff_url":"https://github.com/realm/realm-java/pull/1228.diff","patch_url":"https://github.com/realm/realm-java/pull/1228.patch","issue_url":"https://api.github.com/repos/realm/realm-java/issues/1228","number":1228,"state":"open","locked":false,"title":"Add support for in-memory Realm","user":{"login":"beeender","id":449296,"avatar_url":"https://avatars.githubusercontent.com/u/449296?v=3","gravatar_id":"","url":"https://api.github.com/users/beeender","html_url":"https://github.com/beeender","followers_url":"https://api.github.com/users/beeender/followers","following_url":"https://api.github.com/users/beeender/following{/other_user}","gists_url":"https://api.github.com/users/beeender/gists{/gist_id}","starred_url":"https://api.github.com/users/beeender/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/beeender/subscriptions","organizations_url":"https://api.github.com/users/beeender/orgs","repos_url":"https://api.github.com/users/beeender/repos","events_url":"https://api.github.com/users/beeender/events{/privacy}","received_events_url":"https://api.github.com/users/beeender/received_events","type":"User","site_admin":false},"body":"Implementation of #1007 \r\n1. Add durability to createNativeWithImplicitTransactions.\r\n2. Add durability to RealmConfiguration.\r\n3. Support passing durability to SharedGroup constructor.\r\n4. Add new static method getInMemoryRealm to Realm class.\r\n5. Add test cases.","created_at":"2015-06-21T08:31:41Z","updated_at":"2015-06-23T06:49:53Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e8944d39d4dd66fc3bb3c8ac75d0d7a802aa2b05","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/realm/realm-java/pulls/1228/commits","review_comments_url":"https://api.github.com/repos/realm/realm-java/pulls/1228/comments","review_comment_url":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}","comments_url":"https://api.github.com/repos/realm/realm-java/issues/1228/comments","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/b230647211eaab4574567c747b5b372b1efb9b40","head":{"label":"realm:mc-in-mem-realm","ref":"mc-in-mem-realm","sha":"b230647211eaab4574567c747b5b372b1efb9b40","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","created_at":"2012-04-20T09:18:42Z","updated_at":"2015-06-22T22:49:49Z","pushed_at":"2015-06-23T06:24:12Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":54648,"stargazers_count":1914,"watchers_count":1914,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":175,"mirror_url":null,"open_issues_count":117,"forks":175,"open_issues":117,"watchers":1914,"default_branch":"master"}},"base":{"label":"realm:master","ref":"master","sha":"0314765725e7b93b49139c4d3c6adb7af37c5e30","user":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"repo":{"id":4084908,"name":"realm-java","full_name":"realm/realm-java","owner":{"login":"realm","id":7575099,"avatar_url":"https://avatars.githubusercontent.com/u/7575099?v=3","gravatar_id":"","url":"https://api.github.com/users/realm","html_url":"https://github.com/realm","followers_url":"https://api.github.com/users/realm/followers","following_url":"https://api.github.com/users/realm/following{/other_user}","gists_url":"https://api.github.com/users/realm/gists{/gist_id}","starred_url":"https://api.github.com/users/realm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/realm/subscriptions","organizations_url":"https://api.github.com/users/realm/orgs","repos_url":"https://api.github.com/users/realm/repos","events_url":"https://api.github.com/users/realm/events{/privacy}","received_events_url":"https://api.github.com/users/realm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/realm/realm-java","description":"Realm is a mobile database: a replacement for SQLite & ORMs","fork":false,"url":"https://api.github.com/repos/realm/realm-java","forks_url":"https://api.github.com/repos/realm/realm-java/forks","keys_url":"https://api.github.com/repos/realm/realm-java/keys{/key_id}","collaborators_url":"https://api.github.com/repos/realm/realm-java/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/realm/realm-java/teams","hooks_url":"https://api.github.com/repos/realm/realm-java/hooks","issue_events_url":"https://api.github.com/repos/realm/realm-java/issues/events{/number}","events_url":"https://api.github.com/repos/realm/realm-java/events","assignees_url":"https://api.github.com/repos/realm/realm-java/assignees{/user}","branches_url":"https://api.github.com/repos/realm/realm-java/branches{/branch}","tags_url":"https://api.github.com/repos/realm/realm-java/tags","blobs_url":"https://api.github.com/repos/realm/realm-java/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/realm/realm-java/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/realm/realm-java/git/refs{/sha}","trees_url":"https://api.github.com/repos/realm/realm-java/git/trees{/sha}","statuses_url":"https://api.github.com/repos/realm/realm-java/statuses/{sha}","languages_url":"https://api.github.com/repos/realm/realm-java/languages","stargazers_url":"https://api.github.com/repos/realm/realm-java/stargazers","contributors_url":"https://api.github.com/repos/realm/realm-java/contributors","subscribers_url":"https://api.github.com/repos/realm/realm-java/subscribers","subscription_url":"https://api.github.com/repos/realm/realm-java/subscription","commits_url":"https://api.github.com/repos/realm/realm-java/commits{/sha}","git_commits_url":"https://api.github.com/repos/realm/realm-java/git/commits{/sha}","comments_url":"https://api.github.com/repos/realm/realm-java/comments{/number}","issue_comment_url":"https://api.github.com/repos/realm/realm-java/issues/comments{/number}","contents_url":"https://api.github.com/repos/realm/realm-java/contents/{+path}","compare_url":"https://api.github.com/repos/realm/realm-java/compare/{base}...{head}","merges_url":"https://api.github.com/repos/realm/realm-java/merges","archive_url":"https://api.github.com/repos/realm/realm-java/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/realm/realm-java/downloads","issues_url":"https://api.github.com/repos/realm/realm-java/issues{/number}","pulls_url":"https://api.github.com/repos/realm/realm-java/pulls{/number}","milestones_url":"https://api.github.com/repos/realm/realm-java/milestones{/number}","notifications_url":"https://api.github.com/repos/realm/realm-java/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/realm/realm-java/labels{/name}","releases_url":"https://api.github.com/repos/realm/realm-java/releases{/id}","created_at":"2012-04-20T09:18:42Z","updated_at":"2015-06-22T22:49:49Z","pushed_at":"2015-06-23T06:24:12Z","git_url":"git://github.com/realm/realm-java.git","ssh_url":"git@github.com:realm/realm-java.git","clone_url":"https://github.com/realm/realm-java.git","svn_url":"https://github.com/realm/realm-java","homepage":"http://realm.io","size":54648,"stargazers_count":1914,"watchers_count":1914,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":175,"mirror_url":null,"open_issues_count":117,"forks":175,"open_issues":117,"watchers":1914,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1228"},"html":{"href":"https://github.com/realm/realm-java/pull/1228"},"issue":{"href":"https://api.github.com/repos/realm/realm-java/issues/1228"},"comments":{"href":"https://api.github.com/repos/realm/realm-java/issues/1228/comments"},"review_comments":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1228/comments"},"review_comment":{"href":"https://api.github.com/repos/realm/realm-java/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/realm/realm-java/pulls/1228/commits"},"statuses":{"href":"https://api.github.com/repos/realm/realm-java/statuses/b230647211eaab4574567c747b5b372b1efb9b40"}}}}