Search in sources :

Example 46 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project AppCenter-SDK-Android by Microsoft.

the class DistributeWarnUnknownSourcesTest method clickSettingsOnAndroidO.

@Test
@PrepareForTest(Build.class)
@SuppressLint("InlinedApi")
public void clickSettingsOnAndroidO() throws Exception {
    /* Click settings. */
    Intent intentSecuritySettings = mock(Intent.class);
    whenNew(Intent.class).withArguments(Settings.ACTION_SECURITY_SETTINGS).thenReturn(intentSecuritySettings);
    Intent intentManageUnknownAppSources = mock(Intent.class);
    whenNew(Intent.class).withArguments(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).thenReturn(intentManageUnknownAppSources);
    ArgumentCaptor<DialogInterface.OnClickListener> clickListener = ArgumentCaptor.forClass(DialogInterface.OnClickListener.class);
    verify(mDialogBuilder).setPositiveButton(eq(R.string.appcenter_distribute_unknown_sources_dialog_settings), clickListener.capture());
    /* Verify behaviour on old version. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", BuildConfig.MIN_SDK_VERSION);
    clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
    verify(mFirstActivity).startActivity(intentSecuritySettings);
    verify(mFirstActivity, never()).startActivity(intentManageUnknownAppSources);
    reset(mFirstActivity);
    /* Verify behaviour on Android O. */
    TestUtils.setInternalState(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.O);
    clickListener.getValue().onClick(mUnknownSourcesDialog, DialogInterface.BUTTON_POSITIVE);
    verify(mFirstActivity, never()).startActivity(intentSecuritySettings);
    verify(mFirstActivity).startActivity(intentManageUnknownAppSources);
}
Also used : DialogInterface(android.content.DialogInterface) Build(android.os.Build) Intent(android.content.Intent) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) SuppressLint(android.annotation.SuppressLint)

Example 47 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project AppCenter-SDK-Android by Microsoft.

the class DefaultHttpClientTest method rejectedAsyncTask.

@Test
@PrepareForTest(HandlerUtils.class)
public void rejectedAsyncTask() throws Exception {
    /* Mock HandlerUtils to simulate call from background (this unit test) to main (mock) thread. */
    final Semaphore semaphore = new Semaphore(0);
    mockStatic(HandlerUtils.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(final InvocationOnMock invocation) throws Throwable {
            new Thread("rejectedAsyncTask.handler") {

                @Override
                public void run() {
                    ((Runnable) invocation.getArguments()[0]).run();
                    semaphore.release();
                }
            }.start();
            return null;
        }
    }).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
    /* Mock ingestion to fail on saturated executor in AsyncTask. */
    DefaultHttpClient.Call call = mock(DefaultHttpClient.Call.class);
    whenNew(DefaultHttpClient.Call.class).withAnyArguments().thenReturn(call);
    RejectedExecutionException exception = new RejectedExecutionException();
    when(call.executeOnExecutor(any(Executor.class))).thenThrow(exception);
    DefaultHttpClient httpClient = new DefaultHttpClient();
    /* Test. */
    ServiceCallback serviceCallback = mock(ServiceCallback.class);
    assertNotNull(httpClient.callAsync("", "", new HashMap<String, String>(), mock(HttpClient.CallTemplate.class), serviceCallback));
    /* Verify the callback call from "main" thread. */
    semaphore.acquireUninterruptibly();
    verify(serviceCallback).onCallFailed(exception);
    verify(serviceCallback, never()).onCallSucceeded(notNull(String.class));
}
Also used : HashMap(java.util.HashMap) Semaphore(java.util.concurrent.Semaphore) Matchers.anyString(org.mockito.Matchers.anyString) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) Executor(java.util.concurrent.Executor) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 48 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project incubator-heron by apache.

the class RuntimeManagerRunnerTest method testUpdateTopologyUserRuntimeConfig.

@PrepareForTest({ NetworkUtils.class, Runtime.class })
@Test
public void testUpdateTopologyUserRuntimeConfig() throws Exception {
    String testConfig = "topology.user:test,testSpout:topology.user:1,testBolt:topology.user:4";
    URL expectedURL = new URL("http://host:1/runtime_config/update?topologyid=topology-id&" + "runtime-config=topology.user:test&runtime-config=testSpout:topology.user:1&" + "runtime-config=testBolt:topology.user:4");
    // Success case
    ISchedulerClient client = mock(ISchedulerClient.class);
    SchedulerStateManagerAdaptor manager = mock(SchedulerStateManagerAdaptor.class);
    HttpURLConnection connection = mock(HttpURLConnection.class);
    RuntimeManagerRunner runner = newRuntimeManagerRunner(Command.UPDATE, client);
    TopologyMaster.TMasterLocation location = TopologyMaster.TMasterLocation.newBuilder().setTopologyName("topology-name").setTopologyId("topology-id").setHost("host").setControllerPort(1).setMasterPort(2).build();
    when(manager.getTMasterLocation(TOPOLOGY_NAME)).thenReturn(location);
    when(connection.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK);
    PowerMockito.mockStatic(Runtime.class);
    PowerMockito.when(Runtime.schedulerStateManagerAdaptor(runtime)).thenReturn(manager);
    PowerMockito.mockStatic(NetworkUtils.class);
    PowerMockito.when(NetworkUtils.getProxiedHttpConnectionIfNeeded(eq(expectedURL), any(NetworkUtils.TunnelConfig.class))).thenReturn(connection);
    runner.updateTopologyUserRuntimeConfig(TOPOLOGY_NAME, testConfig);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ISchedulerClient(com.twitter.heron.scheduler.client.ISchedulerClient) TopologyMaster(com.twitter.heron.proto.tmaster.TopologyMaster) URL(java.net.URL) SchedulerStateManagerAdaptor(com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 49 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project incubator-heron by apache.

the class HeronMasterDriverTest method onNextStartTimeStartsSchedulerTMaster.

@Test
@PrepareForTest({ HeronReefUtils.class, SchedulerMain.class })
public void onNextStartTimeStartsSchedulerTMaster() throws Exception {
    PowerMockito.spy(HeronReefUtils.class);
    PowerMockito.doNothing().when(HeronReefUtils.class, "extractPackageInSandbox", anyString(), anyString(), anyString());
    SchedulerMain mockScheduler = mock(SchedulerMain.class);
    PowerMockito.spy(SchedulerMain.class);
    PowerMockito.doReturn(mockScheduler).when(SchedulerMain.class, "createInstance", anyString(), anyString(), anyString(), anyString(), anyString(), eq(0), eq(false));
    spyDriver.new HeronSchedulerLauncher().onNext(new StartTime(System.currentTimeMillis()));
    verify(mockScheduler, times(1)).runScheduler();
}
Also used : StartTime(org.apache.reef.wake.time.event.StartTime) SchedulerMain(com.twitter.heron.scheduler.SchedulerMain) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 50 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project incubator-heron by apache.

the class SchedulerClientFactoryTest method testGetLibrarySchedulerClient.

@Test
@PrepareForTest(ReflectionUtils.class)
public void testGetLibrarySchedulerClient() throws Exception {
    // Instantiate mock objects
    Config config = Mockito.mock(Config.class);
    Config runtime = Mockito.mock(Config.class);
    // Return a MockScheduler
    Mockito.when(config.getStringValue(Key.SCHEDULER_CLASS)).thenReturn(IScheduler.class.getName());
    PowerMockito.mockStatic(ReflectionUtils.class);
    PowerMockito.doReturn(Mockito.mock(IScheduler.class)).when(ReflectionUtils.class, "newInstance", Mockito.eq(IScheduler.class.getName()));
    // Get a LibrarySchedulerClient
    Mockito.when(config.getBooleanValue(Key.SCHEDULER_IS_SERVICE)).thenReturn(false);
    Assert.assertNotNull(new SchedulerClientFactory(config, runtime).getSchedulerClient());
}
Also used : Config(com.twitter.heron.spi.common.Config) IScheduler(com.twitter.heron.spi.scheduler.IScheduler) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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