This repository was archived by the owner on Feb 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated RealmObject as valid when created or updated, and false when …
…deleted.
- Loading branch information
Juan Mendez
committed
Jul 6, 2017
1 parent
305c676
commit 4d6d9f8
Showing
31 changed files
with
3,874 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/info/juanmendez/mockrealm/decorators/RealmConfigurationDecorator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
* [email protected] | ||
*/ | ||
|
||
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()); | ||
} | ||
} |
Oops, something went wrong.