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);
}
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));
}
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);
}
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();
}
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());
}
Aggregations