|
| 1 | +/* |
| 2 | + * Copyright 2013-2019 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.cloud.sleuth.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.lang.reflect.Method; |
| 21 | + |
| 22 | +import brave.sampler.Sampler; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.beans.factory.BeanFactory; |
| 27 | +import org.springframework.beans.factory.annotation.Autowired; |
| 28 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 29 | +import org.springframework.boot.test.context.SpringBootTest; |
| 30 | +import org.springframework.context.annotation.Bean; |
| 31 | +import org.springframework.context.annotation.Configuration; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.assertj.core.api.Assertions.fail; |
| 35 | + |
| 36 | +@SpringBootTest(classes = NullSpanTagAnnotationHandlerTests.TestConfiguration.class) |
| 37 | + |
| 38 | +public class NullSpanTagAnnotationHandlerTests { |
| 39 | + |
| 40 | + @Autowired |
| 41 | + BeanFactory beanFactory; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + TagValueResolver tagValueResolver; |
| 45 | + |
| 46 | + SpanTagAnnotationHandler handler; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + public void setup() { |
| 50 | + this.handler = new SpanTagAnnotationHandler(this.beanFactory); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void shouldUseEmptyStringWheCustomTagValueResolverReturnsNull() |
| 55 | + throws NoSuchMethodException, SecurityException { |
| 56 | + Method method = AnnotationMockClass.class |
| 57 | + .getMethod("getAnnotationForTagValueResolver", String.class); |
| 58 | + Annotation annotation = method.getParameterAnnotations()[0][0]; |
| 59 | + if (annotation instanceof SpanTag) { |
| 60 | + String resolvedValue = this.handler.resolveTagValue((SpanTag) annotation, |
| 61 | + "test"); |
| 62 | + assertThat(resolvedValue).isEqualTo(""); |
| 63 | + } |
| 64 | + else { |
| 65 | + fail("Annotation was not SleuthSpanTag"); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void shouldUseEmptyStringWhenTagValueExpressionReturnNull() |
| 71 | + throws NoSuchMethodException, SecurityException { |
| 72 | + Method method = AnnotationMockClass.class |
| 73 | + .getMethod("getAnnotationForTagValueExpression", String.class); |
| 74 | + Annotation annotation = method.getParameterAnnotations()[0][0]; |
| 75 | + if (annotation instanceof SpanTag) { |
| 76 | + String resolvedValue = this.handler.resolveTagValue((SpanTag) annotation, |
| 77 | + "test"); |
| 78 | + |
| 79 | + assertThat(resolvedValue).isEqualTo(""); |
| 80 | + } |
| 81 | + else { |
| 82 | + fail("Annotation was not SleuthSpanTag"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void shouldUseEmptyStringWhenArgumentIsNull() |
| 88 | + throws NoSuchMethodException, SecurityException { |
| 89 | + Method method = AnnotationMockClass.class |
| 90 | + .getMethod("getAnnotationForArgumentToString", Long.class); |
| 91 | + Annotation annotation = method.getParameterAnnotations()[0][0]; |
| 92 | + if (annotation instanceof SpanTag) { |
| 93 | + String resolvedValue = this.handler.resolveTagValue((SpanTag) annotation, |
| 94 | + null); |
| 95 | + assertThat(resolvedValue).isEqualTo(""); |
| 96 | + } |
| 97 | + else { |
| 98 | + fail("Annotation was not SleuthSpanTag"); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Configuration |
| 103 | + @EnableAutoConfiguration |
| 104 | + protected static class TestConfiguration { |
| 105 | + |
| 106 | + @Bean |
| 107 | + public TagValueResolver tagValueResolver() { |
| 108 | + return parameter -> null; |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + Sampler alwaysSampler() { |
| 113 | + return Sampler.ALWAYS_SAMPLE; |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + protected class AnnotationMockClass { |
| 119 | + |
| 120 | + @NewSpan |
| 121 | + public void getAnnotationForTagValueResolver( |
| 122 | + @SpanTag(key = "test", resolver = TagValueResolver.class) String test) { |
| 123 | + } |
| 124 | + |
| 125 | + @NewSpan |
| 126 | + public void getAnnotationForTagValueExpression( |
| 127 | + @SpanTag(key = "test", expression = "null") String test) { |
| 128 | + } |
| 129 | + |
| 130 | + @NewSpan |
| 131 | + public void getAnnotationForArgumentToString(@SpanTag("test") Long param) { |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments