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 #include <google/protobuf/util/internal/type_info_test_helper.h>
32
33 #include <memory>
34 #ifndef _SHARED_PTR_H
35 #include <google/protobuf/stubs/shared_ptr.h>
36 #endif
37 #include <vector>
38
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/descriptor.h>
42 #include <google/protobuf/util/internal/default_value_objectwriter.h>
43 #include <google/protobuf/util/internal/type_info.h>
44 #include <google/protobuf/util/internal/constants.h>
45 #include <google/protobuf/util/internal/protostream_objectsource.h>
46 #include <google/protobuf/util/internal/protostream_objectwriter.h>
47 #include <google/protobuf/util/type_resolver.h>
48 #include <google/protobuf/util/type_resolver_util.h>
49
50 namespace google {
51 namespace protobuf {
52 namespace util {
53 namespace converter {
54 namespace testing {
55
56
ResetTypeInfo(const vector<const Descriptor * > & descriptors)57 void TypeInfoTestHelper::ResetTypeInfo(
58 const vector<const Descriptor*>& descriptors) {
59 switch (type_) {
60 case USE_TYPE_RESOLVER: {
61 const DescriptorPool* pool = descriptors[0]->file()->pool();
62 for (int i = 1; i < descriptors.size(); ++i) {
63 GOOGLE_CHECK(pool == descriptors[i]->file()->pool())
64 << "Descriptors from different pools are not supported.";
65 }
66 type_resolver_.reset(
67 NewTypeResolverForDescriptorPool(kTypeServiceBaseUrl, pool));
68 typeinfo_.reset(TypeInfo::NewTypeInfo(type_resolver_.get()));
69 return;
70 }
71 }
72 GOOGLE_LOG(FATAL) << "Can not reach here.";
73 }
74
ResetTypeInfo(const Descriptor * descriptor)75 void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor) {
76 vector<const Descriptor*> descriptors;
77 descriptors.push_back(descriptor);
78 ResetTypeInfo(descriptors);
79 }
80
ResetTypeInfo(const Descriptor * descriptor1,const Descriptor * descriptor2)81 void TypeInfoTestHelper::ResetTypeInfo(const Descriptor* descriptor1,
82 const Descriptor* descriptor2) {
83 vector<const Descriptor*> descriptors;
84 descriptors.push_back(descriptor1);
85 descriptors.push_back(descriptor2);
86 ResetTypeInfo(descriptors);
87 }
88
GetTypeInfo()89 TypeInfo* TypeInfoTestHelper::GetTypeInfo() { return typeinfo_.get(); }
90
NewProtoSource(io::CodedInputStream * coded_input,const string & type_url)91 ProtoStreamObjectSource* TypeInfoTestHelper::NewProtoSource(
92 io::CodedInputStream* coded_input, const string& type_url) {
93 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
94 switch (type_) {
95 case USE_TYPE_RESOLVER: {
96 return new ProtoStreamObjectSource(coded_input, type_resolver_.get(),
97 *type);
98 }
99 }
100 GOOGLE_LOG(FATAL) << "Can not reach here.";
101 return NULL;
102 }
103
NewProtoWriter(const string & type_url,strings::ByteSink * output,ErrorListener * listener,const ProtoStreamObjectWriter::Options & options)104 ProtoStreamObjectWriter* TypeInfoTestHelper::NewProtoWriter(
105 const string& type_url, strings::ByteSink* output, ErrorListener* listener,
106 const ProtoStreamObjectWriter::Options& options) {
107 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
108 switch (type_) {
109 case USE_TYPE_RESOLVER: {
110 return new ProtoStreamObjectWriter(type_resolver_.get(), *type, output,
111 listener, options);
112 }
113 }
114 GOOGLE_LOG(FATAL) << "Can not reach here.";
115 return NULL;
116 }
117
NewDefaultValueWriter(const string & type_url,ObjectWriter * writer)118 DefaultValueObjectWriter* TypeInfoTestHelper::NewDefaultValueWriter(
119 const string& type_url, ObjectWriter* writer) {
120 const google::protobuf::Type* type = typeinfo_->GetTypeByTypeUrl(type_url);
121 switch (type_) {
122 case USE_TYPE_RESOLVER: {
123 return new DefaultValueObjectWriter(type_resolver_.get(), *type, writer);
124 }
125 }
126 GOOGLE_LOG(FATAL) << "Can not reach here.";
127 return NULL;
128 }
129
130 } // namespace testing
131 } // namespace converter
132 } // namespace util
133 } // namespace protobuf
134 } // namespace google
135