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.Arrays; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 /** Unit tests for {@link SpanId}. */ 28 @RunWith(JUnit4.class) 29 public class SpanIdTest { 30 private static final byte[] firstBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}; 31 private static final byte[] secondBytes = new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 'A'}; 32 private static final SpanId first = SpanId.fromBytes(firstBytes); 33 private static final SpanId second = SpanId.fromBytes(secondBytes); 34 35 @Test invalidSpanId()36 public void invalidSpanId() { 37 assertThat(SpanId.INVALID.getBytes()).isEqualTo(new byte[8]); 38 } 39 40 @Test isValid()41 public void isValid() { 42 assertThat(SpanId.INVALID.isValid()).isFalse(); 43 assertThat(first.isValid()).isTrue(); 44 assertThat(second.isValid()).isTrue(); 45 } 46 47 @Test fromLowerBase16()48 public void fromLowerBase16() { 49 assertThat(SpanId.fromLowerBase16("0000000000000000")).isEqualTo(SpanId.INVALID); 50 assertThat(SpanId.fromLowerBase16("0000000000000061")).isEqualTo(first); 51 assertThat(SpanId.fromLowerBase16("ff00000000000041")).isEqualTo(second); 52 } 53 54 @Test fromLowerBase16_WithOffset()55 public void fromLowerBase16_WithOffset() { 56 assertThat(SpanId.fromLowerBase16("XX0000000000000000AA", 2)).isEqualTo(SpanId.INVALID); 57 assertThat(SpanId.fromLowerBase16("YY0000000000000061BB", 2)).isEqualTo(first); 58 assertThat(SpanId.fromLowerBase16("ZZff00000000000041CC", 2)).isEqualTo(second); 59 } 60 61 @Test toLowerBase16()62 public void toLowerBase16() { 63 assertThat(SpanId.INVALID.toLowerBase16()).isEqualTo("0000000000000000"); 64 assertThat(first.toLowerBase16()).isEqualTo("0000000000000061"); 65 assertThat(second.toLowerBase16()).isEqualTo("ff00000000000041"); 66 } 67 68 @Test getBytes()69 public void getBytes() { 70 assertThat(first.getBytes()).isEqualTo(firstBytes); 71 assertThat(second.getBytes()).isEqualTo(secondBytes); 72 } 73 74 @Test spanId_CompareTo()75 public void spanId_CompareTo() { 76 assertThat(first.compareTo(second)).isGreaterThan(0); 77 assertThat(second.compareTo(first)).isLessThan(0); 78 assertThat(first.compareTo(SpanId.fromBytes(firstBytes))).isEqualTo(0); 79 } 80 81 @Test spanId_EqualsAndHashCode()82 public void spanId_EqualsAndHashCode() { 83 EqualsTester tester = new EqualsTester(); 84 tester.addEqualityGroup(SpanId.INVALID, SpanId.INVALID); 85 tester.addEqualityGroup(first, SpanId.fromBytes(Arrays.copyOf(firstBytes, firstBytes.length))); 86 tester.addEqualityGroup( 87 second, SpanId.fromBytes(Arrays.copyOf(secondBytes, secondBytes.length))); 88 tester.testEquals(); 89 } 90 91 @Test spanId_ToString()92 public void spanId_ToString() { 93 assertThat(SpanId.INVALID.toString()).contains("0000000000000000"); 94 assertThat(first.toString()).contains("0000000000000061"); 95 assertThat(second.toString()).contains("ff00000000000041"); 96 } 97 } 98