-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Made Realm.compactRealmFile() more failure resilient. #955
Changes from all commits
703512b
7af8aa7
a403a0a
22c6f26
6216a8a
dabadec
ee0ff69
d29179c
458dcba
b69d6ca
880ed79
9ba0f1e
b4dfc2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,7 @@ | |
import android.test.AndroidTestCase; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.channels.FileChannel; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
|
@@ -37,9 +34,9 @@ | |
|
||
import io.realm.entities.AllTypes; | ||
import io.realm.entities.AllTypesPrimaryKey; | ||
import io.realm.entities.Cat; | ||
import io.realm.entities.CyclicType; | ||
import io.realm.entities.CyclicTypePrimaryKey; | ||
import io.realm.entities.Cat; | ||
import io.realm.entities.Dog; | ||
import io.realm.entities.DogPrimaryKey; | ||
import io.realm.entities.NonLatinFieldNames; | ||
|
@@ -92,30 +89,30 @@ protected void tearDown() throws Exception { | |
} | ||
} | ||
|
||
private void populateTestRealm(int objects) { | ||
testRealm.beginTransaction(); | ||
testRealm.allObjects(AllTypes.class).clear(); | ||
testRealm.allObjects(NonLatinFieldNames.class).clear(); | ||
private void populateTestRealm(Realm realm, int objects) { | ||
realm.beginTransaction(); | ||
realm.allObjects(AllTypes.class).clear(); | ||
realm.allObjects(NonLatinFieldNames.class).clear(); | ||
for (int i = 0; i < objects; ++i) { | ||
AllTypes allTypes = testRealm.createObject(AllTypes.class); | ||
AllTypes allTypes = realm.createObject(AllTypes.class); | ||
allTypes.setColumnBoolean((i % 3) == 0); | ||
allTypes.setColumnBinary(new byte[]{1, 2, 3}); | ||
allTypes.setColumnDate(new Date()); | ||
allTypes.setColumnDouble(3.1415); | ||
allTypes.setColumnFloat(1.234567f + i); | ||
allTypes.setColumnString("test data " + i); | ||
allTypes.setColumnLong(i); | ||
NonLatinFieldNames nonLatinFieldNames = testRealm.createObject(NonLatinFieldNames.class); | ||
NonLatinFieldNames nonLatinFieldNames = realm.createObject(NonLatinFieldNames.class); | ||
nonLatinFieldNames.set델타(i); | ||
nonLatinFieldNames.setΔέλτα(i); | ||
nonLatinFieldNames.set베타(1.234567f + i); | ||
nonLatinFieldNames.setΒήτα(1.234567f + i); | ||
} | ||
testRealm.commitTransaction(); | ||
realm.commitTransaction(); | ||
} | ||
|
||
private void populateTestRealm() { | ||
populateTestRealm(TEST_DATA_SIZE); | ||
populateTestRealm(testRealm, TEST_DATA_SIZE); | ||
} | ||
|
||
|
||
|
@@ -287,7 +284,7 @@ public void testShouldReturnResultSet() { | |
|
||
// Note that this test is relying on the values set while initializing the test dataset | ||
public void testQueriesResults() throws IOException { | ||
populateTestRealm(159); | ||
populateTestRealm(testRealm, 159); | ||
RealmResults<AllTypes> resultList = testRealm.where(AllTypes.class).equalTo(FIELD_LONG, 33).findAll(); | ||
assertEquals(1, resultList.size()); | ||
|
||
|
@@ -937,14 +934,66 @@ public void testWriteCopyTo() throws IOException { | |
} | ||
} | ||
|
||
public void testCompactRealmFile() throws IOException { | ||
final String copyRealm = "copy.realm"; | ||
fileCopy( | ||
new File(getContext().getFilesDir(), Realm.DEFAULT_REALM_NAME), | ||
new File(getContext().getFilesDir(), copyRealm)); | ||
long before = new File(getContext().getFilesDir(), copyRealm).length(); | ||
assertTrue(Realm.compactRealmFile(getContext())); | ||
long after = new File(getContext().getFilesDir(), copyRealm).length(); | ||
|
||
public void testCompactRealmFileThrowsIfOpen() throws IOException { | ||
try { | ||
Realm.compactRealmFile(getContext()); | ||
fail(); | ||
} catch (IllegalStateException expected) { | ||
} | ||
} | ||
|
||
public void testCompactEncryptedEmptyRealmFile() { | ||
String REALM_NAME = "enc.realm"; | ||
Realm.deleteRealmFile(getContext(), REALM_NAME); | ||
byte[] key = new byte[64]; | ||
new Random(42).nextBytes(key); | ||
Realm realm = Realm.getInstance(getContext(), REALM_NAME, key); | ||
realm.close(); | ||
// TODO: remove try/catch block when compacting encrypted Realms is supported | ||
try { | ||
assertTrue(Realm.compactRealmFile(getContext(), REALM_NAME, key)); | ||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead it should be
|
||
} | ||
} | ||
|
||
public void testCompactEncryptedPopulatedRealmFile() { | ||
String REALM_NAME = "enc.realm"; | ||
Realm.deleteRealmFile(getContext(), REALM_NAME); | ||
byte[] key = new byte[64]; | ||
new Random(42).nextBytes(key); | ||
Realm realm = Realm.getInstance(getContext(), REALM_NAME, key); | ||
populateTestRealm(realm, 100); | ||
realm.close(); | ||
// TODO: remove try/catch block when compacting encrypted Realms is supported | ||
try { | ||
assertTrue(Realm.compactRealmFile(getContext(), REALM_NAME, key)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
fail(); | ||
} catch (IllegalArgumentException expected) { | ||
} | ||
} | ||
|
||
public void testCompactEmptyRealmFile() throws IOException { | ||
final String REALM_NAME = "test.realm"; | ||
Realm.deleteRealmFile(getContext(), REALM_NAME); | ||
Realm realm = Realm.getInstance(getContext(), REALM_NAME); | ||
realm.close(); | ||
long before = new File(getContext().getFilesDir(), REALM_NAME).length(); | ||
assertTrue(Realm.compactRealmFile(getContext(), REALM_NAME)); | ||
long after = new File(getContext().getFilesDir(), REALM_NAME).length(); | ||
assertTrue(before >= after); | ||
} | ||
|
||
public void testCompactPopulateRealmFile() throws IOException { | ||
final String REALM_NAME = "test.realm"; | ||
Realm.deleteRealmFile(getContext(), REALM_NAME); | ||
Realm realm = Realm.getInstance(getContext(), REALM_NAME); | ||
populateTestRealm(realm, 100); | ||
realm.close(); | ||
long before = new File(getContext().getFilesDir(), REALM_NAME).length(); | ||
assertTrue(Realm.compactRealmFile(getContext(), REALM_NAME)); | ||
long after = new File(getContext().getFilesDir(), REALM_NAME).length(); | ||
assertTrue(before >= after); | ||
} | ||
|
||
|
@@ -991,7 +1040,7 @@ public void testCopManagedObjectToOtherRealm() { | |
|
||
public void testCopyToRealmObject() { | ||
Date date = new Date(); | ||
date.setTime(1000); // Remove ms. precission as Realm doesn't support it yet. | ||
date.setTime(1000); // Remove ms. precision as Realm doesn't support it yet. | ||
Dog dog = new Dog(); | ||
dog.setName("Fido"); | ||
RealmList<Dog> list = new RealmList<Dog>(); | ||
|
@@ -1360,16 +1409,6 @@ public void testCopyOrUpdateIterableChildObjects() { | |
assertEquals(1, testRealm.allObjects(DogPrimaryKey.class).size()); | ||
} | ||
|
||
private void fileCopy(File src, File dst) throws IOException { | ||
FileInputStream inStream = new FileInputStream(src); | ||
FileOutputStream outStream = new FileOutputStream(dst); | ||
FileChannel inChannel = inStream.getChannel(); | ||
FileChannel outChannel = outStream.getChannel(); | ||
inChannel.transferTo(0, inChannel.size(), outChannel); | ||
inStream.close(); | ||
outStream.close(); | ||
} | ||
|
||
public void testOpeningOfEncryptedRealmWithDifferentKeyInstances() { | ||
byte[] key1 = new byte[64]; | ||
byte[] key2 = new byte[64]; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should add
CATCH_FILE()
too.