@Test public void getNativeDataTypeSimpleName_Unknown() throws Exception { KettleValueException e = new KettleValueException(); ValueMetaInterface v = mock( ValueMetaInterface.class ); doThrow( e ).when( v ).getNativeDataTypeClass(); LogChannel log = mock( LogChannel.class ); whenNew( LogChannel.class ).withAnyArguments().thenReturn( log ); assertEquals( "Object", FieldHelper.getNativeDataTypeSimpleName( v ) ); verify( log, times( 1 ) ).logDebug( "Unable to get name from data type" ); }
@Test public void testRemoteExecutionWithSavepoint() throws Exception { SavepointRestoreSettings restoreSettings = SavepointRestoreSettings.forPath("fakePath"); RemoteStreamEnvironment env = new RemoteStreamEnvironment("fakeHost", 1, null, new String[]{}, null, restoreSettings); env.fromElements(1).map(x -> x * 2); RestClusterClient mockedClient = Mockito.mock(RestClusterClient.class); JobExecutionResult expectedResult = new JobExecutionResult(null, 0, null); PowerMockito.whenNew(RestClusterClient.class).withAnyArguments().thenReturn(mockedClient); when(mockedClient.run(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.eq(restoreSettings))) .thenReturn(expectedResult); JobExecutionResult actualResult = env.execute("fakeJobName"); Assert.assertEquals(expectedResult, actualResult); } }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
@Before public void setUp() throws Exception { doReturn( true ).when( repository ).isConnected(); doReturn( null ).when( repository ).getJobEntryAttributeString( any( ObjectId.class ), anyString() ); doReturn( rdi ).when( repository ).loadRepositoryDirectoryTree(); doReturn( directory ).when( rdi ).findDirectory( JOB_ENTRY_FILE_DIRECTORY ); doReturn( directory ).when( rdi ).findDirectory( "/home/admin/folder" ); doReturn( null ).when( space ).environmentSubstitute( anyString() ); doReturn( "" ).when( space ).environmentSubstitute( "" ); doReturn( JOB_ENTRY_FILE_PATH ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_PATH ); doReturn( JOB_ENTRY_FILE_NAME ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_NAME ); doReturn( JOB_ENTRY_FILE_DIRECTORY ).when( space ).environmentSubstitute( JOB_ENTRY_FILE_DIRECTORY ); doReturn( "hdfs://server/path/" ).when( space ).environmentSubstitute( "${hdfs}" ); doReturn( "/home/admin/folder/job.kjb" ).when( space ).environmentSubstitute( "${repositoryfullfilepath}" ); doReturn( "/home/admin/folder/" ).when( space ).environmentSubstitute( "${repositorypath}" ); doReturn( "job.kjb" ).when( space ).environmentSubstitute( "${jobname}" ); doReturn( "job" ).when( space ).environmentSubstitute( "job" ); doCallRealMethod().when( resolver ).normalizeSlashes( anyString() ); doReturn( space ).when( resolver ).resolveCurrentDirectory( any( ObjectLocationSpecificationMethod.class ), any( VariableSpace.class ), any( Repository.class ), any( Job.class ), anyString() ); whenNew( CurrentDirectoryResolver.class ).withNoArguments().thenReturn( resolver ); whenNew( JobMeta.class ).withAnyArguments().thenReturn( mock( JobMeta.class ) ); }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
private void setUpPresenter() throws Exception { mockPresenter = mock(TestPresenter.class); PowerMockito.whenNew(Bundle.class).withNoArguments().thenAnswer(new Answer<Bundle>() { @Override public Bundle answer(InvocationOnMock invocation) throws Throwable { return BundleMock.mock(); } }); mockDelegate = mock(PresenterLifecycleDelegate.class); PowerMockito.whenNew(PresenterLifecycleDelegate.class).withAnyArguments().thenReturn(mockDelegate); when(mockDelegate.getPresenter()).thenReturn(mockPresenter); mockFactory = mock(ReflectionPresenterFactory.class); when(mockFactory.createPresenter()).thenReturn(mockPresenter); PowerMockito.mockStatic(ReflectionPresenterFactory.class); when(ReflectionPresenterFactory.fromViewClass(any(Class.class))).thenReturn(mockFactory); }
/** * Verifies that the port passed to the RemoteStreamEnvironment is used for connecting to the cluster. */ @Test public void testPortForwarding() throws Exception { String host = "fakeHost"; int port = 99; JobExecutionResult expectedResult = new JobExecutionResult(null, 0, null); RestClusterClient mockedClient = Mockito.mock(RestClusterClient.class); when(mockedClient.run(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())) .thenReturn(expectedResult); PowerMockito.whenNew(RestClusterClient.class).withAnyArguments().thenAnswer((invocation) -> { Object[] args = invocation.getArguments(); Configuration config = (Configuration) args[0]; Assert.assertEquals(host, config.getString(RestOptions.ADDRESS)); Assert.assertEquals(port, config.getInteger(RestOptions.PORT)); return mockedClient; } ); final Configuration clientConfiguration = new Configuration(); final StreamExecutionEnvironment env = StreamExecutionEnvironment.createRemoteEnvironment( host, port, clientConfiguration); env.fromElements(1).map(x -> x * 2); JobExecutionResult actualResult = env.execute("fakeJobName"); Assert.assertEquals(expectedResult, actualResult); }
PowerMockito.whenNew(AWSSdkClient.class).withAnyArguments().thenReturn(awsSdkClient);
@Test public void getGetSignature_Serializable() throws Exception { LogChannel log = mock( LogChannel.class ); whenNew( LogChannel.class ).withAnyArguments().thenReturn( log ); ValueMetaSerializable v = new ValueMetaSerializable( "Data" ); String accessor = FieldHelper.getAccessor( true, "Data" ); assertEquals( "Object Data = get(Fields.In, \"Data\").getObject(r);", FieldHelper.getGetSignature( accessor, v ) ); assertNotNull( getMethod( FieldHelper.class, "getObject", Object[].class ) ); }
when(conf.getBoolVar(HiveConf.ConfVars.REPL_INCLUDE_EXTERNAL_TABLES)).thenReturn(false); whenNew(Writer.class).withAnyArguments().thenReturn(mock(Writer.class)); whenNew(HiveWrapper.class).withAnyArguments().thenReturn(mock(HiveWrapper.class));
whenNew(StateSnapshotContextSynchronousImpl.class).withAnyArguments().thenReturn(context);
withAnyArguments(). thenAnswer((Answer<OperatorSnapshotFinalizer>) invocation -> { createSubtask.trigger();
/** * 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(); }
any(CloseableRegistry.class)) .thenReturn(context); whenNew(OperatorSnapshotFutures.class).withAnyArguments().thenReturn(operatorSnapshotResult);
whenNew(LocalDataOutputStream.class).withAnyArguments().thenAnswer(new Answer<LocalDataOutputStream>() {
@Test @PrepareForTest({FileOutputStream.class, SingleWriterMultipleReaderFile.class}) public void testStartWrite_success() throws Exception { when(reader.startWrite()).thenCallRealMethod(); when(workFile.exists()).thenReturn(false); when(workFile.getPath()).thenReturn("/nonexisting/path/for/dns66"); whenNew(FileOutputStream.class).withAnyArguments().thenReturn(fos); assertSame(fos, reader.startWrite()); }
@Test(expected = IOException.class) public void readObjectError() throws Exception { ObjectInputStream reader = mock(ObjectInputStream.class); whenNew(ObjectInputStream.class).withAnyArguments().thenReturn(reader); whenNew(FileInputStream.class).withAnyArguments().thenReturn(mock(FileInputStream.class)); doThrow(new IOException("mock")).when(reader).readObject(); FileManager.readObject(mock(File.class)); verify(reader).close(); }
@Before public void setup() throws Exception { when(mockStreamMetadataCache.getStreamMetadata(argThat(new ArgumentMatcher<scala.collection.immutable.Set<SystemStream>>() { @Override public boolean matches(Object argument) { scala.collection.immutable.Set<SystemStream> set = (scala.collection.immutable.Set<SystemStream>) argument; return set.equals(inputStreamSet); } }), anyBoolean())).thenReturn(mockStreamMetadataMap); when(mockStreamMetadata.getSystemStreamPartitionMetadata()).thenReturn(mockSspMetadataMap); PowerMockito.whenNew(TaskAssignmentManager.class).withAnyArguments().thenReturn(mockTaskManager); when(mockTaskManager.readTaskAssignment()).thenReturn(Collections.EMPTY_MAP); }
@Test public void readErrorAndCloseError() throws Exception { mockStatic(AppCenterLog.class); FileReader fileReader = mock(FileReader.class, new ThrowsException(new IOException())); whenNew(FileReader.class).withAnyArguments().thenReturn(fileReader); assertNull(FileManager.read(new File(""))); verify(fileReader).close(); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(IOException.class)); }
@Test public void deleteWrapperExceptionDataWithLoadingError() throws java.lang.Exception { /* Delete with file that cannot be loaded because invalid content should just log an error. */ File file = mock(File.class); whenNew(File.class).withAnyArguments().thenReturn(file); when(file.exists()).thenReturn(true); WrapperSdkExceptionManager.deleteWrapperExceptionData(UUID.randomUUID()); verifyStatic(); FileManager.delete(any(File.class)); verifyStatic(); AppCenterLog.error(eq(Crashes.LOG_TAG), anyString()); }