下面列出了怎么用io.realm.log.RealmLog的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Init realm configuration and instance
* @return
* @throws Exception
*/
@Provides
@Singleton
RealmConfiguration provideRealmConfiguration(){
mockStatic(RealmCore.class);
mockStatic(RealmLog.class);
mockStatic(Realm.class);
mockStatic(RealmConfiguration.class);
Realm.init(RuntimeEnvironment.application);
// TODO: Better solution would be just mock the RealmConfiguration.Builder class. But it seems there is some
// problems for powermock to mock it (static inner class). We just mock the RealmCore.loadLibrary(Context) which
// will be called by RealmConfiguration.Builder's constructor.
doNothing().when(RealmCore.class);
RealmCore.loadLibrary(any(Context.class));
final RealmConfiguration mockRealmConfig = PowerMockito.mock(RealmConfiguration.class);
try {
whenNew(RealmConfiguration.class).withAnyArguments().thenReturn(mockRealmConfig);
} catch (Exception e) {
e.printStackTrace();
}
when(Realm.getDefaultConfiguration()).thenReturn(mockRealmConfig);
// init mock realm
Realm mockRealm = PowerMockito.mock(Realm.class);;
// Anytime getInstance is called with any configuration, then return the mockRealm
when(Realm.getDefaultInstance()).thenReturn(mockRealm);
when(Realm.getInstance(mockRealmConfig)).thenReturn(mockRealm);
return mockRealmConfig;
}
@Before
public void setup() {
mockStatic(RealmLog.class);
mockStatic(Realm.class);
Realm mockRealm = PowerMockito.mock(Realm.class);
when(Realm.getDefaultInstance()).thenReturn(mockRealm);
this.mockRealm = mockRealm;
}
/**
* Encrypt then save a {@link SyncUser} object. If another user already exists, it will be replaced.
* {@link SyncUser#getIdentity()} is used as a unique identifier of a given {@link SyncUser}.
*
* @param user {@link SyncUser} object to store.
*/
@Override
public void put(SyncUser user) {
try {
String userSerialisedAndEncrypted = cipherClient.encrypt(user.toJson());
nativeUpdateOrCreateUser(user.getIdentity(), userSerialisedAndEncrypted, user.getAuthenticationUrl().toString());
} catch (KeyStoreException e) {
RealmLog.error(e);
}
}
private SyncUser toDecryptedSyncUserOrNull(String userEncryptedJson) {
if (userEncryptedJson != null) {
try {
String userSerialisedAndDecrypted = cipherClient.decrypt(userEncryptedJson);
return SyncUser.fromJson(userSerialisedAndDecrypted);
} catch (KeyStoreException e) {
RealmLog.error(e);
return null;
}
}
return null;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_items);
setSupportActionBar(findViewById(R.id.toolbar));
findViewById(R.id.fab).setOnClickListener(view -> {
View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_task, null);
((EditText) dialogView.findViewById(R.id.task)).setHint(R.string.project_description);
EditText taskText = dialogView.findViewById(R.id.task);
new AlertDialog.Builder(ProjectsActivity.this)
.setTitle("Add a new project")
.setView(dialogView)
.setPositiveButton("Add", (dialog, which) -> realm.executeTransactionAsync(realm -> {
String userId = SyncUser.current().getIdentity();
String name = taskText.getText().toString();
Project project = new Project();
project.setId(UUID.randomUUID().toString());
project.setOwner(userId);
project.setName(name);
project.setTimestamp(new Date());
realm.insert(project);
}, error -> RealmLog.error(error) ))
.setNegativeButton("Cancel", null)
.create()
.show();
});
// Create a subscription that only download the users projects from the server.
realm = Realm.getDefaultInstance();
RealmResults<Project> projects = realm
.where(Project.class)
.equalTo("owner", SyncUser.current().getIdentity())
.sort("timestamp", Sort.DESCENDING)
.findAllAsync();
final ProjectsRecyclerAdapter itemsRecyclerAdapter = new ProjectsRecyclerAdapter(this, projects);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(itemsRecyclerAdapter);
}