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