|
| 1 | +/* |
| 2 | + * Copyright 2002-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cglib.core; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for {@link SpringNamingPolicy}. |
| 28 | + * |
| 29 | + * @author Sam Brannen |
| 30 | + * @since 6.0.13 |
| 31 | + */ |
| 32 | +class SpringNamingPolicyTests { |
| 33 | + |
| 34 | + private final Set<String> reservedClassNames = new HashSet<>(); |
| 35 | + |
| 36 | + |
| 37 | + @Test |
| 38 | + void nullPrefix() { |
| 39 | + assertThat(getClassName(null)).isEqualTo("org.springframework.cglib.empty.Object$$SpringCGLIB$$0"); |
| 40 | + assertThat(getClassName(null)).isEqualTo("org.springframework.cglib.empty.Object$$SpringCGLIB$$1"); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void javaPrefix() { |
| 45 | + assertThat(getClassName("java.util.ArrayList")).isEqualTo("_java.util.ArrayList$$SpringCGLIB$$0"); |
| 46 | + assertThat(getClassName("java.util.ArrayList")).isEqualTo("_java.util.ArrayList$$SpringCGLIB$$1"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void javaxPrefix() { |
| 51 | + assertThat(getClassName("javax.sql.RowSet")).isEqualTo("_javax.sql.RowSet$$SpringCGLIB$$0"); |
| 52 | + assertThat(getClassName("javax.sql.RowSet")).isEqualTo("_javax.sql.RowSet$$SpringCGLIB$$1"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void examplePrefix() { |
| 57 | + assertThat(getClassName("example.MyComponent")).isEqualTo("example.MyComponent$$SpringCGLIB$$0"); |
| 58 | + assertThat(getClassName("example.MyComponent")).isEqualTo("example.MyComponent$$SpringCGLIB$$1"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void prefixContainingSpringLabel() { |
| 63 | + String generated1 = "example.MyComponent$$SpringCGLIB$$0"; |
| 64 | + String generated2 = "example.MyComponent$$SpringCGLIB$$1"; |
| 65 | + |
| 66 | + assertThat(getClassName(generated1)).isEqualTo(generated1); |
| 67 | + assertThat(getClassName(generated1)).isEqualTo(generated2); |
| 68 | + } |
| 69 | + |
| 70 | + private String getClassName(String prefix) { |
| 71 | + return getClassName(prefix, null, null); |
| 72 | + } |
| 73 | + |
| 74 | + private String getClassName(String prefix, String source, Object key) { |
| 75 | + String className = SpringNamingPolicy.INSTANCE.getClassName(prefix, source, key, reservedClassNames::contains); |
| 76 | + reservedClassNames.add(className); |
| 77 | + return className; |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments