public void testFileisEncrypted() throws IOException { EncryptedDbUtils.assertEncryptedDbUsed(db); SimpleEntity simpleEntity = createEntityWithRandomPk(); String text = "Catch me if you can"; simpleEntity.setSimpleString(text); dao.insert(simpleEntity); File dbFile = getContext().getDatabasePath(ENCRYPTED_DB_FILE); int length = (int) dbFile.length(); assertTrue(length > 0); byte[] buffer = new byte[length]; // TODO readAll int read = new FileInputStream(dbFile).read(buffer); String contents = new String(buffer, 0, read, "US-ASCII"); assertFalse(contents, contents.startsWith("SQLite")); assertFalse(contents, contents.contains("CREATE TABLE")); assertFalse(contents, contents.contains(text)); }
@Override public void readEntity(Cursor cursor, SimpleEntity entity, int offset) { entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0)); entity.setSimpleBoolean(cursor.isNull(offset + 1) ? null : cursor.getShort(offset + 1) != 0); entity.setSimpleByte(cursor.isNull(offset + 2) ? null : (byte) cursor.getShort(offset + 2)); entity.setSimpleShort(cursor.isNull(offset + 3) ? null : cursor.getShort(offset + 3)); entity.setSimpleInt(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4)); entity.setSimpleLong(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5)); entity.setSimpleFloat(cursor.isNull(offset + 6) ? null : cursor.getFloat(offset + 6)); entity.setSimpleDouble(cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7)); entity.setSimpleString(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8)); entity.setSimpleByteArray(cursor.isNull(offset + 9) ? null : cursor.getBlob(offset + 9)); }
public void testQuery() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("hello"); daoSession.insert(entity); long id = entity.getId(); SimpleEntity entity2 = new SimpleEntity(); entity2.setSimpleString("content"); daoSession.insert(entity2); long id2 = entity2.getId(); Cursor cursor = getContext().getContentResolver().query(SimpleEntityContentProvider.CONTENT_URI, null, null, null, "_id"); assertEquals(2, cursor.getCount()); int idxId = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.Id.columnName); int idxString = cursor.getColumnIndexOrThrow(SimpleEntityDao.Properties.SimpleString.columnName); assertTrue(cursor.moveToFirst()); assertEquals("hello", cursor.getString(idxString)); assertEquals(id, cursor.getLong(idxId)); assertTrue(cursor.moveToNext()); assertEquals("content", cursor.getString(idxString)); assertEquals(id2, cursor.getLong(idxId)); }
public void testAsyncUpdate() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("heho"); daoSession.insert(entity); entity.setSimpleString("updated"); AsyncOperation operation = asyncSession.update(entity); assertWaitForCompletion1Sec(); daoSession.clear(); SimpleEntity entity2 = daoSession.load(SimpleEntity.class, entity.getId()); assertNotNull(entity2); assertEquals("updated", entity2.getSimpleString()); assertFalse(operation.isFailed()); assertSingleOperationCompleted(operation); }
protected void setValuesToNull(SimpleEntity entity) { entity.setSimpleBoolean(null); entity.setSimpleByte(null); entity.setSimpleShort(null); entity.setSimpleInt(null); entity.setSimpleLong(null); entity.setSimpleFloat(null); entity.setSimpleDouble(null); entity.setSimpleString(null); entity.setSimpleByteArray(null); }
protected void setValues(SimpleEntity entity) { entity.setSimpleBoolean(true); entity.setSimpleByte(Byte.MAX_VALUE); entity.setSimpleShort(Short.MAX_VALUE); entity.setSimpleInt(Integer.MAX_VALUE); entity.setSimpleLong(Long.MAX_VALUE); entity.setSimpleFloat(Float.MAX_VALUE); entity.setSimpleDouble(Double.MAX_VALUE); entity.setSimpleString("greenrobot greenDAO"); byte[] bytes = { 42, -17, 23, 0, 127, -128 }; entity.setSimpleByteArray(bytes); }
public void testMergeInsertAndUpdate() { SimpleEntity entity = new SimpleEntity(); entity.setId(42l); entity.setSimpleString("heho"); SimpleEntity entity2 = new SimpleEntity(); entity2.setId(42l); entity2.setSimpleString("updated"); AsyncOperation op1 = asyncSession.insert(entity, AsyncOperation.FLAG_MERGE_TX); AsyncOperation op2 = asyncSession.update(entity2, AsyncOperation.FLAG_MERGE_TX); assertWaitForCompletion1Sec(); daoSession.clear(); SimpleEntity entity3 = daoSession.load(SimpleEntity.class, 42l); assertNotNull(entity3); assertEquals(entity2.getSimpleString(), entity3.getSimpleString()); assertEquals(2, op1.getMergedOperationsCount()); assertEquals(2, op2.getMergedOperationsCount()); }
public void testAsyncInsert() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("heho"); AsyncOperation operation = asyncSession.insert(entity); assertWaitForCompletion1Sec(); SimpleEntity entity2 = daoSession.load(SimpleEntity.class, entity.getId()); assertNotNull(entity2); assertEquals("heho", entity2.getSimpleString()); assertFalse(operation.isFailed()); assertSingleOperationCompleted(operation); }
public void testOperationGetResult() { SimpleEntity entity = new SimpleEntity(); entity.setSimpleString("heho"); daoSession.insert(entity); daoSession.clear(); AsyncOperation operation = asyncSession.load(SimpleEntity.class, entity.getId()); SimpleEntity result = (SimpleEntity) operation.getResult(); assertTrue(operation.isCompleted()); assertTrue(operation.isCompletedSucessfully()); assertNotNull(result); assertNotSame(entity, result); assertEquals(entity.getId(), result.getId()); assertEquals(entity.getSimpleString(), result.getSimpleString()); }