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
59   @SuppressWarnings("deprecation")
deprecated_fromBytes()60   public void deprecated_fromBytes() {
61     assertThat(TraceOptions.fromBytes(new byte[] {FIRST_BYTE}).getByte()).isEqualTo(FIRST_BYTE);
62     assertThat(TraceOptions.fromBytes(new byte[] {1, FIRST_BYTE}, 1).getByte())
63         .isEqualTo(FIRST_BYTE);
64   }
65 
66   @Test
67   @SuppressWarnings("deprecation")
deprecated_getBytes()68   public void deprecated_getBytes() {
69     assertThat(TraceOptions.fromByte(FIRST_BYTE).getBytes()).isEqualTo(new byte[] {FIRST_BYTE});
70   }
71 
72   @Test
builder_FromOptions()73   public void builder_FromOptions() {
74     assertThat(
75             TraceOptions.builder(TraceOptions.fromByte(THIRD_BYTE))
76                 .setIsSampled(true)
77                 .build()
78                 .getOptions())
79         .isEqualTo(6 | 1);
80   }
81 
82   @Test
traceOptions_EqualsAndHashCode()83   public void traceOptions_EqualsAndHashCode() {
84     EqualsTester tester = new EqualsTester();
85     tester.addEqualityGroup(TraceOptions.DEFAULT);
86     tester.addEqualityGroup(
87         TraceOptions.fromByte(SECOND_BYTE), TraceOptions.builder().setIsSampled(true).build());
88     tester.addEqualityGroup(TraceOptions.fromByte(FIRST_BYTE));
89     tester.testEquals();
90   }
91 
92   @Test
traceOptions_ToString()93   public void traceOptions_ToString() {
94     assertThat(TraceOptions.DEFAULT.toString()).contains("sampled=false");
95     assertThat(TraceOptions.builder().setIsSampled(true).build().toString())
96         .contains("sampled=true");
97   }
98 }
99