use of org.powermock.core.classloader.annotations.PrepareForTest in project alluxio by Alluxio.
the class OSSOutputStreamTest method testCloseSuccess.
/**
* Tests to ensure {@link File#delete()} is called when close the stream.
*/
@Test
@PrepareForTest(OSSOutputStream.class)
public void testCloseSuccess() throws Exception {
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile);
FileOutputStream outputStream = PowerMockito.mock(FileOutputStream.class);
PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile).thenReturn(outputStream);
FileInputStream inputStream = PowerMockito.mock(FileInputStream.class);
PowerMockito.whenNew(FileInputStream.class).withArguments(mFile).thenReturn(inputStream);
OSSOutputStream stream = new OSSOutputStream("testBucketName", "testKey", mOssClient, sConf.getList(PropertyKey.TMP_DIRS, ","));
stream.close();
Mockito.verify(mFile).delete();
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project alluxio by Alluxio.
the class KodoOutputStreamTest method testConstructor.
/**
* Tests to ensure IOException is thrown if {@link FileOutputStream ()} throws an IOException.
*/
@Test
@PrepareForTest(KodoOutputStream.class)
public void testConstructor() throws Exception {
PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile);
String errorMessage = "protocol doesn't support output";
PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile).thenThrow(new IOException(errorMessage));
mThrown.expect(IOException.class);
mThrown.expectMessage(errorMessage);
new KodoOutputStream("testKey", mKodoClient, sTmpDirs).close();
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.
the class HashUtilsTest method algorithmNotFound.
@Test(expected = RuntimeException.class)
@PrepareForTest(HashUtils.class)
public void algorithmNotFound() throws NoSuchAlgorithmException {
mockStatic(MessageDigest.class);
NoSuchAlgorithmException cause = new NoSuchAlgorithmException();
doThrow(cause).when(MessageDigest.class);
MessageDigest.getInstance(anyString());
try {
HashUtils.sha256("");
} catch (RuntimeException e) {
assertEquals(cause, e.getCause());
throw e;
}
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.
the class AppCenterFutureTest method isDoneWithInterruption.
@Test
@PrepareForTest(DefaultAppCenterFuture.class)
public void isDoneWithInterruption() throws Exception {
CountDownLatch latch = mock(CountDownLatch.class);
whenNew(CountDownLatch.class).withAnyArguments().thenReturn(latch);
when(latch.await(anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException()).thenReturn(true);
final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>();
final AtomicReference<Boolean> result = new AtomicReference<>();
Thread thread = new Thread() {
@Override
public void run() {
result.set(future.isDone());
}
};
thread.start();
thread.join();
assertEquals(true, result.get());
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.
the class DistributeWarnUnknownSourcesTest method clickSettingsThenEnableThenBack.
@Test
@PrepareForTest(AsyncTaskUtils.class)
public void clickSettingsThenEnableThenBack() throws Exception {
/* Click settings. */
Intent intent = mock(Intent.class);
whenNew(Intent.class).withArguments(Settings.ACTION_SECURITY_SETTINGS).thenReturn(intent);
ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_unknown_sources_dialog_settings), clickListener.capture());
clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
when(mUnknownSourcesDialog.isShowing()).thenReturn(false);
/* Verify navigation. */
verify(mFirstActivity).startActivity(intent);
/* Simulate we go to settings, change value then go back. */
mockStatic(AsyncTaskUtils.class);
Distribute.getInstance().onActivityPaused(mFirstActivity);
when(InstallerUtils.isUnknownSourcesEnabled(mContext)).thenReturn(true);
Distribute.getInstance().onActivityResumed(mFirstActivity);
/* No more dialog, start download. */
verify(mDialog).show();
verify(mDialog, never()).hide();
verify(mUnknownSourcesDialog).show();
verify(mUnknownSourcesDialog, never()).hide();
verify(mReleaseDownloader).resume();
}
Aggregations