LazyFieldLiteTest.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 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 
45 public class LazyFieldLiteTest extends TestCase {
46 
47  public void testGetValue() {
50  assertEquals(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
51  changeValue(lazyField);
52  assertNotEqual(message, lazyField.getValue(TestAllTypes.getDefaultInstance()));
53  }
54 
55  public void testGetValueEx() throws Exception {
56  TestAllExtensions message = TestUtil.getAllExtensionsSet();
58  assertEquals(message, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
59  changeValue(lazyField);
60  assertNotEqual(message, lazyField.getValue(TestAllExtensions.getDefaultInstance()));
61  }
62 
63  public void testSetValue() {
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 
73  public void testSetValueEx() throws Exception {
74  TestAllExtensions message = TestUtil.getAllExtensionsSet();
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 
83  public void testGetSerializedSize() {
86  assertEquals(message.getSerializedSize(), lazyField.getSerializedSize());
87  changeValue(lazyField);
88  assertNotEqual(message.getSerializedSize(), lazyField.getSerializedSize());
89  }
90 
91  public void testGetSerializedSizeEx() throws Exception {
92  TestAllExtensions message = TestUtil.getAllExtensionsSet();
94  assertEquals(message.getSerializedSize(), lazyField.getSerializedSize());
95  changeValue(lazyField);
96  assertNotEqual(message.getSerializedSize(), lazyField.getSerializedSize());
97  }
98 
99  public void testGetByteString() {
102  assertEquals(message.toByteString(), lazyField.toByteString());
103  changeValue(lazyField);
104  assertNotEqual(message.toByteString(), lazyField.toByteString());
105  }
106 
107  public void testGetByteStringEx() throws Exception {
108  TestAllExtensions message = TestUtil.getAllExtensionsSet();
110  assertEquals(message.toByteString(), lazyField.toByteString());
111  changeValue(lazyField);
112  assertNotEqual(message.toByteString(), lazyField.toByteString());
113  }
114 
115  public void testMergeExtensions() throws Exception {
116  TestAllExtensions message = TestUtil.getAllExtensionsSet();
119  merged.merge(original);
120  TestAllExtensions value =
121  (TestAllExtensions) merged.getValue(TestAllExtensions.getDefaultInstance());
122  assertEquals(message, value);
123  }
124 
125  public void testEmptyLazyField() throws Exception {
127  assertEquals(0, field.getSerializedSize());
128  assertEquals(ByteString.EMPTY, field.toByteString());
129  }
130 
131  public void testInvalidProto() throws Exception {
132  // Silently fails and uses the default instance.
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 
141  public void testMergeBeforeParsing() throws Exception {
142  TestAllTypes message1 = TestAllTypes.newBuilder().setOptionalInt32(1).build();
144  TestAllTypes message2 = TestAllTypes.newBuilder().setOptionalInt64(2).build();
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 
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.
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 
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();
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 
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 
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 
226  }
227 
229  ExtensionRegistryLite extensionRegistry, MessageLite message) {
230  ByteString bytes = message.toByteString();
231  return new LazyFieldLite(extensionRegistry, bytes);
232  }
233 
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 
241  private void assertNotEqual(Object unexpected, Object actual) {
242  assertFalse(unexpected == actual || (unexpected != null && unexpected.equals(actual)));
243  }
244 
245 }
com.google.protobuf.LazyFieldLiteTest.testEmptyLazyField
void testEmptyLazyField()
Definition: LazyFieldLiteTest.java:125
com.google.protobuf.LazyFieldLiteTest
Definition: LazyFieldLiteTest.java:45
com.google.protobuf.LazyFieldLiteTest.testGetValue
void testGetValue()
Definition: LazyFieldLiteTest.java:47
com.google.protobuf.LazyFieldLiteTest.testMergeOneNotParsed
void testMergeOneNotParsed()
Definition: LazyFieldLiteTest.java:153
com.google.protobuf.LazyFieldLiteTest.testSetValueEx
void testSetValueEx()
Definition: LazyFieldLiteTest.java:73
com.google.protobuf.TestUtil.getAllSet
static TestAllTypes getAllSet()
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:278
com.google.protobuf.LazyFieldLite.toByteString
ByteString toByteString()
Definition: LazyFieldLite.java:369
merged
static bool merged(const upb_refcounted *r, const upb_refcounted *r2)
Definition: ruby/ext/google/protobuf_c/upb.c:6184
com.google.protobuf.LazyFieldLiteTest.testMergeBeforeParsing
void testMergeBeforeParsing()
Definition: LazyFieldLiteTest.java:141
com.google.protobuf.LazyFieldLiteTest.testMergeExtensions
void testMergeExtensions()
Definition: LazyFieldLiteTest.java:115
com.google.protobuf.LazyFieldLiteTest.testInvalidProto
void testInvalidProto()
Definition: LazyFieldLiteTest.java:131
com.google.protobuf.LazyFieldLite.setValue
MessageLite setValue(MessageLite value)
Definition: LazyFieldLite.java:233
com.google.protobuf.LazyFieldLiteTest.createLazyFieldLiteFromMessage
LazyFieldLite createLazyFieldLiteFromMessage(MessageLite message)
Definition: LazyFieldLiteTest.java:224
com.google.protobuf.TestUtil.getAllExtensionsSet
static TestAllExtensions getAllExtensionsSet()
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:298
com.google.protobuf.ByteString.EMPTY
static final ByteString EMPTY
Definition: ByteString.java:85
com.google.protobuf.LazyFieldLiteTest.testMergeKeepsExtensionsWhenPossible
void testMergeKeepsExtensionsWhenPossible()
Definition: LazyFieldLiteTest.java:186
com.google.protobuf.LazyFieldLiteTest.testGetByteStringEx
void testGetByteStringEx()
Definition: LazyFieldLiteTest.java:107
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
com.google.protobuf.TestUtil.getExtensionRegistry
static ExtensionRegistryLite getExtensionRegistry()
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:1184
com.google.protobuf.LazyFieldLite.fromValue
static LazyFieldLite fromValue(MessageLite value)
Definition: LazyFieldLite.java:131
com.google.protobuf.LazyFieldLite.getSerializedSize
int getSerializedSize()
Definition: LazyFieldLite.java:354
com.google.protobuf.LazyFieldLiteTest.testGetByteString
void testGetByteString()
Definition: LazyFieldLiteTest.java:99
com.google.protobuf.TestUtil
Definition: core/src/test/java/com/google/protobuf/TestUtil.java:254
com.google.protobuf.LazyFieldLite.merge
void merge(LazyFieldLite other)
Definition: LazyFieldLite.java:249
com.google.protobuf.LazyFieldLiteTest.createLazyFieldLiteFromMessage
LazyFieldLite createLazyFieldLiteFromMessage(ExtensionRegistryLite extensionRegistry, MessageLite message)
Definition: LazyFieldLiteTest.java:228
com.google.protobuf.LazyFieldLiteTest.testSetValue
void testSetValue()
Definition: LazyFieldLiteTest.java:63
com.google.protobuf.LazyFieldLiteTest.testMergeInvalid
void testMergeInvalid()
Definition: LazyFieldLiteTest.java:174
com.google.protobuf.LazyFieldLiteTest.testGetSerializedSize
void testGetSerializedSize()
Definition: LazyFieldLiteTest.java:83
com.google.protobuf.LazyFieldLiteTest.assertNotEqual
void assertNotEqual(Object unexpected, Object actual)
Definition: LazyFieldLiteTest.java:241
com.google.protobuf.LazyFieldLite.getValue
MessageLite getValue(MessageLite defaultInstance)
Definition: LazyFieldLite.java:222
com.google.protobuf.ExtensionRegistryLite
Definition: ExtensionRegistryLite.java:70
com.google.protobuf.LazyFieldLiteTest.testGetValueEx
void testGetValueEx()
Definition: LazyFieldLiteTest.java:55
field
const FieldDescriptor * field
Definition: parser_unittest.cc:2694
com.google.protobuf.LazyFieldLite
Definition: LazyFieldLite.java:56
com.google.protobuf.LazyFieldLiteTest.changeValue
void changeValue(LazyFieldLite lazyField)
Definition: LazyFieldLiteTest.java:234
com.google.protobuf.LazyFieldLiteTest.testGetSerializedSizeEx
void testGetSerializedSizeEx()
Definition: LazyFieldLiteTest.java:91
java
protobuf_unittest
Definition: map_test_util_impl.h:39
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
com.google.protobuf.ExtensionRegistryLite.getEmptyRegistry
static ExtensionRegistryLite getEmptyRegistry()
Definition: ExtensionRegistryLite.java:125
gmock_test_utils.TestCase
TestCase
Definition: gmock_test_utils.py:97
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
com.google.protobuf.MessageLite
Definition: MessageLite.java:65
com.google.protobuf.ByteString
Definition: ByteString.java:67


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