use of io.realm.exceptions.RealmFileException in project realm-java by realm.
the class RealmTests method getInstance_writeProtectedFile.
@Test
public void getInstance_writeProtectedFile() throws IOException {
String REALM_FILE = "readonly.realm";
File folder = configFactory.getRoot();
File realmFile = new File(folder, REALM_FILE);
assertFalse(realmFile.exists());
assertTrue(realmFile.createNewFile());
assertTrue(realmFile.setWritable(false));
try {
Realm.getInstance(new RealmConfiguration.Builder(InstrumentationRegistry.getTargetContext()).directory(folder).name(REALM_FILE).build());
} catch (RealmFileException expected) {
assertEquals(expected.getKind(), RealmFileException.Kind.PERMISSION_DENIED);
}
}
use of io.realm.exceptions.RealmFileException in project realm-java by realm.
the class RealmInMemoryTest method writeCopyTo.
// Tests if an in-memory Realm can be written to disk with/without encryption.
@Test
public void writeCopyTo() {
byte[] key = TestHelper.getRandomKey();
String fileName = IDENTIFIER + ".realm";
String encFileName = IDENTIFIER + ".enc.realm";
RealmConfiguration conf = configFactory.createConfigurationBuilder().name(fileName).build();
RealmConfiguration encConf = configFactory.createConfigurationBuilder().name(encFileName).encryptionKey(key).build();
Realm.deleteRealm(conf);
Realm.deleteRealm(encConf);
testRealm.beginTransaction();
Dog dog = testRealm.createObject(Dog.class);
dog.setName("DinoDog");
testRealm.commitTransaction();
// Tests a normal Realm file.
testRealm.writeCopyTo(new File(configFactory.getRoot(), fileName));
Realm onDiskRealm = Realm.getInstance(conf);
assertEquals(onDiskRealm.where(Dog.class).count(), 1);
onDiskRealm.close();
// Tests a encrypted Realm file.
testRealm.writeEncryptedCopyTo(new File(configFactory.getRoot(), encFileName), key);
onDiskRealm = Realm.getInstance(encConf);
assertEquals(onDiskRealm.where(Dog.class).count(), 1);
onDiskRealm.close();
// Tests with a wrong key to see if it fails as expected.
try {
RealmConfiguration wrongKeyConf = configFactory.createConfigurationBuilder().name(encFileName).encryptionKey(TestHelper.getRandomKey(42)).build();
Realm.getInstance(wrongKeyConf);
fail("Realm.getInstance should fail with RealmFileException");
} catch (RealmFileException expected) {
assertEquals(expected.getKind(), RealmFileException.Kind.ACCESS_ERROR);
}
}
use of io.realm.exceptions.RealmFileException in project realm-java by realm.
the class RealmCache method copyAssetFileIfNeeded.
/**
* Copies Realm database file from Android asset directory to the directory given in the {@link RealmConfiguration}.
* Copy is performed only at the first time when there is no Realm database file.
*
* @param configuration configuration object for Realm instance.
* @throws RealmFileException if copying the file fails.
*/
private static void copyAssetFileIfNeeded(RealmConfiguration configuration) {
IOException exceptionWhenClose = null;
if (configuration.hasAssetFile()) {
File realmFile = new File(configuration.getRealmDirectory(), configuration.getRealmFileName());
if (realmFile.exists()) {
return;
}
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = configuration.getAssetFile();
if (inputStream == null) {
throw new RealmFileException(RealmFileException.Kind.ACCESS_ERROR, "Invalid input stream to asset file.");
}
outputStream = new FileOutputStream(realmFile);
byte[] buf = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > -1) {
outputStream.write(buf, 0, bytesRead);
}
} catch (IOException e) {
throw new RealmFileException(RealmFileException.Kind.ACCESS_ERROR, "Could not resolve the path to the Realm asset file.", e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
exceptionWhenClose = e;
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
// Ignores this one if there was an exception when close inputStream.
if (exceptionWhenClose == null) {
exceptionWhenClose = e;
}
}
}
}
// No other exception has been thrown, only the exception when close. So, throw it.
if (exceptionWhenClose != null) {
throw new RealmFileException(RealmFileException.Kind.ACCESS_ERROR, exceptionWhenClose);
}
}
}
use of io.realm.exceptions.RealmFileException in project realm-java by realm.
the class RealmTests method getInstance_writeProtectedFileWithContext.
@Test
public void getInstance_writeProtectedFileWithContext() throws IOException {
String REALM_FILE = "readonly.realm";
File folder = configFactory.getRoot();
File realmFile = new File(folder, REALM_FILE);
assertFalse(realmFile.exists());
assertTrue(realmFile.createNewFile());
assertTrue(realmFile.setWritable(false));
try {
Realm.getInstance(new RealmConfiguration.Builder(context).directory(folder).name(REALM_FILE).build());
} catch (RealmFileException expected) {
assertEquals(expected.getKind(), RealmFileException.Kind.PERMISSION_DENIED);
}
}
Aggregations