Debian Codegraph @

activemq-protobuf-test/src/test/java/com/google/protobuf/GeneratedMessageTest.java

raw
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.
3 // http://code.google.com/p/protobuf/
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 package com.google.protobuf;
18
19 import java.util.Arrays;
20
21 import junit.framework.TestCase;
22 import protobuf_unittest.EnumWithNoOuter;
23 import protobuf_unittest.MessageWithNoOuter;
24 import protobuf_unittest.UnittestProto.ForeignEnum;
25 import protobuf_unittest.UnittestProto.ForeignMessage;
26 import protobuf_unittest.UnittestProto.TestAllTypes;
27 import protobuf_unittest.UnittestProto.TestExtremeDefaultValues;
28
29 /**
30 * Unit test for generated messages and generated code. See also
31 * {@link MessageTest}, which tests some generated message functionality.
32 *
33 * @author [email protected] Kenton Varda
34 */
35 public class GeneratedMessageTest extends TestCase {
36
37
38 public void testAccessors() throws Exception {
39 TestAllTypes builder = new TestAllTypes();
40 TestUtil.setAllFields(builder);
41 TestAllTypes message = builder;
42 TestUtil.assertAllFieldsSet(message);
43 }
44
45 public void testRepeatedSetters() throws Exception {
46 TestAllTypes builder = new TestAllTypes();
47 TestUtil.setAllFields(builder);
48 TestUtil.modifyRepeatedFields(builder);
49 TestAllTypes message = builder;
50 TestUtil.assertRepeatedFieldsModified(message);
51 }
52
53 public void testRepeatedAppend() throws Exception {
54 TestAllTypes builder = new TestAllTypes();
55
56 builder.addAllRepeatedInt32(Arrays.asList(1, 2, 3, 4));
57 builder.addAllRepeatedForeignEnum(Arrays.asList(ForeignEnum.FOREIGN_BAZ));
58
59 ForeignMessage foreignMessage = new ForeignMessage().setC(12);
60 builder.addAllRepeatedForeignMessage(Arrays.asList(foreignMessage));
61
62 TestAllTypes message = builder;
63 assertEquals(message.getRepeatedInt32List(), Arrays.asList(1, 2, 3, 4));
64 assertEquals(message.getRepeatedForeignEnumList(),
65 Arrays.asList(ForeignEnum.FOREIGN_BAZ));
66 assertEquals(1, message.getRepeatedForeignMessageCount());
67 assertEquals(12, message.getRepeatedForeignMessage(0).getC());
68 }
69
70 public void testSettingForeignMessageUsingBuilder() throws Exception {
71 TestAllTypes message = new TestAllTypes()
72 // Pass builder for foreign message instance.
73 .setOptionalForeignMessage(new ForeignMessage().setC(123))
74 ;
75 TestAllTypes expectedMessage = new TestAllTypes()
76 // Create expected version passing foreign message instance explicitly.
77 .setOptionalForeignMessage(new ForeignMessage().setC(123))
78 ;
79 // TODO(ngd): Upgrade to using real #equals method once implemented
80 assertEquals(expectedMessage.toString(), message.toString());
81 }
82
83 public void testSettingRepeatedForeignMessageUsingBuilder() throws Exception {
84 TestAllTypes message = new TestAllTypes()
85 // Pass builder for foreign message instance.
86 .addRepeatedForeignMessage(new ForeignMessage().setC(456))
87 ;
88 TestAllTypes expectedMessage = new TestAllTypes()
89 // Create expected version passing foreign message instance explicitly.
90 .addRepeatedForeignMessage(
91 new ForeignMessage().setC(456))
92 ;
93 assertEquals(expectedMessage.toString(), message.toString());
94 }
95
96 public void testDefaults() throws Exception {
97 TestUtil.assertClear(new TestAllTypes());
98
99 assertEquals("\u1234", new TestExtremeDefaultValues().getUtf8String());
100 }
101
102 // =================================================================
103 // multiple_files_test
104
105 public void testMultipleFilesOption() throws Exception {
106 // We mostly just want to check that things compile.
107 MessageWithNoOuter message =
108 new MessageWithNoOuter()
109 .setNested(new MessageWithNoOuter.NestedMessage().setI(1))
110 .addForeign(new TestAllTypes().setOptionalInt32(1))
111 .setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ)
112 .setForeignEnum(EnumWithNoOuter.BAR)
113 ;
114
115 byte[] data = message.toUnframedByteArray();
116 MessageWithNoOuter newMessage = MessageWithNoOuter.parseUnframed(data);
117 assertEquals(message.toString(), newMessage.toString());
118 }
119 }