1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 package com.google.protobuf; 9 10 import static com.google.common.truth.Truth.assertThat; 11 import static org.junit.Assert.assertThrows; 12 13 import com.google.protobuf.Descriptors.Descriptor; 14 import protobuf_unittest.UnittestProto; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 @RunWith(JUnit4.class) 20 public final class TypeRegistryTest { 21 22 @Test getDescriptorForTypeUrl_throwsExceptionForUnknownTypes()23 public void getDescriptorForTypeUrl_throwsExceptionForUnknownTypes() throws Exception { 24 assertThrows( 25 InvalidProtocolBufferException.class, 26 () -> TypeRegistry.getEmptyTypeRegistry().getDescriptorForTypeUrl("UnknownType")); 27 assertThrows( 28 InvalidProtocolBufferException.class, 29 () -> TypeRegistry.getEmptyTypeRegistry().getDescriptorForTypeUrl("///")); 30 } 31 32 @Test findDescriptorByFullName()33 public void findDescriptorByFullName() throws Exception { 34 Descriptor descriptor = UnittestProto.TestAllTypes.getDescriptor(); 35 assertThat(TypeRegistry.getEmptyTypeRegistry().find(descriptor.getFullName())).isNull(); 36 37 assertThat(TypeRegistry.newBuilder().add(descriptor).build().find(descriptor.getFullName())) 38 .isSameInstanceAs(descriptor); 39 } 40 41 @Test findDescriptorByTypeUrl()42 public void findDescriptorByTypeUrl() throws Exception { 43 Descriptor descriptor = UnittestProto.TestAllTypes.getDescriptor(); 44 assertThat( 45 TypeRegistry.getEmptyTypeRegistry() 46 .getDescriptorForTypeUrl("type.googleapis.com/" + descriptor.getFullName())) 47 .isNull(); 48 49 assertThat( 50 TypeRegistry.newBuilder() 51 .add(descriptor) 52 .build() 53 .getDescriptorForTypeUrl("type.googleapis.com/" + descriptor.getFullName())) 54 .isSameInstanceAs(descriptor); 55 } 56 } 57