java_plugin_unittest.cc 4.8 KB

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