You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2022/10/07 11:23:27 UTC
[shiro] 02/06: Add unit tests for SHIRO-890
This is an automated email from the ASF dual-hosted git repository.
bdemers pushed a commit to branch aop-1-10-x
in repository https://gitbox.apache.org/repos/asf/shiro.git
commit dd1de0da829d87459da68006ca72f66836bf1ef8
Author: George CAO <ma...@gmail.com>
AuthorDate: Fri Oct 7 16:57:51 2022 +0800
Add unit tests for SHIRO-890
---
support/spring-boot/spring-boot-starter/pom.xml | 14 +++----
.../AspectjAndDefaultProxyCreatorTest.java | 44 ++++++++++++++++++++++
...roAnnotationProcessorAutoConfigurationTest.java | 34 +++++++++++++++++
.../AspectjAndDefaultProxyCreatorApplication.java | 42 +++++++++++++++++++++
.../autoconfigure/AspectjEnabledApplication.java | 31 +++++++++++++++
5 files changed, 158 insertions(+), 7 deletions(-)
diff --git a/support/spring-boot/spring-boot-starter/pom.xml b/support/spring-boot/spring-boot-starter/pom.xml
index 20775410..1e168399 100644
--- a/support/spring-boot/spring-boot-starter/pom.xml
+++ b/support/spring-boot/spring-boot-starter/pom.xml
@@ -18,7 +18,8 @@
~ under the License.
-->
<!--suppress osmorcNonOsgiMavenDependency -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
@@ -32,12 +33,10 @@
<name>Apache Shiro :: Support :: Spring Boot</name>
<dependencies>
-
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
</dependency>
-
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
@@ -58,7 +57,6 @@
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
-
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
@@ -69,20 +67,22 @@
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
-
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
-
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${commons.logging.version}</version>
<scope>test</scope>
</dependency>
-
+ <dependency>
+ <groupId>org.aspectj</groupId>
+ <artifactId>aspectjweaver</artifactId>
+ <optional>true</optional>
+ </dependency>
</dependencies>
<build>
diff --git a/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorTest.java b/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorTest.java
new file mode 100644
index 00000000..95896954
--- /dev/null
+++ b/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorTest.java
@@ -0,0 +1,44 @@
+package org.apache.shiro.spring.boot.autoconfigure;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
+import org.springframework.aop.config.AopConfigUtils;
+import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
+import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.util.Arrays;
+
+import static org.junit.Assert.*;
+
+@SpringBootTest(classes = AspectjAndDefaultProxyCreatorApplication.class)
+@RunWith(SpringJUnit4ClassRunner.class)
+public class AspectjAndDefaultProxyCreatorTest {
+
+ @Autowired
+ private ApplicationContext applicationContext;
+
+ @Test
+ public void defaultAdvisorAutoProxyCreator() throws BeansException {
+ // There are two proxy creators before SHIRO-890 which causes problem when @EnableAspectJAutoProxy is enabled.
+ String[] names = new String[]{"defaultAdvisorAutoProxyCreator", AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME};
+ for (String name : names) {
+ Object creator = applicationContext.getBean(name);
+ assertNotNull(creator);
+ if (!(creator instanceof DefaultAdvisorAutoProxyCreator
+ || creator instanceof AnnotationAwareAspectJAutoProxyCreator)) {
+ fail("It supposed to be one of these two types olny.");
+ }
+ }
+ String[] beanNames = applicationContext.getBeanNamesForType(AbstractAdvisorAutoProxyCreator.class);
+ assertEquals(2, beanNames.length);
+ Arrays.sort(names);
+ Arrays.sort(beanNames);
+ assertArrayEquals(names, beanNames);
+ }
+}
\ No newline at end of file
diff --git a/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfigurationTest.java b/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfigurationTest.java
new file mode 100644
index 00000000..e3552623
--- /dev/null
+++ b/support/spring-boot/spring-boot-starter/src/test/groovy/org/apache/shiro/spring/boot/autoconfigure/ShiroAnnotationProcessorAutoConfigurationTest.java
@@ -0,0 +1,34 @@
+package org.apache.shiro.spring.boot.autoconfigure;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator;
+import org.springframework.aop.config.AopConfigUtils;
+import org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator;
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.ApplicationContext;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.fail;
+
+@SpringBootTest(classes = AspectjEnabledApplication.class)
+@RunWith(SpringJUnit4ClassRunner.class)
+public class ShiroAnnotationProcessorAutoConfigurationTest {
+
+ @Autowired
+ private ApplicationContext applicationContext;
+
+ @Test
+ public void defaultAdvisorAutoProxyCreator() throws BeansException {
+ // There is only one proxy creator, and it's AnnotationAwareAspectJAutoProxyCreator as expected.
+ Object creator = applicationContext.getBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
+ if (!(creator instanceof AnnotationAwareAspectJAutoProxyCreator)) {
+ fail("@EnableAspectJAutoProxy will create in instance of AnnotationAwareAspectJAutoProxyCreator");
+ }
+ String[] names = applicationContext.getBeanNamesForType(AbstractAdvisorAutoProxyCreator.class);
+ assertEquals(1, names.length);
+ }
+}
\ No newline at end of file
diff --git a/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorApplication.java b/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorApplication.java
new file mode 100644
index 00000000..8e541beb
--- /dev/null
+++ b/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjAndDefaultProxyCreatorApplication.java
@@ -0,0 +1,42 @@
+package org.apache.shiro.spring.boot.autoconfigure;
+
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.realm.text.TextConfigurationRealm;
+import org.springframework.aop.config.AopConfigUtils;
+import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+@EnableAutoConfiguration
+@EnableAspectJAutoProxy
+public class AspectjAndDefaultProxyCreatorApplication {
+
+ public static void main(String... args) {
+ SpringApplication.run(AspectjAndDefaultProxyCreatorApplication.class);
+ }
+
+ @Bean
+ @SuppressWarnings("Duplicates")
+ Realm getTextConfigurationRealm() {
+
+ TextConfigurationRealm realm = new TextConfigurationRealm();
+ realm.setUserDefinitions("joe.coder=password,user\n" +
+ "jill.coder=password,admin");
+
+ realm.setRoleDefinitions("admin=read,write\n" +
+ "user=read");
+ realm.setCachingEnabled(true);
+ return realm;
+ }
+
+ @Bean
+ @DependsOn("lifecycleBeanPostProcessor")
+ @ConditionalOnMissingBean(value = DefaultAdvisorAutoProxyCreator.class, name = AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME)
+ public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
+ return new DefaultAdvisorAutoProxyCreator();
+ }
+}
\ No newline at end of file
diff --git a/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjEnabledApplication.java b/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjEnabledApplication.java
new file mode 100644
index 00000000..dc7073b8
--- /dev/null
+++ b/support/spring-boot/spring-boot-starter/src/test/java/org/apache/shiro/spring/boot/autoconfigure/AspectjEnabledApplication.java
@@ -0,0 +1,31 @@
+package org.apache.shiro.spring.boot.autoconfigure;
+
+import org.apache.shiro.realm.Realm;
+import org.apache.shiro.realm.text.TextConfigurationRealm;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.EnableAspectJAutoProxy;
+
+@EnableAutoConfiguration
+@EnableAspectJAutoProxy
+public class AspectjEnabledApplication {
+
+ public static void main(String... args) {
+ SpringApplication.run(AspectjEnabledApplication.class);
+ }
+
+ @Bean
+ @SuppressWarnings("Duplicates")
+ Realm getTextConfigurationRealm() {
+
+ TextConfigurationRealm realm = new TextConfigurationRealm();
+ realm.setUserDefinitions("joe.coder=password,user\n" +
+ "jill.coder=password,admin");
+
+ realm.setRoleDefinitions("admin=read,write\n" +
+ "user=read");
+ realm.setCachingEnabled(true);
+ return realm;
+ }
+}
\ No newline at end of file