• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, 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.metrics;
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 LabelValue}. */
28 @RunWith(JUnit4.class)
29 public class LabelValueTest {
30 
31   private static final LabelValue VALUE = LabelValue.create("value");
32   private static final LabelValue UNSET = LabelValue.create(null);
33   private static final LabelValue EMPTY = LabelValue.create("");
34 
35   @Test
testGetValue()36   public void testGetValue() {
37     assertThat(VALUE.getValue()).isEqualTo("value");
38     assertThat(UNSET.getValue()).isNull();
39     assertThat(EMPTY.getValue()).isEmpty();
40   }
41 
42   @Test
create_NoLengthConstraint()43   public void create_NoLengthConstraint() {
44     // We have a length constraint of 256-characters for TagValue. That constraint doesn't apply to
45     // LabelValue.
46     char[] chars = new char[300];
47     Arrays.fill(chars, 'v');
48     String value = new String(chars);
49     assertThat(LabelValue.create(value).getValue()).isEqualTo(value);
50   }
51 
52   @Test
create_WithUnprintableChars()53   public void create_WithUnprintableChars() {
54     String value = "\2ab\3cd";
55     assertThat(LabelValue.create(value).getValue()).isEqualTo(value);
56   }
57 
58   @Test
create_WithNonAsciiChars()59   public void create_WithNonAsciiChars() {
60     String value = "值";
61     LabelValue nonAsciiValue = LabelValue.create(value);
62     assertThat(nonAsciiValue.getValue()).isEqualTo(value);
63   }
64 
65   @Test
testLabelValueEquals()66   public void testLabelValueEquals() {
67     new EqualsTester()
68         .addEqualityGroup(LabelValue.create("foo"), LabelValue.create("foo"))
69         .addEqualityGroup(UNSET)
70         .addEqualityGroup(EMPTY)
71         .addEqualityGroup(LabelValue.create("bar"))
72         .testEquals();
73   }
74 }
75