1 /* 2 * Copyright 2017, OpenCensus 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 * http://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 io.opencensus.trace; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.testing.EqualsTester; 22 import java.util.Collections; 23 import java.util.HashMap; 24 import java.util.Map; 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link Link}. */ 30 @RunWith(JUnit4.class) 31 public class AnnotationTest { 32 @Test(expected = NullPointerException.class) fromDescription_NullDescription()33 public void fromDescription_NullDescription() { 34 Annotation.fromDescription(null); 35 } 36 37 @Test fromDescription()38 public void fromDescription() { 39 Annotation annotation = Annotation.fromDescription("MyAnnotationText"); 40 assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); 41 assertThat(annotation.getAttributes().size()).isEqualTo(0); 42 } 43 44 @Test(expected = NullPointerException.class) fromDescriptionAndAttributes_NullDescription()45 public void fromDescriptionAndAttributes_NullDescription() { 46 Annotation.fromDescriptionAndAttributes(null, Collections.<String, AttributeValue>emptyMap()); 47 } 48 49 @Test(expected = NullPointerException.class) fromDescriptionAndAttributes_NullAttributes()50 public void fromDescriptionAndAttributes_NullAttributes() { 51 Annotation.fromDescriptionAndAttributes("", null); 52 } 53 54 @Test fromDescriptionAndAttributes()55 public void fromDescriptionAndAttributes() { 56 Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); 57 attributes.put( 58 "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); 59 Annotation annotation = Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes); 60 assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); 61 assertThat(annotation.getAttributes()).isEqualTo(attributes); 62 } 63 64 @Test fromDescriptionAndAttributes_EmptyAttributes()65 public void fromDescriptionAndAttributes_EmptyAttributes() { 66 Annotation annotation = 67 Annotation.fromDescriptionAndAttributes( 68 "MyAnnotationText", Collections.<String, AttributeValue>emptyMap()); 69 assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); 70 assertThat(annotation.getAttributes().size()).isEqualTo(0); 71 } 72 73 @Test annotation_EqualsAndHashCode()74 public void annotation_EqualsAndHashCode() { 75 EqualsTester tester = new EqualsTester(); 76 Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); 77 attributes.put( 78 "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); 79 tester 80 .addEqualityGroup( 81 Annotation.fromDescription("MyAnnotationText"), 82 Annotation.fromDescriptionAndAttributes( 83 "MyAnnotationText", Collections.<String, AttributeValue>emptyMap())) 84 .addEqualityGroup( 85 Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes), 86 Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes)) 87 .addEqualityGroup(Annotation.fromDescription("MyAnnotationText2")); 88 tester.testEquals(); 89 } 90 91 @Test annotation_ToString()92 public void annotation_ToString() { 93 Annotation annotation = Annotation.fromDescription("MyAnnotationText"); 94 assertThat(annotation.toString()).contains("MyAnnotationText"); 95 Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); 96 attributes.put( 97 "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); 98 annotation = Annotation.fromDescriptionAndAttributes("MyAnnotationText2", attributes); 99 assertThat(annotation.toString()).contains("MyAnnotationText2"); 100 assertThat(annotation.toString()).contains(attributes.toString()); 101 } 102 } 103