|
@@ -19,13 +19,12 @@ package org.springframework.security.config;
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.Log;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runner.RunWith;
|
|
-import org.powermock.api.mockito.PowerMockito;
|
|
|
|
-import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
|
|
|
-import org.powermock.core.classloader.annotations.PrepareForTest;
|
|
|
|
-import org.powermock.modules.junit4.PowerMockRunner;
|
|
|
|
|
|
+import org.mockito.Answers;
|
|
|
|
+import org.mockito.Mock;
|
|
|
|
+import org.mockito.MockedStatic;
|
|
|
|
+import org.mockito.junit.MockitoJUnitRunner;
|
|
|
|
|
|
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
|
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
|
-import org.springframework.messaging.Message;
|
|
|
|
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
|
|
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
|
import org.springframework.util.ClassUtils;
|
|
import org.springframework.util.ClassUtils;
|
|
@@ -41,9 +40,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
|
|
* @author Rob Winch
|
|
* @author Rob Winch
|
|
* @since 3.0
|
|
* @since 3.0
|
|
*/
|
|
*/
|
|
-@RunWith(PowerMockRunner.class)
|
|
|
|
-@PrepareForTest({ ClassUtils.class })
|
|
|
|
-@PowerMockIgnore({ "org.w3c.dom.*", "org.xml.sax.*", "org.apache.xerces.*", "javax.xml.parsers.*" })
|
|
|
|
|
|
+@RunWith(MockitoJUnitRunner.class)
|
|
public class SecurityNamespaceHandlerTests {
|
|
public class SecurityNamespaceHandlerTests {
|
|
|
|
|
|
// @formatter:off
|
|
// @formatter:off
|
|
@@ -60,6 +57,9 @@ public class SecurityNamespaceHandlerTests {
|
|
|
|
|
|
private static final String FILTER_CHAIN_PROXY_CLASSNAME = "org.springframework.security.web.FilterChainProxy";
|
|
private static final String FILTER_CHAIN_PROXY_CLASSNAME = "org.springframework.security.web.FilterChainProxy";
|
|
|
|
|
|
|
|
+ @Mock(answer = Answers.CALLS_REAL_METHODS)
|
|
|
|
+ private MockedStatic<ClassUtils> classUtils;
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void constructionSucceeds() {
|
|
public void constructionSucceeds() {
|
|
new SecurityNamespaceHandler();
|
|
new SecurityNamespaceHandler();
|
|
@@ -83,24 +83,18 @@ public class SecurityNamespaceHandlerTests {
|
|
@Test
|
|
@Test
|
|
public void initDoesNotLogErrorWhenFilterChainProxyFailsToLoad() throws Exception {
|
|
public void initDoesNotLogErrorWhenFilterChainProxyFailsToLoad() throws Exception {
|
|
String className = "javax.servlet.Filter";
|
|
String className = "javax.servlet.Filter";
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
Log logger = mock(Log.class);
|
|
Log logger = mock(Log.class);
|
|
SecurityNamespaceHandler handler = new SecurityNamespaceHandler();
|
|
SecurityNamespaceHandler handler = new SecurityNamespaceHandler();
|
|
ReflectionTestUtils.setField(handler, "logger", logger);
|
|
ReflectionTestUtils.setField(handler, "logger", logger);
|
|
|
|
+ expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
|
handler.init();
|
|
handler.init();
|
|
- PowerMockito.verifyStatic(ClassUtils.class);
|
|
|
|
- ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
verifyZeroInteractions(logger);
|
|
verifyZeroInteractions(logger);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void filterNoClassDefFoundError() throws Exception {
|
|
public void filterNoClassDefFoundError() throws Exception {
|
|
String className = "javax.servlet.Filter";
|
|
String className = "javax.servlet.Filter";
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
|
|
+ expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
|
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
|
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
|
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
|
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
|
.withMessageContaining("NoClassDefFoundError: " + className);
|
|
.withMessageContaining("NoClassDefFoundError: " + className);
|
|
@@ -109,9 +103,7 @@ public class SecurityNamespaceHandlerTests {
|
|
@Test
|
|
@Test
|
|
public void filterNoClassDefFoundErrorNoHttpBlock() throws Exception {
|
|
public void filterNoClassDefFoundErrorNoHttpBlock() throws Exception {
|
|
String className = "javax.servlet.Filter";
|
|
String className = "javax.servlet.Filter";
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new NoClassDefFoundError(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
|
|
+ expectClassUtilsForNameThrowsNoClassDefFoundError(className);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
// should load just fine since no http block
|
|
// should load just fine since no http block
|
|
}
|
|
}
|
|
@@ -119,9 +111,7 @@ public class SecurityNamespaceHandlerTests {
|
|
@Test
|
|
@Test
|
|
public void filterChainProxyClassNotFoundException() throws Exception {
|
|
public void filterChainProxyClassNotFoundException() throws Exception {
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
|
|
+ expectClassUtilsForNameThrowsClassNotFoundException(className);
|
|
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
|
assertThatExceptionOfType(BeanDefinitionParsingException.class)
|
|
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
|
.isThrownBy(() -> new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER + XML_HTTP_BLOCK))
|
|
.withMessageContaining("ClassNotFoundException: " + className);
|
|
.withMessageContaining("ClassNotFoundException: " + className);
|
|
@@ -130,9 +120,7 @@ public class SecurityNamespaceHandlerTests {
|
|
@Test
|
|
@Test
|
|
public void filterChainProxyClassNotFoundExceptionNoHttpBlock() throws Exception {
|
|
public void filterChainProxyClassNotFoundExceptionNoHttpBlock() throws Exception {
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(FILTER_CHAIN_PROXY_CLASSNAME), any(ClassLoader.class));
|
|
|
|
|
|
+ expectClassUtilsForNameThrowsClassNotFoundException(className);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
// should load just fine since no http block
|
|
// should load just fine since no http block
|
|
}
|
|
}
|
|
@@ -140,11 +128,19 @@ public class SecurityNamespaceHandlerTests {
|
|
@Test
|
|
@Test
|
|
public void websocketNotFoundExceptionNoMessageBlock() throws Exception {
|
|
public void websocketNotFoundExceptionNoMessageBlock() throws Exception {
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
String className = FILTER_CHAIN_PROXY_CLASSNAME;
|
|
- PowerMockito.spy(ClassUtils.class);
|
|
|
|
- PowerMockito.doThrow(new ClassNotFoundException(className)).when(ClassUtils.class, "forName",
|
|
|
|
- eq(Message.class.getName()), any(ClassLoader.class));
|
|
|
|
|
|
+ expectClassUtilsForNameThrowsClassNotFoundException(className);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
new InMemoryXmlApplicationContext(XML_AUTHENTICATION_MANAGER);
|
|
// should load just fine since no websocket block
|
|
// should load just fine since no websocket block
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void expectClassUtilsForNameThrowsNoClassDefFoundError(String className) {
|
|
|
|
+ this.classUtils.when(() -> ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any()))
|
|
|
|
+ .thenThrow(new NoClassDefFoundError(className));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void expectClassUtilsForNameThrowsClassNotFoundException(String className) {
|
|
|
|
+ this.classUtils.when(() -> ClassUtils.forName(eq(FILTER_CHAIN_PROXY_CLASSNAME), any()))
|
|
|
|
+ .thenThrow(new ClassNotFoundException(className));
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|