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; } }
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()); }
@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 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()); }
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); }
@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 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 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 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 testLogInThrowsIfCannotStartFacebookActivity() { doThrow(new ActivityNotFoundException()).when(mockActivity) .startActivityForResult(any(Intent.class), anyInt()); LoginManager loginManager = new LoginManager(); try { loginManager.logInWithReadPermissions(mockActivity, Arrays.asList("public_profile", "user_friends")); fail(); } catch(FacebookException exception) { } }
@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 testSkipFew() throws IOException { long numBytesToSkip = TEST_RESPONSE_BODY.length / 2; long result = mResponseHandlingInputStream.skip(numBytesToSkip); assertEquals(numBytesToSkip, result); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), (int) numBytesToSkip); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@Test public void testReadOneByte() throws IOException { int result = mResponseHandlingInputStream.read(); assertEquals(TEST_RESPONSE_BODY[0], positionToByte(result)); assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), 1); PowerMockito.mockStatic(CLog.class); PowerMockito.doNothing().when(CLog.class); CLog.writeToConsole( Mockito.any(ChromePeerManager.class), Mockito.any(Console.MessageLevel.class), Mockito.any(Console.MessageSource.class), Mockito.anyString()); mResponseHandlingInputStream.close(); PowerMockito.verifyStatic(); }
@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); }
@Before public void before() throws Exception { mContext = PowerMockito.mock(FileSystemContext.class); mAddress = mock(WorkerNetAddress.class); mClient = mock(BlockWorkerClient.class); mRequestObserver = mock(ClientCallStreamObserver.class); PowerMockito.when(mContext.acquireBlockWorkerClient(mAddress)).thenReturn(mClient); PowerMockito.when(mContext.getClientContext()) .thenReturn(ClientContext.create(mConf)); PowerMockito.when(mContext.getConf()).thenReturn(mConf); 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 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()); } }