Search in sources :

Example 91 with PrepareForTest

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();
}
Also used : FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 92 with PrepareForTest

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();
}
Also used : IOException(java.io.IOException) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 93 with PrepareForTest

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;
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 94 with PrepareForTest

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 95 with PrepareForTest

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();
}
Also used : DialogInterface(android.content.DialogInterface) Intent(android.content.Intent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)196 Test (org.junit.Test)194 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)30 StringWriter (java.io.StringWriter)28 PrintWriter (java.io.PrintWriter)27 File (java.io.File)24 ArrayList (java.util.ArrayList)16 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)12 Config (com.twitter.heron.spi.common.Config)12 Matchers.anyString (org.mockito.Matchers.anyString)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 IOException (java.io.IOException)11 Date (java.util.Date)10 HashMap (java.util.HashMap)10