• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-17, 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 TraceId}. */
28 @RunWith(JUnit4.class)
29 public class TraceIdTest {
30   private static final byte[] firstBytes =
31       new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'};
32   private static final byte[] secondBytes =
33       new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'A'};
34   private static final TraceId first = TraceId.fromBytes(firstBytes);
35   private static final TraceId second = TraceId.fromBytes(secondBytes);
36 
37   @Test
invalidTraceId()38   public void invalidTraceId() {
39     assertThat(TraceId.INVALID.getBytes()).isEqualTo(new byte[16]);
40   }
41 
42   @Test
isValid()43   public void isValid() {
44     assertThat(TraceId.INVALID.isValid()).isFalse();
45     assertThat(first.isValid()).isTrue();
46     assertThat(second.isValid()).isTrue();
47   }
48 
49   @Test
getBytes()50   public void getBytes() {
51     assertThat(first.getBytes()).isEqualTo(firstBytes);
52     assertThat(second.getBytes()).isEqualTo(secondBytes);
53   }
54 
55   @Test
getLowerLong()56   public void getLowerLong() {
57     assertThat(first.getLowerLong()).isEqualTo(0);
58     assertThat(second.getLowerLong()).isEqualTo(-0xFF00000000000000L);
59   }
60 
61   @Test
fromLowerBase16()62   public void fromLowerBase16() {
63     assertThat(TraceId.fromLowerBase16("00000000000000000000000000000000"))
64         .isEqualTo(TraceId.INVALID);
65     assertThat(TraceId.fromLowerBase16("00000000000000000000000000000061")).isEqualTo(first);
66     assertThat(TraceId.fromLowerBase16("ff000000000000000000000000000041")).isEqualTo(second);
67   }
68 
69   @Test
fromLowerBase16_WithOffset()70   public void fromLowerBase16_WithOffset() {
71     assertThat(TraceId.fromLowerBase16("XX00000000000000000000000000000000CC", 2))
72         .isEqualTo(TraceId.INVALID);
73     assertThat(TraceId.fromLowerBase16("YY00000000000000000000000000000061AA", 2)).isEqualTo(first);
74     assertThat(TraceId.fromLowerBase16("ZZff000000000000000000000000000041BB", 2))
75         .isEqualTo(second);
76   }
77 
78   @Test
toLowerBase16()79   public void toLowerBase16() {
80     assertThat(TraceId.INVALID.toLowerBase16()).isEqualTo("00000000000000000000000000000000");
81     assertThat(first.toLowerBase16()).isEqualTo("00000000000000000000000000000061");
82     assertThat(second.toLowerBase16()).isEqualTo("ff000000000000000000000000000041");
83   }
84 
85   @Test
traceId_CompareTo()86   public void traceId_CompareTo() {
87     assertThat(first.compareTo(second)).isGreaterThan(0);
88     assertThat(second.compareTo(first)).isLessThan(0);
89     assertThat(first.compareTo(TraceId.fromBytes(firstBytes))).isEqualTo(0);
90   }
91 
92   @Test
traceId_EqualsAndHashCode()93   public void traceId_EqualsAndHashCode() {
94     EqualsTester tester = new EqualsTester();
95     tester.addEqualityGroup(TraceId.INVALID, TraceId.INVALID);
96     tester.addEqualityGroup(first, TraceId.fromBytes(Arrays.copyOf(firstBytes, firstBytes.length)));
97     tester.addEqualityGroup(
98         second, TraceId.fromBytes(Arrays.copyOf(secondBytes, secondBytes.length)));
99     tester.testEquals();
100   }
101 
102   @Test
traceId_ToString()103   public void traceId_ToString() {
104     assertThat(TraceId.INVALID.toString()).contains("00000000000000000000000000000000");
105     assertThat(first.toString()).contains("00000000000000000000000000000061");
106     assertThat(second.toString()).contains("ff000000000000000000000000000041");
107   }
108 }
109