From d17c75a7efe0399b2fdf12873171f963b7ec50da Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 25 Sep 2023 17:44:42 +0200 Subject: [PATCH] Test status quo for SpringNamingPolicy See gh-31272 --- .../cglib/core/SpringNamingPolicyTests.java | 80 +++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 spring-core/src/test/java/org/springframework/cglib/core/SpringNamingPolicyTests.java diff --git a/spring-core/src/test/java/org/springframework/cglib/core/SpringNamingPolicyTests.java b/spring-core/src/test/java/org/springframework/cglib/core/SpringNamingPolicyTests.java new file mode 100644 index 00000000000..75fd0a89c4a --- /dev/null +++ b/spring-core/src/test/java/org/springframework/cglib/core/SpringNamingPolicyTests.java @@ -0,0 +1,80 @@ +/* + * Copyright 2002-2023 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.cglib.core; + +import java.util.HashSet; +import java.util.Set; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link SpringNamingPolicy}. + * + * @author Sam Brannen + * @since 6.0.13 + */ +class SpringNamingPolicyTests { + + private final Set reservedClassNames = new HashSet<>(); + + + @Test + void nullPrefix() { + assertThat(getClassName(null)).isEqualTo("org.springframework.cglib.empty.Object$$SpringCGLIB$$0"); + assertThat(getClassName(null)).isEqualTo("org.springframework.cglib.empty.Object$$SpringCGLIB$$1"); + } + + @Test + void javaPrefix() { + assertThat(getClassName("java.util.ArrayList")).isEqualTo("_java.util.ArrayList$$SpringCGLIB$$0"); + assertThat(getClassName("java.util.ArrayList")).isEqualTo("_java.util.ArrayList$$SpringCGLIB$$1"); + } + + @Test + void javaxPrefix() { + assertThat(getClassName("javax.sql.RowSet")).isEqualTo("_javax.sql.RowSet$$SpringCGLIB$$0"); + assertThat(getClassName("javax.sql.RowSet")).isEqualTo("_javax.sql.RowSet$$SpringCGLIB$$1"); + } + + @Test + void examplePrefix() { + assertThat(getClassName("example.MyComponent")).isEqualTo("example.MyComponent$$SpringCGLIB$$0"); + assertThat(getClassName("example.MyComponent")).isEqualTo("example.MyComponent$$SpringCGLIB$$1"); + } + + @Test + void prefixContainingSpringLabel() { + String generated1 = "example.MyComponent$$SpringCGLIB$$0"; + String generated2 = "example.MyComponent$$SpringCGLIB$$1"; + + assertThat(getClassName(generated1)).isEqualTo(generated1); + assertThat(getClassName(generated1)).isEqualTo(generated2); + } + + private String getClassName(String prefix) { + return getClassName(prefix, null, null); + } + + private String getClassName(String prefix, String source, Object key) { + String className = SpringNamingPolicy.INSTANCE.getClassName(prefix, source, key, reservedClassNames::contains); + reservedClassNames.add(className); + return className; + } + +}