|
| 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.jmx.export.naming; |
| 18 | + |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +import javax.management.MalformedObjectNameException; |
| 22 | +import javax.management.ObjectName; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource; |
| 27 | +import org.springframework.util.ClassUtils; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.assertj.core.api.Assertions.entry; |
| 31 | + |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests for {@link MetadataNamingStrategy}. |
| 35 | + * |
| 36 | + * @author Stephane Nicoll |
| 37 | + */ |
| 38 | +class MetadataNamingStrategyTests { |
| 39 | + |
| 40 | + private static final TestBean TEST_BEAN = new TestBean(); |
| 41 | + |
| 42 | + private final MetadataNamingStrategy strategy; |
| 43 | + |
| 44 | + MetadataNamingStrategyTests() { |
| 45 | + this.strategy = new MetadataNamingStrategy(); |
| 46 | + this.strategy.setDefaultDomain("com.example"); |
| 47 | + this.strategy.setAttributeSource(new AnnotationJmxAttributeSource()); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void getObjectNameWhenBeanNameIsSimple() throws MalformedObjectNameException { |
| 52 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "myBean"); |
| 53 | + assertThat(name.getDomain()).isEqualTo("com.example"); |
| 54 | + assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "myBean")); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void getObjectNameWhenBeanNameIsValidObjectName() throws MalformedObjectNameException { |
| 59 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "com.another:name=myBean"); |
| 60 | + assertThat(name.getDomain()).isEqualTo("com.another"); |
| 61 | + assertThat(name.getKeyPropertyList()).containsOnly(entry("name", "myBean")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void getObjectNameWhenBeanNamContainsComma() throws MalformedObjectNameException { |
| 66 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "myBean,"); |
| 67 | + assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"myBean,\"")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void getObjectNameWhenBeanNamContainsEquals() throws MalformedObjectNameException { |
| 72 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "my=Bean"); |
| 73 | + assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"my=Bean\"")); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void getObjectNameWhenBeanNamContainsColon() throws MalformedObjectNameException { |
| 78 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "my:Bean"); |
| 79 | + assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"my:Bean\"")); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void getObjectNameWhenBeanNamContainsQuote() throws MalformedObjectNameException { |
| 84 | + ObjectName name = this.strategy.getObjectName(TEST_BEAN, "\"myBean\""); |
| 85 | + assertThat(name).satisfies(hasDefaultProperties(TEST_BEAN, "\"\\\"myBean\\\"\"")); |
| 86 | + } |
| 87 | + |
| 88 | + private Consumer<ObjectName> hasDefaultProperties(Object instance, String expectedName) { |
| 89 | + return objectName -> assertThat(objectName.getKeyPropertyList()).containsOnly( |
| 90 | + entry("type", ClassUtils.getShortName(instance.getClass())), |
| 91 | + entry("name", expectedName)); |
| 92 | + } |
| 93 | + |
| 94 | + static class TestBean {} |
| 95 | + |
| 96 | +} |
0 commit comments