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 import static org.junit.Assert.fail; 21 22 import com.google.common.testing.EqualsTester; 23 import io.opencensus.common.Function; 24 import io.opencensus.common.Functions; 25 import javax.annotation.Nullable; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Unit tests for {@link AttributeValue}. */ 31 @RunWith(JUnit4.class) 32 public class AttributeValueTest { 33 @Test stringAttributeValue()34 public void stringAttributeValue() { 35 AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue"); 36 attribute.match( 37 new Function<String, Object>() { 38 @Override 39 @Nullable 40 public Object apply(String stringValue) { 41 assertThat(stringValue).isEqualTo("MyStringAttributeValue"); 42 return null; 43 } 44 }, 45 new Function<Boolean, Object>() { 46 @Override 47 @Nullable 48 public Object apply(Boolean booleanValue) { 49 fail("Expected a String"); 50 return null; 51 } 52 }, 53 new Function<Long, Object>() { 54 @Override 55 @Nullable 56 public Object apply(Long longValue) { 57 fail("Expected a String"); 58 return null; 59 } 60 }, 61 Functions.throwIllegalArgumentException()); 62 } 63 64 @Test booleanAttributeValue()65 public void booleanAttributeValue() { 66 AttributeValue attribute = AttributeValue.booleanAttributeValue(true); 67 attribute.match( 68 new Function<String, Object>() { 69 @Override 70 @Nullable 71 public Object apply(String stringValue) { 72 fail("Expected a Boolean"); 73 return null; 74 } 75 }, 76 new Function<Boolean, Object>() { 77 @Override 78 @Nullable 79 public Object apply(Boolean booleanValue) { 80 assertThat(booleanValue).isTrue(); 81 return null; 82 } 83 }, 84 new Function<Long, Object>() { 85 @Override 86 @Nullable 87 public Object apply(Long longValue) { 88 fail("Expected a Boolean"); 89 return null; 90 } 91 }, 92 Functions.throwIllegalArgumentException()); 93 } 94 95 @Test longAttributeValue()96 public void longAttributeValue() { 97 AttributeValue attribute = AttributeValue.longAttributeValue(123456L); 98 attribute.match( 99 new Function<String, Object>() { 100 @Override 101 @Nullable 102 public Object apply(String stringValue) { 103 fail("Expected a Long"); 104 return null; 105 } 106 }, 107 new Function<Boolean, Object>() { 108 @Override 109 @Nullable 110 public Object apply(Boolean booleanValue) { 111 fail("Expected a Long"); 112 return null; 113 } 114 }, 115 new Function<Long, Object>() { 116 @Override 117 @Nullable 118 public Object apply(Long longValue) { 119 assertThat(longValue).isEqualTo(123456L); 120 return null; 121 } 122 }, 123 Functions.throwIllegalArgumentException()); 124 } 125 126 @Test doubleAttributeValue()127 public void doubleAttributeValue() { 128 AttributeValue attribute = AttributeValue.doubleAttributeValue(1.23456); 129 attribute.match( 130 new Function<String, Object>() { 131 @Override 132 @Nullable 133 public Object apply(String stringValue) { 134 fail("Expected a Double"); 135 return null; 136 } 137 }, 138 new Function<Boolean, Object>() { 139 @Override 140 @Nullable 141 public Object apply(Boolean booleanValue) { 142 fail("Expected a Double"); 143 return null; 144 } 145 }, 146 new Function<Long, Object>() { 147 @Override 148 @Nullable 149 public Object apply(Long longValue) { 150 fail("Expected a Double"); 151 return null; 152 } 153 }, 154 new Function<Double, Object>() { 155 @Override 156 @Nullable 157 public Object apply(Double doubleValue) { 158 assertThat(doubleValue).isEqualTo(1.23456); 159 return null; 160 } 161 }, 162 Functions.throwIllegalArgumentException()); 163 } 164 165 @Test doubleAttributeValue_DeprecatedMatchFunction()166 public void doubleAttributeValue_DeprecatedMatchFunction() { 167 AttributeValue attribute = AttributeValue.doubleAttributeValue(1.23456); 168 attribute.match( 169 new Function<String, Object>() { 170 @Override 171 @Nullable 172 public Object apply(String stringValue) { 173 fail("Expected a Double"); 174 return null; 175 } 176 }, 177 new Function<Boolean, Object>() { 178 @Override 179 @Nullable 180 public Object apply(Boolean booleanValue) { 181 fail("Expected a Double"); 182 return null; 183 } 184 }, 185 new Function<Long, Object>() { 186 @Override 187 @Nullable 188 public Object apply(Long longValue) { 189 fail("Expected a Double"); 190 return null; 191 } 192 }, 193 new Function<Object, Object>() { 194 @Override 195 @Nullable 196 public Object apply(Object value) { 197 assertThat(value).isEqualTo(1.23456); 198 return null; 199 } 200 }); 201 } 202 203 @Test attributeValue_EqualsAndHashCode()204 public void attributeValue_EqualsAndHashCode() { 205 EqualsTester tester = new EqualsTester(); 206 tester.addEqualityGroup( 207 AttributeValue.stringAttributeValue("MyStringAttributeValue"), 208 AttributeValue.stringAttributeValue("MyStringAttributeValue")); 209 tester.addEqualityGroup(AttributeValue.stringAttributeValue("MyStringAttributeDiffValue")); 210 tester.addEqualityGroup( 211 AttributeValue.booleanAttributeValue(true), AttributeValue.booleanAttributeValue(true)); 212 tester.addEqualityGroup(AttributeValue.booleanAttributeValue(false)); 213 tester.addEqualityGroup( 214 AttributeValue.longAttributeValue(123456L), AttributeValue.longAttributeValue(123456L)); 215 tester.addEqualityGroup(AttributeValue.longAttributeValue(1234567L)); 216 tester.addEqualityGroup( 217 AttributeValue.doubleAttributeValue(1.23456), AttributeValue.doubleAttributeValue(1.23456)); 218 tester.addEqualityGroup(AttributeValue.doubleAttributeValue(1.234567)); 219 tester.testEquals(); 220 } 221 222 @Test attributeValue_ToString()223 public void attributeValue_ToString() { 224 AttributeValue attribute = AttributeValue.stringAttributeValue("MyStringAttributeValue"); 225 assertThat(attribute.toString()).contains("MyStringAttributeValue"); 226 attribute = AttributeValue.booleanAttributeValue(true); 227 assertThat(attribute.toString()).contains("true"); 228 attribute = AttributeValue.longAttributeValue(123456L); 229 assertThat(attribute.toString()).contains("123456"); 230 attribute = AttributeValue.doubleAttributeValue(1.23456); 231 assertThat(attribute.toString()).contains("1.23456"); 232 } 233 } 234