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 #include <memory>
34 #include <string>
35
36 #include <google/protobuf/testing/file.h>
37 #include <google/protobuf/testing/file.h>
38 #include <google/protobuf/compiler/java/generator.h>
39 #include <google/protobuf/compiler/command_line_interface.h>
40 #include <google/protobuf/io/printer.h>
41 #include <google/protobuf/io/zero_copy_stream.h>
42 #include <google/protobuf/stubs/strutil.h>
43 #include <google/protobuf/testing/googletest.h>
44 #include <gtest/gtest.h>
45
46 namespace google {
47 namespace protobuf {
48 namespace compiler {
49 namespace java {
50 namespace {
51
52 class TestGenerator : public CodeGenerator {
53 public:
TestGenerator()54 TestGenerator() {}
~TestGenerator()55 ~TestGenerator() override {}
56
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const57 bool Generate(const FileDescriptor* file, const std::string& parameter,
58 GeneratorContext* context, std::string* error) const override {
59 std::string filename = "Test.java";
60 TryInsert(filename, "outer_class_scope", context);
61 TryInsert(filename, "class_scope:foo.Bar", context);
62 TryInsert(filename, "class_scope:foo.Bar.Baz", context);
63 TryInsert(filename, "builder_scope:foo.Bar", context);
64 TryInsert(filename, "builder_scope:foo.Bar.Baz", context);
65 TryInsert(filename, "enum_scope:foo.Qux", context);
66 return true;
67 }
68
TryInsert(const std::string & filename,const std::string & insertion_point,GeneratorContext * context) const69 void TryInsert(const std::string& filename,
70 const std::string& insertion_point,
71 GeneratorContext* context) const {
72 std::unique_ptr<io::ZeroCopyOutputStream> output(
73 context->OpenForInsert(filename, insertion_point));
74 io::Printer printer(output.get(), '$');
75 printer.Print("// inserted $name$\n", "name", insertion_point);
76 }
77 };
78
79 // This test verifies that all the expected insertion points exist. It does
80 // not verify that they are correctly-placed; that would require actually
81 // compiling the output which is a bit more than I care to do for this test.
TEST(JavaPluginTest,PluginTest)82 TEST(JavaPluginTest, PluginTest) {
83 GOOGLE_CHECK_OK(File::SetContents(TestTempDir() + "/test.proto",
84 "syntax = \"proto2\";\n"
85 "package foo;\n"
86 "option java_package = \"\";\n"
87 "option java_outer_classname = \"Test\";\n"
88 "message Bar {\n"
89 " message Baz {}\n"
90 "}\n"
91 "enum Qux { BLAH = 1; }\n",
92 true));
93
94 CommandLineInterface cli;
95 cli.SetInputsAreProtoPathRelative(true);
96
97 JavaGenerator java_generator;
98 TestGenerator test_generator;
99 cli.RegisterGenerator("--java_out", &java_generator, "");
100 cli.RegisterGenerator("--test_out", &test_generator, "");
101
102 std::string proto_path = "-I" + TestTempDir();
103 std::string java_out = "--java_out=" + TestTempDir();
104 std::string test_out = "--test_out=" + TestTempDir();
105
106 const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(),
107 test_out.c_str(), "test.proto"};
108
109 EXPECT_EQ(0, cli.Run(5, argv));
110
111 // Loop over the lines of the generated code and verify that we find what we
112 // expect
113
114 std::string output;
115 GOOGLE_CHECK_OK(File::GetContents(TestTempDir() + "/Test.java", &output,
116 true));
117 std::vector<std::string> lines = Split(output, "\n");
118 bool found_generated_annotation = false;
119 bool found_do_not_edit = false;
120 for (const auto& line : lines) {
121 if (line.find(" DO NOT EDIT!") != std::string::npos) {
122 found_do_not_edit = true;
123 }
124 if (line.find("@com.google.protobuf.Generated") != std::string::npos) {
125 found_generated_annotation = true;
126 }
127 }
128 EXPECT_TRUE(found_do_not_edit);
129 (void)found_generated_annotation;
130 }
131
132 } // namespace
133 } // namespace java
134 } // namespace compiler
135 } // namespace protobuf
136 } // namespace google
137