@Test public void testShouldCreateCorrectTrustFactoryForTrustAllSSLUrl() throws Exception { String uri = "imap+ssl://xoauth:foooo@imap.gmail.com"; BackupImapStore store = new BackupImapStore(RuntimeEnvironment.application, uri, true); assertThat(store.getTrustedSocketFactory()).isInstanceOf(AllTrustedSocketFactory.class); }
@Override public String toString() { return "BackupImapStore{" + "uri=" + getStoreUriForLogging() + '}'; }
@Test public void shouldTestForValidFolder() throws Exception { assertThat(isValidImapFolder(null)).isFalse(); assertThat(isValidImapFolder("")).isFalse(); assertThat(isValidImapFolder("foo")).isTrue(); assertThat(isValidImapFolder("foo bar")).isTrue(); assertThat(isValidImapFolder(" foo")).isFalse(); assertThat(isValidImapFolder("foo ")).isFalse(); assertThat(isValidImapFolder("foo/nested")).isTrue(); assertThat(isValidImapFolder("/foo/nested")).isFalse(); }
protected BackupImapStore getBackupImapStore() throws MessagingException { final String uri = getAuthPreferences().getStoreUri(); if (!BackupImapStore.isValidUri(uri)) { throw new MessagingException("No valid IMAP URI: "+uri); } return new BackupImapStore(getApplicationContext(), uri, getAuthPreferences().isTrustAllCertificates()); }
@Test public void shouldHaveToStringWithStoreUriForLogging() throws Exception { BackupImapStore store = new BackupImapStore(RuntimeEnvironment.application, "imap://xoauth:foooo@imap.gmail.com", false); assertThat(store.toString()).isEqualTo("BackupImapStore{uri=imap://xoauth:XXXXX@imap.gmail.com}"); } }
try { publishProgress(LOGIN); imapStore.checkSettings(); msgs.addAll(imapStore.getFolder(SMS, preferences.getDataTypePreferences()).getMessages(config.maxRestore, config.restoreOnlyStarred, null)); msgs.addAll(imapStore.getFolder(CALLLOG, preferences.getDataTypePreferences()).getMessages(config.maxRestore, config.restoreOnlyStarred, null)); imapStore.closeFolders();
@Test public void shouldHaveToStringWithObfuscatedStoreURIWithPort() throws Exception { BackupImapStore store = new BackupImapStore(RuntimeEnvironment.application, "imap://xoauth:foooo@imap.gmail.com:456", false); assertThat(store.getStoreUriForLogging()).isEqualTo("imap://xoauth:XXXXX@imap.gmail.com:456"); }
@Test public void shouldCreateFoldersLazilyOnlyForNeededTypes() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), eq(SMS))).thenReturn(result(SMS, 1)); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenReturn(folder); task.doInBackground(config); verify(store).getFolder(SMS, dataTypePreferences); verify(store, never()).getFolder(MMS, dataTypePreferences); verify(store, never()).getFolder(CALLLOG, dataTypePreferences); }
@Test public void shouldVerifyStoreSettings() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), eq(SMS))).thenReturn(result(SMS, 1)); when(store.getFolder(SMS, dataTypePreferences)).thenReturn(folder); task.doInBackground(config); verify(store).checkSettings(); }
@Test public void shouldCloseImapFolderAfterBackup() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), eq(SMS))).thenReturn(result(SMS, 1)); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenReturn(folder); task.doInBackground(config); verify(store).closeFolders(); }
@Test public void testAccountHasStoreUri() throws Exception { String uri = "imap://xoauth:foooo@imap.gmail.com"; BackupImapStore store = new BackupImapStore(RuntimeEnvironment.application, uri, false); assertThat(store.getStoreUri()).isEqualTo(uri); }
@Test public void shouldCloseFolders() throws Exception { task.doInBackground(config); verify(store).closeFolders(); }
@Test(expected = MessagingException.class) public void shouldThrowExceptionIfUsernameIsMissing() throws Exception { new BackupImapStore(RuntimeEnvironment.application, "imap://imap.gmail.com:1234", false); }
@Test public void shouldTestForValidUri() throws Exception { assertThat(isValidUri("imap+ssl+://xoauth:foooo@imap.gmail.com:993")).isTrue(); assertThat(isValidUri("imap://xoauth:foooo@imap.gmail.com")).isTrue(); assertThat(isValidUri("imap+ssl+://xoauth:user:token@:993")).isFalse(); assertThat(isValidUri("imap+ssl://user%40domain:password@imap.gmail.com:993")).isFalse(); assertThat(isValidUri("imap+tls+://user:password@imap.gmail.com:993")).isTrue(); assertThat(isValidUri("imap+tls://user:password@imap.gmail.com:993")).isFalse(); assertThat(isValidUri("imap://user:password@imap.gmail.com:993")).isTrue(); assertThat(isValidUri("http://xoauth:foooo@imap.gmail.com:993")).isFalse(); }
@Test public void shouldVerifyStoreSettings() throws Exception { task.doInBackground(config); verify(store).checkSettings(); }
public BackupFolder getFolder(DataType type, DataTypePreferences preferences) throws MessagingException { BackupFolder folder = openFolders.get(type); if (folder == null) { String label = preferences.getFolder(type); if (label == null) throw new IllegalStateException("label is null"); folder = createAndOpenFolder(type, label); openFolders.put(type, folder); } return folder; }
Log.i(TAG, String.format(Locale.ENGLISH, "Starting backup (%d messages)", itemsToSync)); publish(LOGIN); store.checkSettings(); store.getFolder(cursor.type, preferences.getDataTypePreferences()).appendMessages(messages); backupType, null, null); } finally { store.closeFolders();
@Test public void shouldHaveToStringWithObfuscatedStoreURI() throws Exception { BackupImapStore store = new BackupImapStore(RuntimeEnvironment.application, "imap://xoauth:foooo@imap.gmail.com", false); assertThat(store.getStoreUriForLogging()).isEqualTo("imap://xoauth:XXXXX@imap.gmail.com"); }
@Before public void before() throws MessagingException { initMocks(this); config = new RestoreConfig(store, 0, true, false, false, -1, 0); when(service.getApplicationContext()).thenReturn(RuntimeEnvironment.application); when(service.getState()).thenReturn(state); when(service.getPreferences()).thenReturn(new Preferences(RuntimeEnvironment.application)); when(store.getFolder(any(DataType.class), any(DataTypePreferences.class))).thenReturn(folder); task = new RestoreTask(service, converter, resolver, tokenRefresher); }
@Test public void shouldRestoreItems() throws Exception { Date now = new Date(); List<ImapMessage> messages = new ArrayList<ImapMessage>(); ContentValues values = new ContentValues(); values.put(Telephony.TextBasedSmsColumns.TYPE, Telephony.TextBasedSmsColumns.MESSAGE_TYPE_INBOX); values.put(Telephony.TextBasedSmsColumns.DATE, now.getTime()); ImapMessage mockMessage = mock(ImapMessage.class); when(mockMessage.getFolder()).thenReturn(folder); when(converter.getDataType(mockMessage)).thenReturn(DataType.SMS); when(converter.messageToContentValues(mockMessage)).thenReturn(values); messages.add(mockMessage); when(folder.getMessages(anyInt(), anyBoolean(), any(Date.class))).thenReturn(messages); when(resolver.insert(Consts.SMS_PROVIDER, values)).thenReturn(Uri.parse("content://sms/123")); task.doInBackground(config); verify(resolver).insert(Consts.SMS_PROVIDER, values); verify(resolver).delete(Uri.parse("content://sms/conversations/-1"), null, null); assertThat(service.getPreferences().getDataTypePreferences().getMaxSyncedDate(DataType.SMS)).isEqualTo(now.getTime()); assertThat(task.getSmsIds()).containsExactly("123"); verify(store).closeFolders(); } }