Proto2ExtensionLookupSchemaTest.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 import static org.junit.Assert.assertFalse;
35 import static org.junit.Assert.assertNull;
36 
37 import com.google.protobuf.testing.Proto2Testing;
38 import com.google.protobuf.testing.Proto2Testing.Proto2Message;
39 import com.google.protobuf.testing.Proto2Testing.Proto2Message.TestEnum;
40 import com.google.protobuf.testing.Proto2Testing.Proto2MessageWithExtensions;
41 import java.util.List;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.junit.runners.JUnit4;
46 
47 @RunWith(JUnit4.class)
49  private byte[] data;
51 
52  @Before
53  public void setup() {
55 
56  Protobuf.getInstance().schemaFor(Proto2MessageWithExtensions.class);
57  data = new Proto2MessageFactory(10, 20, 1, 1).newMessage().toByteArray();
58  extensionRegistry = ExtensionRegistry.newInstance();
59  Proto2Testing.registerAllExtensions(extensionRegistry);
60  }
61 
62  @Test
63  public void testExtensions() throws Exception {
64  Proto2MessageWithExtensions base =
65  Proto2MessageWithExtensions.parseFrom(data, extensionRegistry);
66 
67  Proto2MessageWithExtensions message =
69  data, Proto2MessageWithExtensions.class, extensionRegistry);
70  assertEquals(base, message);
71 
72  Proto2MessageWithExtensions roundtripMessage =
75  Proto2MessageWithExtensions.class,
76  extensionRegistry);
77  assertEquals(base, roundtripMessage);
78  }
79 
80  @Test
81  public void testUnknownEnum() throws Exception {
82  // Use unknown fields to hold invalid enum values.
83  UnknownFieldSetLite unknowns = UnknownFieldSetLite.newInstance();
84  final int outOfRange = 1000;
85  assertNull(TestEnum.forNumber(outOfRange));
86  unknowns.storeField(
87  WireFormat.makeTag(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
88  (long) outOfRange);
89  unknowns.storeField(
90  WireFormat.makeTag(
91  Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
92  (long) TestEnum.ONE_VALUE);
93  unknowns.storeField(
94  WireFormat.makeTag(
95  Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
96  (long) outOfRange);
97  unknowns.storeField(
98  WireFormat.makeTag(
99  Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER, WireFormat.WIRETYPE_VARINT),
100  (long) TestEnum.TWO_VALUE);
101 
102  {
103  // Construct a packed enum list.
104  int packedSize =
105  CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE)
107  + CodedOutputStream.computeUInt32SizeNoTag(TestEnum.ONE_VALUE);
108  ByteString.CodedBuilder packedBuilder = ByteString.newCodedBuilder(packedSize);
109  CodedOutputStream packedOut = packedBuilder.getCodedOutput();
110  packedOut.writeEnumNoTag(TestEnum.ONE_VALUE);
111  packedOut.writeEnumNoTag(outOfRange);
112  packedOut.writeEnumNoTag(TestEnum.TWO_VALUE);
113  unknowns.storeField(
114  WireFormat.makeTag(
115  Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER,
117  packedBuilder.build());
118  }
119  int size = unknowns.getSerializedSize();
120  byte[] output = new byte[size];
122  unknowns.writeTo(codedOutput);
123  codedOutput.flush();
124 
125  Proto2MessageWithExtensions parsed =
127  output, Proto2MessageWithExtensions.class, extensionRegistry);
128  assertFalse(
129  "out-of-range singular enum should not be in message",
130  parsed.hasExtension(Proto2Testing.fieldEnum13));
131  {
132  List<Long> singularEnum =
133  parsed
134  .getUnknownFields()
135  .getField(Proto2Message.FIELD_ENUM_13_FIELD_NUMBER)
136  .getVarintList();
137  assertEquals(1, singularEnum.size());
138  assertEquals((Long) (long) outOfRange, singularEnum.get(0));
139  }
140  {
141  List<Long> repeatedEnum =
142  parsed
143  .getUnknownFields()
144  .getField(Proto2Message.FIELD_ENUM_LIST_30_FIELD_NUMBER)
145  .getVarintList();
146  assertEquals(1, repeatedEnum.size());
147  assertEquals((Long) (long) outOfRange, repeatedEnum.get(0));
148  }
149  {
150  List<Long> packedRepeatedEnum =
151  parsed
152  .getUnknownFields()
153  .getField(Proto2Message.FIELD_ENUM_LIST_PACKED_44_FIELD_NUMBER)
154  .getVarintList();
155  assertEquals(1, packedRepeatedEnum.size());
156  assertEquals((Long) (long) outOfRange, packedRepeatedEnum.get(0));
157  }
158  assertEquals(
159  "out-of-range repeated enum should not be in message",
160  2,
161  parsed.getExtension(Proto2Testing.fieldEnumList30).size());
162  assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumList30, 0));
163  assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumList30, 1));
164  assertEquals(
165  "out-of-range packed repeated enum should not be in message",
166  2,
167  parsed.getExtension(Proto2Testing.fieldEnumListPacked44).size());
168  assertEquals(TestEnum.ONE, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 0));
169  assertEquals(TestEnum.TWO, parsed.getExtension(Proto2Testing.fieldEnumListPacked44, 1));
170  }
171 }
com.google.protobuf.WireFormat.WIRETYPE_VARINT
static final int WIRETYPE_VARINT
Definition: WireFormat.java:55
com.google.protobuf.Proto2ExtensionLookupSchemaTest.data
byte[] data
Definition: Proto2ExtensionLookupSchemaTest.java:49
Proto2Testing
base
Definition: logging.cc:2162
com.google.protobuf.UnknownFieldSetLite
Definition: UnknownFieldSetLite.java:46
com.google.protobuf.Proto2ExtensionLookupSchemaTest
Definition: Proto2ExtensionLookupSchemaTest.java:48
com.google.protobuf.Proto2ExtensionLookupSchemaTest.testUnknownEnum
void testUnknownEnum()
Definition: Proto2ExtensionLookupSchemaTest.java:81
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.CodedOutputStream.flush
abstract void flush()
com.google.protobuf.WireFormat
Definition: WireFormat.java:45
com.google.protobuf.ExperimentalSerializationUtil.toByteArray
static< T > byte[] toByteArray(T msg)
Definition: ExperimentalSerializationUtil.java:43
com.google.protobuf.Proto2MessageFactory.newMessage
Proto2Message newMessage()
Definition: Proto2MessageFactory.java:77
com.google.protobuf.ExperimentalSerializationUtil.fromByteArray
static< T > T fromByteArray(byte[] data, Class< T > messageType)
Definition: ExperimentalSerializationUtil.java:75
com.google.protobuf.Proto2MessageFactory
Definition: Proto2MessageFactory.java:41
com.google.protobuf.UnknownFieldSetLite.writeTo
void writeTo(CodedOutputStream output)
Definition: UnknownFieldSetLite.java:131
com.google.protobuf.Proto2ExtensionLookupSchemaTest.extensionRegistry
ExtensionRegistry extensionRegistry
Definition: Proto2ExtensionLookupSchemaTest.java:50
com.google.protobuf.TestSchemas.registerGenericProto2Schemas
static void registerGenericProto2Schemas()
Definition: TestSchemas.java:49
size
#define size
Definition: glcorearb.h:2944
com.google.protobuf.Proto2ExtensionLookupSchemaTest.testExtensions
void testExtensions()
Definition: Proto2ExtensionLookupSchemaTest.java:63
com.google.protobuf.TestSchemas
Definition: TestSchemas.java:43
com.google.protobuf.CodedOutputStream.writeEnumNoTag
final void writeEnumNoTag(final int value)
Definition: CodedOutputStream.java:454
java
com.google.protobuf.WireFormat.WIRETYPE_LENGTH_DELIMITED
static final int WIRETYPE_LENGTH_DELIMITED
Definition: WireFormat.java:57
com.google.protobuf.Proto2ExtensionLookupSchemaTest.setup
void setup()
Definition: Proto2ExtensionLookupSchemaTest.java:53
com.google.protobuf.ExtensionRegistry.newInstance
static ExtensionRegistry newInstance()
Definition: ExtensionRegistry.java:93
com.google.protobuf.UnknownFieldSetLite.getSerializedSize
int getSerializedSize()
Definition: UnknownFieldSetLite.java:266
size
GLsizeiptr size
Definition: glcorearb.h:2943
com.google.protobuf.ExperimentalSerializationUtil
Definition: ExperimentalSerializationUtil.java:38
com.google
com
com.google.protobuf.CodedOutputStream.newInstance
static CodedOutputStream newInstance(final OutputStream output)
Definition: CodedOutputStream.java:92
com.google.protobuf.ExtensionRegistry
Definition: ExtensionRegistry.java:91
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.CodedOutputStream.computeUInt32SizeNoTag
static int computeUInt32SizeNoTag(final int value)
Definition: CodedOutputStream.java:727
message
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: glcorearb.h:2695
com.google.protobuf.ByteString
Definition: ByteString.java:67


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