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 com.google.common.truth.Truth.assertWithMessage; 12 13 import any_test.AnyTestProto.TestAny; 14 import protobuf_unittest.UnittestProto.TestAllTypes; 15 import java.util.Objects; 16 import org.junit.Test; 17 import org.junit.runner.RunWith; 18 import org.junit.runners.JUnit4; 19 20 /** Unit tests for Any message. */ 21 @RunWith(JUnit4.class) 22 public class AnyTest { 23 @Test testAnyGeneratedApi()24 public void testAnyGeneratedApi() throws Exception { 25 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 26 TestUtil.setAllFields(builder); 27 TestAllTypes message = builder.build(); 28 29 TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); 30 31 assertThat(container.getValue().is(TestAllTypes.class)).isTrue(); 32 assertThat(container.getValue().is(TestAny.class)).isFalse(); 33 34 TestAllTypes result = container.getValue().unpack(TestAllTypes.class); 35 TestUtil.assertAllFieldsSet(result); 36 37 // Unpacking to a wrong type will throw an exception. 38 try { 39 container.getValue().unpack(TestAny.class); 40 assertWithMessage("Exception is expected.").fail(); 41 } catch (InvalidProtocolBufferException e) { 42 // expected. 43 } 44 45 // Test that unpacking throws an exception if parsing fails. 46 TestAny.Builder containerBuilder = container.toBuilder(); 47 containerBuilder.getValueBuilder().setValue(ByteString.copyFrom(new byte[] {0x11})); 48 container = containerBuilder.build(); 49 try { 50 container.getValue().unpack(TestAllTypes.class); 51 assertWithMessage("Exception is expected.").fail(); 52 } catch (InvalidProtocolBufferException e) { 53 // expected. 54 } 55 } 56 57 @Test testAnyGeneratedExemplarApi()58 public void testAnyGeneratedExemplarApi() throws Exception { 59 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 60 TestUtil.setAllFields(builder); 61 TestAllTypes message = builder.build(); 62 63 TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); 64 65 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 66 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 67 68 TestAllTypes result = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 69 TestUtil.assertAllFieldsSet(result); 70 71 // Unpacking to a wrong exemplar will throw an exception. 72 try { 73 container.getValue().unpackSameTypeAs(TestAny.getDefaultInstance()); 74 assertWithMessage("Exception is expected.").fail(); 75 } catch (InvalidProtocolBufferException e) { 76 // expected. 77 } 78 79 // Test that unpacking throws an exception if parsing fails. 80 TestAny.Builder containerBuilder = container.toBuilder(); 81 containerBuilder.getValueBuilder().setValue(ByteString.copyFrom(new byte[] {0x11})); 82 container = containerBuilder.build(); 83 try { 84 container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 85 assertWithMessage("Exception is expected.").fail(); 86 } catch (InvalidProtocolBufferException e) { 87 // expected. 88 } 89 } 90 91 @Test testCustomTypeUrls()92 public void testCustomTypeUrls() throws Exception { 93 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 94 TestUtil.setAllFields(builder); 95 TestAllTypes message = builder.build(); 96 97 TestAny container = TestAny.newBuilder().setValue(Any.pack(message, "xxx.com")).build(); 98 99 assertThat(container.getValue().getTypeUrl()) 100 .isEqualTo("xxx.com/" + TestAllTypes.getDescriptor().getFullName()); 101 102 assertThat(container.getValue().is(TestAllTypes.class)).isTrue(); 103 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 104 assertThat(container.getValue().is(TestAny.class)).isFalse(); 105 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 106 107 TestAllTypes result = container.getValue().unpack(TestAllTypes.class); 108 TestUtil.assertAllFieldsSet(result); 109 110 container = TestAny.newBuilder().setValue(Any.pack(message, "yyy.com/")).build(); 111 112 assertThat(container.getValue().getTypeUrl()) 113 .isEqualTo("yyy.com/" + TestAllTypes.getDescriptor().getFullName()); 114 115 assertThat(container.getValue().is(TestAllTypes.class)).isTrue(); 116 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 117 assertThat(container.getValue().is(TestAny.class)).isFalse(); 118 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 119 120 result = container.getValue().unpack(TestAllTypes.class); 121 TestUtil.assertAllFieldsSet(result); 122 123 container = TestAny.newBuilder().setValue(Any.pack(message, "")).build(); 124 125 assertThat(container.getValue().getTypeUrl()) 126 .isEqualTo("/" + TestAllTypes.getDescriptor().getFullName()); 127 128 assertThat(container.getValue().is(TestAllTypes.class)).isTrue(); 129 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 130 assertThat(container.getValue().is(TestAny.class)).isFalse(); 131 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 132 133 result = container.getValue().unpack(TestAllTypes.class); 134 TestUtil.assertAllFieldsSet(result); 135 } 136 137 @Test testCustomTypeUrlsWithExemplars()138 public void testCustomTypeUrlsWithExemplars() throws Exception { 139 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 140 TestUtil.setAllFields(builder); 141 TestAllTypes message = builder.build(); 142 143 TestAny container = TestAny.newBuilder().setValue(Any.pack(message, "xxx.com")).build(); 144 145 assertThat(container.getValue().getTypeUrl()) 146 .isEqualTo("xxx.com/" + TestAllTypes.getDescriptor().getFullName()); 147 148 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 149 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 150 151 TestAllTypes result = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 152 TestUtil.assertAllFieldsSet(result); 153 154 container = TestAny.newBuilder().setValue(Any.pack(message, "yyy.com/")).build(); 155 156 assertThat(container.getValue().getTypeUrl()) 157 .isEqualTo("yyy.com/" + TestAllTypes.getDescriptor().getFullName()); 158 159 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 160 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 161 162 result = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 163 TestUtil.assertAllFieldsSet(result); 164 165 container = TestAny.newBuilder().setValue(Any.pack(message, "")).build(); 166 167 assertThat(container.getValue().getTypeUrl()) 168 .isEqualTo("/" + TestAllTypes.getDescriptor().getFullName()); 169 170 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 171 assertThat(container.getValue().isSameTypeAs(TestAny.getDefaultInstance())).isFalse(); 172 173 result = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 174 TestUtil.assertAllFieldsSet(result); 175 } 176 177 @Test testCachedUnpackResult()178 public void testCachedUnpackResult() throws Exception { 179 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 180 TestUtil.setAllFields(builder); 181 TestAllTypes message = builder.build(); 182 183 TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); 184 185 assertThat(container.getValue().is(TestAllTypes.class)).isTrue(); 186 187 TestAllTypes result1 = container.getValue().unpack(TestAllTypes.class); 188 TestAllTypes result2 = container.getValue().unpack(TestAllTypes.class); 189 assertThat(Objects.equals(result1, result2)).isTrue(); 190 } 191 192 @Test testCachedUnpackExemplarResult()193 public void testCachedUnpackExemplarResult() throws Exception { 194 TestAllTypes.Builder builder = TestAllTypes.newBuilder(); 195 TestUtil.setAllFields(builder); 196 TestAllTypes message = builder.build(); 197 198 TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build(); 199 200 assertThat(container.getValue().isSameTypeAs(TestAllTypes.getDefaultInstance())).isTrue(); 201 202 TestAllTypes result1 = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 203 TestAllTypes result2 = container.getValue().unpackSameTypeAs(TestAllTypes.getDefaultInstance()); 204 assertThat(Objects.equals(result1, result2)).isTrue(); 205 } 206 } 207