private void setTimeOnNanos(long nanoTime) throws Exception { PowerMockito.doReturn(nanoTime) .when(rateLimiter, "currentNanoTime"); }
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); }
/** * 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(); }
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); }
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 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()); }
private static ClusterClient<String> createClusterClient(@Nullable Exception exception) throws Exception { final ClusterClient<String> clusterClient = mock(ClusterClient.class); if (exception != null) { doThrow(exception).when(clusterClient).stop(any(JobID.class)); } return clusterClient; } }
@Test public void noLoggingIfSpringVersionNull() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("1").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn(null).when(SpringVersion.class, "getVersion"); performChecks(); verifyZeroInteractions(logger); }
@Test public void websocketNotFoundExceptionNoMessageBlock() throws Exception { String className = FILTER_CHAIN_PROXY_CLASSNAME; spy(ClassUtils.class); doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName", eq(Message.class.getName()), any(ClassLoader.class)); new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER); // should load just fine since no websocket block } }
@Test public void noWarnIfSpringVersionLarger() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("4.0.0.RELEASE").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("4.0.0.RELEASE").when(SpringVersion.class, "getVersion"); performChecks(); verify(logger, never()).warn(any()); }
@Test public void noWarnIfSpringPatchVersionDoubleDigits() throws Exception { String minSpringVersion = "3.2.8.RELEASE"; spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3.2.0.RELEASE").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("3.2.10.RELEASE").when(SpringVersion.class, "getVersion"); performChecks(minSpringVersion); verify(logger, never()).warn(any()); }
@Test public void filterNoClassDefFoundErrorNoHttpBlock() throws Exception { String className = "javax.servlet.Filter"; spy(ClassUtils.class); doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName", eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER); // should load just fine since no http block }
@Test public void filterChainProxyClassNotFoundExceptionNoHttpBlock() throws Exception { String className = FILTER_CHAIN_PROXY_CLASSNAME; spy(ClassUtils.class); doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName", eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER); // should load just fine since no http block }
@Test public void noLoggingIfVersionsAreEqual() throws Exception { String version = "1"; spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn(version).when(SpringSecurityCoreVersion.class, "getVersion"); doReturn(version).when(SpringVersion.class, "getVersion"); performChecks(); verifyZeroInteractions(logger); }
@Test public void warnIfSpringVersionTooSmall() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("2").when(SpringVersion.class, "getVersion"); performChecks(); verify(logger, times(1)).warn(any()); }
@Test public void noLoggingIfPropertySet() throws Exception { spy(SpringSecurityCoreVersion.class); spy(SpringVersion.class); doReturn("3").when(SpringSecurityCoreVersion.class, "getVersion"); doReturn("2").when(SpringVersion.class, "getVersion"); System.setProperty(getDisableChecksProperty(), Boolean.TRUE.toString()); performChecks(); verifyZeroInteractions(logger); }
@Test public void filterChainProxyClassNotFoundException() throws Exception { String className = FILTER_CHAIN_PROXY_CLASSNAME; thrown.expect(BeanDefinitionParsingException.class); thrown.expectMessage("ClassNotFoundException: " + className); spy(ClassUtils.class); doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName", eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK); }
@Test public void filterNoClassDefFoundError() throws Exception { String className = "javax.servlet.Filter"; thrown.expect(BeanDefinitionParsingException.class); thrown.expectMessage("NoClassDefFoundError: " + className); spy(ClassUtils.class); doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName", eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK); }
@Test public void initDoesNotLogErrorWhenFilterChainProxyFailsToLoad() throws Exception { String className = "javax.servlet.Filter"; spy(ClassUtils.class); doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName", eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); Log logger = mock(Log.class); SecurityNamespaceHandler handler = new SecurityNamespaceHandler(); ReflectionTestUtils.setField(handler, "logger", logger); handler.init(); verifyStatic(ClassUtils.class); ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class)); verifyZeroInteractions(logger); }
@Test public void testIoExceptionWhenLoadingHeader() throws Exception { final HeaderCheck check = PowerMockito.spy(new HeaderCheck()); PowerMockito.doThrow(new IOException("expected exception")).when(check, "loadHeader", any()); try { check.setHeader("header"); fail("Exception expected"); } catch (IllegalArgumentException ex) { assertTrue("Invalid exception cause", ex.getCause() instanceof IOException); assertEquals("Invalid exception message", "unable to load header", ex.getMessage()); } }