1
0
mirror of synced 2026-08-05 17:57:15 +00:00

Removal of some unused internal methods, plus additional tests for some areas lacking coverage.

This commit is contained in:
Luke Taylor
2011-02-07 00:24:20 +00:00
parent 20e65a93ea
commit eb9482b33b
15 changed files with 259 additions and 82 deletions
@@ -23,29 +23,13 @@ import java.lang.reflect.Field;
/**
* Offers static methods for directly manipulating static fields.
* Offers static methods for directly manipulating fields.
*
* @author Ben Alex
*/
public final class FieldUtils {
//~ Constructors ===================================================================================================
private FieldUtils() {
}
//~ Methods ========================================================================================================
public static String getAccessorName(String fieldName, Class<?> type) {
Assert.hasText(fieldName, "FieldName required");
Assert.notNull(type, "Type required");
if (type.getName().equals("boolean")) {
return "is" + org.springframework.util.StringUtils.capitalize(fieldName);
} else {
return "get" + org.springframework.util.StringUtils.capitalize(fieldName);
}
}
/**
* Attempts to locate the specified field on the class.
*
@@ -98,12 +82,6 @@ public final class FieldUtils {
}
public static String getMutatorName(String fieldName) {
Assert.hasText(fieldName, "FieldName required");
return "set" + org.springframework.util.StringUtils.capitalize(fieldName);
}
public static Object getProtectedFieldValue(String protectedField, Object object) {
Field field = FieldUtils.getField(object.getClass(), protectedField);
@@ -63,7 +63,7 @@ public class InMemoryResource extends AbstractResource {
}
public int hashCode() {
return source.hashCode();
return 1;
}
public boolean equals(Object res) {
@@ -31,10 +31,6 @@ import org.springframework.util.Assert;
* @author Ben Alex
*/
public final class MethodInvocationUtils {
//~ Constructors ===================================================================================================
private MethodInvocationUtils() {
}
//~ Methods ========================================================================================================
@@ -15,13 +15,15 @@
package org.springframework.security.access;
import static org.junit.Assert.assertSame;
import org.junit.Test;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.event.AuthorizationFailureEvent;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.util.SimpleMethodInvocation;
import java.util.*;
/**
* Tests {@link AuthorizationFailureEvent}.
@@ -29,28 +31,35 @@ import org.springframework.security.util.SimpleMethodInvocation;
* @author Ben Alex
*/
public class AuthorizationFailureEventTests {
private final UsernamePasswordAuthenticationToken foo = new UsernamePasswordAuthenticationToken("foo", "bar");
private List<ConfigAttribute> attributes = SecurityConfig.createList("TEST");
private AccessDeniedException exception = new AuthorizationServiceException("error", new Throwable());
@Test(expected=IllegalArgumentException.class)
public void testRejectsNulls() {
new AuthorizationFailureEvent(null, SecurityConfig.createList("TEST"),
new UsernamePasswordAuthenticationToken("foo", "bar"), new AccessDeniedException("error"));
public void rejectsNullSecureObject() {
new AuthorizationFailureEvent(null, attributes, foo, exception);
}
@Test(expected=IllegalArgumentException.class)
public void testRejectsNulls2() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), null,
new UsernamePasswordAuthenticationToken("foo", "bar"), new AccessDeniedException("error"));
public void rejectsNullAttributesList() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), null, foo, exception);
}
@Test(expected=IllegalArgumentException.class)
public void testRejectsNulls3() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"), null,
new AccessDeniedException("error"));
public void rejectsNullAuthentication() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), attributes, null, exception);
}
@Test(expected=IllegalArgumentException.class)
public void testRejectsNulls4() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), SecurityConfig.createList("TEST"),
new UsernamePasswordAuthenticationToken("foo", "bar"), null);
public void rejectsNullException() {
new AuthorizationFailureEvent(new SimpleMethodInvocation(), attributes, foo, null);
}
@Test
public void gettersReturnCtorSuppliedData() throws Exception {
AuthorizationFailureEvent event = new AuthorizationFailureEvent(new Object(), attributes , foo, exception);
assertSame(attributes, event.getConfigAttributes());
assertSame(exception, event.getAccessDeniedException());
assertSame(foo, event.getAuthentication());
}
}
@@ -0,0 +1,39 @@
package org.springframework.security.util;
import static org.junit.Assert.*;
import org.junit.*;
/**
* @author Luke Taylor
*/
public class FieldUtilsTests {
@Test
public void gettingAndSettingProtectedFieldIsSuccessful() throws Exception {
new FieldUtils();
Object tc = new TestClass();
assertEquals("x", FieldUtils.getProtectedFieldValue("protectedField", tc));
assertEquals("z", FieldUtils.getFieldValue(tc, "nested.protectedField"));
FieldUtils.setProtectedFieldValue("protectedField", tc, "y");
assertEquals("y", FieldUtils.getProtectedFieldValue("protectedField", tc));
try {
FieldUtils.getProtectedFieldValue("nonExistentField", tc);
} catch (IllegalStateException expected) {
}
}
}
@SuppressWarnings("unused")
class TestClass {
private String protectedField = "x";
private Nested nested = new Nested();
}
@SuppressWarnings("unused")
class Nested {
private String protectedField = "z";
}
@@ -0,0 +1,26 @@
package org.springframework.security.util;
import static org.junit.Assert.*;
import org.junit.*;
/**
* @author Luke Taylor
*/
public class InMemoryResourceTests {
@Test
public void resourceContainsExpectedData() throws Exception {
InMemoryResource resource = new InMemoryResource("blah");
assertNull(resource.getDescription());
assertEquals(1, resource.hashCode());
assertNotNull(resource.getInputStream());
}
@Test
public void resourceIsEqualToOneWithSameContent() throws Exception {
assertEquals(new InMemoryResource("xxx"), new InMemoryResource("xxx"));
assertFalse(new InMemoryResource("xxx").equals(new InMemoryResource("xxxx")));
assertFalse(new InMemoryResource("xxx").equals(new Object()));
}
}
@@ -3,9 +3,12 @@ package org.springframework.security.util;
import static org.junit.Assert.*;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.junit.*;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.security.access.annotation.BusinessServiceImpl;
import java.io.Serializable;
/**
*
* @author Luke Taylor
@@ -14,6 +17,8 @@ public class MethodInvocationUtilsTests {
@Test
public void createFromClassReturnsMethodWithNoArgInfoForMethodWithNoArgs() {
new MethodInvocationUtils();
MethodInvocation mi = MethodInvocationUtils.createFromClass(String.class, "length");
assertNotNull(mi);
}
@@ -36,4 +41,28 @@ public class MethodInvocationUtilsTests {
assertNotNull(mi);
}
@Test
public void createFromObjectLocatesExistingMethods() throws Exception {
AdvisedTarget t = new AdvisedTarget();
// Just lie about interfaces
t.setInterfaces(new Class[] {Serializable.class, MethodInvocation.class, Blah.class});
MethodInvocation mi = MethodInvocationUtils.create(t, "blah");
assertNotNull(mi);
t.setProxyTargetClass(true);
mi = MethodInvocationUtils.create(t, "blah");
assertNotNull(mi);
assertNull(MethodInvocationUtils.create(t, "blah", "non-existent arg"));
}
interface Blah {
void blah();
}
class AdvisedTarget extends AdvisedSupport implements Blah {
public void blah() {
}
}
}