Refine search
@Test public void releaseEntryShouldSetValueToRemovePhase2AndSetsAsyncToFalseForDiskEntry() { // mock region entry OffHeapRegionEntry re = mock(VersionedStatsDiskRegionEntryOffHeap.class); when(re.getAddress()).thenReturn(1L); when(re.setAddress(1L, OffHeapRegionEntryHelper.REMOVED_PHASE2_ADDRESS)) .thenReturn(Boolean.TRUE); DiskId spy = Mockito.spy(DiskId.class); when(((DiskEntry) re).getDiskId()).thenReturn(spy); when(spy.isPendingAsync()).thenReturn(Boolean.TRUE); // mock required methods PowerMockito.spy(OffHeapRegionEntryHelper.class); PowerMockito.doNothing().when(OffHeapRegionEntryHelper.class); OffHeapRegionEntryHelper.setValue(re, Token.REMOVED_PHASE2); OffHeapRegionEntryHelper.releaseEntry(re); verify(spy, times(1)).setPendingAsync(Boolean.FALSE); PowerMockito.verifyStatic(OffHeapRegionEntryHelper.class); OffHeapRegionEntryHelper.setValue(re, Token.REMOVED_PHASE2); }
@Test public void createHDFSRDDTest() throws Exception { deepSparkContext = createDeepSparkContext(); DeepSparkContext deepSparkContextSpy = PowerMockito.spy(deepSparkContext); SQLContext sqlContext = mock(SQLContext.class); Whitebox.setInternalState(deepSparkContextSpy, "sc", sparkContext); Whitebox.setInternalState(deepSparkContextSpy, "sqlContext", sqlContext); RDD<String> rdd = mock(RDD.class); JavaRDD<String> javaRdd = mock(JavaRDD.class); when(deepSparkContextSpy.sc().textFile(anyString(), anyInt())).thenReturn(rdd); doReturn(javaRdd).when(deepSparkContextSpy).textFile(anyString()); when(rdd.toJavaRDD()).thenReturn(javaRdd); when(rdd.toJavaRDD().map(any(Function.class))).thenReturn(singleRdd); ExtractorConfig<Cells> config = createHDFSDeepJobConfig(); RDD rddReturn = deepSparkContextSpy.createHDFSRDD(config); verify(deepSparkContextSpy.sc(), times(1)).textFile(anyString(), anyInt()); verify(javaRdd, times(1)).map(any(Function.class)); }
@Test public void registerWithoutFirebaseStartServiceThrowsRuntimeException() { when(FirebaseInstanceId.getInstance()).thenThrow(new IllegalStateException()); setSenderId(); doThrow(new RuntimeException()).when(mContext).startService(any(Intent.class)); start(Push.getInstance(), mock(Channel.class)); assertTrue(Push.isEnabled().get()); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(Exception.class)); }
@Test public void testFinishWrite_success() throws Exception { doCallRealMethod().when(reader).finishWrite(any(FileOutputStream.class)); when(workFile.renameTo(activeFile)).thenReturn(true); try { reader.finishWrite(fos); Mockito.verify(fos).close(); } catch (IOException e) { fail("Successful rename commits transaction"); } }
@Before public void before() throws Exception { mClientContext = ClientContext.create(mConf); mContext = PowerMockito.mock(FileSystemContext.class); mAddress = Mockito.mock(WorkerNetAddress.class); mClient = mock(BlockWorkerClient.class); mRequestObserver = mock(ClientCallStreamObserver.class); PowerMockito.when(mContext.getClientContext()).thenReturn(mClientContext); PowerMockito.when(mContext.getConf()).thenReturn(mConf); PowerMockito.when(mContext.acquireBlockWorkerClient(mAddress)).thenReturn(mClient); PowerMockito.doNothing().when(mContext).releaseBlockWorkerClient(mAddress, mClient); PowerMockito.when(mClient.writeBlock(any(StreamObserver.class))).thenReturn(mRequestObserver); PowerMockito.when(mRequestObserver.isReady()).thenReturn(true); }
return null; }).when(mAppCenterHandler).post(any(Runnable.class), any(Runnable.class)); whenNew(DistributeInfoTracker.class).withAnyArguments().thenReturn(mDistributeInfoTracker); when(SharedPreferencesManager.getBoolean(DISTRIBUTE_ENABLED_KEY, true)).thenReturn(true); return null; }).when(SharedPreferencesManager.class); SharedPreferencesManager.putBoolean(eq(DISTRIBUTE_ENABLED_KEY), anyBoolean()); doReturn(mHttpClient).when(HttpUtils.class, "createHttpClient", any(Context.class)); return null; }).when(mDialog).show(); return null; }).when(mDialog).hide(); return null; }).when(HandlerUtils.class);
@Test public void canEndTheSshConnectionTest() throws Exception { PowerMockito.spy(SshHelper.class); Session mockedSession = Mockito.mock(Session.class); PowerMockito.doReturn(true).when(SshHelper.class, "isChannelConditionEof", Mockito.anyInt()); Mockito.when(mockedSession.waitForCondition(ChannelCondition.EXIT_STATUS, 1l)).thenReturn(0); PowerMockito.doNothing().when(SshHelper.class, "throwSshExceptionIfConditionsTimeout", Mockito.anyInt()); SshHelper.canEndTheSshConnection(1, mockedSession, 0); PowerMockito.verifyStatic(); SshHelper.isChannelConditionEof(Mockito.anyInt()); SshHelper.throwSshExceptionIfConditionsTimeout(Mockito.anyInt()); Mockito.verify(mockedSession).waitForCondition(ChannelCondition.EXIT_STATUS, 1l); }
@Test public void createJavaSchemaRDDTest() throws Exception { deepSparkContext = createDeepSparkContext(); DeepSparkContext deepSparkContextSpy = PowerMockito.spy(deepSparkContext); SQLContext sqlContext = PowerMockito.mock(SQLContext.class); ExtractorConfig config = createDeepJobConfig(); Whitebox.setInternalState(deepSparkContextSpy, "sc", sparkContext); Whitebox.setInternalState(deepSparkContextSpy, "sqlContext", sqlContext); PowerMockito.doReturn(singleRdd).when(deepSparkContextSpy).createJavaRDD(config); JavaRDD<Row> rowRDD = mock(JavaRDD.class); mockStatic(DeepSparkContext.class); when(DeepSparkContext.createJavaRowRDD(singleRdd)).thenReturn(rowRDD); Cells cells = mock(Cells.class); when(singleRdd.first()).thenReturn(cells); StructType schema = mock(StructType.class); mockStatic(CellsUtils.class); when(CellsUtils.getStructTypeFromCells(cells)).thenReturn(schema); deepSparkContextSpy.createJavaSchemaRDD(config); verify(sqlContext).applySchema(rowRDD, schema); }
@Test public void releaseEntryShouldSetValueToRemovePhase2() { // mock region entry OffHeapRegionEntry re = mock(OffHeapRegionEntry.class); when(re.getAddress()).thenReturn(1L); when(re.setAddress(1L, OffHeapRegionEntryHelper.REMOVED_PHASE2_ADDRESS)) .thenReturn(Boolean.TRUE); // mock required methods PowerMockito.spy(OffHeapRegionEntryHelper.class); PowerMockito.doNothing().when(OffHeapRegionEntryHelper.class); OffHeapRegionEntryHelper.setValue(re, Token.REMOVED_PHASE2); OffHeapRegionEntryHelper.releaseEntry(re); PowerMockito.verifyStatic(OffHeapRegionEntryHelper.class); OffHeapRegionEntryHelper.setValue(re, Token.REMOVED_PHASE2); }
@Test public void textFileHDFSTest() throws Exception { deepSparkContext = createDeepSparkContext(); DeepSparkContext deepSparkContextSpy = PowerMockito.spy(deepSparkContext); SQLContext sqlContext = mock(SQLContext.class); Whitebox.setInternalState(deepSparkContextSpy, "sc", sparkContext); Whitebox.setInternalState(deepSparkContextSpy, "sqlContext", sqlContext); RDD<Cells> result = mock(RDD.class); ExtractorConfig<Cells> config = createHDFSDeepJobConfig(); PowerMockito.doReturn(result).when(deepSparkContextSpy).createHDFSRDD(config); deepSparkContextSpy.textFile(config); verify(deepSparkContextSpy, times(1)).createHDFSRDD(config); }
InternalNode holder = mock(InternalNode.class); InternalNode resolved = mock(InternalNode.class); .build(mContext); when(holder.getRootComponent()).thenReturn(component); when(LayoutState.resolveNestedTree(mContext, holder, 100, 100)).thenCallRealMethod(); when(LayoutState.createAndMeasureTreeForComponent(mContext, component, holder, 100, 100, null)) .thenReturn(resolved); PowerMockito.verifyStatic(); doReturn(false).when(component).canUsePreviousLayout(any(ComponentContext.class));
RunnableFuture<SnapshotResult<KeyedStateHandle>> futureKeyedStateHandle = mock(RunnableFuture.class); RunnableFuture<SnapshotResult<OperatorStateHandle>> futureOperatorStateHandle = mock(RunnableFuture.class); StateSnapshotContextSynchronousImpl context = spy(new StateSnapshotContextSynchronousImpl(checkpointId, timestamp)); when(context.getKeyedStateStreamFuture()).thenReturn(futureKeyedStateHandle); when(context.getOperatorStateStreamFuture()).thenReturn(futureOperatorStateHandle); OperatorSnapshotFutures operatorSnapshotResult = spy(new OperatorSnapshotFutures()); when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class), any(CheckpointStreamFactory.class))).thenCallRealMethod(); doCallRealMethod().when(operator).close(); doCallRealMethod().when(operator).dispose(); doReturn(containingTask).when(operator).getContainingTask(); RunnableFuture<SnapshotResult<OperatorStateHandle>> futureManagedOperatorStateHandle = mock(RunnableFuture.class); verify(context).close(); verify(operatorSnapshotResult).cancel();
StateSnapshotContextSynchronousImpl context = mock(StateSnapshotContextSynchronousImpl.class); whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context); StreamTask<Void, AbstractStreamOperator<Void>> containingTask = mock(StreamTask.class); when(containingTask.getCancelables()).thenReturn(closeableRegistry); AbstractStreamOperator<Void> operator = mock(AbstractStreamOperator.class); when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class), any(CheckpointStreamFactory.class))).thenCallRealMethod(); doReturn(containingTask).when(operator).getContainingTask(); doThrow(failingException).when(operator).snapshotState(eq(context)); verify(context).close();
/** * Tests {@link ToGerritRunListener#obtainUnsuccessfulMessage}. Results in a message being retrieved. * * @throws Exception if so. */ @Test public void testObtainUnsuccessfulMessageWithMatchingFiles() throws Exception { AbstractBuild build = mockBuild("projectX", 2); String filepath = "error-file*.txt"; String message = "This is the failure"; FilePath[] fileList = {new FilePath(File.createTempFile("error-file", ".txt"))}; PatchsetCreated event = spy(Setup.createPatchsetCreated()); ToGerritRunListener toGerritRunListener = Setup.createFailureMessageRunListener(build, event, filepath); doReturn(fileList).when(toGerritRunListener).getMatchingWorkspaceFiles(any(FilePath.class), eq(filepath)); doReturn(message).when(toGerritRunListener).getExpandedContent(eq(fileList[0]), any(EnvVars.class)); BuildMemory memory = Whitebox.getInternalState(toGerritRunListener, BuildMemory.class); memory.started(event, build); toGerritRunListener.onCompleted(build, mock(TaskListener.class)); verify(toGerritRunListener, times(1)).getMatchingWorkspaceFiles(any(FilePath.class), eq(filepath)); verify(toGerritRunListener, times(1)).getExpandedContent(any(FilePath.class), any(EnvVars.class)); }
@Test(expected = UnsupportedOperationException.class) public void createJavaSchemaFromEmptyRDDTest() throws Exception { deepSparkContext = createDeepSparkContext(); DeepSparkContext deepSparkContextSpy = PowerMockito.spy(deepSparkContext); SQLContext sqlContext = mock(SQLContext.class); ExtractorConfig config = createDeepJobConfig(); Whitebox.setInternalState(deepSparkContextSpy, "sc", sparkContext); Whitebox.setInternalState(deepSparkContextSpy, "sqlContext", sqlContext); PowerMockito.doReturn(singleRdd).when(deepSparkContextSpy).createJavaRDD(config); JavaRDD<Row> rowRDD = mock(JavaRDD.class); mockStatic(DeepSparkContext.class); when(DeepSparkContext.createJavaRowRDD(singleRdd)).thenReturn(rowRDD); when(singleRdd.first()).thenThrow(new UnsupportedOperationException()); deepSparkContextSpy.createJavaSchemaRDD(config); }
@Before public void setUp() { mockStatic(System.class); mockStatic(SystemClock.class); mockStatic(SharedPreferencesManager.class); PowerMockito.doAnswer(new Answer<Void>() { @Override @SuppressWarnings("unchecked") public Void answer(InvocationOnMock invocation) { /* Whenever the new state is persisted, make further calls return the new state. */ String key = (String) invocation.getArguments()[0]; Set<String> value = (Set<String>) invocation.getArguments()[1]; when(SharedPreferencesManager.getStringSet(key)).thenReturn(value); return null; } }).when(SharedPreferencesManager.class); SharedPreferencesManager.putStringSet(anyString(), anySetOf(String.class)); when(SharedPreferencesManager.getStringSet(anyString())).thenReturn(null); SessionContext.unsetInstance(); spendTime(1000); mChannel = mock(Channel.class); mSessionTracker = new SessionTracker(mChannel, TEST_GROUP); }
@Before public void before() throws Exception { BlockWorkerClient workerClient = PowerMockito.mock(BlockWorkerClient.class); ClientCallStreamObserver requestObserver = PowerMockito.mock(ClientCallStreamObserver.class); when(requestObserver.isReady()).thenReturn(true); when(workerClient.openLocalBlock(any(StreamObserver.class))) .thenAnswer(new Answer() { public Object answer(InvocationOnMock invocation) { StreamObserver<OpenLocalBlockResponse> observer = invocation.getArgumentAt(0, StreamObserver.class); observer.onNext(OpenLocalBlockResponse.newBuilder().setPath("/tmp").build()); observer.onCompleted(); return requestObserver; } }); mMockContext = PowerMockito.mock(FileSystemContext.class); PowerMockito.when(mMockContext.acquireBlockWorkerClient(Matchers.any(WorkerNetAddress.class))) .thenReturn(workerClient); PowerMockito.when(mMockContext.getClientContext()).thenReturn(ClientContext.create(mConf)); PowerMockito.when(mMockContext.getConf()).thenReturn(mConf); PowerMockito.doNothing().when(mMockContext) .releaseBlockWorkerClient(Matchers.any(WorkerNetAddress.class), Matchers.any(BlockWorkerClient.class)); mInfo = new BlockInfo().setBlockId(1); mOptions = new InStreamOptions(new URIStatus(new FileInfo().setBlockIds(Collections .singletonList(1L))), mConf); }
/** * Checks that the state snapshot context is closed after a successful snapshot operation. */ @Test public void testSnapshotMethod() throws Exception { final long checkpointId = 42L; final long timestamp = 1L; final CloseableRegistry closeableRegistry = new CloseableRegistry(); StateSnapshotContextSynchronousImpl context = spy(new StateSnapshotContextSynchronousImpl(0L, 0L)); whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context); StreamTask<Void, AbstractStreamOperator<Void>> containingTask = mock(StreamTask.class); when(containingTask.getCancelables()).thenReturn(closeableRegistry); AbstractStreamOperator<Void> operator = mock(AbstractStreamOperator.class); when(operator.snapshotState(anyLong(), anyLong(), any(CheckpointOptions.class), any(CheckpointStreamFactory.class))).thenCallRealMethod(); doReturn(containingTask).when(operator).getContainingTask(); operator.snapshotState( checkpointId, timestamp, CheckpointOptions.forCheckpointWithDefaultLocation(), new MemCheckpointStreamFactory(Integer.MAX_VALUE)); verify(context).close(); }
doThrow(new CertificateExpiredException()).doNothing().when(mRsaCert).checkValidity(); decryptedData = cryptoUtils.decrypt(encrypted, false); assertEquals("anything", decryptedData.getDecryptedData()); verify(mRsaBuilder, times(2)).setAlias(alias.capture()); String alias0 = alias.getAllValues().get(0); String alias1 = alias.getAllValues().get(1); assertNotEquals(alias0, alias1); verify(mKeyStore).getEntry(alias1, null); verify(mKeyStore, atLeastOnce()).getEntry(alias.capture(), any(KeyStore.ProtectionParameter.class)); int alias0count = 0; for (String aliasValue : alias.getAllValues()) { when(mKeyStore.containsAlias(alias0)).thenReturn(true); when(mKeyStore.containsAlias(alias1)).thenReturn(true); Calendar calendar = Calendar.getInstance(); when(mKeyStore.getCreationDate(alias0)).thenReturn(calendar.getTime()); calendar.add(Calendar.YEAR, 1); when(mKeyStore.getCreationDate(alias1)).thenReturn(calendar.getTime()); doThrow(new CertificateExpiredException()).doNothing().when(mRsaCert).checkValidity(); encrypted = cryptoUtils.encrypt("anything"); assertEquals(CIPHER_RSA + "/" + RSA_KEY_SIZE + ALGORITHM_DATA_SEPARATOR + "anything", encrypted);