java_plugin_unittest.cc
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 // Author: kenton@google.com (Kenton Varda)
32 //
33 // TODO(kenton): Share code with the versions of this test in other languages?
34 // It seemed like parameterizing it would add more complexity than it is
35 // worth.
36 
37 #include <memory>
38 
43 
47 #include <gtest/gtest.h>
48 
49 namespace google {
50 namespace protobuf {
51 namespace compiler {
52 namespace java {
53 namespace {
54 
55 class TestGenerator : public CodeGenerator {
56  public:
57  TestGenerator() {}
58  ~TestGenerator() {}
59 
60  virtual bool Generate(const FileDescriptor* file,
61  const std::string& parameter, GeneratorContext* context,
62  std::string* error) const {
63  std::string filename = "Test.java";
64  TryInsert(filename, "outer_class_scope", context);
65  TryInsert(filename, "class_scope:foo.Bar", context);
66  TryInsert(filename, "class_scope:foo.Bar.Baz", context);
67  TryInsert(filename, "builder_scope:foo.Bar", context);
68  TryInsert(filename, "builder_scope:foo.Bar.Baz", context);
69  TryInsert(filename, "enum_scope:foo.Qux", context);
70  return true;
71  }
72 
73  void TryInsert(const std::string& filename,
74  const std::string& insertion_point,
75  GeneratorContext* context) const {
76  std::unique_ptr<io::ZeroCopyOutputStream> output(
77  context->OpenForInsert(filename, insertion_point));
78  io::Printer printer(output.get(), '$');
79  printer.Print("// inserted $name$\n", "name", insertion_point);
80  }
81 };
82 
83 // This test verifies that all the expected insertion points exist. It does
84 // not verify that they are correctly-placed; that would require actually
85 // compiling the output which is a bit more than I care to do for this test.
86 TEST(JavaPluginTest, PluginTest) {
88  "syntax = \"proto2\";\n"
89  "package foo;\n"
90  "option java_package = \"\";\n"
91  "option java_outer_classname = \"Test\";\n"
92  "message Bar {\n"
93  " message Baz {}\n"
94  "}\n"
95  "enum Qux { BLAH = 1; }\n",
96  true));
97 
98  CommandLineInterface cli;
99  cli.SetInputsAreProtoPathRelative(true);
100 
101  JavaGenerator java_generator;
102  TestGenerator test_generator;
103  cli.RegisterGenerator("--java_out", &java_generator, "");
104  cli.RegisterGenerator("--test_out", &test_generator, "");
105 
106  std::string proto_path = "-I" + TestTempDir();
107  std::string java_out = "--java_out=" + TestTempDir();
108  std::string test_out = "--test_out=" + TestTempDir();
109 
110  const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(),
111  test_out.c_str(), "test.proto"};
112 
113  EXPECT_EQ(0, cli.Run(5, argv));
114 }
115 
116 } // namespace
117 } // namespace java
118 } // namespace compiler
119 } // namespace protobuf
120 } // namespace google
zero_copy_stream.h
google::protobuf::File::SetContents
static bool SetContents(const string &name, const string &contents, bool)
Definition: file.h:93
gtest.h
java_generator.h
FileDescriptor
Definition: ruby/ext/google/protobuf_c/protobuf.h:125
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
error
Definition: cJSON.c:88
printer.h
google::protobuf::TEST
TEST(ArenaTest, ArenaConstructable)
Definition: arena_unittest.cc:156
java
command_line_interface.h
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
googletest.h
file.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::TestTempDir
string TestTempDir()
Definition: googletest.cc:189
compiler
Definition: plugin.pb.cc:22
google
Definition: data_proto2_to_proto3_util.h:11


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