1
0
mirror of synced 2026-05-22 21:33:16 +00:00

Convert to assertj

Fixes gh-3175
This commit is contained in:
Billy Korando
2015-12-19 09:29:21 -06:00
committed by Rob Winch
parent bb600a473e
commit 71d4ce96ad
153 changed files with 2472 additions and 2202 deletions
@@ -15,40 +15,38 @@
package org.springframework.security.remoting.httpinvoker;
import junit.framework.TestCase;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.remoting.httpinvoker.AuthenticationSimpleHttpInvokerRequestExecutor;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* Tests {@link AuthenticationSimpleHttpInvokerRequestExecutor}.
*
* @author Ben Alex
* @author Rob Winch
*/
public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends TestCase {
public class AuthenticationSimpleHttpInvokerRequestExecutorTests {
// ~ Methods
// ========================================================================================================
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() throws Exception {
SecurityContextHolder.clearContext();
}
@Test
public void testNormalOperation() throws Exception {
// Setup client-side context
Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken(
@@ -64,10 +62,11 @@ public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends TestCas
// Check connection properties
// See http://www.faqs.org/rfcs/rfc1945.html section 11.1 for example
// we are comparing against
assertEquals("Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
conn.getRequestProperty("Authorization"));
assertThat(conn.getRequestProperty("Authorization")).isEqualTo(
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==");
}
@Test
public void testNullContextHolderIsNull() throws Exception {
SecurityContextHolder.getContext().setAuthentication(null);
@@ -82,6 +81,7 @@ public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends TestCas
}
// SEC-1975
@Test
public void testNullContextHolderWhenAnonymous() throws Exception {
AnonymousAuthenticationToken anonymous = new AnonymousAuthenticationToken("key",
"principal", AuthorityUtils.createAuthorityList("ROLE_ANONYMOUS"));
@@ -101,6 +101,7 @@ public class AuthenticationSimpleHttpInvokerRequestExecutorTests extends TestCas
// ==================================================================================================
private class MockHttpURLConnection extends HttpURLConnection {
private Map<String, String> requestProperties = new HashMap<String, String>();
public MockHttpURLConnection(URL u) {
@@ -15,8 +15,14 @@
package org.springframework.security.remoting.rmi;
import junit.framework.TestCase;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.After;
import org.junit.Test;
import org.springframework.security.TargetObject;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -24,21 +30,18 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.util.SimpleMethodInvocation;
import org.springframework.test.util.ReflectionTestUtils;
import java.lang.reflect.Method;
/**
* Tests {@link ContextPropagatingRemoteInvocation} and
* {@link ContextPropagatingRemoteInvocationFactory}.
*
* @author Ben Alex
*/
public class ContextPropagatingRemoteInvocationTests extends TestCase {
public class ContextPropagatingRemoteInvocationTests {
// ~ Methods
// ========================================================================================================
protected void tearDown() throws Exception {
super.tearDown();
@After
public void tearDown() throws Exception {
SecurityContextHolder.clearContext();
}
@@ -53,6 +56,7 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
return (ContextPropagatingRemoteInvocation) factory.createRemoteInvocation(mi);
}
@Test
public void testContextIsResetEvenIfExceptionOccurs() throws Exception {
// Setup client-side context
Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken(
@@ -71,10 +75,12 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
// expected
}
assertThat(SecurityContextHolder.getContext().as("Authentication must be null ").isNull()
.getAuthentication());
assertThat(
SecurityContextHolder.getContext().getAuthentication()).withFailMessage(
"Authentication must be null").isNull();
}
@Test
public void testNormalOperation() throws Exception {
// Setup client-side context
Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken(
@@ -90,28 +96,31 @@ public class ContextPropagatingRemoteInvocationTests extends TestCase {
// The result from invoking the TargetObject should contain the
// Authentication class delivered via the SecurityContextHolder
assertEquals(
"some_string org.springframework.security.authentication.UsernamePasswordAuthenticationToken false",
remoteInvocation.invoke(new TargetObject()));
assertThat(remoteInvocation.invoke(new TargetObject())).isEqualTo(
"some_string org.springframework.security.authentication.UsernamePasswordAuthenticationToken false");
}
@Test
public void testNullContextHolderDoesNotCauseInvocationProblems() throws Exception {
SecurityContextHolder.clearContext(); // just to be explicit
ContextPropagatingRemoteInvocation remoteInvocation = getRemoteInvocation();
SecurityContextHolder.clearContext(); // unnecessary, but for explicitness
SecurityContextHolder.clearContext(); // unnecessary, but for
// explicitness
assertEquals("some_string Authentication empty",
remoteInvocation.invoke(new TargetObject()));
assertThat(remoteInvocation.invoke(new TargetObject())).isEqualTo(
"some_string Authentication empty");
}
// SEC-1867
@Test
public void testNullCredentials() throws Exception {
Authentication clientSideAuthentication = new UsernamePasswordAuthenticationToken(
"rod", null);
SecurityContextHolder.getContext().setAuthentication(clientSideAuthentication);
ContextPropagatingRemoteInvocation remoteInvocation = getRemoteInvocation();
assertThat("credentials")).isEqualTo(null, ReflectionTestUtils.getField(remoteInvocation);
assertThat(
ReflectionTestUtils.getField(remoteInvocation, "credentials")).isNull();
}
}