• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.junit.runners.JUnit4;
25 
26 /** Unit tests for {@link TraceOptions}. */
27 @RunWith(JUnit4.class)
28 public class TraceOptionsTest {
29   private static final byte FIRST_BYTE = (byte) 0xff;
30   private static final byte SECOND_BYTE = 1;
31   private static final byte THIRD_BYTE = 6;
32 
33   @Test
getOptions()34   public void getOptions() {
35     assertThat(TraceOptions.DEFAULT.getOptions()).isEqualTo(0);
36     assertThat(TraceOptions.builder().setIsSampled(false).build().getOptions()).isEqualTo(0);
37     assertThat(TraceOptions.builder().setIsSampled(true).build().getOptions()).isEqualTo(1);
38     assertThat(TraceOptions.builder().setIsSampled(true).setIsSampled(false).build().getOptions())
39         .isEqualTo(0);
40     assertThat(TraceOptions.fromByte(FIRST_BYTE).getOptions()).isEqualTo(-1);
41     assertThat(TraceOptions.fromByte(SECOND_BYTE).getOptions()).isEqualTo(1);
42     assertThat(TraceOptions.fromByte(THIRD_BYTE).getOptions()).isEqualTo(6);
43   }
44 
45   @Test
isSampled()46   public void isSampled() {
47     assertThat(TraceOptions.DEFAULT.isSampled()).isFalse();
48     assertThat(TraceOptions.builder().setIsSampled(true).build().isSampled()).isTrue();
49   }
50 
51   @Test
toFromByte()52   public void toFromByte() {
53     assertThat(TraceOptions.fromByte(FIRST_BYTE).getByte()).isEqualTo(FIRST_BYTE);
54     assertThat(TraceOptions.fromByte(SECOND_BYTE).getByte()).isEqualTo(SECOND_BYTE);
55     assertThat(TraceOptions.fromByte(THIRD_BYTE).getByte()).isEqualTo(THIRD_BYTE);
56   }
57 
58   @Test
toFromBase16()59   public void toFromBase16() {
60     assertThat(TraceOptions.fromLowerBase16("ff", 0).toLowerBase16()).isEqualTo("ff");
61     assertThat(TraceOptions.fromLowerBase16("01", 0).toLowerBase16()).isEqualTo("01");
62     assertThat(TraceOptions.fromLowerBase16("06", 0).toLowerBase16()).isEqualTo("06");
63   }
64 
65   @Test
66   @SuppressWarnings("deprecation")
deprecated_fromBytes()67   public void deprecated_fromBytes() {
68     assertThat(TraceOptions.fromBytes(new byte[] {FIRST_BYTE}).getByte()).isEqualTo(FIRST_BYTE);
69     assertThat(TraceOptions.fromBytes(new byte[] {1, FIRST_BYTE}, 1).getByte())
70         .isEqualTo(FIRST_BYTE);
71   }
72 
73   @Test
74   @SuppressWarnings("deprecation")
deprecated_getBytes()75   public void deprecated_getBytes() {
76     assertThat(TraceOptions.fromByte(FIRST_BYTE).getBytes()).isEqualTo(new byte[] {FIRST_BYTE});
77   }
78 
79   @Test
builder_FromOptions()80   public void builder_FromOptions() {
81     assertThat(
82             TraceOptions.builder(TraceOptions.fromByte(THIRD_BYTE))
83                 .setIsSampled(true)
84                 .build()
85                 .getOptions())
86         .isEqualTo(6 | 1);
87   }
88 
89   @Test
traceOptions_EqualsAndHashCode()90   public void traceOptions_EqualsAndHashCode() {
91     EqualsTester tester = new EqualsTester();
92     tester.addEqualityGroup(TraceOptions.DEFAULT);
93     tester.addEqualityGroup(
94         TraceOptions.fromByte(SECOND_BYTE), TraceOptions.builder().setIsSampled(true).build());
95     tester.addEqualityGroup(TraceOptions.fromByte(FIRST_BYTE));
96     tester.testEquals();
97   }
98 
99   @Test
traceOptions_ToString()100   public void traceOptions_ToString() {
101     assertThat(TraceOptions.DEFAULT.toString()).contains("sampled=false");
102     assertThat(TraceOptions.builder().setIsSampled(true).build().toString())
103         .contains("sampled=true");
104   }
105 }
106