@Before public void before() { mMockJobMasterContext = Mockito.mock(JobMasterContext.class); mMockFileSystem = Mockito.mock(FileSystem.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class); PowerMockito.mockStatic(AlluxioBlockStore.class); PowerMockito.when(AlluxioBlockStore.create(mMockFileSystemContext)).thenReturn(mMockBlockStore); }
@Test @PrepareForTest( { Encode.class } ) public void testCleanupTransServletEscapesHtmlWhenTransNotFound() throws ServletException, IOException { HttpServletRequest mockHttpServletRequest = mock( HttpServletRequest.class ); HttpServletResponse mockHttpServletResponse = mock( HttpServletResponse.class ); StringWriter out = new StringWriter(); PrintWriter printWriter = new PrintWriter( out ); PowerMockito.spy( Encode.class ); when( mockHttpServletRequest.getContextPath() ).thenReturn( CleanupTransServlet.CONTEXT_PATH ); when( mockHttpServletRequest.getParameter( anyString() ) ).thenReturn( ServletTestUtils.BAD_STRING_TO_TEST ); when( mockHttpServletResponse.getWriter() ).thenReturn( printWriter ); cleanupTransServlet.doGet( mockHttpServletRequest, mockHttpServletResponse ); assertFalse( ServletTestUtils.hasBadText( ServletTestUtils.getInsideOfTag( "H1", out.toString() ) ) ); PowerMockito.verifyStatic( atLeastOnce() ); Encode.forHtml( anyString() ); }
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); }
public final void currentSecurityContextPowermockSetup() throws Exception { spy(DelegatingSecurityContextCallable.class); doReturn(wrappedCallable).when(DelegatingSecurityContextCallable.class, "create", callable, null); spy(DelegatingSecurityContextRunnable.class); doReturn(wrappedRunnable).when(DelegatingSecurityContextRunnable.class, "create", runnable, null); }
public final void explicitSecurityContextPowermockSetup() throws Exception { spy(DelegatingSecurityContextCallable.class); doReturn(wrappedCallable).when(DelegatingSecurityContextCallable.class, "create", eq(callable), securityContextCaptor.capture()); spy(DelegatingSecurityContextRunnable.class); doReturn(wrappedRunnable).when(DelegatingSecurityContextRunnable.class, "create", eq(runnable), securityContextCaptor.capture()); }
@Before public void setup() throws KettlePluginException { mockSpace = mock( VariableSpace.class ); doReturn("N" ).when( mockSpace ).getVariable( any(), anyString() ); rowMeta = spy( new RowMeta() ); memoryGroupByMeta = spy( new MemoryGroupByMeta() ); mockStatic( ValueMetaFactory.class ); when( ValueMetaFactory.createValueMeta( anyInt() ) ).thenCallRealMethod(); when( ValueMetaFactory.createValueMeta( anyString(), anyInt() ) ).thenCallRealMethod(); when( ValueMetaFactory.createValueMeta( "maxDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "maxDate" ) ); when( ValueMetaFactory.createValueMeta( "minDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "minDate" ) ); when( ValueMetaFactory.createValueMeta( "countDate", 5, -1, -1 ) ).thenReturn( new ValueMetaInteger( "countDate" ) ); when( ValueMetaFactory.getValueMetaName( 3 ) ).thenReturn( "Date" ); when( ValueMetaFactory.getValueMetaName( 5 ) ).thenReturn( "Integer" ); }
@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); }
@Test public void testRemoveChannelFromBufferCallInGetLogBufferInFirstJobExecution() { StringBuffer sb = new StringBuffer( "" ); LoggingBuffer lb = mock( LoggingBuffer.class ); doReturn( sb ).when( lb ).getBuffer( anyString(), anyBoolean() ); mockStatic( KettleLogStore.class ); mockStatic( Utils.class ); mockStatic( Const.class ); when( KettleLogStore.getAppender() ).thenReturn( lb ); BaseLogTable baseLogTable = mock( BaseLogTable.class ); doCallRealMethod().when( baseLogTable ).getLogBuffer( any( VariableSpace.class ), anyString(), any( LogStatus.class ), anyString() ); VariableSpace vs = mock( VariableSpace.class ); String s1 = baseLogTable.getLogBuffer( vs, "1", LogStatus.START, null ); String s2 = baseLogTable.getLogBuffer( vs, "1", LogStatus.END, null ); assertEquals( Const.CR + "START" + Const.CR, s1 ); assertEquals( Const.CR + "START" + Const.CR, s1 + Const.CR + "END" + Const.CR, s2 ); verify( lb, times( 1 ) ).removeChannelFromBuffer( "1" ); }
@Before public void setup() throws SQLException { connection = mock(Connection.class); dbmd = mock(DatabaseMetaData.class); jdbcMetadata = mock(DefaultJdbcMetadata.class); PowerMockito.stub(PowerMockito.method(SqlUtil.class, "getConnection")).toReturn(connection); PowerMockito.mockStatic(JdbcMetadataFactory.class); when(JdbcMetadataFactory.getJdbcMetadata(anyString(), any(DBConnConf.class))).thenReturn(jdbcMetadata); when(connection.getMetaData()).thenReturn(dbmd); jdbcExplorer = spy(JdbcExplorer.class); }
@Before public void before() { mMockJobMasterContext = mock(JobMasterContext.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); when(mMockFileSystemContext.getClientContext()) .thenReturn(ClientContext.create(ServerConfiguration.global())); mMockBlockStore = PowerMockito.mock(AlluxioBlockStore.class); mMockFileSystem = mock(FileSystem.class); mMockUfsManager = mock(UfsManager.class); }
@Before public void before() throws Exception { AlluxioConfiguration conf = ConfigurationTestUtils.defaults(); mMockFileSystem = Mockito.mock(FileSystem.class); mMockFileSystemContext = PowerMockito.mock(FileSystemContext.class); when(mMockFileSystemContext.getClientContext()) .thenReturn(ClientContext.create(conf)); when(mMockFileSystemContext.getConf()) .thenReturn(conf); mMockInStream = new MockFileInStream(mMockFileSystemContext, TEST_SOURCE_CONTENTS, conf); when(mMockFileSystem.openFile(new AlluxioURI(TEST_SOURCE))).thenReturn(mMockInStream); mMockOutStream = new MockFileOutStream(mMockFileSystemContext); when(mMockFileSystem.createFile(eq(new AlluxioURI(TEST_DESTINATION)), any(CreateFilePOptions.class))).thenReturn(mMockOutStream); mMockUfsManager = Mockito.mock(UfsManager.class); }
/** * Tests * {@link TriggerMonitor#buildCompleted(com.sonymobile.tools.gerrit.gerritevents.dto.GerritEvent, hudson.model.Run)}. * @throws Exception if so. */ @Test public void testBuildCompleted() throws Exception { TriggerMonitor monitor = new TriggerMonitor(); ManualPatchsetCreated patch = Setup.createManualPatchsetCreated(); monitor.add(patch); monitor.triggerScanStarting(patch); AbstractProject project = PowerMockito.mock(AbstractProject.class); doReturn("projectX").when(project).getFullName(); AbstractBuild build = mock(AbstractBuild.class); when(build.getProject()).thenReturn(project); monitor.projectTriggered(patch, project); monitor.buildStarted(patch, build); monitor.buildCompleted(patch, build); TriggerMonitor.EventState state = monitor.getEvents().get(0); assertEquals(1, state.getBuilds().size()); assertSame(build, state.getBuilds().get(0).getBuild()); }
@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 ) ); }
@Before public void setup() throws Exception { PowerMockito.mockStatic( SSHData.class ); PowerMockito.mockStatic( KettleVFS.class ); when( SSHData.createConnection( server, port ) ).thenReturn( connection ); when( SSHData.OpenConnection( any(), anyInt(), any(), any(), anyBoolean(), any(), any(), anyInt(), anyObject(), any(), anyInt(), any(), any() ) ).thenCallRealMethod(); when( KettleVFS.getFileObject( keyFilePath ) ).thenReturn( fileObject ); }
@Before public void setup() throws KettlePluginException { rowMeta = spy( new RowMeta() ); groupByMeta = spy( new GroupByMeta() ); mockStatic( ValueMetaFactory.class ); when( ValueMetaFactory.createValueMeta( anyInt() ) ).thenCallRealMethod(); when( ValueMetaFactory.createValueMeta( anyString(), anyInt() ) ).thenCallRealMethod(); when( ValueMetaFactory.createValueMeta( "maxDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "maxDate" ) ); when( ValueMetaFactory.createValueMeta( "minDate", 3, -1, -1 ) ).thenReturn( new ValueMetaDate( "minDate" ) ); when( ValueMetaFactory.createValueMeta( "countDate", 5, -1, -1 ) ).thenReturn( new ValueMetaInteger( "countDate" ) ); when( ValueMetaFactory.getValueMetaName( 3 ) ).thenReturn( "Date" ); when( ValueMetaFactory.getValueMetaName( 5 ) ).thenReturn( "Integer" ); }
@Before public void setup() throws Exception { Cluster cluster = Mockito.spy(Cluster.class); repository = Mockito.spy(Repository.class); spy(Repository.class); when(Repository.class, "get").thenReturn(repository); doReturn(cluster).when(repository).getCluster(); handler = new LogoutHandler("/defaultTargetUrl"); }
@Test public void callStopCarteRestService() throws Exception { WebResource status = mock( WebResource.class ); doReturn( "<serverstatus>" ).when( status ).get( String.class ); WebResource stop = mock( WebResource.class ); doReturn( "Shutting Down" ).when( stop ).get( String.class ); Client client = mock( Client.class ); doCallRealMethod().when( client ).addFilter( any( HTTPBasicAuthFilter.class ) ); doCallRealMethod().when( client ).getHeadHandler(); doReturn( status ).when( client ).resource( "http://localhost:8080/kettle/status/?xml=Y" ); doReturn( stop ).when( client ).resource( "http://localhost:8080/kettle/stopCarte" ); mockStatic( Client.class ); when( Client.create( any( ClientConfig.class ) ) ).thenReturn( client ); Carte.callStopCarteRestService( "localhost", "8080", "admin", "Encrypted 2be98afc86aa7f2e4bb18bd63c99dbdde" ); // the expected value is: "Basic <base64 encoded username:password>" assertEquals( "Basic " + new String( Base64.getEncoder().encode( "admin:password".getBytes( "utf-8" ) ) ), getInternalState( client.getHeadHandler(), "authentication" ) ); } }
@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(expected = IOException.class) public void shouldThrowExceptionOnDistcpFailure() throws Exception { Path destination = mock(Path.class); Path source = mock(Path.class); FileSystem fs = mock(FileSystem.class); List<Path> srcPaths = Arrays.asList(source, source); HiveConf conf = mock(HiveConf.class); CopyUtils copyUtils = Mockito.spy(new CopyUtils(null, conf)); mockStatic(FileUtils.class); mockStatic(Utils.class); when(destination.getFileSystem(same(conf))).thenReturn(fs); when(source.getFileSystem(same(conf))).thenReturn(fs); when(FileUtils.distCp(same(fs), anyListOf(Path.class), same(destination), anyBoolean(), eq(null), same(conf), same(ShimLoader.getHadoopShims()))) .thenReturn(false); when(Utils.getUGI()).thenReturn(mock(UserGroupInformation.class)); doReturn(false).when(copyUtils).regularCopy(same(fs), same(fs), anyListOf(ReplChangeManager.FileInfo.class)); copyUtils.doCopy(destination, srcPaths); } }