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
toLowerBase16()55   public void toLowerBase16() {
56     assertThat(SpanId.INVALID.toLowerBase16()).isEqualTo("0000000000000000");
57     assertThat(first.toLowerBase16()).isEqualTo("0000000000000061");
58     assertThat(second.toLowerBase16()).isEqualTo("ff00000000000041");
59   }
60 
61   @Test
getBytes()62   public void getBytes() {
63     assertThat(first.getBytes()).isEqualTo(firstBytes);
64     assertThat(second.getBytes()).isEqualTo(secondBytes);
65   }
66 
67   @Test
traceId_CompareTo()68   public void traceId_CompareTo() {
69     assertThat(first.compareTo(second)).isGreaterThan(0);
70     assertThat(second.compareTo(first)).isLessThan(0);
71     assertThat(first.compareTo(SpanId.fromBytes(firstBytes))).isEqualTo(0);
72   }
73 
74   @Test
traceId_EqualsAndHashCode()75   public void traceId_EqualsAndHashCode() {
76     EqualsTester tester = new EqualsTester();
77     tester.addEqualityGroup(SpanId.INVALID, SpanId.INVALID);
78     tester.addEqualityGroup(first, SpanId.fromBytes(Arrays.copyOf(firstBytes, firstBytes.length)));
79     tester.addEqualityGroup(
80         second, SpanId.fromBytes(Arrays.copyOf(secondBytes, secondBytes.length)));
81     tester.testEquals();
82   }
83 
84   @Test
traceId_ToString()85   public void traceId_ToString() {
86     assertThat(SpanId.INVALID.toString()).contains("0000000000000000");
87     assertThat(first.toString()).contains("0000000000000061");
88     assertThat(second.toString()).contains("ff00000000000041");
89   }
90 }
91