Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
updated RealmObject as valid when created or updated, and false when …
Browse files Browse the repository at this point in the history
…deleted.
  • Loading branch information
Juan Mendez committed Jul 6, 2017
1 parent 305c676 commit 4d6d9f8
Show file tree
Hide file tree
Showing 31 changed files with 3,874 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public static RealmModel decorate(RealmModel realmModel ){
if( realmModel instanceof RealmObject ){
RealmObjectDecorator.handleDeleteActions( (RealmObject) realmModel);
RealmObjectDecorator.handleAsyncMethods( (RealmObject) realmModel);
setValid( realmModel, true );
}

return realmModel;
Expand Down
2 changes: 1 addition & 1 deletion mockrealm/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_name">library</string>
<string name="app_name">Mocking Realm</string>
</resources>
25 changes: 25 additions & 0 deletions proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\musta\AppData\Local\Android\Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
9 changes: 9 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="info.juanmendez.mockrealm">

<application android:allowBackup="true" android:label="@string/app_name"
android:supportsRtl="true">
</application>

</manifest>
62 changes: 62 additions & 0 deletions src/main/java/info/juanmendez/mockrealm/MockRealm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package info.juanmendez.mockrealm;

import info.juanmendez.mockrealm.decorators.RealmConfigurationDecorator;
import info.juanmendez.mockrealm.decorators.RealmDecorator;
import info.juanmendez.mockrealm.decorators.RealmListDecorator;
import info.juanmendez.mockrealm.decorators.RealmModelDecorator;
import info.juanmendez.mockrealm.decorators.RealmObjectDecorator;
import info.juanmendez.mockrealm.dependencies.RealmStorage;
import info.juanmendez.mockrealm.models.RealmAnnotation;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmList;
import io.realm.RealmObject;
import io.realm.RealmQuery;
import io.realm.RealmResults;

import static org.powermock.api.mockito.PowerMockito.mockStatic;

/**
* Created by @juanmendezinfo on 2/15/2017.
*/
public class MockRealm {

/**
* This is a method required in order to start up
* testing.
* @throws Exception
*/
public static void prepare() throws Exception {
mockStatic( RealmList.class );
mockStatic( Realm.class );
mockStatic( RealmConfiguration.class);
mockStatic( RealmQuery.class );
mockStatic( RealmResults.class );
mockStatic( RealmObject.class );

RealmListDecorator.prepare();
RealmModelDecorator.prepare();
RealmObjectDecorator.prepare();
RealmDecorator.prepare();
RealmConfigurationDecorator.prepare();
}

/**
* Make sure to include each of your class annotation references through RealmAnnotation
* before testing each type of realmModel in your project
* @param annotations
*/
public static void addAnnotations(RealmAnnotation ... annotations ){
for( RealmAnnotation annotation: annotations ){
RealmStorage.addAnnotations( annotation );
}
}

/**
* Call this method each time you want to clear your realm entries;
* specially, when starting a new test.
*/
public static void clearData(){
RealmStorage.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package info.juanmendez.mockrealm.decorators;

import java.io.File;

import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.rx.RxObservableFactory;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyVararg;
import static org.powermock.api.mockito.PowerMockito.doAnswer;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.whenNew;

/**
* Created by Juan Mendez on 2/23/2017.
* www.juanmendez.info
* contact@juanmendez.info
*/

public class RealmConfigurationDecorator {


public static void prepare() throws Exception {

RealmConfiguration mockRealmConfig = mock(RealmConfiguration.class);

// make a mockery of our inner class
RealmConfiguration.Builder mockedBuilder = mock(RealmConfiguration.Builder.class);

// magically return the mock when a new instance is required
whenNew(RealmConfiguration.Builder.class).withNoArguments().thenAnswer(invocation -> mockedBuilder);
whenNew(RealmConfiguration.Builder.class).withAnyArguments().thenAnswer(invocation -> mockedBuilder);
doAnswer(invocation -> mockRealmConfig ).when(mockedBuilder ).build();

//what to do with builder configs
doReturn(mockedBuilder).when( mockedBuilder ).name(anyString());
doReturn(mockedBuilder).when( mockedBuilder ).directory(any(File.class));
doReturn(mockedBuilder).when( mockedBuilder ).encryptionKey(any());
doReturn(mockedBuilder).when( mockedBuilder ).schemaVersion(anyLong());
doReturn(mockedBuilder).when( mockedBuilder ).migration(any(RealmMigration.class));
doReturn(mockedBuilder).when( mockedBuilder ).deleteRealmIfMigrationNeeded();
doReturn(mockedBuilder).when( mockedBuilder ).inMemory();
doReturn(mockedBuilder).when( mockedBuilder ).modules(any(), anyVararg());
doReturn(mockedBuilder).when( mockedBuilder ).rxFactory(any(RxObservableFactory.class));
doReturn(mockedBuilder).when( mockedBuilder ).assetFile(anyString());
}
}
Loading

0 comments on commit 4d6d9f8

Please sign in to comment.