AbstractProto3LiteSchemaTest.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 org.junit.Assert.assertEquals;
34 
35 import com.google.protobuf.testing.Proto3TestingLite.Proto3EmptyLite;
36 import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLite;
37 import com.google.protobuf.testing.Proto3TestingLite.Proto3MessageLiteWithMaps;
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.nio.ByteBuffer;
41 import java.util.ArrayList;
42 import java.util.List;
43 import org.junit.Test;
44 
46 public abstract class AbstractProto3LiteSchemaTest extends AbstractSchemaTest<Proto3MessageLite> {
47  @Override
49  return new Proto3MessageLiteFactory(10, 20, 2, 2);
50  }
51 
52  @Override
53  protected List<ByteBuffer> serializedBytesWithInvalidUtf8() throws IOException {
54  List<ByteBuffer> invalidBytes = new ArrayList<>();
55  byte[] invalid = new byte[] {(byte) 0x80};
56  {
57  ByteBuffer buffer = ByteBuffer.allocate(100);
59  codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_9_FIELD_NUMBER, invalid);
60  codedOutput.flush();
61  buffer.flip();
62  invalidBytes.add(buffer);
63  }
64  {
65  ByteBuffer buffer = ByteBuffer.allocate(100);
67  codedOutput.writeByteArray(Proto3MessageLite.FIELD_STRING_LIST_26_FIELD_NUMBER, invalid);
68  codedOutput.flush();
69  buffer.flip();
70  invalidBytes.add(buffer);
71  }
72  return invalidBytes;
73  }
74 
75  @Test
76  public void mergeOptionalMessageFields() throws Exception {
77  Proto3MessageLite message1 =
78  newBuilder()
79  .setFieldMessage10(newBuilder().setFieldInt643(123).clearFieldInt325().build())
80  .build();
81  Proto3MessageLite message2 =
82  newBuilder()
83  .setFieldMessage10(newBuilder().clearFieldInt643().setFieldInt325(456).build())
84  .build();
85  Proto3MessageLite message3 =
86  newBuilder()
87  .setFieldMessage10(newBuilder().setFieldInt643(789).clearFieldInt325().build())
88  .build();
89  ByteArrayOutputStream output = new ByteArrayOutputStream();
90  message1.writeTo(output);
91  message2.writeTo(output);
92  message3.writeTo(output);
93  byte[] data = output.toByteArray();
94 
95  Proto3MessageLite merged =
96  ExperimentalSerializationUtil.fromByteArray(data, Proto3MessageLite.class);
97  assertEquals(789, merged.getFieldMessage10().getFieldInt643());
98  assertEquals(456, merged.getFieldMessage10().getFieldInt325());
99  }
100 
101  @Test
102  public void oneofFieldsShouldRoundtrip() throws IOException {
103  roundtrip("Field 53", newBuilder().setFieldDouble53(100).build());
104  roundtrip("Field 54", newBuilder().setFieldFloat54(100).build());
105  roundtrip("Field 55", newBuilder().setFieldInt6455(100).build());
106  roundtrip("Field 56", newBuilder().setFieldUint6456(100L).build());
107  roundtrip("Field 57", newBuilder().setFieldInt3257(100).build());
108  roundtrip("Field 58", newBuilder().setFieldFixed6458(100).build());
109  roundtrip("Field 59", newBuilder().setFieldFixed3259(100).build());
110  roundtrip("Field 60", newBuilder().setFieldBool60(true).build());
111  roundtrip("Field 61", newBuilder().setFieldString61(data().getString()).build());
112  roundtrip(
113  "Field 62", newBuilder().setFieldMessage62(newBuilder().setFieldDouble1(100)).build());
114  roundtrip("Field 63", newBuilder().setFieldBytes63(data().getBytes()).build());
115  roundtrip("Field 64", newBuilder().setFieldUint3264(100).build());
116  roundtrip("Field 65", newBuilder().setFieldSfixed3265(100).build());
117  roundtrip("Field 66", newBuilder().setFieldSfixed6466(100).build());
118  roundtrip("Field 67", newBuilder().setFieldSint3267(100).build());
119  roundtrip("Field 68", newBuilder().setFieldSint6468(100).build());
120  }
121 
122  @Test
123  public void retainUnknownFields() {
124  // Unknown fields are retained in lite runtime.
125  Proto3MessageLite expectedMessage = messageFactory().newMessage();
126  Proto3EmptyLite empty =
128  expectedMessage.toByteArray(), Proto3EmptyLite.class);
129  assertEquals(expectedMessage.getSerializedSize(), empty.getSerializedSize());
130  }
131 
132  @Test
133  public void mapsShouldRoundtrip() throws IOException {
134  roundtrip(
135  "Proto3MessageLiteWithMaps",
136  new Proto3MessageLiteFactory(2, 10, 2, 2).newMessageWithMaps(),
137  Protobuf.getInstance().schemaFor(Proto3MessageLiteWithMaps.class));
138  }
139 
140  private static Proto3MessageLite.Builder newBuilder() {
141  return Proto3MessageLite.newBuilder();
142  }
143 }
com.google.protobuf.AbstractProto3LiteSchemaTest
Definition: AbstractProto3LiteSchemaTest.java:46
com.google.protobuf.AbstractSchemaTest
Definition: AbstractSchemaTest.java:46
com.google.protobuf.Proto3MessageLiteFactory.newMessage
Proto3MessageLite newMessage()
Definition: Proto3MessageLiteFactory.java:77
merged
static bool merged(const upb_refcounted *r, const upb_refcounted *r2)
Definition: ruby/ext/google/protobuf_c/upb.c:6184
com.google.protobuf.AbstractProto3LiteSchemaTest.mapsShouldRoundtrip
void mapsShouldRoundtrip()
Definition: AbstractProto3LiteSchemaTest.java:133
Proto3MessageLiteWithMaps
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.AbstractSchemaTest< Proto3MessageLite >::roundtrip
static final< M extends MessageLite > void roundtrip(String failureMessage, M msg, Schema< M > schema)
Definition: AbstractSchemaTest.java:112
com.google.protobuf.CodedOutputStream.flush
abstract void flush()
com.google.protobuf.ExperimentalSerializationUtil.fromByteArray
static< T > T fromByteArray(byte[] data, Class< T > messageType)
Definition: ExperimentalSerializationUtil.java:75
buffer
Definition: buffer_processor.h:43
byte
SETUP_TEARDOWN_TESTCONTEXT typedef uint8_t byte
Definition: test_stream.cpp:12
Proto3EmptyLite
com.google.protobuf.AbstractProto3LiteSchemaTest.serializedBytesWithInvalidUtf8
List< ByteBuffer > serializedBytesWithInvalidUtf8()
Definition: AbstractProto3LiteSchemaTest.java:53
java
com.google.protobuf.AbstractProto3LiteSchemaTest.newBuilder
static Proto3MessageLite.Builder newBuilder()
Definition: AbstractProto3LiteSchemaTest.java:140
com.google.protobuf.AbstractProto3LiteSchemaTest.mergeOptionalMessageFields
void mergeOptionalMessageFields()
Definition: AbstractProto3LiteSchemaTest.java:76
com.google.protobuf.ExperimentalSerializationUtil
Definition: ExperimentalSerializationUtil.java:38
Proto3MessageLite
com.google
com
com.google.protobuf.CodedOutputStream.newInstance
static CodedOutputStream newInstance(final OutputStream output)
Definition: CodedOutputStream.java:92
com.google.protobuf.AbstractSchemaTest< Proto3MessageLite >::data
final ExperimentalTestDataProvider data()
Definition: AbstractSchemaTest.java:138
com.google.protobuf.CodedOutputStream.writeByteArray
abstract void writeByteArray(int fieldNumber, byte[] value)
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
com.google.protobuf.CodedOutputStream
Definition: CodedOutputStream.java:59
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
com.google.protobuf.AbstractProto3LiteSchemaTest.retainUnknownFields
void retainUnknownFields()
Definition: AbstractProto3LiteSchemaTest.java:123
com.google.protobuf.Proto3MessageLiteFactory
Definition: Proto3MessageLiteFactory.java:39
com.google.protobuf.AbstractProto3LiteSchemaTest.messageFactory
Proto3MessageLiteFactory messageFactory()
Definition: AbstractProto3LiteSchemaTest.java:48
com.google.protobuf.AbstractProto3LiteSchemaTest.oneofFieldsShouldRoundtrip
void oneofFieldsShouldRoundtrip()
Definition: AbstractProto3LiteSchemaTest.java:102


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