Search in sources :

Example 1 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project checkstyle by checkstyle.

the class MainFrameModelTest method testOpenFileWithUnknownParseMode.

@Test
@PrepareForTest(ParseMode.class)
public void testOpenFileWithUnknownParseMode() throws CheckstyleException {
    final ParseMode unknownParseMode = PowerMockito.mock(ParseMode.class);
    Whitebox.setInternalState(unknownParseMode, "ordinal", 3);
    PowerMockito.when(unknownParseMode.toString()).thenReturn("Unknown parse mode");
    PowerMockito.mockStatic(ParseMode.class);
    PowerMockito.when(ParseMode.values()).thenReturn(new ParseMode[] { ParseMode.PLAIN_JAVA, ParseMode.JAVA_WITH_COMMENTS, ParseMode.JAVA_WITH_JAVADOC_AND_COMMENTS, unknownParseMode });
    try {
        model.setParseMode(unknownParseMode);
        model.openFile(testData);
        fail("Expected IllegalArgumentException is not thrown.");
    } catch (IllegalArgumentException ex) {
        assertEquals("Unknown mode: Unknown parse mode", ex.getMessage());
    }
}
Also used : ParseMode(com.puppycrawl.tools.checkstyle.gui.MainFrameModel.ParseMode) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project checkstyle by checkstyle.

the class CommonUtilsTest method testLoadSuppressionsUriSyntaxException.

@Test
@PrepareForTest({ CommonUtils.class, CommonUtilsTest.class })
@SuppressWarnings("unchecked")
public void testLoadSuppressionsUriSyntaxException() throws Exception {
    final URL configUrl = mock(URL.class);
    when(configUrl.toURI()).thenThrow(URISyntaxException.class);
    mockStatic(CommonUtils.class, Mockito.CALLS_REAL_METHODS);
    final String fileName = "suppressions_none.xml";
    when(CommonUtils.class.getResource(fileName)).thenReturn(configUrl);
    try {
        CommonUtils.getUriByFilename(fileName);
        fail("Exception is expected");
    } catch (CheckstyleException ex) {
        assertTrue(ex.getCause() instanceof URISyntaxException);
        assertEquals("Unable to find: " + fileName, ex.getMessage());
    }
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project weex-example by KalicyZhou.

the class WXImageTest method testInitComponentHostView.

@Test
@PrepareForTest(WXImageView.class)
public void testInitComponentHostView() throws Exception {
    ImageView imageView = mWXImage.initComponentHostView(Robolectric.setupActivity(TestActivity.class));
    assertEquals(imageView.getClass(), WXImageView.class);
}
Also used : TestActivity(com.taobao.weex.TestActivity) ImageView(android.widget.ImageView) WXImageView(com.taobao.weex.ui.view.WXImageView) WXSDKInstanceTest(com.taobao.weex.WXSDKInstanceTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class DistributeDownloadTest method notifyThenRestartAppTwice.

@Test
@PrepareForTest(Uri.class)
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void notifyThenRestartAppTwice() throws Exception {
    /* Simulate async task. */
    waitDownloadTask();
    /* Process fake download completion, should not interfere and will be ignored. */
    {
        Intent completionIntent = mock(Intent.class);
        when(completionIntent.getAction()).thenReturn(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        when(completionIntent.getLongExtra(eq(EXTRA_DOWNLOAD_ID), anyLong())).thenReturn(404L);
        new DownloadManagerReceiver().onReceive(mContext, completionIntent);
        waitCheckDownloadTask();
        verify(mDownloadManager, never()).query(any(DownloadManager.Query.class));
    }
    /* Process download completion with the real download identifier. */
    completeDownload();
    Cursor cursor = mockSuccessCursor();
    Intent installIntent = mockInstallIntent();
    /* In background. */
    Distribute.getInstance().onActivityPaused(mActivity);
    /* Mock notification. */
    when(mPackageManager.getApplicationInfo(mContext.getPackageName(), 0)).thenReturn(mock(ApplicationInfo.class));
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN);
    Notification.Builder notificationBuilder = mockNotificationBuilderChain();
    when(notificationBuilder.build()).thenReturn(mock(Notification.class));
    /* Simulate task. */
    waitCheckDownloadTask();
    /* Verify notification. */
    verify(mContext, never()).startActivity(installIntent);
    verifyStatic();
    PreferencesStorage.putInt(PREFERENCE_KEY_DOWNLOAD_STATE, DOWNLOAD_STATE_NOTIFIED);
    verify(notificationBuilder).build();
    verify(notificationBuilder, never()).getNotification();
    verify(mNotificationManager).notify(eq(DistributeUtils.getNotificationId()), any(Notification.class));
    verifyNoMoreInteractions(mNotificationManager);
    verify(cursor).close();
    /* Launch app should pop install U.I. and cancel notification. */
    when(mActivity.getPackageManager()).thenReturn(mPackageManager);
    Intent launcherIntent = mock(Intent.class);
    when(mPackageManager.getLaunchIntentForPackage(anyString())).thenReturn(launcherIntent);
    ComponentName launcher = mock(ComponentName.class);
    when(launcherIntent.resolveActivity(mPackageManager)).thenReturn(launcher);
    when(launcher.getClassName()).thenReturn(mActivity.getClass().getName());
    restartActivity();
    /* Wait again. */
    waitCheckDownloadTask();
    /* Verify U.I shown after restart and workflow completed. */
    verify(mContext).startActivity(installIntent);
    verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
    verifyStatic();
    /* Verify workflow completed. */
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_STATE);
    /* Verify however downloaded file was kept. */
    verifyStatic(never());
    PreferencesStorage.remove(PREFERENCE_KEY_DOWNLOAD_ID);
    verify(mDownloadManager, never()).remove(DOWNLOAD_ID);
    /* Verify second download (restart app again) cleans first one. */
    when(mDownloadManager.enqueue(mDownloadRequest)).thenReturn(DOWNLOAD_ID + 1);
    restartActivity();
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder, times(2)).setPositiveButton(eq(R.string.mobile_center_distribute_update_dialog_download), clickListener.capture());
    clickListener.getValue().onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    waitDownloadTask();
    /* Verify new download id in storage. */
    verifyStatic();
    PreferencesStorage.putLong(PREFERENCE_KEY_DOWNLOAD_ID, DOWNLOAD_ID + 1);
    /* Verify previous download removed. */
    verify(mDownloadManager).remove(DOWNLOAD_ID);
    /* Notification already canceled so no more call, i.e. only once. */
    verify(mNotificationManager).cancel(DistributeUtils.getNotificationId());
}
Also used : DialogInterface(android.content.DialogInterface) Build(android.os.Build) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) ComponentName(android.content.ComponentName) Cursor(android.database.Cursor) DownloadManager(android.app.DownloadManager) Notification(android.app.Notification) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) TargetApi(android.annotation.TargetApi)

Example 5 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.mobile_center_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();
    verifyStatic();
    AsyncTaskUtils.execute(anyString(), argThat(new ArgumentMatcher<AsyncTask<Object, ?, ?>>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof DownloadTask;
        }
    }), anyVararg());
}
Also used : DialogInterface(android.content.DialogInterface) ArgumentMatcher(org.mockito.ArgumentMatcher) 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)188 Test (org.junit.Test)186 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)21 ArrayList (java.util.ArrayList)15 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 Config (com.twitter.heron.spi.common.Config)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 Point (org.pentaho.di.core.gui.Point)10 Date (java.util.Date)9 HashMap (java.util.HashMap)9 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9