• 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 protobuf_unittest.LazyFieldsLite.LazyExtension;
34 import protobuf_unittest.LazyFieldsLite.LazyInnerMessageLite;
35 import protobuf_unittest.LazyFieldsLite.LazyMessageLite;
36 import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite;
37 
38 import junit.framework.TestCase;
39 
40 import java.util.ArrayList;
41 
42 /**
43  * Unit test for messages with lazy fields.
44  *
45  * @author niwasaki@google.com (Naoki Iwasaki)
46  */
47 public class LazyMessageLiteTest extends TestCase {
48 
49   private Parser<LazyInnerMessageLite> originalLazyInnerMessageLiteParser;
50 
51   @Override
setUp()52   protected void setUp() throws Exception {
53     super.setUp();
54   }
55 
56   @Override
tearDown()57   protected void tearDown() throws Exception {
58     super.tearDown();
59   }
60 
testSetValues()61   public void testSetValues() {
62     LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder()
63         .setNum(3)
64         .build();
65     LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder()
66         .setNum(2)
67         .setNested(nested)
68         .build();
69     LazyMessageLite outer = LazyMessageLite.newBuilder()
70         .setNum(1)
71         .setInner(inner)
72         .setOneofNum(123)
73         .setOneofInner(inner)
74         .build();
75 
76     assertEquals(1, outer.getNum());
77     assertEquals(421, outer.getNumWithDefault());
78 
79     assertEquals(2, outer.getInner().getNum());
80     assertEquals(42, outer.getInner().getNumWithDefault());
81 
82     assertEquals(3, outer.getInner().getNested().getNum());
83     assertEquals(4, outer.getInner().getNested().getNumWithDefault());
84 
85     assertFalse(outer.hasOneofNum());
86     assertTrue(outer.hasOneofInner());
87 
88     assertEquals(2, outer.getOneofInner().getNum());
89     assertEquals(42, outer.getOneofInner().getNumWithDefault());
90     assertEquals(3, outer.getOneofInner().getNested().getNum());
91     assertEquals(4, outer.getOneofInner().getNested().getNumWithDefault());
92   }
93 
testSetRepeatedValues()94   public void testSetRepeatedValues() {
95     LazyMessageLite outer = LazyMessageLite.newBuilder()
96         .setNum(1)
97         .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119))
98         .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122))
99         .build();
100 
101     assertEquals(1, outer.getNum());
102     assertEquals(2, outer.getRepeatedInnerCount());
103     assertEquals(119, outer.getRepeatedInner(0).getNum());
104     assertEquals(122, outer.getRepeatedInner(1).getNum());
105   }
106 
testAddAll()107   public void testAddAll() {
108     ArrayList<LazyInnerMessageLite> inners = new ArrayList<LazyInnerMessageLite>();
109     int count = 4;
110     for (int i = 0; i < count; i++) {
111       LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder()
112           .setNum(i)
113           .build();
114       inners.add(inner);
115     }
116 
117     LazyMessageLite outer = LazyMessageLite.newBuilder()
118         .addAllRepeatedInner(inners)
119         .build();
120     assertEquals(count, outer.getRepeatedInnerCount());
121     for (int i = 0; i < count; i++) {
122       assertEquals(i, outer.getRepeatedInner(i).getNum());
123     }
124   }
125 
testGetDefaultValues()126   public void testGetDefaultValues() {
127     LazyMessageLite outer = LazyMessageLite.newBuilder()
128         .build();
129 
130     assertEquals(0, outer.getNum());
131     assertEquals(421, outer.getNumWithDefault());
132 
133     assertEquals(0, outer.getInner().getNum());
134     assertEquals(42, outer.getInner().getNumWithDefault());
135 
136     assertEquals(0, outer.getInner().getNested().getNum());
137     assertEquals(4, outer.getInner().getNested().getNumWithDefault());
138 
139     assertEquals(0, outer.getOneofNum());
140 
141     assertEquals(0, outer.getOneofInner().getNum());
142     assertEquals(42, outer.getOneofInner().getNumWithDefault());
143     assertEquals(0, outer.getOneofInner().getNested().getNum());
144     assertEquals(4, outer.getOneofInner().getNested().getNumWithDefault());
145   }
146 
testClearValues()147   public void testClearValues() {
148     LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder()
149         .setNum(115)
150         .build();
151 
152     LazyMessageLite.Builder outerBuilder = LazyMessageLite.newBuilder();
153 
154     assertEquals(0, outerBuilder.build().getNum());
155 
156 
157     // Set/Clear num
158     outerBuilder.setNum(100);
159 
160     assertEquals(100, outerBuilder.build().getNum());
161     assertEquals(421, outerBuilder.build().getNumWithDefault());
162     assertFalse(outerBuilder.build().hasInner());
163 
164     outerBuilder.clearNum();
165 
166     assertEquals(0, outerBuilder.build().getNum());
167     assertEquals(421, outerBuilder.build().getNumWithDefault());
168     assertFalse(outerBuilder.build().hasInner());
169 
170 
171     // Set/Clear all
172     outerBuilder.setNum(100)
173         .setInner(inner)
174         .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(119))
175         .addRepeatedInner(LazyInnerMessageLite.newBuilder().setNum(122))
176         .setOneofInner(LazyInnerMessageLite.newBuilder().setNum(123));
177 
178     LazyMessageLite outer = outerBuilder.build();
179     assertEquals(100, outer.getNum());
180     assertEquals(421, outer.getNumWithDefault());
181     assertTrue(outer.hasInner());
182     assertEquals(115, outer.getInner().getNum());
183     assertEquals(2, outer.getRepeatedInnerCount());
184     assertEquals(119, outer.getRepeatedInner(0).getNum());
185     assertEquals(122, outer.getRepeatedInner(1).getNum());
186     assertTrue(outer.hasOneofInner());
187     assertEquals(123, outer.getOneofInner().getNum());
188 
189     outerBuilder.clear();
190 
191     outer = outerBuilder.build();
192 
193     assertEquals(0, outer.getNum());
194     assertEquals(421, outer.getNumWithDefault());
195     assertFalse(outer.hasInner());
196     assertEquals(0, outer.getRepeatedInnerCount());
197     assertFalse(outer.hasOneofInner());
198     assertEquals(0, outer.getOneofInner().getNum());
199   }
200 
testMergeValues()201   public void testMergeValues() {
202     LazyMessageLite outerBase = LazyMessageLite.newBuilder()
203         .setNumWithDefault(122)
204         .build();
205 
206     LazyInnerMessageLite innerMerging = LazyInnerMessageLite.newBuilder()
207         .setNum(115)
208         .build();
209     LazyMessageLite outerMerging = LazyMessageLite.newBuilder()
210         .setNum(119)
211         .setInner(innerMerging)
212         .setOneofInner(innerMerging)
213         .build();
214 
215     LazyMessageLite merged = LazyMessageLite
216         .newBuilder(outerBase)
217         .mergeFrom(outerMerging)
218         .build();
219     assertEquals(119, merged.getNum());
220     assertEquals(122, merged.getNumWithDefault());
221     assertEquals(115, merged.getInner().getNum());
222     assertEquals(42, merged.getInner().getNumWithDefault());
223     assertEquals(115, merged.getOneofInner().getNum());
224     assertEquals(42, merged.getOneofInner().getNumWithDefault());
225   }
226 
testMergeDefaultValues()227   public void testMergeDefaultValues() {
228     LazyInnerMessageLite innerBase = LazyInnerMessageLite.newBuilder()
229         .setNum(115)
230         .build();
231     LazyMessageLite outerBase = LazyMessageLite.newBuilder()
232         .setNum(119)
233         .setNumWithDefault(122)
234         .setInner(innerBase)
235         .setOneofInner(innerBase)
236         .build();
237 
238     LazyMessageLite outerMerging = LazyMessageLite.newBuilder()
239         .build();
240 
241     LazyMessageLite merged = LazyMessageLite
242         .newBuilder(outerBase)
243         .mergeFrom(outerMerging)
244         .build();
245     // Merging default-instance shouldn't overwrite values in the base message.
246     assertEquals(119, merged.getNum());
247     assertEquals(122, merged.getNumWithDefault());
248     assertEquals(115, merged.getInner().getNum());
249     assertEquals(42, merged.getInner().getNumWithDefault());
250     assertEquals(115, merged.getOneofInner().getNum());
251     assertEquals(42, merged.getOneofInner().getNumWithDefault());
252   }
253 
254   // Regression test for b/28198805.
testMergeOneofMessages()255   public void testMergeOneofMessages() throws Exception {
256     LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder().build();
257     LazyMessageLite outer = LazyMessageLite.newBuilder().setOneofInner(inner).build();
258     ByteString data1 = outer.toByteString();
259 
260     // The following should not alter the content of the 'outer' message.
261     LazyMessageLite.Builder merged = LazyMessageLite.newBuilder().mergeFrom(outer);
262     LazyInnerMessageLite anotherInner = LazyInnerMessageLite.newBuilder().setNum(12345).build();
263     merged.setOneofInner(anotherInner);
264 
265     // Check that the 'outer' stays the same.
266     ByteString data2 = outer.toByteString();
267     assertEquals(data1, data2);
268     assertEquals(0, outer.getOneofInner().getNum());
269   }
270 
testSerialize()271   public void testSerialize() throws InvalidProtocolBufferException {
272     LazyNestedInnerMessageLite nested = LazyNestedInnerMessageLite.newBuilder()
273         .setNum(3)
274         .build();
275     LazyInnerMessageLite inner = LazyInnerMessageLite.newBuilder()
276         .setNum(2)
277         .setNested(nested)
278         .build();
279     LazyMessageLite outer = LazyMessageLite.newBuilder()
280         .setNum(1)
281         .setInner(inner)
282         .setOneofInner(inner)
283         .build();
284 
285     ByteString bytes = outer.toByteString();
286     assertEquals(bytes.size(), outer.getSerializedSize());
287 
288     LazyMessageLite deserialized = LazyMessageLite.parseFrom(bytes);
289 
290     assertEquals(1, deserialized.getNum());
291     assertEquals(421,  deserialized.getNumWithDefault());
292 
293     assertEquals(2,  deserialized.getInner().getNum());
294     assertEquals(42,  deserialized.getInner().getNumWithDefault());
295 
296     assertEquals(3,  deserialized.getInner().getNested().getNum());
297     assertEquals(4,  deserialized.getInner().getNested().getNumWithDefault());
298 
299     assertEquals(2,  deserialized.getOneofInner().getNum());
300     assertEquals(42,  deserialized.getOneofInner().getNumWithDefault());
301     assertEquals(3,  deserialized.getOneofInner().getNested().getNum());
302     assertEquals(4,  deserialized.getOneofInner().getNested().getNumWithDefault());
303 
304     assertEquals(bytes, deserialized.toByteString());
305   }
306 
testExtensions()307   public void testExtensions() throws Exception {
308     LazyInnerMessageLite.Builder innerBuilder = LazyInnerMessageLite.newBuilder();
309     innerBuilder.setExtension(
310         LazyExtension.extension, LazyExtension.newBuilder()
311         .setName("name").build());
312     assertTrue(innerBuilder.hasExtension(LazyExtension.extension));
313     assertEquals("name", innerBuilder.getExtension(LazyExtension.extension).getName());
314 
315     LazyInnerMessageLite innerMessage = innerBuilder.build();
316     assertTrue(innerMessage.hasExtension(LazyExtension.extension));
317     assertEquals("name", innerMessage.getExtension(LazyExtension.extension).getName());
318 
319     LazyMessageLite lite = LazyMessageLite.newBuilder()
320         .setInner(innerMessage).build();
321     assertTrue(lite.getInner().hasExtension(LazyExtension.extension));
322     assertEquals("name", lite.getInner().getExtension(LazyExtension.extension).getName());
323   }
324 }
325