From 849c0d6a64382d0c05bafca6e9ac481d134f78ef Mon Sep 17 00:00:00 2001 From: spencergibb Date: Wed, 16 Feb 2022 14:28:08 -0500 Subject: [PATCH] Separates ShortcutConfigurableNonRestrictiveTests --- ...ortcutConfigurableNonRestrictiveTests.java | 110 ++++++++++++++++++ .../support/ShortcutConfigurableTests.java | 37 ------ 2 files changed, 110 insertions(+), 37 deletions(-) create mode 100644 spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableNonRestrictiveTests.java diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableNonRestrictiveTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableNonRestrictiveTests.java new file mode 100644 index 00000000..cd5c9048 --- /dev/null +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableNonRestrictiveTests.java @@ -0,0 +1,110 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed 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 + * + * https://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 org.springframework.cloud.gateway.support; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.gateway.support.ShortcutConfigurable.ShortcutType; +import org.springframework.context.annotation.Bean; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.expression.spel.standard.SpelExpressionParser; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(properties = "spring.cloud.gateway.restrictive-property-accessor.enabled=false") +public class ShortcutConfigurableNonRestrictiveTests { + + @Autowired + BeanFactory beanFactory; + + @Autowired + ConfigurableEnvironment env; + + private SpelExpressionParser parser; + + @Test + public void testNormalizeDefaultTypeWithSpelAndPropertyReferenceEnabled() { + parser = new SpelExpressionParser(); + ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { + @Override + public List shortcutFieldOrder() { + return Arrays.asList("bean", "arg1"); + } + }; + Map args = new HashMap<>(); + args.put("barproperty", "#{@bar.getInt}"); + args.put("arg1", "val1"); + Map map = ShortcutType.DEFAULT.normalize(args, shortcutConfigurable, parser, this.beanFactory); + assertThat(map).isNotNull().containsEntry("barproperty", 42).containsEntry("arg1", "val1"); + } + + @Test + public void testNormalizeDefaultTypeWithSpelAndMethodReferenceEnabled() { + parser = new SpelExpressionParser(); + ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { + @Override + public List shortcutFieldOrder() { + return Arrays.asList("bean", "arg1"); + } + }; + Map args = new HashMap<>(); + args.put("barmethod", "#{@bar.myMethod}"); + args.put("arg1", "val1"); + Map map = ShortcutType.DEFAULT.normalize(args, shortcutConfigurable, parser, this.beanFactory); + assertThat(map).isNotNull().containsEntry("barmethod", 42).containsEntry("arg1", "val1"); + } + + @SpringBootConfiguration + protected static class TestConfig { + + @Bean + public Integer foo() { + return 42; + } + + @Bean + public Bar bar() { + return new Bar(); + } + + } + + protected static class Bar { + + public int getInt() { + return 42; + } + + public int myMethod() { + return 42; + } + + } + +} diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableTests.java index 42f7ef19..81dadfd8 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/support/ShortcutConfigurableTests.java @@ -28,7 +28,6 @@ import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.cloud.gateway.support.ShortcutConfigurable.ShortcutType; import org.springframework.context.annotation.Bean; import org.springframework.core.env.ConfigurableEnvironment; @@ -70,7 +69,6 @@ public class ShortcutConfigurableTests { @Test public void testNormalizeDefaultTypeWithSpelAndInvalidPropertyReferenceFails() { - TestPropertyValues.of("spring.cloud.gateway.restrictive-property-accessor.enabled=true").applyTo(env); parser = new SpelExpressionParser(); ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { @Override @@ -88,7 +86,6 @@ public class ShortcutConfigurableTests { @Test public void testNormalizeDefaultTypeWithSpelAndInvalidMethodReferenceFails() { - TestPropertyValues.of("ispring.cloud.gateway.restrictve-property-accessor.enabled=true").applyTo(env); parser = new SpelExpressionParser(); ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { @Override @@ -120,40 +117,6 @@ public class ShortcutConfigurableTests { assertThat(map).isNotNull().containsEntry("bean", 42).containsEntry("arg1", "val1"); } - @Test - public void testNormalizeDefaultTypeWithSpelAndPropertyReferenceEnabled() { - TestPropertyValues.of("spring.cloud.gateway.restrictive-property-accessor.enabled=false").applyTo(env); - parser = new SpelExpressionParser(); - ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { - @Override - public List shortcutFieldOrder() { - return Arrays.asList("bean", "arg1"); - } - }; - Map args = new HashMap<>(); - args.put("barproperty", "#{@bar.getInt}"); - args.put("arg1", "val1"); - Map map = ShortcutType.DEFAULT.normalize(args, shortcutConfigurable, parser, this.beanFactory); - assertThat(map).isNotNull().containsEntry("barproperty", 42).containsEntry("arg1", "val1"); - } - - @Test - public void testNormalizeDefaultTypeWithSpelAndMethodReferenceEnabled() { - TestPropertyValues.of("spring.cloud.gateway.restrictive-property-accessor.enabled=false").applyTo(env); - parser = new SpelExpressionParser(); - ShortcutConfigurable shortcutConfigurable = new ShortcutConfigurable() { - @Override - public List shortcutFieldOrder() { - return Arrays.asList("bean", "arg1"); - } - }; - Map args = new HashMap<>(); - args.put("barmethod", "#{@bar.myMethod}"); - args.put("arg1", "val1"); - Map map = ShortcutType.DEFAULT.normalize(args, shortcutConfigurable, parser, this.beanFactory); - assertThat(map).isNotNull().containsEntry("barmethod", 42).containsEntry("arg1", "val1"); - } - @Test @SuppressWarnings("unchecked") public void testNormalizeGatherListTypeWithSpel() {