protobuf/java/core/src/test/java/com/google/protobuf/AnyTest.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static com.google.common.truth.Truth.assertThat;
34 import static com.google.common.truth.Truth.assertWithMessage;
35 
36 import any_test.AnyTestProto.TestAny;
37 import protobuf_unittest.UnittestProto.TestAllTypes;
38 import java.util.Objects;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.junit.runners.JUnit4;
42 
44 @RunWith(JUnit4.class)
45 public class AnyTest {
46 
47  @Test
48  public void testAnyGeneratedApi() throws Exception {
49  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
51  TestAllTypes message = builder.build();
52 
53  TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build();
54 
55  assertThat(container.getValue().is(TestAllTypes.class)).isTrue();
56  assertThat(container.getValue().is(TestAny.class)).isFalse();
57 
58  TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
60 
61 
62  // Unpacking to a wrong type will throw an exception.
63  try {
64  container.getValue().unpack(TestAny.class);
65  assertWithMessage("Exception is expected.").fail();
66  } catch (InvalidProtocolBufferException e) {
67  // expected.
68  }
69 
70  // Test that unpacking throws an exception if parsing fails.
71  TestAny.Builder containerBuilder = container.toBuilder();
72  containerBuilder.getValueBuilder().setValue(ByteString.copyFrom(new byte[] {0x11}));
73  container = containerBuilder.build();
74  try {
75  container.getValue().unpack(TestAllTypes.class);
76  assertWithMessage("Exception is expected.").fail();
77  } catch (InvalidProtocolBufferException e) {
78  // expected.
79  }
80  }
81 
82  @Test
83  public void testCustomTypeUrls() throws Exception {
84  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
86  TestAllTypes message = builder.build();
87 
88  TestAny container = TestAny.newBuilder().setValue(Any.pack(message, "xxx.com")).build();
89 
90  assertThat(container.getValue().getTypeUrl())
91  .isEqualTo("xxx.com/" + TestAllTypes.getDescriptor().getFullName());
92 
93  assertThat(container.getValue().is(TestAllTypes.class)).isTrue();
94  assertThat(container.getValue().is(TestAny.class)).isFalse();
95 
96  TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
98 
99  container = TestAny.newBuilder().setValue(Any.pack(message, "yyy.com/")).build();
100 
101  assertThat(container.getValue().getTypeUrl())
102  .isEqualTo("yyy.com/" + TestAllTypes.getDescriptor().getFullName());
103 
104  assertThat(container.getValue().is(TestAllTypes.class)).isTrue();
105  assertThat(container.getValue().is(TestAny.class)).isFalse();
106 
107  result = container.getValue().unpack(TestAllTypes.class);
109 
110  container = TestAny.newBuilder().setValue(Any.pack(message, "")).build();
111 
112  assertThat(container.getValue().getTypeUrl())
113  .isEqualTo("/" + TestAllTypes.getDescriptor().getFullName());
114 
115  assertThat(container.getValue().is(TestAllTypes.class)).isTrue();
116  assertThat(container.getValue().is(TestAny.class)).isFalse();
117 
118  result = container.getValue().unpack(TestAllTypes.class);
120  }
121 
122  @Test
123  public void testCachedUnpackResult() throws Exception {
124  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
126  TestAllTypes message = builder.build();
127 
128  TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build();
129 
130  assertThat(container.getValue().is(TestAllTypes.class)).isTrue();
131 
132  TestAllTypes result1 = container.getValue().unpack(TestAllTypes.class);
133  TestAllTypes result2 = container.getValue().unpack(TestAllTypes.class);
134  assertThat(Objects.equals(result1, result2)).isTrue();
135  }
136 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
Test
void Test(StringPiece pattern, const RE2::Options &options, StringPiece text)
Definition: bloaty/third_party/re2/re2/fuzzing/re2_fuzzer.cc:20
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
com.google.protobuf.AnyTest.testCustomTypeUrls
void testCustomTypeUrls()
Definition: protobuf/java/core/src/test/java/com/google/protobuf/AnyTest.java:83
com.google.protobuf.TestUtil
Definition: bloaty/third_party/protobuf/java/core/src/test/java/com/google/protobuf/TestUtil.java:254
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
java
com.google.protobuf.AnyTest.testCachedUnpackResult
void testCachedUnpackResult()
Definition: protobuf/java/core/src/test/java/com/google/protobuf/AnyTest.java:123
protobuf_unittest
Definition: bloaty/third_party/protobuf/src/google/protobuf/map_test_util_impl.h:39
com.google
com
com.google.protobuf.AnyTest.testAnyGeneratedApi
void testAnyGeneratedApi()
Definition: protobuf/java/core/src/test/java/com/google/protobuf/AnyTest.java:48
com.google.protobuf.TestUtil.setAllFields
static void setAllFields(TestAllTypes.Builder message)
Definition: bloaty/third_party/protobuf/java/core/src/test/java/com/google/protobuf/TestUtil.java:323
com.google.protobuf.InvalidProtocolBufferException
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/InvalidProtocolBufferException.java:41
com.google.protobuf.TestUtil.assertAllFieldsSet
static void assertAllFieldsSet(TestAllTypesOrBuilder message)
Definition: bloaty/third_party/protobuf/java/core/src/test/java/com/google/protobuf/TestUtil.java:489
Any
Definition: bloaty/third_party/protobuf/src/google/protobuf/any.pb.h:70
Test
Definition: hpack_parser_test.cc:43
container
static struct async_container * container
Definition: benchmark-million-async.c:33
com.google.protobuf.ByteString
Definition: bloaty/third_party/protobuf/java/core/src/main/java/com/google/protobuf/ByteString.java:67


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:41