~ubuntu-branches/debian/squeeze/protobuf/squeeze

« back to all changes in this revision

Viewing changes to src/google/protobuf/generated_message_reflection_unittest.cc

  • Committer: Bazaar Package Importer
  • Author(s): Julien Cristau
  • Date: 2009-06-02 16:19:00 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090602161900-vm176i3ryt35yk91
Tags: 2.0.3-2.2
* Non-maintainer upload.
* Fix FTBFS from -2.1: don't fail when we can't clean up the java build,
  such as when openjdk isn't installed.
* Disable parallel builds, because libtool is made of fail (if binary-arch
  and build-indep run concurrently, we relink a library while it's being
  used; that doesn't work so well).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Protocol Buffers - Google's data interchange format
2
 
// Copyright 2008 Google Inc.
 
2
// Copyright 2008 Google Inc.  All rights reserved.
3
3
// http://code.google.com/p/protobuf/
4
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.
 
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.
16
30
 
17
31
// Author: kenton@google.com (Kenton Varda)
18
32
//  Based on original Protocol Buffers design by
56
70
  TestUtil::ReflectionTester reflection_tester(
57
71
    unittest::TestAllTypes::descriptor());
58
72
 
59
 
  reflection_tester.ExpectClearViaReflection(*message.GetReflection());
 
73
  reflection_tester.ExpectClearViaReflection(message);
60
74
 
61
 
  const Message::Reflection& reflection = *message.GetReflection();
 
75
  const Reflection* reflection = message.GetReflection();
62
76
 
63
77
  // Messages should return pointers to default instances until first use.
64
78
  // (This is not checked by ExpectClear() since it is not actually true after
65
79
  // the fields have been set and then cleared.)
66
80
  EXPECT_EQ(&unittest::TestAllTypes::OptionalGroup::default_instance(),
67
 
            &reflection.GetMessage(F("optionalgroup")));
 
81
            &reflection->GetMessage(message, F("optionalgroup")));
68
82
  EXPECT_EQ(&unittest::TestAllTypes::NestedMessage::default_instance(),
69
 
            &reflection.GetMessage(F("optional_nested_message")));
 
83
            &reflection->GetMessage(message, F("optional_nested_message")));
70
84
  EXPECT_EQ(&unittest::ForeignMessage::default_instance(),
71
 
            &reflection.GetMessage(F("optional_foreign_message")));
 
85
            &reflection->GetMessage(message, F("optional_foreign_message")));
72
86
  EXPECT_EQ(&unittest_import::ImportMessage::default_instance(),
73
 
            &reflection.GetMessage(F("optional_import_message")));
 
87
            &reflection->GetMessage(message, F("optional_import_message")));
74
88
}
75
89
 
76
90
TEST(GeneratedMessageReflectionTest, Accessors) {
80
94
  TestUtil::ReflectionTester reflection_tester(
81
95
    unittest::TestAllTypes::descriptor());
82
96
 
83
 
  reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
 
97
  reflection_tester.SetAllFieldsViaReflection(&message);
84
98
  TestUtil::ExpectAllFieldsSet(message);
85
 
  reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
 
99
  reflection_tester.ExpectAllFieldsSetViaReflection(message);
86
100
 
87
 
  reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
 
101
  reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
88
102
  TestUtil::ExpectRepeatedFieldsModified(message);
89
103
}
90
104
 
95
109
  message.set_optional_string("foo");
96
110
  message.add_repeated_string("foo");
97
111
 
98
 
  const Message::Reflection& reflection = *message.GetReflection();
 
112
  const Reflection* reflection = message.GetReflection();
99
113
  string scratch;
100
114
 
101
115
  EXPECT_EQ(&message.optional_string(),
102
 
      &reflection.GetStringReference(F("optional_string"), &scratch))
 
116
      &reflection->GetStringReference(message, F("optional_string"), &scratch))
103
117
    << "For simple string fields, GetStringReference() should return a "
104
118
       "reference to the underlying string.";
105
119
  EXPECT_EQ(&message.repeated_string(0),
106
 
      &reflection.GetRepeatedStringReference(F("repeated_string"), 0, &scratch))
 
120
      &reflection->GetRepeatedStringReference(message, F("repeated_string"),
 
121
                                              0, &scratch))
107
122
    << "For simple string fields, GetRepeatedStringReference() should return "
108
123
       "a reference to the underlying string.";
109
124
}
119
134
  TestUtil::SetAllFields(&message);
120
135
  message.Clear();
121
136
 
122
 
  const Message::Reflection& reflection = *message.GetReflection();
 
137
  const Reflection* reflection = message.GetReflection();
123
138
 
124
139
  EXPECT_NE(&unittest::TestAllTypes::OptionalGroup::default_instance(),
125
 
            &reflection.GetMessage(F("optionalgroup")));
 
140
            &reflection->GetMessage(message, F("optionalgroup")));
126
141
  EXPECT_NE(&unittest::TestAllTypes::NestedMessage::default_instance(),
127
 
            &reflection.GetMessage(F("optional_nested_message")));
 
142
            &reflection->GetMessage(message, F("optional_nested_message")));
128
143
  EXPECT_NE(&unittest::ForeignMessage::default_instance(),
129
 
            &reflection.GetMessage(F("optional_foreign_message")));
 
144
            &reflection->GetMessage(message, F("optional_foreign_message")));
130
145
  EXPECT_NE(&unittest_import::ImportMessage::default_instance(),
131
 
            &reflection.GetMessage(F("optional_import_message")));
 
146
            &reflection->GetMessage(message, F("optional_import_message")));
132
147
}
133
148
 
134
149
TEST(GeneratedMessageReflectionTest, Extensions) {
138
153
  TestUtil::ReflectionTester reflection_tester(
139
154
    unittest::TestAllExtensions::descriptor());
140
155
 
141
 
  reflection_tester.SetAllFieldsViaReflection(message.GetReflection());
 
156
  reflection_tester.SetAllFieldsViaReflection(&message);
142
157
  TestUtil::ExpectAllExtensionsSet(message);
143
 
  reflection_tester.ExpectAllFieldsSetViaReflection(*message.GetReflection());
 
158
  reflection_tester.ExpectAllFieldsSetViaReflection(message);
144
159
 
145
 
  reflection_tester.ModifyRepeatedFieldsViaReflection(message.GetReflection());
 
160
  reflection_tester.ModifyRepeatedFieldsViaReflection(&message);
146
161
  TestUtil::ExpectRepeatedExtensionsModified(message);
147
162
}
148
163
 
149
164
TEST(GeneratedMessageReflectionTest, FindExtensionTypeByNumber) {
150
 
  const Message::Reflection& reflection =
151
 
    *unittest::TestAllExtensions::default_instance().GetReflection();
 
165
  const Reflection* reflection =
 
166
    unittest::TestAllExtensions::default_instance().GetReflection();
152
167
 
153
168
  const FieldDescriptor* extension1 =
154
169
    unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
158
173
      "repeated_string_extension");
159
174
 
160
175
  EXPECT_EQ(extension1,
161
 
            reflection.FindKnownExtensionByNumber(extension1->number()));
 
176
            reflection->FindKnownExtensionByNumber(extension1->number()));
162
177
  EXPECT_EQ(extension2,
163
 
            reflection.FindKnownExtensionByNumber(extension2->number()));
 
178
            reflection->FindKnownExtensionByNumber(extension2->number()));
164
179
 
165
180
  // Non-existent extension.
166
 
  EXPECT_TRUE(reflection.FindKnownExtensionByNumber(62341) == NULL);
 
181
  EXPECT_TRUE(reflection->FindKnownExtensionByNumber(62341) == NULL);
167
182
 
168
183
  // Extensions of TestAllExtensions should not show up as extensions of
169
184
  // other types.
172
187
}
173
188
 
174
189
TEST(GeneratedMessageReflectionTest, FindKnownExtensionByName) {
175
 
  const Message::Reflection& reflection =
176
 
    *unittest::TestAllExtensions::default_instance().GetReflection();
 
190
  const Reflection* reflection =
 
191
    unittest::TestAllExtensions::default_instance().GetReflection();
177
192
 
178
193
  const FieldDescriptor* extension1 =
179
194
    unittest::TestAllExtensions::descriptor()->file()->FindExtensionByName(
183
198
      "repeated_string_extension");
184
199
 
185
200
  EXPECT_EQ(extension1,
186
 
            reflection.FindKnownExtensionByName(extension1->full_name()));
 
201
            reflection->FindKnownExtensionByName(extension1->full_name()));
187
202
  EXPECT_EQ(extension2,
188
 
            reflection.FindKnownExtensionByName(extension2->full_name()));
 
203
            reflection->FindKnownExtensionByName(extension2->full_name()));
189
204
 
190
205
  // Non-existent extension.
191
 
  EXPECT_TRUE(reflection.FindKnownExtensionByName("no_such_ext") == NULL);
 
206
  EXPECT_TRUE(reflection->FindKnownExtensionByName("no_such_ext") == NULL);
192
207
 
193
208
  // Extensions of TestAllExtensions should not show up as extensions of
194
209
  // other types.
200
215
 
201
216
TEST(GeneratedMessageReflectionTest, UsageErrors) {
202
217
  unittest::TestAllTypes message;
203
 
  Message::Reflection* reflection = message.GetReflection();
 
218
  const Reflection* reflection = message.GetReflection();
204
219
  const Descriptor* descriptor = message.GetDescriptor();
205
220
 
206
221
#define f(NAME) descriptor->FindFieldByName(NAME)
208
223
  // Testing every single failure mode would be too much work.  Let's just
209
224
  // check a few.
210
225
  EXPECT_DEATH(
211
 
    reflection->GetInt32(descriptor->FindFieldByName("optional_int64")),
 
226
    reflection->GetInt32(
 
227
      message, descriptor->FindFieldByName("optional_int64")),
212
228
    "Protocol Buffer reflection usage error:\n"
213
 
    "  Method      : google::protobuf::Message::Reflection::GetInt32\n"
 
229
    "  Method      : google::protobuf::Reflection::GetInt32\n"
214
230
    "  Message type: protobuf_unittest\\.TestAllTypes\n"
215
231
    "  Field       : protobuf_unittest\\.TestAllTypes\\.optional_int64\n"
216
232
    "  Problem     : Field is not the right type for this message:\n"
217
233
    "    Expected  : CPPTYPE_INT32\n"
218
234
    "    Field type: CPPTYPE_INT64");
219
235
  EXPECT_DEATH(
220
 
    reflection->GetInt32(descriptor->FindFieldByName("repeated_int32")),
 
236
    reflection->GetInt32(
 
237
      message, descriptor->FindFieldByName("repeated_int32")),
221
238
    "Protocol Buffer reflection usage error:\n"
222
 
    "  Method      : google::protobuf::Message::Reflection::GetInt32\n"
 
239
    "  Method      : google::protobuf::Reflection::GetInt32\n"
223
240
    "  Message type: protobuf_unittest.TestAllTypes\n"
224
241
    "  Field       : protobuf_unittest.TestAllTypes.repeated_int32\n"
225
242
    "  Problem     : Field is repeated; the method requires a singular field.");
226
243
  EXPECT_DEATH(
227
244
    reflection->GetInt32(
228
 
      unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
 
245
      message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
229
246
    "Protocol Buffer reflection usage error:\n"
230
 
    "  Method      : google::protobuf::Message::Reflection::GetInt32\n"
 
247
    "  Method      : google::protobuf::Reflection::GetInt32\n"
231
248
    "  Message type: protobuf_unittest.TestAllTypes\n"
232
249
    "  Field       : protobuf_unittest.ForeignMessage.c\n"
233
250
    "  Problem     : Field does not match message type.");
234
251
  EXPECT_DEATH(
235
252
    reflection->HasField(
236
 
      unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
 
253
      message, unittest::ForeignMessage::descriptor()->FindFieldByName("c")),
237
254
    "Protocol Buffer reflection usage error:\n"
238
 
    "  Method      : google::protobuf::Message::Reflection::HasField\n"
 
255
    "  Method      : google::protobuf::Reflection::HasField\n"
239
256
    "  Message type: protobuf_unittest.TestAllTypes\n"
240
257
    "  Field       : protobuf_unittest.ForeignMessage.c\n"
241
258
    "  Problem     : Field does not match message type.");