1 /* 2 * Copyright 2016 The gRPC 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.grpc; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNotSame; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertSame; 24 25 import org.junit.Test; 26 import org.junit.runner.RunWith; 27 import org.junit.runners.JUnit4; 28 29 /** Unit tests for {@link Attributes}. */ 30 @RunWith(JUnit4.class) 31 public class AttributesTest { 32 private static final Attributes.Key<String> YOLO_KEY = Attributes.Key.create("yolo"); 33 34 @Test buildAttributes()35 public void buildAttributes() { 36 Attributes attrs = Attributes.newBuilder().set(YOLO_KEY, "To be, or not to be?").build(); 37 assertSame("To be, or not to be?", attrs.get(YOLO_KEY)); 38 assertThat(attrs.keysForTest()).hasSize(1); 39 } 40 41 @Test duplicates()42 public void duplicates() { 43 Attributes attrs = Attributes.newBuilder() 44 .set(YOLO_KEY, "To be?") 45 .set(YOLO_KEY, "Or not to be?") 46 .set(Attributes.Key.create("yolo"), "I'm not a duplicate") 47 .build(); 48 assertThat(attrs.get(YOLO_KEY)).isEqualTo("Or not to be?"); 49 assertThat(attrs.keysForTest()).hasSize(2); 50 } 51 52 @Test toBuilder()53 public void toBuilder() { 54 Attributes attrs = Attributes.newBuilder() 55 .set(YOLO_KEY, "To be?") 56 .build() 57 .toBuilder() 58 .set(YOLO_KEY, "Or not to be?") 59 .set(Attributes.Key.create("yolo"), "I'm not a duplicate") 60 .build(); 61 assertThat(attrs.get(YOLO_KEY)).isEqualTo("Or not to be?"); 62 assertThat(attrs.keysForTest()).hasSize(2); 63 } 64 65 @Test empty()66 public void empty() { 67 assertThat(Attributes.EMPTY.keysForTest()).isEmpty(); 68 } 69 70 @Test valueEquality()71 public void valueEquality() { 72 class EqualObject { 73 @Override public boolean equals(Object o) { 74 return o instanceof EqualObject; 75 } 76 77 @Override public int hashCode() { 78 return 42; 79 } 80 } 81 82 Attributes.Key<EqualObject> key = Attributes.Key.create("ints"); 83 EqualObject v1 = new EqualObject(); 84 EqualObject v2 = new EqualObject(); 85 86 assertNotSame(v1, v2); 87 assertEquals(v1, v2); 88 89 Attributes attr1 = Attributes.newBuilder().set(key, v1).build(); 90 Attributes attr2 = Attributes.newBuilder().set(key, v2).build(); 91 92 assertEquals(attr1, attr2); 93 assertEquals(attr1.hashCode(), attr2.hashCode()); 94 } 95 96 @Test discard_baseAttributes()97 public void discard_baseAttributes() { 98 Attributes attrs = Attributes.newBuilder().set(YOLO_KEY, "value").build(); 99 100 Attributes newAttrs = attrs.toBuilder().discard(YOLO_KEY).build(); 101 assertNull(newAttrs.get(YOLO_KEY)); 102 assertThat(newAttrs.keysForTest()).doesNotContain(YOLO_KEY); 103 } 104 105 @Test discard_noBase()106 public void discard_noBase() { 107 Attributes.Builder attrs = Attributes.newBuilder().set(YOLO_KEY, "value"); 108 109 Attributes newAttrs = attrs.discard(YOLO_KEY).build(); 110 assertNull(newAttrs.get(YOLO_KEY)); 111 assertThat(newAttrs.keysForTest()).doesNotContain(YOLO_KEY); 112 } 113 114 @Test discard_baseAttributesAndBuilder()115 public void discard_baseAttributesAndBuilder() { 116 Attributes attrs = Attributes.newBuilder().set(YOLO_KEY, "value").build(); 117 118 Attributes.Builder attrsBuilder = attrs.toBuilder().set(YOLO_KEY, "other value"); 119 Attributes newAttrs = attrsBuilder.discard(YOLO_KEY).build(); 120 assertNull(newAttrs.get(YOLO_KEY)); 121 assertThat(newAttrs.keysForTest()).doesNotContain(YOLO_KEY); 122 } 123 124 @Test discard_empty()125 public void discard_empty() { 126 Attributes newAttrs = Attributes.EMPTY.toBuilder().discard(YOLO_KEY).build(); 127 128 assertNull(newAttrs.get(YOLO_KEY)); 129 assertThat(newAttrs.keysForTest()).doesNotContain(YOLO_KEY); 130 } 131 } 132