• 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 LabelKey}. */
28 @RunWith(JUnit4.class)
29 public class LabelKeyTest {
30 
31   private static final LabelKey KEY = LabelKey.create("key", "description");
32 
33   @Test
testGetKey()34   public void testGetKey() {
35     assertThat(KEY.getKey()).isEqualTo("key");
36   }
37 
38   @Test
testGetDescription()39   public void testGetDescription() {
40     assertThat(KEY.getDescription()).isEqualTo("description");
41   }
42 
43   @Test
create_NoLengthConstraint()44   public void create_NoLengthConstraint() {
45     // We have a length constraint of 256-characters for TagKey. That constraint doesn't apply to
46     // LabelKey.
47     char[] chars = new char[300];
48     Arrays.fill(chars, 'k');
49     String key = new String(chars);
50     assertThat(LabelKey.create(key, "").getKey()).isEqualTo(key);
51   }
52 
53   @Test
create_WithUnprintableChars()54   public void create_WithUnprintableChars() {
55     String key = "\2ab\3cd";
56     String description = "\4ef\5gh";
57     LabelKey labelKey = LabelKey.create(key, description);
58     assertThat(labelKey.getKey()).isEqualTo(key);
59     assertThat(labelKey.getDescription()).isEqualTo(description);
60   }
61 
62   @Test
create_WithNonAsciiChars()63   public void create_WithNonAsciiChars() {
64     String key = "键";
65     String description = "测试用键";
66     LabelKey nonAsciiKey = LabelKey.create(key, description);
67     assertThat(nonAsciiKey.getKey()).isEqualTo(key);
68     assertThat(nonAsciiKey.getDescription()).isEqualTo(description);
69   }
70 
71   @Test
create_Empty()72   public void create_Empty() {
73     LabelKey emptyKey = LabelKey.create("", "");
74     assertThat(emptyKey.getKey()).isEmpty();
75     assertThat(emptyKey.getDescription()).isEmpty();
76   }
77 
78   @Test
testLabelKeyEquals()79   public void testLabelKeyEquals() {
80     new EqualsTester()
81         .addEqualityGroup(LabelKey.create("foo", ""), LabelKey.create("foo", ""))
82         .addEqualityGroup(LabelKey.create("foo", "description"))
83         .addEqualityGroup(LabelKey.create("bar", ""))
84         .testEquals();
85   }
86 }
87