• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 
10 #include <memory>
11 #include <string>
12 
13 #include "google/protobuf/testing/file.h"
14 #include "google/protobuf/testing/file.h"
15 #include <gtest/gtest.h>
16 #include "absl/log/absl_check.h"
17 #include "absl/strings/str_split.h"
18 #include "google/protobuf/compiler/command_line_interface.h"
19 #include "google/protobuf/compiler/java/generator.h"
20 #include "google/protobuf/io/printer.h"
21 #include "google/protobuf/io/zero_copy_stream.h"
22 
23 namespace google {
24 namespace protobuf {
25 namespace compiler {
26 namespace java {
27 namespace {
28 
29 class TestGenerator : public CodeGenerator {
30  public:
TestGenerator()31   TestGenerator() {}
~TestGenerator()32   ~TestGenerator() override {}
33 
Generate(const FileDescriptor * file,const std::string & parameter,GeneratorContext * context,std::string * error) const34   bool Generate(const FileDescriptor* file, const std::string& parameter,
35                 GeneratorContext* context, std::string* error) const override {
36     std::string filename = "Test.java";
37     TryInsert(filename, "outer_class_scope", context);
38     TryInsert(filename, "class_scope:foo.Bar", context);
39     TryInsert(filename, "class_scope:foo.Bar.Baz", context);
40     TryInsert(filename, "builder_scope:foo.Bar", context);
41     TryInsert(filename, "builder_scope:foo.Bar.Baz", context);
42     TryInsert(filename, "enum_scope:foo.Qux", context);
43     return true;
44   }
45 
TryInsert(const std::string & filename,const std::string & insertion_point,GeneratorContext * context) const46   void TryInsert(const std::string& filename,
47                  const std::string& insertion_point,
48                  GeneratorContext* context) const {
49     std::unique_ptr<io::ZeroCopyOutputStream> output(
50         context->OpenForInsert(filename, insertion_point));
51     io::Printer printer(output.get(), '$');
52     printer.Print("// inserted $name$\n", "name", insertion_point);
53   }
54 
GetSupportedFeatures() const55   uint64_t GetSupportedFeatures() const override {
56     return CodeGenerator::Feature::FEATURE_SUPPORTS_EDITIONS;
57   }
58 
GetMinimumEdition() const59   Edition GetMinimumEdition() const override { return Edition::EDITION_PROTO2; }
GetMaximumEdition() const60   Edition GetMaximumEdition() const override { return Edition::EDITION_2023; }
61 };
62 
63 // This test verifies that all the expected insertion points exist.  It does
64 // not verify that they are correctly-placed; that would require actually
65 // compiling the output which is a bit more than I care to do for this test.
TEST(JavaPluginTest,PluginTest)66 TEST(JavaPluginTest, PluginTest) {
67   ABSL_CHECK_OK(
68       File::SetContents(absl::StrCat(::testing::TempDir(), "/test.proto"),
69                         "edition = \"2023\";\n"
70                         "package foo;\n"
71                         "option java_package = \"\";\n"
72                         "option java_outer_classname = \"Test\";\n"
73                         "message Bar {\n"
74                         "  message Baz {}\n"
75                         "}\n"
76                         "enum Qux {\n"
77                         "  option features.enum_type = CLOSED;\n"
78                         "  BLAH = 1;\n"
79                         "}\n",
80                         true));
81 
82   CommandLineInterface cli;
83   cli.SetInputsAreProtoPathRelative(true);
84 
85   JavaGenerator java_generator;
86   TestGenerator test_generator;
87   cli.RegisterGenerator("--java_out", &java_generator, "");
88   cli.RegisterGenerator("--test_out", &test_generator, "");
89 
90   std::string proto_path = absl::StrCat("-I", ::testing::TempDir());
91   std::string java_out = absl::StrCat("--java_out=", ::testing::TempDir());
92   std::string test_out = absl::StrCat("--test_out=", ::testing::TempDir());
93 
94   const char* argv[] = {"protoc", proto_path.c_str(), java_out.c_str(),
95                         test_out.c_str(), "test.proto"};
96 
97   EXPECT_EQ(0, cli.Run(5, argv));
98 
99   // Loop over the lines of the generated code and verify that we find what we
100   // expect
101 
102   std::string output;
103   ABSL_CHECK_OK(
104       File::GetContents(absl::StrCat(::testing::TempDir(), "/Test.java"),
105                         &output, true));
106   std::vector<std::string> lines = absl::StrSplit(output, "\n");
107   bool found_generated_annotation = false;
108   bool found_do_not_edit = false;
109   for (const auto& line : lines) {
110     if (line.find(" DO NOT EDIT!") != std::string::npos) {
111       found_do_not_edit = true;
112     }
113     if (line.find("@com.google.protobuf.Generated") != std::string::npos) {
114       found_generated_annotation = true;
115     }
116   }
117   EXPECT_TRUE(found_do_not_edit);
118   (void)found_generated_annotation;
119 }
120 
121 }  // namespace
122 }  // namespace java
123 }  // namespace compiler
124 }  // namespace protobuf
125 }  // namespace google
126