Merge pull request #14 from weihubeats/wh/expression_utils

🆕 add spring-boot-test
This commit is contained in:
weihubeats 2024-06-01 22:37:48 +08:00 committed by GitHub
commit 15e7de1cae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 25 deletions

View File

@ -32,6 +32,14 @@
<maven.compiler.target>11</maven.compiler.target> <maven.compiler.target>11</maven.compiler.target>
<spotless-maven-plugin.version>2.43.0</spotless-maven-plugin.version> <spotless-maven-plugin.version>2.43.0</spotless-maven-plugin.version>
</properties> </properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

View File

@ -28,7 +28,6 @@
<artifactId>spring-boot-nebula-common</artifactId> <artifactId>spring-boot-nebula-common</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
@ -40,7 +39,7 @@
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -20,6 +20,7 @@ package com.nebula.web.common.utils;
import com.nebula.base.utils.DataUtils; import com.nebula.base.utils.DataUtils;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Objects; import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer; import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.ExpressionParser; import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.standard.SpelExpressionParser;
@ -46,20 +47,21 @@ public class ExpressionUtil {
} }
// 获取被拦截方法参数名列表 // 获取被拦截方法参数名列表
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
String[] paramNameArr = discoverer.getParameterNames(method); // SPEL解析
String[] paramNames = discoverer.getParameterNames(method);
if (paramNames == null || args == null || paramNames.length != args.length) {
throw new IllegalArgumentException("Method parameter names and argument values do not match.");
}
// SPEL解析 // SPEL解析
ExpressionParser parser = new SpelExpressionParser(); ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext(); StandardEvaluationContext context = new StandardEvaluationContext();
for (int i = 0; i < Objects.requireNonNull(paramNameArr).length; i++) { for (int i = 0; i < Objects.requireNonNull(paramNames).length; i++) {
context.setVariable(paramNameArr[i], args[i]); context.setVariable(paramNames[i], args[i]);
} }
return parser.parseExpression(expressionString).getValue(context); return parser.parseExpression(expressionString).getValue(context);
} }
public static boolean isEl(String param) { public static boolean isEl(String param) {
if (DataUtils.isEmpty(param)) { return !StringUtils.isEmpty(param) && param.startsWith("#");
return false;
}
return Objects.equals(param.substring(0, 1), "#");
} }
} }

View File

@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.nebula.web.common.utils; package com.nebula.web.common.utils;
import com.nebula.web.common.autoconfigure.NebulaWebCommonAutoConfiguration; import com.nebula.web.common.autoconfigure.NebulaWebCommonAutoConfiguration;
@ -10,6 +27,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -20,34 +38,34 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/ */
@SpringBootTest(classes = SpringBeanUtilsTest.TestConfig.class) @SpringBootTest(classes = SpringBeanUtilsTest.TestConfig.class)
public class SpringBeanUtilsTest { public class SpringBeanUtilsTest {
@Test @Test
public void getBean() { public void getBean() {
Object bean = SpringBeanUtils.getBean("testBean"); Object bean = SpringBeanUtils.getBean("testBean");
assertTrue(bean instanceof TestBean); assertInstanceOf(TestBean.class, bean);
TestBean bean1 = SpringBeanUtils.getBean(TestBean.class); TestBean bean1 = SpringBeanUtils.getBean(TestBean.class);
assertTrue(Objects.nonNull(bean1)); assertTrue(Objects.nonNull(bean1));
assertThrowsExactly(NoSuchBeanDefinitionException.class, () -> SpringBeanUtils.getBean(NoTestBean.class)); assertThrowsExactly(NoSuchBeanDefinitionException.class, () -> SpringBeanUtils.getBean(NoTestBean.class));
} }
static final class TestBean { static final class TestBean {
} }
static final class NoTestBean { static final class NoTestBean {
} }
@Configuration @Configuration
@EnableAutoConfiguration @EnableAutoConfiguration
@ImportAutoConfiguration({NebulaWebCommonAutoConfiguration.class}) @ImportAutoConfiguration({NebulaWebCommonAutoConfiguration.class})
public static class TestConfig { public static class TestConfig {
@Bean @Bean
public TestBean testBean() { public TestBean testBean() {
return new TestBean(); return new TestBean();
} }
} }
} }

View File

@ -28,12 +28,6 @@
<artifactId>spring-boot-starter-actuator</artifactId> <artifactId>spring-boot-starter-actuator</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>