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 
36 import junit.framework.TestCase;
37 
38 /**
39  * Unit tests for Any message.
40  */
41 public class AnyTest extends TestCase {
testAnyGeneratedApi()42   public void testAnyGeneratedApi() throws Exception {
43     TestAllTypes.Builder builder = TestAllTypes.newBuilder();
44     TestUtil.setAllFields(builder);
45     TestAllTypes message = builder.build();
46 
47     TestAny container = TestAny.newBuilder()
48         .setValue(Any.pack(message)).build();
49 
50     assertTrue(container.getValue().is(TestAllTypes.class));
51     assertFalse(container.getValue().is(TestAny.class));
52 
53     TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
54     TestUtil.assertAllFieldsSet(result);
55 
56 
57     // Unpacking to a wrong type will throw an exception.
58     try {
59       TestAny wrongMessage = container.getValue().unpack(TestAny.class);
60       fail("Exception is expected.");
61     } catch (InvalidProtocolBufferException e) {
62       // expected.
63     }
64 
65     // Test that unpacking throws an exception if parsing fails.
66     TestAny.Builder containerBuilder = container.toBuilder();
67     containerBuilder.getValueBuilder().setValue(
68         ByteString.copyFrom(new byte[]{0x11}));
69     container = containerBuilder.build();
70     try {
71       TestAllTypes parsingFailed = container.getValue().unpack(TestAllTypes.class);
72       fail("Exception is expected.");
73     } catch (InvalidProtocolBufferException e) {
74       // expected.
75     }
76   }
77 
testCustomTypeUrls()78   public void testCustomTypeUrls() throws Exception {
79     TestAllTypes.Builder builder = TestAllTypes.newBuilder();
80     TestUtil.setAllFields(builder);
81     TestAllTypes message = builder.build();
82 
83     TestAny container = TestAny.newBuilder()
84         .setValue(Any.pack(message, "xxx.com")).build();
85 
86     assertEquals(
87         "xxx.com/" + TestAllTypes.getDescriptor().getFullName(),
88         container.getValue().getTypeUrl());
89 
90     assertTrue(container.getValue().is(TestAllTypes.class));
91     assertFalse(container.getValue().is(TestAny.class));
92 
93     TestAllTypes result = container.getValue().unpack(TestAllTypes.class);
94     TestUtil.assertAllFieldsSet(result);
95 
96     container = TestAny.newBuilder()
97         .setValue(Any.pack(message, "yyy.com/")).build();
98 
99     assertEquals(
100         "yyy.com/" + TestAllTypes.getDescriptor().getFullName(),
101         container.getValue().getTypeUrl());
102 
103     assertTrue(container.getValue().is(TestAllTypes.class));
104     assertFalse(container.getValue().is(TestAny.class));
105 
106     result = container.getValue().unpack(TestAllTypes.class);
107     TestUtil.assertAllFieldsSet(result);
108 
109     container = TestAny.newBuilder()
110         .setValue(Any.pack(message, "")).build();
111 
112     assertEquals(
113         "/" + TestAllTypes.getDescriptor().getFullName(),
114         container.getValue().getTypeUrl());
115 
116     assertTrue(container.getValue().is(TestAllTypes.class));
117     assertFalse(container.getValue().is(TestAny.class));
118 
119     result = container.getValue().unpack(TestAllTypes.class);
120     TestUtil.assertAllFieldsSet(result);
121   }
122 
testCachedUnpackResult()123   public void testCachedUnpackResult() throws Exception {
124     TestAllTypes.Builder builder = TestAllTypes.newBuilder();
125     TestUtil.setAllFields(builder);
126     TestAllTypes message = builder.build();
127 
128     TestAny container = TestAny.newBuilder()
129         .setValue(Any.pack(message)).build();
130 
131     assertTrue(container.getValue().is(TestAllTypes.class));
132 
133     TestAllTypes result1 = container.getValue().unpack(TestAllTypes.class);
134     TestAllTypes result2 = container.getValue().unpack(TestAllTypes.class);
135     assertTrue(result1 == result2);
136   }
137 }
138