• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 protobuf_unittest.UnittestProto.optionalInt32Extension;
34 
35 import protobuf_unittest.UnittestProto.TestAllExtensions;
36 import protobuf_unittest.UnittestProto.TestAllTypes;
37 import java.io.IOException;
38 import junit.framework.TestCase;
39 
40 /**
41  * Unit test for {@link LazyFieldLite}.
42  *
43  * @author xiangl@google.com (Xiang Li)
44  */
45 public class LazyFieldLiteTest extends TestCase {
46 
testGetValue()47   public void testGetValue() {
48     MessageLite message = TestUtil.getAllSet();
49     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
50     assertEquals(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
51     changeValue(lazyField);
52     assertNotEqual(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
53   }
54 
testGetValueEx()55   public void testGetValueEx() throws Exception {
56     TestAllExtensions message = TestUtil.getAllExtensionsSet();
57     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
58     assertEquals(message, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
59     changeValue(lazyField);
60     assertNotEqual(message, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
61   }
62 
testSetValue()63   public void testSetValue() {
64     MessageLite message = TestUtil.getAllSet();
65     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
66     changeValue(lazyField);
67     assertNotEqual(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
68     message = lazyField.getValue(TestAllTypes.getDefaultInstance());
69     changeValue(lazyField);
70     assertEquals(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
71   }
72 
testSetValueEx()73   public void testSetValueEx() throws Exception {
74     TestAllExtensions message = TestUtil.getAllExtensionsSet();
75     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
76     changeValue(lazyField);
77     assertNotEqual(message, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
78     MessageLite value = lazyField.getValue(TestAllExtensions.getDefaultInstance());
79     changeValue(lazyField);
80     assertEquals(value, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
81   }
82 
testGetSerializedSize()83   public void testGetSerializedSize() {
84     MessageLite message = TestUtil.getAllSet();
85     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
86     assertEquals(message.getSerializedSize(), lazyField.getSerializedSize());
87     changeValue(lazyField);
88     assertNotEqual(message.getSerializedSize(), lazyField.getSerializedSize());
89   }
90 
testGetSerializedSizeEx()91   public void testGetSerializedSizeEx() throws Exception {
92     TestAllExtensions message = TestUtil.getAllExtensionsSet();
93     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
94     assertEquals(message.getSerializedSize(), lazyField.getSerializedSize());
95     changeValue(lazyField);
96     assertNotEqual(message.getSerializedSize(), lazyField.getSerializedSize());
97   }
98 
testGetByteString()99   public void testGetByteString() {
100     MessageLite message = TestUtil.getAllSet();
101     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
102     assertEquals(message.toByteString(), lazyField.toByteString());
103     changeValue(lazyField);
104     assertNotEqual(message.toByteString(), lazyField.toByteString());
105   }
106 
testGetByteStringEx()107   public void testGetByteStringEx() throws Exception {
108     TestAllExtensions message = TestUtil.getAllExtensionsSet();
109     LazyFieldLite lazyField = createLazyFieldLiteFromMessage(message);
110     assertEquals(message.toByteString(), lazyField.toByteString());
111     changeValue(lazyField);
112     assertNotEqual(message.toByteString(), lazyField.toByteString());
113   }
114 
testMergeExtensions()115   public void testMergeExtensions() throws Exception {
116     TestAllExtensions message = TestUtil.getAllExtensionsSet();
117     LazyFieldLite original = createLazyFieldLiteFromMessage(message);
118     LazyFieldLite merged = new LazyFieldLite();
119     merged.merge(original);
120     TestAllExtensions value =
121         (TestAllExtensions) merged.getValue(TestAllExtensions.getDefaultInstance());
122     assertEquals(message, value);
123   }
124 
testEmptyLazyField()125   public void testEmptyLazyField() throws Exception {
126     LazyFieldLite field = new LazyFieldLite();
127     assertEquals(0, field.getSerializedSize());
128     assertEquals(ByteString.EMPTY, field.toByteString());
129   }
130 
testInvalidProto()131   public void testInvalidProto() throws Exception {
132     // Silently fails and uses the default instance.
133     LazyFieldLite field =
134         new LazyFieldLite(TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid"));
135     assertEquals(
136         TestAllTypes.getDefaultInstance(), field.getValue(TestAllTypes.getDefaultInstance()));
137     assertEquals(0, field.getSerializedSize());
138     assertEquals(ByteString.EMPTY, field.toByteString());
139   }
140 
testMergeBeforeParsing()141   public void testMergeBeforeParsing() throws Exception {
142     TestAllTypes message1 = TestAllTypes.newBuilder().setOptionalInt32(1).build();
143     LazyFieldLite field1 = createLazyFieldLiteFromMessage(message1);
144     TestAllTypes message2 = TestAllTypes.newBuilder().setOptionalInt64(2).build();
145     LazyFieldLite field2 = createLazyFieldLiteFromMessage(message2);
146 
147     field1.merge(field2);
148     TestAllTypes expected =
149         TestAllTypes.newBuilder().setOptionalInt32(1).setOptionalInt64(2).build();
150     assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
151   }
152 
testMergeOneNotParsed()153   public void testMergeOneNotParsed() throws Exception {
154     // Test a few different paths that involve one message that was not parsed.
155     TestAllTypes message1 = TestAllTypes.newBuilder().setOptionalInt32(1).build();
156     TestAllTypes message2 = TestAllTypes.newBuilder().setOptionalInt64(2).build();
157     TestAllTypes expected =
158         TestAllTypes.newBuilder().setOptionalInt32(1).setOptionalInt64(2).build();
159 
160     LazyFieldLite field1 = LazyFieldLite.fromValue(message1);
161     field1.getValue(TestAllTypes.getDefaultInstance()); // Force parsing.
162     LazyFieldLite field2 = createLazyFieldLiteFromMessage(message2);
163     field1.merge(field2);
164     assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
165 
166     // Now reverse which one is parsed first.
167     field1 = LazyFieldLite.fromValue(message1);
168     field2 = createLazyFieldLiteFromMessage(message2);
169     field2.getValue(TestAllTypes.getDefaultInstance()); // Force parsing.
170     field1.merge(field2);
171     assertEquals(expected, field1.getValue(TestAllTypes.getDefaultInstance()));
172   }
173 
testMergeInvalid()174   public void testMergeInvalid() throws Exception {
175     // Test a few different paths that involve one message that was not parsed.
176     TestAllTypes message = TestAllTypes.newBuilder().setOptionalInt32(1).build();
177     LazyFieldLite valid = LazyFieldLite.fromValue(message);
178     LazyFieldLite invalid =
179         new LazyFieldLite(TestUtil.getExtensionRegistry(), ByteString.copyFromUtf8("invalid"));
180     invalid.merge(valid);
181 
182     // We swallow the exception and just use the set field.
183     assertEquals(message, invalid.getValue(TestAllTypes.getDefaultInstance()));
184   }
185 
testMergeKeepsExtensionsWhenPossible()186   public void testMergeKeepsExtensionsWhenPossible() throws Exception {
187     // In this test we attempt to only use the empty registry, which will strip out all extensions
188     // when serializing and then parsing. We verify that each code path will attempt to not
189     // serialize and parse a message that was set directly without going through the
190     // extensionRegistry.
191     TestAllExtensions messageWithExtensions =
192         TestAllExtensions.newBuilder().setExtension(optionalInt32Extension, 42).build();
193     TestAllExtensions emptyMessage = TestAllExtensions.newBuilder().build();
194 
195     ExtensionRegistryLite emptyRegistry = ExtensionRegistryLite.getEmptyRegistry();
196 
197     LazyFieldLite field = LazyFieldLite.fromValue(messageWithExtensions);
198     field.merge(createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage));
199     assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
200 
201     // Now reverse the order of the merging.
202     field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
203     field.merge(LazyFieldLite.fromValue(messageWithExtensions));
204     assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
205 
206     // Now try parsing the empty field first.
207     field = LazyFieldLite.fromValue(messageWithExtensions);
208     LazyFieldLite other = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
209     other.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing.
210     field.merge(other);
211     assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
212 
213     // And again reverse.
214     field = createLazyFieldLiteFromMessage(emptyRegistry, emptyMessage);
215     field.getValue(TestAllExtensions.getDefaultInstance()); // Force parsing.
216     other = LazyFieldLite.fromValue(messageWithExtensions);
217     field.merge(other);
218     assertEquals(messageWithExtensions, field.getValue(TestAllExtensions.getDefaultInstance()));
219   }
220 
221 
222   // Help methods.
223 
createLazyFieldLiteFromMessage(MessageLite message)224   private LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message) {
225     return createLazyFieldLiteFromMessage(TestUtil.getExtensionRegistry(), message);
226   }
227 
createLazyFieldLiteFromMessage( ExtensionRegistryLite extensionRegistry, MessageLite message)228   private LazyFieldLite createLazyFieldLiteFromMessage(
229       ExtensionRegistryLite extensionRegistry, MessageLite message) {
230     ByteString bytes = message.toByteString();
231     return new LazyFieldLite(extensionRegistry, bytes);
232   }
233 
changeValue(LazyFieldLite lazyField)234   private void changeValue(LazyFieldLite lazyField) {
235     TestAllTypes.Builder builder = TestUtil.getAllSet().toBuilder();
236     builder.addRepeatedBool(true);
237     MessageLite newMessage = builder.build();
238     lazyField.setValue(newMessage);
239   }
240 
assertNotEqual(Object unexpected, Object actual)241   private void assertNotEqual(Object unexpected, Object actual) {
242     assertFalse(unexpected == actual || (unexpected != null && unexpected.equals(actual)));
243   }
244 
245 }
246