From e98a263d223b7dbbf0d839d5abbf62e77a8a794c Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Fri, 25 Oct 2024 16:08:49 -0700 Subject: [PATCH] Add some java plugin integration tests. PiperOrigin-RevId: 689943588 --- src/google/protobuf/compiler/java/BUILD.bazel | 15 ++++ src/google/protobuf/compiler/java/file.cc | 5 +- .../compiler/java/generator_unittest.cc | 72 +++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/google/protobuf/compiler/java/generator_unittest.cc diff --git a/src/google/protobuf/compiler/java/BUILD.bazel b/src/google/protobuf/compiler/java/BUILD.bazel index 230547e84f..cb2c1c36bd 100644 --- a/src/google/protobuf/compiler/java/BUILD.bazel +++ b/src/google/protobuf/compiler/java/BUILD.bazel @@ -129,6 +129,7 @@ cc_library( deps = [ ":generator_common", ":helpers", + ":internal_helpers", ":java_features_bootstrap", ":names", "//src/google/protobuf", @@ -194,6 +195,20 @@ cc_library( ], ) +cc_test( + name = "generator_unittest", + srcs = ["generator_unittest.cc"], + deps = [ + ":java", + ":java_features_bootstrap", + "//:protobuf", + "//src/google/protobuf", + "//src/google/protobuf/compiler:command_line_interface_tester", + "@com_google_googletest//:gtest", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "doc_comment_unittest", srcs = ["doc_comment_unittest.cc"], diff --git a/src/google/protobuf/compiler/java/file.cc b/src/google/protobuf/compiler/java/file.cc index d59b0dcdc4..c4eb3752d2 100644 --- a/src/google/protobuf/compiler/java/file.cc +++ b/src/google/protobuf/compiler/java/file.cc @@ -26,6 +26,7 @@ #include "google/protobuf/compiler/java/generator_factory.h" #include "google/protobuf/compiler/java/helpers.h" #include "google/protobuf/compiler/java/full/generator_factory.h" +#include "google/protobuf/compiler/java/internal_helpers.h" #include "google/protobuf/compiler/java/lite/generator_factory.h" #include "google/protobuf/compiler/java/name_resolver.h" #include "google/protobuf/compiler/java/options.h" @@ -33,6 +34,7 @@ #include "google/protobuf/compiler/retention.h" #include "google/protobuf/compiler/versions.h" #include "google/protobuf/descriptor.pb.h" +#include "google/protobuf/descriptor_visitor.h" #include "google/protobuf/dynamic_message.h" #include "google/protobuf/io/printer.h" #include "google/protobuf/io/zero_copy_stream.h" @@ -250,7 +252,8 @@ bool FileGenerator::Validate(std::string* error) { "https://github.com/protocolbuffers/protobuf/blob/main/java/" "lite.md"; } - return true; + + return error->empty(); } void FileGenerator::Generate(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/java/generator_unittest.cc b/src/google/protobuf/compiler/java/generator_unittest.cc new file mode 100644 index 0000000000..1d11c2d628 --- /dev/null +++ b/src/google/protobuf/compiler/java/generator_unittest.cc @@ -0,0 +1,72 @@ +// Protocol Buffers - Google's data interchange format +// Copyright 2023 Google Inc. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file or at +// https://developers.google.com/open-source/licenses/bsd + +#include "google/protobuf/compiler/java/generator.h" + +#include + +#include "google/protobuf/descriptor.pb.h" +#include +#include "google/protobuf/compiler/java/java_features.pb.h" +#include "google/protobuf/compiler/command_line_interface_tester.h" + +namespace google { +namespace protobuf { +namespace compiler { +namespace java { +namespace { + +class JavaGeneratorTest : public CommandLineInterfaceTester { + protected: + JavaGeneratorTest() { + RegisterGenerator("--java_out", "--java_opt", + std::make_unique(), "Java test generator"); + + // Generate built-in protos. + CreateTempFile( + "google/protobuf/descriptor.proto", + google::protobuf::DescriptorProto::descriptor()->file()->DebugString()); + CreateTempFile("third_party/java/protobuf/java_features.proto", + pb::JavaFeatures::descriptor()->file()->DebugString()); + } +}; + +TEST_F(JavaGeneratorTest, Basic) { + CreateTempFile("foo.proto", + R"schema( + syntax = "proto2"; + message Foo { + optional int32 bar = 1; + })schema"); + + RunProtoc( + "protocol_compiler --proto_path=$tmpdir --java_out=$tmpdir foo.proto"); + + ExpectNoErrors(); +} + +TEST_F(JavaGeneratorTest, BasicError) { + CreateTempFile("foo.proto", + R"schema( + syntax = "proto2"; + message Foo { + int32 bar = 1; + })schema"); + + RunProtoc( + "protocol_compiler --proto_path=$tmpdir --java_out=$tmpdir foo.proto"); + + ExpectErrorSubstring( + "foo.proto:4:7: Expected \"required\", \"optional\", or \"repeated\""); +} + + +} // namespace +} // namespace java +} // namespace compiler +} // namespace protobuf +} // namespace google