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 any_test.AnyTestProto.TestAny;
34 import protobuf_unittest.UnittestProto.TestAllTypes;
35 import java.util.Objects;
36 import junit.framework.TestCase;
37 
39 public class AnyTest extends TestCase {
40  public void testAnyGeneratedApi() throws Exception {
41  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
42  TestUtil.setAllFields(builder);
43  TestAllTypes message = builder.build();
44 
45  TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build();
46 
47  assertTrue(container.getValue().is(TestAllTypes.class));
48  assertFalse(container.getValue().is(TestAny.class));
49 
50  TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
52 
53 
54  // Unpacking to a wrong type will throw an exception.
55  try {
56  container.getValue().unpack(TestAny.class);
57  fail("Exception is expected.");
58  } catch (InvalidProtocolBufferException e) {
59  // expected.
60  }
61 
62  // Test that unpacking throws an exception if parsing fails.
63  TestAny.Builder containerBuilder = container.toBuilder();
64  containerBuilder.getValueBuilder().setValue(ByteString.copyFrom(new byte[] {0x11}));
65  container = containerBuilder.build();
66  try {
67  container.getValue().unpack(TestAllTypes.class);
68  fail("Exception is expected.");
69  } catch (InvalidProtocolBufferException e) {
70  // expected.
71  }
72  }
73 
74  public void testCustomTypeUrls() throws Exception {
75  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
76  TestUtil.setAllFields(builder);
77  TestAllTypes message = builder.build();
78 
79  TestAny container = TestAny.newBuilder().setValue(Any.pack(message, "xxx.com")).build();
80 
81  assertEquals(
82  "xxx.com/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl());
83 
84  assertTrue(container.getValue().is(TestAllTypes.class));
85  assertFalse(container.getValue().is(TestAny.class));
86 
87  TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
89 
90  container = TestAny.newBuilder().setValue(Any.pack(message, "yyy.com/")).build();
91 
92  assertEquals(
93  "yyy.com/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl());
94 
95  assertTrue(container.getValue().is(TestAllTypes.class));
96  assertFalse(container.getValue().is(TestAny.class));
97 
98  result = container.getValue().unpack(TestAllTypes.class);
100 
101  container = TestAny.newBuilder().setValue(Any.pack(message, "")).build();
102 
103  assertEquals(
104  "/" + TestAllTypes.getDescriptor().getFullName(), container.getValue().getTypeUrl());
105 
106  assertTrue(container.getValue().is(TestAllTypes.class));
107  assertFalse(container.getValue().is(TestAny.class));
108 
109  result = container.getValue().unpack(TestAllTypes.class);
111  }
112 
113  public void testCachedUnpackResult() throws Exception {
114  TestAllTypes.Builder builder = TestAllTypes.newBuilder();
115  TestUtil.setAllFields(builder);
116  TestAllTypes message = builder.build();
117 
118  TestAny container = TestAny.newBuilder().setValue(Any.pack(message)).build();
119 
120  assertTrue(container.getValue().is(TestAllTypes.class));
121 
122  TestAllTypes result1 = container.getValue().unpack(TestAllTypes.class);
123  TestAllTypes result2 = container.getValue().unpack(TestAllTypes.class);
124  assertTrue(Objects.equals(result1, result2));
125  }
126 }
com.google.protobuf.AnyTest
Definition: AnyTest.java:39
com.google.protobuf.AnyTest.testCustomTypeUrls
void testCustomTypeUrls()
Definition: AnyTest.java:74
com.google.protobuf.TestUtil
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:254
java
com.google.protobuf.AnyTest.testCachedUnpackResult
void testCachedUnpackResult()
Definition: AnyTest.java:113
protobuf_unittest
Definition: map_test_util_impl.h:39
com.google.protobuf.AnyTest.testAnyGeneratedApi
void testAnyGeneratedApi()
Definition: AnyTest.java:40
com.google.protobuf.TestUtil.setAllFields
static void setAllFields(TestAllTypes.Builder message)
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:323
com.google.protobuf.InvalidProtocolBufferException
Definition: InvalidProtocolBufferException.java:41
com.google.protobuf.TestUtil.assertAllFieldsSet
static void assertAllFieldsSet(TestAllTypesOrBuilder message)
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:489
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
Any
Definition: any.pb.h:70
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
com.google.protobuf.ByteString
Definition: ByteString.java:67


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:47